From 659ff0d1affbbcbe637df594d6dbb2344e28bff9 Mon Sep 17 00:00:00 2001 From: Georgy Krivoruchko Date: Mon, 16 Dec 2024 09:37:40 +0400 Subject: [PATCH] [ONNX] Fixed wrong type casting for u/int16 when it stored externally (#28016) ### Details: - Fixed wrong type casting for u/int16 - Added more tests for correctness validation of a values from external file ### Tickets: - 159142 --- .../onnx/frontend/src/core/tensor.hpp | 2 +- .../external_data_int16.prototxt | 75 ++++++++++++++++++ .../external_data/external_data_int4.prototxt | 65 ++++++++++++++++ .../external_data/external_data_int8.prototxt | 75 ++++++++++++++++++ .../external_data_uint16.prototxt | 75 ++++++++++++++++++ .../external_data_uint4.prototxt | 65 ++++++++++++++++ .../external_data_uint8.prototxt | 75 ++++++++++++++++++ .../onnx/tests/onnx_reader_external_data.cpp | 76 +++++++++++++++++++ 8 files changed, 507 insertions(+), 1 deletion(-) create mode 100644 src/frontends/onnx/tests/models/external_data/external_data_int16.prototxt create mode 100644 src/frontends/onnx/tests/models/external_data/external_data_int4.prototxt create mode 100644 src/frontends/onnx/tests/models/external_data/external_data_int8.prototxt create mode 100644 src/frontends/onnx/tests/models/external_data/external_data_uint16.prototxt create mode 100644 src/frontends/onnx/tests/models/external_data/external_data_uint4.prototxt create mode 100644 src/frontends/onnx/tests/models/external_data/external_data_uint8.prototxt diff --git a/src/frontends/onnx/frontend/src/core/tensor.hpp b/src/frontends/onnx/frontend/src/core/tensor.hpp index af4d299f9d45e7..a63cdfd1906bb0 100644 --- a/src/frontends/onnx/frontend/src/core/tensor.hpp +++ b/src/frontends/onnx/frontend/src/core/tensor.hpp @@ -313,7 +313,7 @@ class Tensor { } else { buffer = ext_data.load_external_data(m_model_dir); } - return std::vector(buffer->get_ptr(), buffer->get_ptr() + buffer->size()); + return std::vector(buffer->get_ptr(), buffer->get_ptr() + (buffer->size() / sizeof(T))); } const void* get_data_ptr() const { diff --git a/src/frontends/onnx/tests/models/external_data/external_data_int16.prototxt b/src/frontends/onnx/tests/models/external_data/external_data_int16.prototxt new file mode 100644 index 00000000000000..450e0d8ddc5325 --- /dev/null +++ b/src/frontends/onnx/tests/models/external_data/external_data_int16.prototxt @@ -0,0 +1,75 @@ +ir_version: 3 +producer_name: "OpenVINO ONNX Frontend" +graph { + node { + input: "A" + input: "B" + output: "Y" + name: "add_node1" + op_type: "Add" + } + name: "test_graph" + initializer { + dims: 2 + dims: 2 + data_type: 5 + name: "A" + external_data { + key: "location", + value: "tensors_data/tensor.data" + } + external_data { + key: "length", + value: "8" + } + data_location: 1 + } + input { + name: "A" + type { + tensor_type { + elem_type: 5 + shape { + dim { + dim_value: 2 + } + dim { + dim_value: 2 + } + } + } + } + } + input { + name: "B" + type { + tensor_type { + elem_type: 5 + shape { + dim { + dim_value: 1 + } + } + } + } + } + output { + name: "Y" + type { + tensor_type { + elem_type: 5 + shape { + dim { + dim_value: 2 + } + dim { + dim_value: 2 + } + } + } + } + } +} +opset_import { + version: 4 +} diff --git a/src/frontends/onnx/tests/models/external_data/external_data_int4.prototxt b/src/frontends/onnx/tests/models/external_data/external_data_int4.prototxt new file mode 100644 index 00000000000000..836d2fab8f0cb4 --- /dev/null +++ b/src/frontends/onnx/tests/models/external_data/external_data_int4.prototxt @@ -0,0 +1,65 @@ +ir_version: 3 +producer_name: "OpenVINO ONNX Frontend" +graph { + node { + input: "A" + output: "Y" + name: "add_node1" + op_type: "Identity" + } + name: "test_graph" + initializer { + dims: 2 + dims: 2 + data_type: 22 + name: "A" + external_data { + key: "location", + value: "tensors_data/tensor.data" + } + external_data { + key: "offset", + value: "2" + } + external_data { + key: "length", + value: "2" + } + data_location: 1 + } + input { + name: "A" + type { + tensor_type { + elem_type: 22 + shape { + dim { + dim_value: 2 + } + dim { + dim_value: 2 + } + } + } + } + } + output { + name: "Y" + type { + tensor_type { + elem_type: 22 + shape { + dim { + dim_value: 2 + } + dim { + dim_value: 2 + } + } + } + } + } +} +opset_import { + version: 21 +} diff --git a/src/frontends/onnx/tests/models/external_data/external_data_int8.prototxt b/src/frontends/onnx/tests/models/external_data/external_data_int8.prototxt new file mode 100644 index 00000000000000..d741a7e8168530 --- /dev/null +++ b/src/frontends/onnx/tests/models/external_data/external_data_int8.prototxt @@ -0,0 +1,75 @@ +ir_version: 3 +producer_name: "OpenVINO ONNX Frontend" +graph { + node { + input: "A" + input: "B" + output: "Y" + name: "add_node1" + op_type: "Add" + } + name: "test_graph" + initializer { + dims: 2 + dims: 2 + data_type: 3 + name: "A" + external_data { + key: "location", + value: "tensors_data/tensor.data" + } + external_data { + key: "length", + value: "4" + } + data_location: 1 + } + input { + name: "A" + type { + tensor_type { + elem_type: 3 + shape { + dim { + dim_value: 2 + } + dim { + dim_value: 2 + } + } + } + } + } + input { + name: "B" + type { + tensor_type { + elem_type: 3 + shape { + dim { + dim_value: 1 + } + } + } + } + } + output { + name: "Y" + type { + tensor_type { + elem_type: 3 + shape { + dim { + dim_value: 2 + } + dim { + dim_value: 2 + } + } + } + } + } +} +opset_import { + version: 4 +} diff --git a/src/frontends/onnx/tests/models/external_data/external_data_uint16.prototxt b/src/frontends/onnx/tests/models/external_data/external_data_uint16.prototxt new file mode 100644 index 00000000000000..8d144ebc054e40 --- /dev/null +++ b/src/frontends/onnx/tests/models/external_data/external_data_uint16.prototxt @@ -0,0 +1,75 @@ +ir_version: 3 +producer_name: "OpenVINO ONNX Frontend" +graph { + node { + input: "A" + input: "B" + output: "Y" + name: "add_node1" + op_type: "Add" + } + name: "test_graph" + initializer { + dims: 2 + dims: 2 + data_type: 4 + name: "A" + external_data { + key: "location", + value: "tensors_data/tensor.data" + } + external_data { + key: "length", + value: "8" + } + data_location: 1 + } + input { + name: "A" + type { + tensor_type { + elem_type: 4 + shape { + dim { + dim_value: 2 + } + dim { + dim_value: 2 + } + } + } + } + } + input { + name: "B" + type { + tensor_type { + elem_type: 4 + shape { + dim { + dim_value: 1 + } + } + } + } + } + output { + name: "Y" + type { + tensor_type { + elem_type: 4 + shape { + dim { + dim_value: 2 + } + dim { + dim_value: 2 + } + } + } + } + } +} +opset_import { + version: 4 +} diff --git a/src/frontends/onnx/tests/models/external_data/external_data_uint4.prototxt b/src/frontends/onnx/tests/models/external_data/external_data_uint4.prototxt new file mode 100644 index 00000000000000..df60a23ba29929 --- /dev/null +++ b/src/frontends/onnx/tests/models/external_data/external_data_uint4.prototxt @@ -0,0 +1,65 @@ +ir_version: 3 +producer_name: "OpenVINO ONNX Frontend" +graph { + node { + input: "A" + output: "Y" + name: "add_node1" + op_type: "Identity" + } + name: "test_graph" + initializer { + dims: 2 + dims: 2 + data_type: 21 + name: "A" + external_data { + key: "location", + value: "tensors_data/tensor.data" + } + external_data { + key: "offset", + value: "2" + } + external_data { + key: "length", + value: "2" + } + data_location: 1 + } + input { + name: "A" + type { + tensor_type { + elem_type: 21 + shape { + dim { + dim_value: 2 + } + dim { + dim_value: 2 + } + } + } + } + } + output { + name: "Y" + type { + tensor_type { + elem_type: 21 + shape { + dim { + dim_value: 2 + } + dim { + dim_value: 2 + } + } + } + } + } +} +opset_import { + version: 21 +} diff --git a/src/frontends/onnx/tests/models/external_data/external_data_uint8.prototxt b/src/frontends/onnx/tests/models/external_data/external_data_uint8.prototxt new file mode 100644 index 00000000000000..3f0cfa8406744e --- /dev/null +++ b/src/frontends/onnx/tests/models/external_data/external_data_uint8.prototxt @@ -0,0 +1,75 @@ +ir_version: 3 +producer_name: "OpenVINO ONNX Frontend" +graph { + node { + input: "A" + input: "B" + output: "Y" + name: "add_node1" + op_type: "Add" + } + name: "test_graph" + initializer { + dims: 2 + dims: 2 + data_type: 2 + name: "A" + external_data { + key: "location", + value: "tensors_data/tensor.data" + } + external_data { + key: "length", + value: "4" + } + data_location: 1 + } + input { + name: "A" + type { + tensor_type { + elem_type: 2 + shape { + dim { + dim_value: 2 + } + dim { + dim_value: 2 + } + } + } + } + } + input { + name: "B" + type { + tensor_type { + elem_type: 2 + shape { + dim { + dim_value: 1 + } + } + } + } + } + output { + name: "Y" + type { + tensor_type { + elem_type: 2 + shape { + dim { + dim_value: 2 + } + dim { + dim_value: 2 + } + } + } + } + } +} +opset_import { + version: 4 +} diff --git a/src/frontends/onnx/tests/onnx_reader_external_data.cpp b/src/frontends/onnx/tests/onnx_reader_external_data.cpp index 103099d08bced9..7d0fbbf6240f55 100644 --- a/src/frontends/onnx/tests/onnx_reader_external_data.cpp +++ b/src/frontends/onnx/tests/onnx_reader_external_data.cpp @@ -255,4 +255,80 @@ TEST_P(OnnxFeMmapFixture, onnx_external_data_in_constant_node) { test_case.run(); } +TEST_P(OnnxFeMmapFixture, onnx_external_data_int16) { + const auto path = test::utils::getModelFromTestModelZoo(string(TEST_ONNX_MODELS_DIRNAME) + + "external_data/external_data_int16.onnx"); + Core core; + core.set_property(enable_mmap(GetParam())); + const auto model = core.read_model(path); + auto test_case = test::TestCase(model); + test_case.add_input({-100}); + test_case.add_expected_output(Shape{2, 2}, {-100, 16156, -100, 16284}); + + test_case.run(); +} + +TEST_P(OnnxFeMmapFixture, onnx_external_data_uint16) { + const auto path = test::utils::getModelFromTestModelZoo(string(TEST_ONNX_MODELS_DIRNAME) + + "external_data/external_data_uint16.onnx"); + Core core; + core.set_property(enable_mmap(GetParam())); + const auto model = core.read_model(path); + auto test_case = test::TestCase(model); + test_case.add_input({100}); + test_case.add_expected_output(Shape{2, 2}, {100, 16356, 100, 16484}); + + test_case.run(); +} + +TEST_P(OnnxFeMmapFixture, onnx_external_data_int8) { + const auto path = test::utils::getModelFromTestModelZoo(string(TEST_ONNX_MODELS_DIRNAME) + + "external_data/external_data_int8.onnx"); + Core core; + core.set_property(enable_mmap(GetParam())); + const auto model = core.read_model(path); + auto test_case = test::TestCase(model); + test_case.add_input({-100}); + test_case.add_expected_output(Shape{2, 2}, {-100, 106, -100, -37}); + + test_case.run(); +} + +TEST_P(OnnxFeMmapFixture, onnx_external_data_uint8) { + const auto path = test::utils::getModelFromTestModelZoo(string(TEST_ONNX_MODELS_DIRNAME) + + "external_data/external_data_uint8.onnx"); + Core core; + core.set_property(enable_mmap(GetParam())); + const auto model = core.read_model(path); + auto test_case = test::TestCase(model); + test_case.add_input({100}); + test_case.add_expected_output(Shape{2, 2}, {100, 100, 228, 163}); + + test_case.run(); +} + +TEST_P(OnnxFeMmapFixture, onnx_external_data_int4) { + const auto path = test::utils::getModelFromTestModelZoo(string(TEST_ONNX_MODELS_DIRNAME) + + "external_data/external_data_int4.onnx"); + Core core; + core.set_property(enable_mmap(GetParam())); + const auto model = core.read_model(path); + auto test_case = test::TestCase(model); + test_case.add_expected_output(Shape{2, 2}, {static_cast(0x80), 0x3f}); + + test_case.run(); +} + +TEST_P(OnnxFeMmapFixture, onnx_external_data_uint4) { + const auto path = test::utils::getModelFromTestModelZoo(string(TEST_ONNX_MODELS_DIRNAME) + + "external_data/external_data_uint4.onnx"); + Core core; + core.set_property(enable_mmap(GetParam())); + const auto model = core.read_model(path); + auto test_case = test::TestCase(model); + test_case.add_expected_output(Shape{2, 2}, {0x80, 0x3f}); + + test_case.run(); +} + INSTANTIATE_TEST_SUITE_P(OnnxFeMMapReadModel, OnnxFeMmapFixture, ::testing::Bool());