Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[ONNX FE] Set WeightlessCacheAttribute with correct offset for external constants (#37016)

Maxim Vafin174e2996c223
diff --git a/src/frontends/onnx/frontend/src/core/tensor.cpp b/src/frontends/onnx/frontend/src/core/tensor.cpp
index 75af5419e29..b63ecc82b88 100644
--- a/src/frontends/onnx/frontend/src/core/tensor.cpp
+++ b/src/frontends/onnx/frontend/src/core/tensor.cpp
@@ -5,6 +5,7 @@
#include "core/tensor.hpp"
#include "input_model.hpp"
+#include "openvino/core/rt_info/weightless_caching_attributes.hpp"
#include "openvino/util/file_util.hpp"
namespace ov {
@@ -478,6 +479,10 @@ std::shared_ptr<ov::op::v0::Constant> Tensor::get_ov_constant() const {
}
std::shared_ptr<ov::AlignedBuffer> constant_buffer;
bool external_data_valid = has_external_data();
+ // Only set for data backed by an external file. In the ORT case the "offset" is a raw
+ // memory address of a temporary buffer, not a reloadable file offset, so it is skipped.
+ bool has_weightless_offset = false;
+ size_t weightless_offset = 0;
if (external_data_valid) {
const auto ext_data = m_tensor_place != nullptr
? detail::TensorExternalData(*m_tensor_place->get_data_location(),
@@ -489,8 +494,12 @@ std::shared_ptr<ov::op::v0::Constant> Tensor::get_ov_constant() const {
constant_buffer = ext_data.load_external_mem_data();
} else if (m_mmap_cache) {
constant_buffer = ext_data.load_external_mmap_data(m_model_dir, m_mmap_cache);
+ has_weightless_offset = true;
+ weightless_offset = ext_data.offset();
} else {
constant_buffer = ext_data.load_external_data(m_model_dir);
+ has_weightless_offset = true;
+ weightless_offset = ext_data.offset();
}
if (element_count == 0 && constant_buffer) {
element_count = constant_buffer->size() * 8 / ov_type.bitwidth();
@@ -507,6 +516,8 @@ std::shared_ptr<ov::op::v0::Constant> Tensor::get_ov_constant() const {
}
if (element_count == 0) {
+ // A failsafe constant holds no external data, so it has no offset there
+ has_weightless_offset = false;
constant = common::make_failsafe_constant(ov_type);
} else if (constant_buffer) {
try {
@@ -658,6 +669,12 @@ std::shared_ptr<ov::op::v0::Constant> Tensor::get_ov_constant() const {
constant->get_default_output().set_names({names.begin(), names.end()});
}
}
+
+ if (has_weightless_offset) {
+ constant->get_rt_info()[ov::WeightlessCacheAttribute::get_type_info_static()] =
+ ov::WeightlessCacheAttribute(constant->get_byte_size(), weightless_offset, constant->get_element_type());
+ }
+
return constant;
}
diff --git a/src/frontends/onnx/frontend/src/frontend.cpp b/src/frontends/onnx/frontend/src/frontend.cpp
index 4364279b127..059078dbe19 100644
--- a/src/frontends/onnx/frontend/src/frontend.cpp
+++ b/src/frontends/onnx/frontend/src/frontend.cpp
@@ -104,10 +104,20 @@ bool is_graph_iterator_enabled() {
}
// !!! Experimental feature, it may be changed or removed in the future !!!
+// Constants loaded from an external data file are already marked with a real offset there.
+// If a model has such constants, the rest are left unmarked, otherwise all constants are marked
+// with an index in order of walking through the model.
void enumerate_constants(const std::shared_ptr<ov::Model>& model) {
const auto& operations = model->get_ordered_ops();
+ const auto has_external_data =
+ std::any_of(operations.begin(), operations.end(), [](const std::shared_ptr<ov::Node>& operation) {
+ return operation->get_rt_info().count(ov::WeightlessCacheAttribute::get_type_info_static()) > 0;
+ });
+ if (has_external_data)
+ return;
+
for (uint32_t idx = 0; idx < operations.size(); ++idx) {
- const auto& const_node = std::dynamic_pointer_cast<ov::op::v0::Constant>(operations[idx]);
+ const auto& const_node = ov::as_type_ptr<ov::op::v0::Constant>(operations[idx]);
if (const_node == nullptr)
continue;
const_node->get_rt_info()[ov::WeightlessCacheAttribute::get_type_info_static()] =
diff --git a/src/frontends/onnx/frontend/src/utils/tensor_external_data.hpp b/src/frontends/onnx/frontend/src/utils/tensor_external_data.hpp
index f0239503713..b59b6395035 100644
--- a/src/frontends/onnx/frontend/src/utils/tensor_external_data.hpp
+++ b/src/frontends/onnx/frontend/src/utils/tensor_external_data.hpp
@@ -75,6 +75,14 @@ public:
return m_data_location;
}
+ /// \brief Object contains a data offset after construction. Method allows read-only access to this
+ /// information.
+ ///
+ /// \return Returns a stored offset (in bytes) of the data in the external file
+ uint64_t offset() const {
+ return m_offset;
+ }
+
private:
std::string m_data_location{};
uint64_t m_offset = 0;
diff --git a/src/frontends/onnx/tests/onnx_reader_external_data.cpp b/src/frontends/onnx/tests/onnx_reader_external_data.cpp
index f5449fdb538..4f301429ce2 100644
--- a/src/frontends/onnx/tests/onnx_reader_external_data.cpp
+++ b/src/frontends/onnx/tests/onnx_reader_external_data.cpp
@@ -68,28 +68,64 @@ TEST_P(OnnxFeMmapFixture, onnx_external_data_0_size_offset) {
test_case.run();
}
-// !!! Experimental feature, it may be changed or removed in the future !!!
-TEST_P(OnnxFeMmapFixture, onnx_external_data_enumerating) {
+TEST_P(OnnxFeMmapFixture, onnx_external_data_weightless_cache_attribute) {
const auto path =
test::utils::getModelFromTestModelZoo(string(TEST_ONNX_MODELS_DIRNAME) + "external_data/external_data.onnx");
Core core;
core.set_property(enable_mmap(GetParam()));
const auto model = core.read_model(path);
+
+ // "A" is an external initializer, so a real offset and size are expected.
+ // The model has external data, so other constants are expected to have no attribute.
+ const auto& operations = model->get_ordered_ops();
+ size_t external_constants = 0;
+ for (const auto& operation : operations) {
+ const auto& const_node = ov::as_type_ptr<ov::op::v0::Constant>(operation);
+ if (const_node == nullptr)
+ continue;
+ const auto& rt_info = const_node->get_rt_info();
+ const auto it = rt_info.find(ov::WeightlessCacheAttribute::get_type_info_static());
+ if (const_node->get_friendly_name() == "A") {
+ ASSERT_NE(it, rt_info.end()) << "Constant 'A' is expected to have WeightlessCacheAttribute";
+ const auto& weightless_cache = it->second.as<ov::WeightlessCacheAttribute>();
+ EXPECT_EQ(weightless_cache.original_dtype, const_node->get_element_type());
+ EXPECT_EQ(weightless_cache.bin_offset, 0);
+ EXPECT_EQ(weightless_cache.original_size, const_node->get_byte_size());
+ ++external_constants;
+ } else {
+ EXPECT_EQ(it, rt_info.end()) << "Constant '" << const_node->get_friendly_name()
+ << "' is not external, so it is expected to have no WeightlessCacheAttribute";
+ }
+ }
+ EXPECT_EQ(external_constants, 1);
+}
+
+TEST(OnnxWeightlessCacheAttribute, onnx_no_external_data_weightless_cache_attribute) {
+ const auto path =
+ test::utils::getModelFromTestModelZoo(string(TEST_ONNX_MODELS_DIRNAME) + "add_abc_initializers.onnx");
+ Core core;
+ const auto model = core.read_model(path);
+
+ // The model has no external data, so all constants are enumerated:
+ // an index is used as an offset and a size stays unknown.
const auto& operations = model->get_ordered_ops();
+ size_t constants = 0;
for (uint32_t idx = 0; idx < operations.size(); ++idx) {
- const auto& const_node = std::dynamic_pointer_cast<ov::op::v0::Constant>(operations[idx]);
+ const auto& const_node = ov::as_type_ptr<ov::op::v0::Constant>(operations[idx]);
if (const_node == nullptr)
continue;
- EXPECT_TRUE(const_node->get_rt_info()[ov::WeightlessCacheAttribute::get_type_info_static()]
- .is<ov::WeightlessCacheAttribute>());
- const auto& weightless_cache = const_node->get_rt_info()[ov::WeightlessCacheAttribute::get_type_info_static()]
- .as<ov::WeightlessCacheAttribute>();
- EXPECT_EQ(weightless_cache.original_size, 0);
- EXPECT_EQ(weightless_cache.bin_offset, idx);
+ const auto& rt_info = const_node->get_rt_info();
+ const auto it = rt_info.find(ov::WeightlessCacheAttribute::get_type_info_static());
+ ASSERT_NE(it, rt_info.end()) << "Constant '" << const_node->get_friendly_name()
+ << "' is expected to have WeightlessCacheAttribute";
+ const auto& weightless_cache = it->second.as<ov::WeightlessCacheAttribute>();
EXPECT_EQ(weightless_cache.original_dtype, const_node->get_element_type());
+ EXPECT_EQ(weightless_cache.bin_offset, idx);
+ EXPECT_EQ(weightless_cache.original_size, 0);
+ ++constants;
}
+ EXPECT_GT(constants, 0);
}
-// !!! End of Experimental feature
TEST_P(OnnxFeMmapFixture, onnx_external_data_from_stream) {
const auto path =