Skip to content

Commit

Permalink
Applied review comments and added additional tests (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkrivor committed Aug 16, 2024
1 parent 386dced commit 849c582
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 17 deletions.
24 changes: 7 additions & 17 deletions src/frontends/onnx/frontend/src/core/tensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "exceptions.hpp"
#include "onnx_common/utils.hpp"
#include "openvino/core/shape.hpp"
#include "openvino/core/type/element_iterator.hpp"
#include "openvino/core/type/element_type.hpp"
#include "openvino/frontend/exception.hpp"
#include "openvino/runtime/aligned_buffer.hpp"
Expand Down Expand Up @@ -281,24 +282,13 @@ class Tensor {
std::shared_ptr<ov::op::v0::Constant> make_ov_constant(const ov::element::Type& type) const {
std::shared_ptr<ov::op::v0::Constant> constant{nullptr};
auto data = get_data<T>();
auto data_size = data.size();
switch (type) {
case ov::element::i4:
case ov::element::u4:
data_size *= 2; // Each byte contains 2 data items
break;
auto element_count = data.size();
if(ov::element::is_nibble_type(type)) {
element_count *= 2; // Each byte contains 2 data items
}
if (data_size == shape_size(m_shape)) {
switch (type) {
case ov::element::i4:
case ov::element::u4:
constant = std::make_shared<ov::op::v0::Constant>(type, m_shape, data.data());
break;
default:
constant = std::make_shared<ov::op::v0::Constant>(type, m_shape, data);
break;
}
} else if (data_size == 0 && m_shape.size() == 0) {
if (element_count == shape_size(m_shape)) {
constant = std::make_shared<ov::op::v0::Constant>(type, m_shape, data.data());
} else if (element_count == 0 && m_shape.size() == 0) {
constant = common::make_failsafe_constant(type);
} else {
FRONT_END_THROW("Tensor shape doesn't match data size");
Expand Down
57 changes: 57 additions & 0 deletions src/frontends/onnx/tests/models/int4_input.prototxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
ir_version: 9
producer_name: "OpenVINO ONNX Frontend"
graph {
name: "test"
node {
input: "I"
output: "O"
op_type: "Shape"
}
node {
input: "I"
output: "V"
op_type: "Identity"
}
input {
name: "I"
type {
tensor_type {
elem_type: 22
shape {
dim {
dim_value: 5
}
}
}
}
}
output {
name: "O"
type {
tensor_type {
elem_type: 7
shape {
dim {
dim_value: 1
}
}
}
}
}
output {
name: "V"
type {
tensor_type {
elem_type: 22
shape {
dim {
dim_value: 5
}
}
}
}
}
}
opset_import {
version: 21
}
57 changes: 57 additions & 0 deletions src/frontends/onnx/tests/models/uint4_input.prototxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
ir_version: 9
producer_name: "OpenVINO ONNX Frontend"
graph {
name: "test"
node {
input: "I"
output: "O"
op_type: "Shape"
}
node {
input: "I"
output: "V"
op_type: "Identity"
}
input {
name: "I"
type {
tensor_type {
elem_type: 21
shape {
dim {
dim_value: 3
}
}
}
}
}
output {
name: "O"
type {
tensor_type {
elem_type: 7
shape {
dim {
dim_value: 1
}
}
}
}
}
output {
name: "V"
type {
tensor_type {
elem_type: 21
shape {
dim {
dim_value: 3
}
}
}
}
}
}
opset_import {
version: 21
}
20 changes: 20 additions & 0 deletions src/frontends/onnx/tests/onnx_import.in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_int4_const) {
test_case.run();
}

OPENVINO_TEST(${BACKEND_NAME}, onnx_int4_input) {
const auto model = convert_model("int4_input.onnx");
auto test_case = test::TestCase(model);
test_case.add_input<uint8_t>({0xEF, 0x01, 0x70});
test_case.add_expected_output<int64_t>({5});
test_case.add_expected_output<uint8_t>({0xEF, 0x01, 0x70});

test_case.run();
}

OPENVINO_TEST(${BACKEND_NAME}, onnx_uint4_const) {
auto model = convert_model("uint4_const.onnx");

Expand All @@ -175,6 +185,16 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_uint4_const) {
test_case.run();
}

OPENVINO_TEST(${BACKEND_NAME}, onnx_uint4_input) {
const auto model = convert_model("uint4_input.onnx");
auto test_case = test::TestCase(model);
test_case.add_input<uint8_t>({0x01, 0xF0});
test_case.add_expected_output<int64_t>({3});
test_case.add_expected_output<uint8_t>({0x01, 0xF0});

test_case.run();
}

OPENVINO_TEST(${BACKEND_NAME}, onnx_model_add_abc_initializers) {
auto model = convert_model("add_abc_initializers.onnx");

Expand Down

0 comments on commit 849c582

Please sign in to comment.