-
Notifications
You must be signed in to change notification settings - Fork 178
[circle-resizer] Add ModelEditor #15224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #ifndef __CIRCLE_RESIZER_MODEL_EDITOR_H__ | ||
| #define __CIRCLE_RESIZER_MODEL_EDITOR_H__ | ||
|
|
||
| #include "Shape.h" | ||
| #include "CircleModel.h" | ||
|
|
||
| #include <string> | ||
| #include <vector> | ||
| #include <memory> | ||
|
|
||
| namespace circle_resizer | ||
| { | ||
|
|
||
| /** | ||
| * The class to modify circle models. | ||
| */ | ||
| class ModelEditor | ||
| { | ||
| public: | ||
| /** | ||
| * @brief Initialize the editor with CircleModel object. | ||
| */ | ||
| explicit ModelEditor(std::shared_ptr<CircleModel> circle_model); | ||
|
|
||
| public: | ||
| /** | ||
| * @brief Resize the model. In means changing shape of the inputs | ||
| * and propagating changes through the graph. | ||
| * | ||
| * Exceptions: | ||
| * - std::runtime_error if the new_inputs_shapes are invalid. It can happens for scenarios like: | ||
| * - new shapes for NOT all inputs are provided | ||
| * - an exception was thrown during shape inference pass | ||
| */ | ||
| ModelEditor &resize_inputs(const std::vector<Shape> &new_inputs_shapes); | ||
|
|
||
| private: | ||
| std::shared_ptr<CircleModel> _circle_model; | ||
| }; | ||
|
|
||
| } // namespace circle_resizer | ||
|
|
||
| #endif // __CIRCLE_RESIZER_MODEL_EDITOR_H__ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "ModelEditor.h" | ||
|
|
||
| #include <mio/circle/schema_generated.h> | ||
|
|
||
| #include <loco/IR/Graph.h> | ||
| #include <logo/Phase.h> | ||
| #include <logo/RemoveDeadNodeWithQueryPass.h> | ||
| #include <luci/IR/Nodes/CircleInput.h> | ||
| #include <luci/Pass/CircleShapeInferencePass.h> | ||
| #include <luci/Pass/CircleTypeInferencePass.h> | ||
|
|
||
| using namespace circle_resizer; | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| void change_single_input_shape(luci::CircleInput *circle_input, const Shape &new_shape) | ||
| { | ||
| circle_input->rank(new_shape.rank()); | ||
| for (uint32_t i = 0; i < new_shape.rank(); ++i) | ||
| { | ||
| if (new_shape[i].is_dynamic()) | ||
| { | ||
| circle_input->dim(i) = loco::Dimension(); // empty ctor means dynamic dimension | ||
| } | ||
| else | ||
| { | ||
| circle_input->dim(i) = loco::Dimension(static_cast<uint32_t>(new_shape[i].value())); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void change_inputs_shapes(loco::Graph *graph, const std::vector<Shape> &new_inputs_shapes) | ||
| { | ||
| auto graph_inputs = loco::input_nodes(graph); | ||
| if (graph_inputs.size() != new_inputs_shapes.size()) | ||
| { | ||
| throw std::runtime_error("Expected " + std::to_string(graph_inputs.size()) + | ||
| " shapes but provided only " + | ||
| std::to_string(new_inputs_shapes.size())); | ||
| } | ||
| for (size_t in_idx = 0; in_idx < new_inputs_shapes.size(); ++in_idx) | ||
| { | ||
| auto circle_input = loco::must_cast<luci::CircleInput *>(graph_inputs[in_idx]); | ||
| change_single_input_shape(circle_input, new_inputs_shapes[in_idx]); | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| ModelEditor::ModelEditor(std::shared_ptr<CircleModel> circle_model) : _circle_model{circle_model} {} | ||
mbencer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ModelEditor &ModelEditor::resize_inputs(const std::vector<Shape> &new_inputs_shapes) | ||
| { | ||
| auto graph = _circle_model->module()->graph(); | ||
| change_inputs_shapes(graph, new_inputs_shapes); | ||
|
|
||
| logo::Phase phase; | ||
| phase.emplace_back(std::make_unique<logo::RemoveDeadNodeWithQueryPass>()); | ||
| phase.emplace_back(std::make_unique<luci::CircleShapeInferencePass>()); | ||
| phase.emplace_back(std::make_unique<luci::CircleTypeInferencePass>()); | ||
|
|
||
| logo::PhaseRunner<logo::PhaseStrategy::Restart> phase_runner{graph}; | ||
| try | ||
| { | ||
| phase_runner.run(phase); | ||
| } | ||
| catch (const std::exception &e) | ||
| { | ||
| throw std::runtime_error("Exception during shape inference with message: " + | ||
| std::string{e.what()}); | ||
mbencer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return *this; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /* | ||
| * Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "ModelEditor.h" | ||
|
|
||
| #include <gmock/gmock.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <iostream> | ||
| #include <cstdlib> | ||
| #include <fstream> | ||
| #include <vector> | ||
|
|
||
| using namespace circle_resizer; | ||
| using ::testing::HasSubstr; | ||
|
|
||
| class ModelEditorTest : public ::testing::Test | ||
| { | ||
| protected: | ||
| void SetUp() override | ||
| { | ||
| char *path = std::getenv("ARTIFACTS_PATH"); | ||
| if (path == nullptr) | ||
| { | ||
| throw std::runtime_error("environmental variable ARTIFACTS_PATH required for circle-resizer " | ||
| "tests was not not provided"); | ||
mbencer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| _test_models_dir = path; | ||
| } | ||
|
|
||
| protected: | ||
| std::string _test_models_dir; | ||
| }; | ||
|
|
||
| TEST_F(ModelEditorTest, single_input_single_output) | ||
| { | ||
| auto circle_model = std::make_shared<CircleModel>(_test_models_dir + "/ExpandDims_000.circle"); | ||
| ModelEditor editor(circle_model); | ||
| const auto new_input_shapes = std::vector<Shape>{Shape{4, 6}}; | ||
| editor.resize_inputs(new_input_shapes); | ||
| EXPECT_EQ(circle_model->input_shapes(), new_input_shapes); | ||
| EXPECT_EQ(circle_model->output_shapes(), (std::vector<Shape>{Shape{4, 1, 6}})); | ||
| } | ||
|
|
||
| TEST_F(ModelEditorTest, single_input_two_outputs) | ||
| { | ||
| auto circle_model = std::make_shared<CircleModel>(_test_models_dir + "/CSE_Quantize_000.circle"); | ||
| ModelEditor editor(circle_model); | ||
| const auto new_input_shapes = std::vector<Shape>{Shape{1, 6, 6, 4}}; | ||
| editor.resize_inputs(new_input_shapes); | ||
| EXPECT_EQ(circle_model->input_shapes(), new_input_shapes); | ||
| EXPECT_EQ(circle_model->output_shapes(), | ||
| (std::vector<Shape>{Shape{1, 6, 6, 4}, Shape{1, 6, 6, 4}})); | ||
| } | ||
|
|
||
| TEST_F(ModelEditorTest, two_inputs_single_output) | ||
| { | ||
| auto circle_model = std::make_shared<CircleModel>(_test_models_dir + "/Add_000.circle"); | ||
| ModelEditor editor(circle_model); | ||
| const auto new_input_shapes = std::vector<Shape>{Shape{1, 5, 5, 3}, Shape{1, 5, 5, 3}}; | ||
| editor.resize_inputs(new_input_shapes); | ||
| EXPECT_EQ(circle_model->input_shapes(), new_input_shapes); | ||
| EXPECT_EQ(circle_model->output_shapes(), (std::vector<Shape>{Shape{1, 5, 5, 3}})); | ||
| } | ||
|
|
||
| TEST_F(ModelEditorTest, two_inputs_two_outputs) | ||
| { | ||
| auto circle_model = | ||
| std::make_shared<CircleModel>(_test_models_dir + "/Part_Add_Sqrt_Rsqrt_000.circle"); | ||
| ModelEditor editor(circle_model); | ||
| const auto new_input_shapes = std::vector<Shape>{Shape{1, 5, 5, 2}, Shape{1, 5, 5, 2}}; | ||
| editor.resize_inputs(new_input_shapes); | ||
| EXPECT_EQ(circle_model->input_shapes(), new_input_shapes); | ||
| EXPECT_EQ(circle_model->output_shapes(), | ||
| (std::vector<Shape>{Shape{1, 5, 5, 2}, Shape{1, 5, 5, 2}})); | ||
| } | ||
|
|
||
| TEST_F(ModelEditorTest, resize_applied_after_save) | ||
| { | ||
| auto circle_model = std::make_shared<CircleModel>(_test_models_dir + "/ExpandDims_000.circle"); | ||
| ModelEditor editor(circle_model); | ||
| std::stringstream out_stream; | ||
| const auto new_input_shapes = std::vector<Shape>{Shape{4, 6}}; | ||
| editor.resize_inputs(new_input_shapes); | ||
| circle_model->save(out_stream); | ||
| const std::string &model_buf_str = out_stream.str(); | ||
| std::vector<uint8_t> model_buffer(std::begin(model_buf_str), std::end(model_buf_str)); | ||
| model_buffer.insert(std::end(model_buffer), std::begin(model_buf_str), std::end(model_buf_str)); | ||
|
|
||
| auto circle_model_from_saved_buffer = std::make_shared<CircleModel>(model_buffer); | ||
| EXPECT_EQ(circle_model_from_saved_buffer->input_shapes(), new_input_shapes); | ||
| EXPECT_EQ(circle_model_from_saved_buffer->output_shapes(), (std::vector<Shape>{Shape{4, 1, 6}})); | ||
| } | ||
|
|
||
| TEST_F(ModelEditorTest, single_input_single_output_double_resizing) | ||
| { | ||
| auto circle_model = std::make_shared<CircleModel>(_test_models_dir + "/ExpandDims_000.circle"); | ||
| ModelEditor editor(circle_model); | ||
| const auto new_input_shapes = std::vector<Shape>{Shape{4, 6}}; | ||
| editor.resize_inputs(std::vector<Shape>{Shape{6, 8}}).resize_inputs(new_input_shapes); | ||
| // check if the last applied shape is set after double resizing call | ||
| EXPECT_EQ(circle_model->input_shapes(), new_input_shapes); | ||
| EXPECT_EQ(circle_model->output_shapes(), (std::vector<Shape>{Shape{4, 1, 6}})); | ||
| } | ||
|
|
||
| TEST_F(ModelEditorTest, change_input_rank) | ||
| { | ||
| auto circle_model = std::make_shared<CircleModel>(_test_models_dir + "/ExpandDims_000.circle"); | ||
| ModelEditor editor(circle_model); | ||
| const auto new_input_shapes = std::vector<Shape>{Shape{1, 2, 3, 4}}; | ||
| editor.resize_inputs(new_input_shapes); | ||
| EXPECT_EQ(circle_model->input_shapes(), new_input_shapes); | ||
| EXPECT_EQ(circle_model->output_shapes(), (std::vector<Shape>{Shape{1, 1, 2, 3, 4}})); | ||
| } | ||
|
|
||
| TEST_F(ModelEditorTest, resize_to_dynamic) | ||
| { | ||
| auto circle_model = std::make_shared<CircleModel>(_test_models_dir + "/ExpandDims_000.circle"); | ||
| ModelEditor editor(circle_model); | ||
| const auto new_input_shapes = std::vector<Shape>{Shape{Dim{4}, Dim::dynamic()}}; | ||
| editor.resize_inputs(new_input_shapes); | ||
| EXPECT_EQ(circle_model->input_shapes(), new_input_shapes); | ||
| EXPECT_EQ(circle_model->output_shapes(), | ||
| (std::vector<Shape>{Shape{Dim{4}, Dim{1}, Dim::dynamic()}})); | ||
| } | ||
|
|
||
| TEST_F(ModelEditorTest, not_all_input_shapes_provided_NEG) | ||
| { | ||
| auto circle_model = std::make_shared<CircleModel>(_test_models_dir + "/Add_000.circle"); | ||
| ModelEditor editor(circle_model); | ||
| try | ||
| { | ||
| editor.resize_inputs(std::vector<Shape>{Shape{1, 5, 5, 3}}); | ||
| FAIL() << "Unexpected successful resizing with invalid shapes."; | ||
| } | ||
| catch (const std::runtime_error &err) | ||
| { | ||
| EXPECT_THAT(err.what(), HasSubstr("Expected 2 shapes but provided only 1")); | ||
| } | ||
| catch (...) | ||
| { | ||
| FAIL() << "Expected std::runtime_error, other exception thrown"; | ||
| } | ||
| } | ||
|
|
||
| TEST_F(ModelEditorTest, exception_during_shape_inference_NEG) | ||
| { | ||
| auto circle_model = std::make_shared<CircleModel>(_test_models_dir + "/Add_000.circle"); | ||
| ModelEditor editor(circle_model); | ||
| try | ||
| { | ||
| editor.resize_inputs(std::vector<Shape>{Shape{1, 2, 3}, Shape{4, 5, 6}}); | ||
| FAIL() << "Unexpected successful resizing with invalid shapes."; | ||
| } | ||
| catch (const std::runtime_error &err) | ||
| { | ||
| EXPECT_THAT(err.what(), HasSubstr("Exception during shape inference with message:")); | ||
| } | ||
| catch (...) | ||
| { | ||
| FAIL() << "Expected std::runtime_error, other exception thrown"; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.