Skip to content

Commit f01eae9

Browse files
committed
[ONNX] Refactor delegate memory management
Signed-off-by: Maxim Vafin <[email protected]>
1 parent 1806c69 commit f01eae9

File tree

10 files changed

+263
-373
lines changed

10 files changed

+263
-373
lines changed

src/frontends/onnx/frontend/include/openvino/frontend/onnx/decoder.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "openvino/core/type/element_type.hpp"
88
#include "openvino/frontend/decoder.hpp"
99
#include "openvino/frontend/onnx/visibility.hpp"
10+
#include "openvino/runtime/aligned_buffer.hpp"
1011

1112
namespace ov {
1213
namespace frontend {
@@ -15,12 +16,8 @@ namespace onnx {
1516
struct ONNX_FRONTEND_API TensorMetaInfo {
1617
ov::PartialShape m_partial_shape;
1718
ov::element::Type m_element_type;
18-
const uint8_t* m_tensor_data;
19-
ov::Any m_tensor_data_any;
20-
size_t m_tensor_data_size;
19+
std::shared_ptr<ov::AlignedBuffer> m_buffer;
2120
const std::string* m_tensor_name;
22-
std::shared_ptr<std::string> m_external_location;
23-
bool m_is_raw;
2421
};
2522

2623
class ONNX_FRONTEND_API DecoderBase : public ov::frontend::DecoderBase {

src/frontends/onnx/frontend/src/core/decoder_proto.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ class DecoderProtoTensor : public ov::frontend::onnx::DecoderBaseTensor {
6666
m_tensor_meta_info.m_tensor_name = &name;
6767
m_tensor_meta_info.m_element_type = ov::element::dynamic;
6868
m_tensor_meta_info.m_partial_shape = ov::PartialShape::dynamic();
69-
m_tensor_meta_info.m_tensor_data = nullptr;
70-
m_tensor_meta_info.m_tensor_data_size = 0;
7169
}
7270

7371
const ov::frontend::onnx::TensorMetaInfo& get_tensor_info() const override {

src/frontends/onnx/frontend/src/core/graph_iterator_proto.cpp

Lines changed: 170 additions & 149 deletions
Large diffs are not rendered by default.

src/frontends/onnx/frontend/src/core/node.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -797,11 +797,7 @@ Tensor Node::get_attribute_value(const std::string& name) const {
797797
tensor_meta_info.m_partial_shape,
798798
tensor_meta_info.m_element_type,
799799
std::vector<std::string>{*tensor_meta_info.m_tensor_name},
800-
tensor_meta_info.m_tensor_data,
801-
tensor_meta_info.m_tensor_data_size,
802-
tensor_meta_info.m_tensor_data_any,
803-
tensor_meta_info.m_external_location,
804-
tensor_meta_info.m_is_raw);
800+
tensor_meta_info.m_buffer);
805801
return {tensor_place};
806802
}
807803
FRONT_END_NOT_IMPLEMENTED(get_attribute_value);
@@ -824,11 +820,7 @@ SparseTensor Node::get_attribute_value(const std::string& name) const {
824820
values_meta_info.m_partial_shape,
825821
values_meta_info.m_element_type,
826822
std::vector<std::string>{*values_meta_info.m_tensor_name},
827-
values_meta_info.m_tensor_data,
828-
values_meta_info.m_tensor_data_size,
829-
values_meta_info.m_tensor_data_any,
830-
values_meta_info.m_external_location,
831-
values_meta_info.m_is_raw);
823+
values_meta_info.m_buffer);
832824

833825
auto indices_decoder =
834826
std::dynamic_pointer_cast<ov::frontend::onnx::DecoderBaseTensor>(sparse_tensor_info.m_indices);
@@ -838,11 +830,7 @@ SparseTensor Node::get_attribute_value(const std::string& name) const {
838830
indices_meta_info.m_partial_shape,
839831
indices_meta_info.m_element_type,
840832
std::vector<std::string>{*indices_meta_info.m_tensor_name},
841-
indices_meta_info.m_tensor_data,
842-
indices_meta_info.m_tensor_data_size,
843-
indices_meta_info.m_tensor_data_any,
844-
indices_meta_info.m_external_location,
845-
indices_meta_info.m_is_raw);
833+
indices_meta_info.m_buffer);
846834
return {values_place, indices_place, sparse_tensor_info.m_partial_shape};
847835
}
848836
FRONT_END_NOT_IMPLEMENTED(get_attribute_value);

src/frontends/onnx/frontend/src/core/tensor.cpp

Lines changed: 66 additions & 134 deletions
Large diffs are not rendered by default.

src/frontends/onnx/frontend/src/core/tensor.hpp

Lines changed: 12 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,6 @@ inline std::vector<T> __get_data(const Container& container) {
5252
#endif
5353
}
5454

55-
template <typename T, typename SRC>
56-
inline std::vector<T> __get_data(const void* data, const size_t data_size) {
57-
#if defined(_MSC_VER)
58-
# pragma warning(push)
59-
# pragma warning(disable : 4267)
60-
# pragma warning(disable : 4244)
61-
#endif
62-
return std::vector<T>(static_cast<const SRC*>(data), static_cast<const SRC*>(data) + data_size);
63-
#if defined(_MSC_VER)
64-
# pragma warning(pop)
65-
#endif
66-
}
67-
6855
template <typename T>
6956
inline std::vector<T> __get_raw_data(const std::string& raw_data, int onnx_data_type) {
7057
auto it = reinterpret_cast<const T*>(raw_data.data());
@@ -83,18 +70,10 @@ class TensorONNXPlace : public ov::frontend::onnx::TensorPlace {
8370
const ov::PartialShape& pshape,
8471
ov::element::Type type,
8572
const std::vector<std::string>& names,
86-
const void* data,
87-
const size_t data_size,
88-
const ov::Any& data_any,
89-
std::shared_ptr<std::string> data_location,
90-
const bool is_raw)
73+
const std::shared_ptr<ov::AlignedBuffer>& buffer)
9174
: ov::frontend::onnx::TensorPlace(input_model, pshape, type, names),
9275
m_input_model(input_model),
93-
m_data(data),
94-
m_data_any(data_any),
95-
m_data_size(data_size),
96-
m_data_location(data_location),
97-
m_is_raw(is_raw) {};
76+
m_buffer(buffer) {};
9877

9978
void translate(ov::Output<ov::Node>& output);
10079

@@ -119,24 +98,8 @@ class TensorONNXPlace : public ov::frontend::onnx::TensorPlace {
11998
m_output_idx = idx;
12099
}
121100

122-
const void* get_data() const {
123-
return m_data;
124-
}
125-
126-
size_t get_data_size() const {
127-
return m_data_size;
128-
}
129-
130-
const ov::Any get_data_any() const {
131-
return m_data_any;
132-
}
133-
134-
std::shared_ptr<std::string> get_data_location() const {
135-
return m_data_location;
136-
}
137-
138-
bool is_raw() const {
139-
return m_is_raw;
101+
std::shared_ptr<ov::AlignedBuffer> get_buffer() const {
102+
return m_buffer;
140103
}
141104

142105
detail::MappedMemoryHandles get_mmap_cache();
@@ -145,11 +108,7 @@ class TensorONNXPlace : public ov::frontend::onnx::TensorPlace {
145108
protected:
146109
int64_t m_input_idx = -1, m_output_idx = -1;
147110
const ov::frontend::InputModel& m_input_model;
148-
const void* m_data;
149-
ov::Any m_data_any;
150-
size_t m_data_size;
151-
std::shared_ptr<std::string> m_data_location;
152-
bool m_is_raw;
111+
std::shared_ptr<ov::AlignedBuffer> m_buffer;
153112
};
154113

155114
class Tensor {
@@ -298,19 +257,18 @@ class Tensor {
298257
private:
299258
bool has_external_data() const {
300259
if (m_tensor_place != nullptr) {
301-
return m_tensor_place->get_data_location() != nullptr;
260+
return false;
302261
}
303262
return m_tensor_proto->has_data_location() &&
304263
m_tensor_proto->data_location() == TensorProto_DataLocation::TensorProto_DataLocation_EXTERNAL;
305264
}
306265

307266
template <typename T>
308267
std::vector<T> get_external_data() const {
309-
const auto ext_data = m_tensor_place != nullptr
310-
? detail::TensorExternalData(*m_tensor_place->get_data_location(),
311-
reinterpret_cast<size_t>(m_tensor_place->get_data()),
312-
m_tensor_place->get_data_size())
313-
: detail::TensorExternalData(*m_tensor_proto);
268+
if (m_tensor_place != nullptr) {
269+
FRONT_END_NOT_IMPLEMENTED(get_external_data);
270+
}
271+
const auto ext_data = detail::TensorExternalData(*m_tensor_proto);
314272
std::shared_ptr<ov::AlignedBuffer> buffer = nullptr;
315273
if (ext_data.data_location() == detail::ORT_MEM_ADDR) {
316274
buffer = ext_data.load_external_mem_data();
@@ -327,7 +285,7 @@ class Tensor {
327285
FRONT_END_THROW("Unexpected usage of method for externally stored data");
328286
}
329287
if (m_tensor_place != nullptr) {
330-
return m_tensor_place->get_data();
288+
FRONT_END_NOT_IMPLEMENTED(get_data_ptr);
331289
}
332290

333291
if (m_tensor_proto->has_raw_data()) {
@@ -350,12 +308,7 @@ class Tensor {
350308

351309
size_t get_data_size() const {
352310
if (m_tensor_place != nullptr) {
353-
if (m_tensor_place->is_raw()) {
354-
return m_tensor_place->get_data_size() /
355-
get_onnx_data_size(ov_to_onnx_data_type(m_tensor_place->get_element_type()));
356-
} else {
357-
return m_tensor_place->get_data_size();
358-
}
311+
FRONT_END_NOT_IMPLEMENTED(get_data_size);
359312
}
360313
if (has_external_data()) {
361314
const auto ext_data = detail::TensorExternalData(*m_tensor_proto);

src/frontends/onnx/frontend/src/frontend.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ ONNX_FRONTEND_C_API void* get_front_end_data() {
8484
}
8585

8686
ov::frontend::InputModel::Ptr FrontEnd::load_impl(const std::vector<ov::Any>& variants) const {
87-
const bool gi_enabled = std::getenv("ONNX_ITERATOR") != nullptr;
87+
// const bool gi_enabled = std::getenv("ONNX_ITERATOR") != nullptr;
88+
const bool gi_enabled = true;
8889
if (variants.empty()) {
8990
return nullptr;
9091
}

src/frontends/onnx/frontend/src/input_model.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -651,11 +651,7 @@ std::shared_ptr<ov::frontend::onnx::TensorONNXPlace> decode_tensor_place(
651651
tensor_meta_info.m_partial_shape,
652652
tensor_meta_info.m_element_type,
653653
std::vector<std::string>{*tensor_meta_info.m_tensor_name},
654-
tensor_meta_info.m_tensor_data,
655-
tensor_meta_info.m_tensor_data_size,
656-
tensor_meta_info.m_tensor_data_any,
657-
tensor_meta_info.m_external_location,
658-
tensor_meta_info.m_is_raw);
654+
tensor_meta_info.m_buffer);
659655
return tensor_place;
660656
}
661657

@@ -688,7 +684,7 @@ void InputModel::InputModelONNXImpl::load_model() {
688684
tensor_place->set_input_index(tensor_decoder->get_input_idx());
689685
tensor_place->set_output_index(tensor_decoder->get_output_idx());
690686
// Constant with data has been found
691-
if (tensor_place->get_data() != nullptr)
687+
if (tensor_place->get_buffer() != nullptr)
692688
continue;
693689
auto name = tensor_place->get_names()[0];
694690
if (m_tensor_places.count(name) == 0) {

src/frontends/onnx/frontend/src/translate_session.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void TranslateSession::translate_graph(const ov::frontend::InputModel::Ptr& inpu
8181
auto create_const_or_param = [&](const std::string& name,
8282
const std::shared_ptr<ov::frontend::onnx::TensorONNXPlace>& input_tensor) {
8383
std::shared_ptr<ov::Node> node;
84-
if (input_tensor->get_data_location() != nullptr || input_tensor->get_data() != nullptr) {
84+
if (input_tensor->get_buffer() != nullptr) {
8585
Tensor tensor = Tensor(input_tensor);
8686
node = tensor.get_ov_constant();
8787
} else if (input_tensor->get_partial_shape() == PartialShape{0}) { // empty constant

src/frontends/onnx/tests/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ set(SRC
8888
skip_tests_config.cpp
8989
../frontend/src/core/graph_iterator_proto.cpp
9090
../frontend/src/core/decoder_proto.cpp
91+
../frontend/src/utils/tensor_external_data.cpp
9192
)
9293

9394
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)
129130
add_dependencies(ov_onnx_frontend_tests openvino_template_extension)
130131

131132

132-
target_include_directories(ov_onnx_frontend_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
133+
target_include_directories(ov_onnx_frontend_tests PRIVATE
134+
"${CMAKE_CURRENT_SOURCE_DIR}"
135+
"${CMAKE_CURRENT_SOURCE_DIR}/../frontend/src"
136+
)
133137

134138
target_compile_definitions(ov_onnx_frontend_tests
135139
PRIVATE

0 commit comments

Comments
 (0)