-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[ONNX] Refactor delegate memory management #32661
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,19 +52,6 @@ inline std::vector<T> __get_data(const Container& container) { | |
| #endif | ||
| } | ||
|
|
||
| template <typename T, typename SRC> | ||
| inline std::vector<T> __get_data(const void* data, const size_t data_size) { | ||
| #if defined(_MSC_VER) | ||
| # pragma warning(push) | ||
| # pragma warning(disable : 4267) | ||
| # pragma warning(disable : 4244) | ||
| #endif | ||
| return std::vector<T>(static_cast<const SRC*>(data), static_cast<const SRC*>(data) + data_size); | ||
| #if defined(_MSC_VER) | ||
| # pragma warning(pop) | ||
| #endif | ||
| } | ||
|
|
||
| template <typename T> | ||
| inline std::vector<T> __get_raw_data(const std::string& raw_data, int onnx_data_type) { | ||
| auto it = reinterpret_cast<const T*>(raw_data.data()); | ||
|
|
@@ -83,18 +70,10 @@ class TensorONNXPlace : public ov::frontend::onnx::TensorPlace { | |
| const ov::PartialShape& pshape, | ||
| ov::element::Type type, | ||
| const std::vector<std::string>& names, | ||
| const void* data, | ||
| const size_t data_size, | ||
| const ov::Any& data_any, | ||
| std::shared_ptr<std::string> data_location, | ||
| const bool is_raw) | ||
| const std::shared_ptr<ov::AlignedBuffer>& buffer) | ||
| : ov::frontend::onnx::TensorPlace(input_model, pshape, type, names), | ||
| m_input_model(input_model), | ||
| m_data(data), | ||
| m_data_any(data_any), | ||
| m_data_size(data_size), | ||
| m_data_location(data_location), | ||
| m_is_raw(is_raw) {}; | ||
| m_buffer(buffer) {}; | ||
|
|
||
| void translate(ov::Output<ov::Node>& output); | ||
|
|
||
|
|
@@ -119,24 +98,8 @@ class TensorONNXPlace : public ov::frontend::onnx::TensorPlace { | |
| m_output_idx = idx; | ||
| } | ||
|
|
||
| const void* get_data() const { | ||
| return m_data; | ||
| } | ||
|
|
||
| size_t get_data_size() const { | ||
| return m_data_size; | ||
| } | ||
|
|
||
| const ov::Any get_data_any() const { | ||
| return m_data_any; | ||
| } | ||
|
|
||
| std::shared_ptr<std::string> get_data_location() const { | ||
| return m_data_location; | ||
| } | ||
|
|
||
| bool is_raw() const { | ||
| return m_is_raw; | ||
| std::shared_ptr<ov::AlignedBuffer> get_buffer() const { | ||
| return m_buffer; | ||
| } | ||
|
|
||
| detail::MappedMemoryHandles get_mmap_cache(); | ||
|
|
@@ -145,11 +108,7 @@ class TensorONNXPlace : public ov::frontend::onnx::TensorPlace { | |
| protected: | ||
| int64_t m_input_idx = -1, m_output_idx = -1; | ||
| const ov::frontend::InputModel& m_input_model; | ||
| const void* m_data; | ||
| ov::Any m_data_any; | ||
| size_t m_data_size; | ||
| std::shared_ptr<std::string> m_data_location; | ||
| bool m_is_raw; | ||
| std::shared_ptr<ov::AlignedBuffer> m_buffer; | ||
| }; | ||
|
|
||
| class Tensor { | ||
|
|
@@ -298,19 +257,18 @@ class Tensor { | |
| private: | ||
| bool has_external_data() const { | ||
| if (m_tensor_place != nullptr) { | ||
| return m_tensor_place->get_data_location() != nullptr; | ||
| return false; | ||
| } | ||
| return m_tensor_proto->has_data_location() && | ||
| m_tensor_proto->data_location() == TensorProto_DataLocation::TensorProto_DataLocation_EXTERNAL; | ||
| } | ||
|
|
||
| template <typename T> | ||
| std::vector<T> get_external_data() const { | ||
| const auto ext_data = m_tensor_place != nullptr | ||
| ? detail::TensorExternalData(*m_tensor_place->get_data_location(), | ||
| reinterpret_cast<size_t>(m_tensor_place->get_data()), | ||
| m_tensor_place->get_data_size()) | ||
| : detail::TensorExternalData(*m_tensor_proto); | ||
| if (m_tensor_place != nullptr) { | ||
| FRONT_END_NOT_IMPLEMENTED(get_external_data); | ||
| } | ||
| const auto ext_data = detail::TensorExternalData(*m_tensor_proto); | ||
| std::shared_ptr<ov::AlignedBuffer> buffer = nullptr; | ||
| if (ext_data.data_location() == detail::ORT_MEM_ADDR) { | ||
| buffer = ext_data.load_external_mem_data(); | ||
|
|
@@ -327,7 +285,7 @@ class Tensor { | |
| FRONT_END_THROW("Unexpected usage of method for externally stored data"); | ||
| } | ||
| if (m_tensor_place != nullptr) { | ||
| return m_tensor_place->get_data(); | ||
| FRONT_END_NOT_IMPLEMENTED(get_data_ptr); | ||
| } | ||
|
|
||
| if (m_tensor_proto->has_raw_data()) { | ||
|
|
@@ -350,12 +308,7 @@ class Tensor { | |
|
|
||
| size_t get_data_size() const { | ||
| if (m_tensor_place != nullptr) { | ||
| if (m_tensor_place->is_raw()) { | ||
| return m_tensor_place->get_data_size() / | ||
| get_onnx_data_size(ov_to_onnx_data_type(m_tensor_place->get_element_type())); | ||
| } else { | ||
| return m_tensor_place->get_data_size(); | ||
| } | ||
| FRONT_END_NOT_IMPLEMENTED(get_data_size); | ||
|
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. Question: is it planned to be implemented? 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. Maybe yes, or maybe will delete it later |
||
| } | ||
| if (has_external_data()) { | ||
| const auto ext_data = detail::TensorExternalData(*m_tensor_proto); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,6 +88,7 @@ set(SRC | |
| skip_tests_config.cpp | ||
| ../frontend/src/core/graph_iterator_proto.cpp | ||
| ../frontend/src/core/decoder_proto.cpp | ||
| ../frontend/src/utils/tensor_external_data.cpp | ||
| ) | ||
|
|
||
| foreach(src IN LISTS SRC MULTI_TEST_SRC) | ||
|
|
@@ -129,7 +130,10 @@ set_property(TEST ov_onnx_frontend_tests PROPERTY LABELS OV UNIT ONNX_FE) | |
| add_dependencies(ov_onnx_frontend_tests openvino_template_extension) | ||
|
|
||
|
|
||
| target_include_directories(ov_onnx_frontend_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}") | ||
| target_include_directories(ov_onnx_frontend_tests PRIVATE | ||
|
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. I have a feeling, that objects in In kosher CMake these libraries would export their interface directories. So that linking them would "infect" the current target with necessary paths. See: https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_INCLUDE_DIRECTORIES.html 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. Yeah, those are files from frontend library. We include them here to not duplicate code (or to not create a separate target for decoder) |
||
| "${CMAKE_CURRENT_SOURCE_DIR}" | ||
| "${CMAKE_CURRENT_SOURCE_DIR}/../frontend/src" | ||
| ) | ||
|
|
||
| target_compile_definitions(ov_onnx_frontend_tests | ||
| PRIVATE | ||
|
|
||
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.
Missing include:
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.
Actually your
AlignedBufferalready is a resource manager. Maybe you could keep it by value inTensorMetaInfo(instead ofshared_ptr) in order to avoid managing the memory twice (and paying with memory fragmentation / non-locality)?