-
Notifications
You must be signed in to change notification settings - Fork 178
[circle-resizer] Add ModelData #15177
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
Closed
Closed
Changes from all commits
Commits
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,121 @@ | ||
| /* | ||
| * 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_DATA_H__ | ||
| #define __CIRCLE_RESIZER_MODEL_DATA_H__ | ||
|
|
||
| #include "Shape.h" | ||
|
|
||
| #include <string> | ||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| namespace luci | ||
| { | ||
| class Module; | ||
| } | ||
|
|
||
| namespace circle_resizer | ||
| { | ||
|
|
||
| /** | ||
| * The representation of Circle Model. | ||
| * The purpose of the class is to keep the buffer and the module representation of the model | ||
| * synchronized. | ||
| */ | ||
| class ModelData | ||
| { | ||
| public: | ||
| /** | ||
| * @brief Initialize the model with buffer representation. | ||
| * | ||
| * Exceptions: | ||
| * - std::runtime_error if interpretation of provided buffer as a circle model failed. | ||
| */ | ||
| explicit ModelData(const std::vector<uint8_t> &buffer); | ||
|
|
||
| /** | ||
| * @brief Initialize the model with buffer representation. | ||
| * | ||
| * Exceptions: | ||
| * - std::runtime_error if reading a model from provided path failed. | ||
| */ | ||
| explicit ModelData(const std::string &model_path); | ||
|
|
||
| /** | ||
| * @brief Dtor of ModelData. Note that explicit declaration is needed to satisfy forward | ||
| * declaration + unique_ptr. | ||
| */ | ||
| ~ModelData(); | ||
|
|
||
| /** | ||
| * @brief Notify that the buffer representation of the model has been modified so the module is no | ||
| * more valid. | ||
| */ | ||
| void invalidate_module(); | ||
|
|
||
| /** | ||
| * @brief Notify that the module representation of the model has been modified so the buffer is no | ||
| * more valid. | ||
| */ | ||
| void invalidate_buffer(); | ||
|
|
||
| /** | ||
| * @brief Get the loaded model as the buffer. | ||
| */ | ||
| std::vector<uint8_t> &buffer(); | ||
|
|
||
| /** | ||
| * @brief Get the loaded model as the module. | ||
| */ | ||
| luci::Module *module(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. module representation is need to run passes related with shape inference on |
||
|
|
||
| /** | ||
| * @brief Get input shapes of the loaded model. | ||
| */ | ||
| std::vector<Shape> input_shapes(); | ||
|
|
||
| /** | ||
| * @brief Get output shapes of the loaded model. | ||
| * | ||
| */ | ||
| std::vector<Shape> output_shapes(); | ||
|
|
||
| /** | ||
| * @brief Save the loaded model to the stream. | ||
| * | ||
| * Exceptions: | ||
| * - std::runtime_error if saving the model the given stream failed. | ||
| */ | ||
| void save(std::ostream &stream); | ||
|
|
||
| /** | ||
| * @brief Save the loaded model to the location indicated by output_path. | ||
| * | ||
| * Exceptions: | ||
| * - std::runtime_error if saving the model the given path failed. | ||
| */ | ||
| void save(const std::string &output_path); | ||
|
|
||
| private: | ||
| bool _module_invalidated = false, _buffer_invalidated = false; | ||
| std::vector<uint8_t> _buffer; | ||
| std::unique_ptr<luci::Module> _module; | ||
| }; | ||
|
|
||
| } // namespace circle_resizer | ||
|
|
||
| #endif // __CIRCLE_RESIZER_MODEL_DATA_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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| require("arser") | ||
| require("common-artifacts") | ||
| require("mio-circle08") | ||
| require("safemain") | ||
| require("vconone") |
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 |
|---|---|---|
| @@ -1,9 +1,14 @@ | ||
| list(APPEND CIRCLE_RESIZER_CORE_SOURCES Dim.cpp) | ||
| list(APPEND CIRCLE_RESIZER_CORE_SOURCES Shape.cpp) | ||
| list(APPEND CIRCLE_RESIZER_CORE_SOURCES ShapeParser.cpp) | ||
| list(APPEND CIRCLE_RESIZER_SOURCES Dim.cpp) | ||
| list(APPEND CIRCLE_RESIZER_SOURCES Shape.cpp) | ||
| list(APPEND CIRCLE_RESIZER_SOURCES ShapeParser.cpp) | ||
| list(APPEND CIRCLE_RESIZER_SOURCES ModelData.cpp) | ||
|
|
||
| add_library(circle_resizer_core STATIC "${CIRCLE_RESIZER_CORE_SOURCES}") | ||
| add_library(circle_resizer_core SHARED "${CIRCLE_RESIZER_SOURCES}") | ||
|
|
||
| target_include_directories(circle_resizer_core PUBLIC ../include) | ||
|
|
||
| target_link_libraries(circle_resizer_core PRIVATE mio_circle08) | ||
| target_link_libraries(circle_resizer_core PRIVATE luci_export) | ||
| target_link_libraries(circle_resizer_core PRIVATE luci_import) | ||
|
|
||
| install(TARGETS circle_resizer_core DESTINATION lib) |
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,180 @@ | ||
| /* | ||
| * 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 "ModelData.h" | ||
|
|
||
| #include <mio/circle/schema_generated.h> | ||
|
|
||
| #include <luci/Importer.h> | ||
| #include <luci/CircleExporter.h> | ||
| #include <luci/CircleFileExpContract.h> | ||
|
|
||
| #include <fstream> | ||
| #include <vector> | ||
|
|
||
| using namespace circle_resizer; | ||
|
|
||
| namespace | ||
| { | ||
| std::vector<uint8_t> read_model(const std::string &model_path) | ||
| { | ||
| std::ifstream file_stream(model_path, std::ios::in | std::ios::binary | std::ifstream::ate); | ||
| if (!file_stream.is_open()) | ||
| { | ||
| throw std::runtime_error("Failed to open file: " + model_path); | ||
| } | ||
|
|
||
| std::streamsize size = file_stream.tellg(); | ||
| file_stream.seekg(0, std::ios::beg); | ||
|
|
||
| std::vector<uint8_t> buffer(size); | ||
| if (!file_stream.read(reinterpret_cast<char *>(buffer.data()), size)) | ||
| { | ||
| throw std::runtime_error("Failed to read file: " + model_path); | ||
| } | ||
|
|
||
| return buffer; | ||
| } | ||
|
|
||
| std::unique_ptr<luci::Module> load_module(const std::vector<uint8_t> &model_buffer) | ||
| { | ||
| flatbuffers::Verifier verifier{model_buffer.data(), model_buffer.size()}; | ||
| if (!circle::VerifyModelBuffer(verifier)) | ||
| { | ||
| throw std::runtime_error("Verification of the model failed"); | ||
| } | ||
|
|
||
| const luci::GraphBuilderSource *source_ptr = &luci::GraphBuilderRegistry::get(); | ||
| luci::Importer importer(source_ptr); | ||
| return importer.importModule(model_buffer.data(), model_buffer.size()); | ||
| } | ||
|
|
||
| class BufferModelContract : public luci::CircleExporter::Contract | ||
| { | ||
| public: | ||
| BufferModelContract(luci::Module *module) | ||
| : _module(module), _buffer{std::make_unique<std::vector<uint8_t>>()} | ||
| { | ||
| } | ||
|
|
||
| luci::Module *module() const override { return _module; } | ||
|
|
||
| bool store(const char *ptr, const size_t size) const override | ||
| { | ||
| _buffer->resize(size); | ||
| std::copy(ptr, ptr + size, _buffer->begin()); | ||
| return true; | ||
| } | ||
|
|
||
| std::vector<uint8_t> get_buffer() { return *_buffer; } | ||
|
|
||
| private: | ||
| luci::Module *_module; | ||
| std::unique_ptr<std::vector<uint8_t>> _buffer; | ||
| }; | ||
|
|
||
| template <typename NodeType> | ||
| std::vector<Shape> extract_shapes(const std::vector<loco::Node *> &nodes) | ||
| { | ||
| std::vector<Shape> shapes; | ||
| for (const auto &loco_node : nodes) | ||
| { | ||
| std::vector<Dim> dims; | ||
| const auto circle_node = loco::must_cast<const NodeType *>(loco_node); | ||
| for (uint32_t dim_idx = 0; dim_idx < circle_node->rank(); dim_idx++) | ||
| { | ||
| if (circle_node->dim(dim_idx).known()) | ||
| { | ||
| const int32_t dim_val = circle_node->dim(dim_idx).value(); | ||
| dims.push_back(Dim{dim_val}); | ||
| } | ||
| else | ||
| { | ||
| dims.push_back(Dim{-1}); | ||
| } | ||
| } | ||
| shapes.push_back(Shape{dims}); | ||
| } | ||
| return shapes; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| ModelData::ModelData(const std::vector<uint8_t> &buffer) | ||
| : _buffer{buffer}, _module{load_module(buffer)} | ||
| { | ||
| } | ||
|
|
||
| ModelData::ModelData(const std::string &model_path) : ModelData(read_model(model_path)) {} | ||
|
|
||
| void ModelData::invalidate_module() { _module_invalidated = true; } | ||
|
|
||
| void ModelData::invalidate_buffer() { _buffer_invalidated = true; } | ||
|
|
||
| std::vector<uint8_t> &ModelData::buffer() | ||
| { | ||
| if (_buffer_invalidated) | ||
| { | ||
| luci::CircleExporter exporter; | ||
| BufferModelContract contract(module()); | ||
|
|
||
| if (!exporter.invoke(&contract)) | ||
| { | ||
| throw std::runtime_error("Exporting buffer from the model failed"); | ||
| } | ||
| _buffer = contract.get_buffer(); | ||
| _buffer_invalidated = false; | ||
| } | ||
| return _buffer; | ||
| } | ||
|
|
||
| luci::Module *ModelData::module() | ||
| { | ||
| if (_module_invalidated) | ||
| { | ||
| _module = load_module(_buffer); | ||
| _module_invalidated = false; | ||
| } | ||
| return _module.get(); | ||
| } | ||
|
|
||
| void ModelData::save(std::ostream &stream) | ||
| { | ||
| auto &buff = buffer(); | ||
| stream.write(reinterpret_cast<const char *>(buff.data()), buff.size()); | ||
| if (!stream.good()) | ||
| { | ||
| throw std::runtime_error("Failed to write to output stream"); | ||
| } | ||
| } | ||
|
|
||
| void ModelData::save(const std::string &output_path) | ||
| { | ||
| std::ofstream out_stream(output_path, std::ios::out | std::ios::binary); | ||
| save(out_stream); | ||
| } | ||
|
|
||
| std::vector<Shape> ModelData::input_shapes() | ||
| { | ||
| return extract_shapes<luci::CircleInput>(loco::input_nodes(module()->graph())); | ||
| } | ||
|
|
||
| std::vector<Shape> ModelData::output_shapes() | ||
| { | ||
| return extract_shapes<luci::CircleOutput>(loco::output_nodes(module()->graph())); | ||
| } | ||
|
|
||
| ModelData::~ModelData() = default; |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Buffer representation of the model (after circle::GetMutableModel) is need to change the inputs shapes like shown in https://github.com/Samsung/ONE/pull/14727/files#diff-89115d8f21eafbf362d2bfbe1ea5677c609681b09b0ad094d6eec8a8e6e5a27eR44