Commit 56d3a46a authored by Michał Karzyński's avatar Michał Karzyński Committed by Scott Cyphers

[ONNX] Unit tests for DequantizeLinear (#2704)

* [ONNX] Unit test models for DequantizeLinear

* add unit tests and suppot axis

* ignore unit tests on GPU

* fix build warning
parent 7d69434a
......@@ -55,10 +55,25 @@ namespace ngraph
Shape y_scale_shape = x_scale->get_shape();
Shape y_zero_point_shape = zero_point->get_shape();
ASSERT_VALID_ARGUMENT(node, y_scale_shape.size() == 0)
<< "x_scale must be a scalar.";
ASSERT_VALID_ARGUMENT(node, y_zero_point_shape.size() == 0)
<< "zero_point must be a scalar.";
// get axis twice with two default values to see if it is set
int64_t axis_0{node.get_attribute_value<int64_t>("axis", 0)};
int64_t axis_1{node.get_attribute_value<int64_t>("axis", 1)};
AxisSet axes;
// if axis attribute is set
if (axis_0 == axis_1)
{
// positive axis
if (axis_0 >= 0)
{
axes.insert(axis_0);
}
// negative axis
else if (axis_0 < 0)
{
axes.insert(x->get_shape().size() + axis_0);
}
}
if (x->get_element_type() != zero_point->get_element_type())
{
......@@ -67,7 +82,7 @@ namespace ngraph
}
return {std::make_shared<ngraph::op::Dequantize>(
x, x_scale, zero_point, x_scale->get_element_type(), AxisSet{})};
x, x_scale, zero_point, x_scale->get_element_type(), axes)};
}
} // namespace set_1
......
......@@ -115,6 +115,12 @@ pad_reflect_2d_with_neg
# Quantized operators are not supported on gpu backend
model_dequantize_linear
model_dequantize_linear_scalar_zero_scale_uint8
model_dequantize_linear_scalar_zero_scale_int8
model_dequantize_linear_1d_zero_scale_uint8
model_dequantize_linear_1d_zero_scale_int8
model_dequantize_linear_1d_zero_scale_int8_4d
model_dequantize_linear_1d_zero_scale_uint8_negative_axis
model_quantize_linear
model_quantize_linear_zero_point
quantize_linear_axis_zero
......
ir_version: 3
producer_name: "ngraph ONNXImporter"
graph {
node {
input: "x"
input: "x_scale"
input: "x_zero_point"
output: "y"
name: "node1"
op_type: "DequantizeLinear"
}
name: "test"
input {
name: "x"
type {
tensor_type {
elem_type: 2
shape {
dim {
dim_value: 4
}
}
}
}
}
input {
name: "x_scale"
type {
tensor_type {
elem_type: 1
shape {
}
}
}
}
input {
name: "x_zero_point"
type {
tensor_type {
elem_type: 2
shape {
}
}
}
}
output {
name: "y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 4
}
}
}
}
}
}
opset_import {
version: 10
}
ir_version: 3
producer_name: "ngraph ONNXImporter"
graph {
node {
input: "x"
input: "x_scale"
input: "x_zero_point"
output: "y"
name: "node1"
op_type: "DequantizeLinear"
}
name: "test"
input {
name: "x"
type {
tensor_type {
elem_type: 3
shape {
dim {
dim_value: 4
}
}
}
}
}
input {
name: "x_scale"
type {
tensor_type {
elem_type: 1
shape {
}
}
}
}
input {
name: "x_zero_point"
type {
tensor_type {
elem_type: 3
shape {
}
}
}
}
output {
name: "y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 4
}
}
}
}
}
}
opset_import {
version: 10
}
ir_version: 3
producer_name: "ngraph ONNXImporter"
graph {
node {
input: "X"
input: "scale"
input: "zero_point"
output: "Y"
name: "node1"
op_type: "DequantizeLinear"
attribute {
name: "axis"
i: 0
type: INT
}
}
name: "test"
input {
name: "X"
type {
tensor_type {
elem_type: 2
shape {
dim {
dim_value: 3
}
dim {
dim_value: 4
}
}
}
}
}
input {
name: "scale"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
input {
name: "zero_point"
type {
tensor_type {
elem_type: 2
shape {
dim {
dim_value: 3
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
dim {
dim_value: 4
}
}
}
}
}
}
opset_import {
version: 10
}
ir_version: 3
producer_name: "ngraph ONNXImporter"
graph {
node {
input: "X"
input: "scale"
input: "zero_point"
output: "Y"
name: "node1"
op_type: "DequantizeLinear"
attribute {
name: "axis"
i: 1
type: INT
}
}
name: "test"
input {
name: "X"
type {
tensor_type {
elem_type: 3
shape {
dim {
dim_value: 3
}
dim {
dim_value: 4
}
}
}
}
}
input {
name: "scale"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 4
}
}
}
}
}
input {
name: "zero_point"
type {
tensor_type {
elem_type: 3
shape {
dim {
dim_value: 4
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
dim {
dim_value: 4
}
}
}
}
}
}
opset_import {
version: 10
}
ir_version: 3
producer_name: "ngraph ONNXImporter"
graph {
node {
input: "X"
input: "scale"
input: "zero_point"
output: "Y"
name: "node1"
op_type: "DequantizeLinear"
attribute {
name: "axis"
i: 1
type: INT
}
}
name: "test"
input {
name: "X"
type {
tensor_type {
elem_type: 3
shape {
dim {
dim_value: 2
}
dim {
dim_value: 3
}
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
input {
name: "scale"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
input {
name: "zero_point"
type {
tensor_type {
elem_type: 3
shape {
dim {
dim_value: 3
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 3
}
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
}
opset_import {
version: 10
}
ir_version: 3
producer_name: "ngraph ONNXImporter"
graph {
node {
input: "X"
input: "scale"
input: "zero_point"
output: "Y"
name: "node1"
op_type: "DequantizeLinear"
attribute {
name: "axis"
i: -2
type: INT
}
}
name: "test"
input {
name: "X"
type {
tensor_type {
elem_type: 2
shape {
dim {
dim_value: 3
}
dim {
dim_value: 4
}
}
}
}
}
input {
name: "scale"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
input {
name: "zero_point"
type {
tensor_type {
elem_type: 2
shape {
dim {
dim_value: 3
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
dim {
dim_value: 4
}
}
}
}
}
}
opset_import {
version: 10
}
......@@ -2417,6 +2417,251 @@ NGRAPH_TEST(onnx_${BACKEND_NAME}, model_dequantize_linear)
EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_dequantize_linear_scalar_zero_scale_uint8)
{
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/dequantize_linear_0.prototxt"));
auto x = std::vector<uint8_t>{0, 3, 128, 255};
auto scale = std::vector<float>{2.0f};
auto zero_point = std::vector<uint8_t>{128};
auto backend = ngraph::runtime::Backend::create("${BACKEND_NAME}");
auto params = function->get_parameters();
std::vector<std::shared_ptr<ngraph::runtime::Tensor>> input_tensors;
input_tensors.push_back(
backend->create_tensor(params.at(0)->get_element_type(), params.at(0)->get_shape()));
input_tensors.push_back(
backend->create_tensor(params.at(1)->get_element_type(), params.at(1)->get_shape()));
input_tensors.push_back(
backend->create_tensor(params.at(2)->get_element_type(), params.at(2)->get_shape()));
copy_data(input_tensors[0], x);
copy_data(input_tensors[1], scale);
copy_data(input_tensors[2], zero_point);
auto results = function->get_results();
std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors;
result_tensors.push_back(
backend->create_tensor(results.at(0)->get_element_type(), results.at(0)->get_shape()));
auto handle = backend->compile(function);
handle->call_with_validate(result_tensors, input_tensors);
std::vector<std::vector<float>> outputs;
outputs.push_back(read_vector<float>(result_tensors[0]));
auto expected_output = std::vector<std::vector<float>>{{-256.0f, -250.0f, 0.0f, 254.0f}};
EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_dequantize_linear_scalar_zero_scale_int8)
{
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/dequantize_linear_1.prototxt"));
auto x = std::vector<int8_t>{-30, -3, 100, 127};
auto scale = std::vector<float>{2.0f};
auto zero_point = std::vector<int8_t>{-10};
auto backend = ngraph::runtime::Backend::create("${BACKEND_NAME}");
auto params = function->get_parameters();
std::vector<std::shared_ptr<ngraph::runtime::Tensor>> input_tensors;
input_tensors.push_back(
backend->create_tensor(params.at(0)->get_element_type(), params.at(0)->get_shape()));
input_tensors.push_back(
backend->create_tensor(params.at(1)->get_element_type(), params.at(1)->get_shape()));
input_tensors.push_back(
backend->create_tensor(params.at(2)->get_element_type(), params.at(2)->get_shape()));
copy_data(input_tensors[0], x);
copy_data(input_tensors[1], scale);
copy_data(input_tensors[2], zero_point);
auto results = function->get_results();
std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors;
result_tensors.push_back(
backend->create_tensor(results.at(0)->get_element_type(), results.at(0)->get_shape()));
auto handle = backend->compile(function);
handle->call_with_validate(result_tensors, input_tensors);
std::vector<std::vector<float>> outputs;
outputs.push_back(read_vector<float>(result_tensors[0]));
auto expected_output = std::vector<std::vector<float>>{{-40.0f, 14.0f, 220.0f, 274.0f}};
EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_dequantize_linear_1d_zero_scale_uint8)
{
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/dequantize_linear_2.prototxt"));
auto x = std::vector<uint8_t>{0, 1, 2, 3, 0, 1, 2, 3, 0, 10, 20, 30};
auto scale = std::vector<float>{1.0f, 2.0f, 4.0f};
auto zero_point = std::vector<uint8_t>{0, 0, 0};
auto backend = ngraph::runtime::Backend::create("${BACKEND_NAME}");
auto params = function->get_parameters();
std::vector<std::shared_ptr<ngraph::runtime::Tensor>> input_tensors;
input_tensors.push_back(
backend->create_tensor(params.at(0)->get_element_type(), params.at(0)->get_shape()));
input_tensors.push_back(
backend->create_tensor(params.at(1)->get_element_type(), params.at(1)->get_shape()));
input_tensors.push_back(
backend->create_tensor(params.at(2)->get_element_type(), params.at(2)->get_shape()));
copy_data(input_tensors[0], x);
copy_data(input_tensors[1], scale);
copy_data(input_tensors[2], zero_point);
auto results = function->get_results();
std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors;
result_tensors.push_back(
backend->create_tensor(results.at(0)->get_element_type(), results.at(0)->get_shape()));
auto handle = backend->compile(function);
handle->call_with_validate(result_tensors, input_tensors);
std::vector<std::vector<float>> outputs;
outputs.push_back(read_vector<float>(result_tensors[0]));
auto expected_output = std::vector<std::vector<float>>{
{0.0f, 1.0f, 2.0f, 3.0f, 0.0f, 2.0f, 4.0f, 6.0f, 0.0f, 40.0f, 80.0f, 120.0f}};
EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_dequantize_linear_1d_zero_scale_int8)
{
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/dequantize_linear_3.prototxt"));
auto x = std::vector<int8_t>{0, 1, 2, 3, 0, 2, 4, 6, 0, 10, 20, 30};
auto scale = std::vector<float>{1.0f, 2.0f, 4.0f, 8.0f};
auto zero_point = std::vector<int8_t>{0, -10, -20, -30};
auto backend = ngraph::runtime::Backend::create("${BACKEND_NAME}");
auto params = function->get_parameters();
std::vector<std::shared_ptr<ngraph::runtime::Tensor>> input_tensors;
input_tensors.push_back(
backend->create_tensor(params.at(0)->get_element_type(), params.at(0)->get_shape()));
input_tensors.push_back(
backend->create_tensor(params.at(1)->get_element_type(), params.at(1)->get_shape()));
input_tensors.push_back(
backend->create_tensor(params.at(2)->get_element_type(), params.at(2)->get_shape()));
copy_data(input_tensors[0], x);
copy_data(input_tensors[1], scale);
copy_data(input_tensors[2], zero_point);
auto results = function->get_results();
std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors;
result_tensors.push_back(
backend->create_tensor(results.at(0)->get_element_type(), results.at(0)->get_shape()));
auto handle = backend->compile(function);
handle->call_with_validate(result_tensors, input_tensors);
std::vector<std::vector<float>> outputs;
outputs.push_back(read_vector<float>(result_tensors[0]));
auto expected_output = std::vector<std::vector<float>>{
{0.0f, 22.0f, 88.0f, 264.0f, 0.0f, 24.0f, 96.0f, 288.0f, 0.0f, 40.0f, 160.0f, 480.0f}};
EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_dequantize_linear_1d_zero_scale_int8_4d)
{
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/dequantize_linear_4.prototxt"));
auto x = std::vector<int8_t>{7, 9, 10, 10, 5, 8, 9, 1, 8, 6, 7, 9, 10, 0, 7, 10,
8, 2, 6, 0, 5, 9, 8, 1, 2, 7, 5, 3, 2, 4, 1, 3,
8, 7, 4, 8, 10, 1, 5, 5, 7, 7, 0, 2, 4, 4, 0, 5};
auto scale = std::vector<float>{1.0f, 10.0f, 7.0f};
auto zero_point = std::vector<int8_t>{10, 2, 1};
auto backend = ngraph::runtime::Backend::create("${BACKEND_NAME}");
auto params = function->get_parameters();
std::vector<std::shared_ptr<ngraph::runtime::Tensor>> input_tensors;
input_tensors.push_back(
backend->create_tensor(params.at(0)->get_element_type(), params.at(0)->get_shape()));
input_tensors.push_back(
backend->create_tensor(params.at(1)->get_element_type(), params.at(1)->get_shape()));
input_tensors.push_back(
backend->create_tensor(params.at(2)->get_element_type(), params.at(2)->get_shape()));
copy_data(input_tensors[0], x);
copy_data(input_tensors[1], scale);
copy_data(input_tensors[2], zero_point);
auto results = function->get_results();
std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors;
result_tensors.push_back(
backend->create_tensor(results.at(0)->get_element_type(), results.at(0)->get_shape()));
auto handle = backend->compile(function);
handle->call_with_validate(result_tensors, input_tensors);
std::vector<std::vector<float>> outputs;
outputs.push_back(read_vector<float>(result_tensors[0]));
auto expected_output = std::vector<std::vector<float>>{
{-3.0f, -1.0f, 0.0f, 0.0f, -5.0f, -2.0f, -1.0f, -9.0f, 60.0f, 40.0f, 50.0f, 70.0f,
80.0f, -20.0f, 50.0f, 80.0f, 49.0f, 7.0f, 35.0f, -7.0f, 28.0f, 56.0f, 49.0f, 0.0f,
-8.0f, -3.0f, -5.0f, -7.0f, -8.0f, -6.0f, -9.0f, -7.0f, 60.0f, 50.0f, 20.0f, 60.0f,
80.0f, -10.0f, 30.0f, 30.0f, 42.0f, 42.0f, -7.0f, 7.0f, 21.0f, 21.0f, -7.0f, 28.0f}};
EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_dequantize_linear_1d_zero_scale_uint8_negative_axis)
{
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/dequantize_linear_5.prototxt"));
auto x = std::vector<uint8_t>{0, 1, 2, 3, 0, 1, 2, 3, 0, 10, 20, 30};
auto scale = std::vector<float>{1.0f, 2.0f, 4.0f};
auto zero_point = std::vector<uint8_t>{0, 0, 0};
auto backend = ngraph::runtime::Backend::create("${BACKEND_NAME}");
auto params = function->get_parameters();
std::vector<std::shared_ptr<ngraph::runtime::Tensor>> input_tensors;
input_tensors.push_back(
backend->create_tensor(params.at(0)->get_element_type(), params.at(0)->get_shape()));
input_tensors.push_back(
backend->create_tensor(params.at(1)->get_element_type(), params.at(1)->get_shape()));
input_tensors.push_back(
backend->create_tensor(params.at(2)->get_element_type(), params.at(2)->get_shape()));
copy_data(input_tensors[0], x);
copy_data(input_tensors[1], scale);
copy_data(input_tensors[2], zero_point);
auto results = function->get_results();
std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors;
result_tensors.push_back(
backend->create_tensor(results.at(0)->get_element_type(), results.at(0)->get_shape()));
auto handle = backend->compile(function);
handle->call_with_validate(result_tensors, input_tensors);
std::vector<std::vector<float>> outputs;
outputs.push_back(read_vector<float>(result_tensors[0]));
auto expected_output = std::vector<std::vector<float>>{
{0.0f, 1.0f, 2.0f, 3.0f, 0.0f, 2.0f, 4.0f, 6.0f, 0.0f, 40.0f, 80.0f, 120.0f}};
EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_quant_conv_linear)
{
auto function = onnx_import::import_onnx_model(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment