[ONNX FE] ONNX Clip type aligned default min/max fix (#8489)

This commit is contained in:
Katarzyna Mitrus 2021-11-16 09:25:21 +01:00 committed by GitHub
parent d0f16d205a
commit 95e1a423c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 641 additions and 4 deletions

View File

@ -216,6 +216,9 @@ NGRAPH_API std::shared_ptr<op::Constant> get_constant_max_of_type(element::Type_
/// \brief Returns a Constant storing scalar value equal to std::numeric_limits<t>::min()
NGRAPH_API std::shared_ptr<op::Constant> get_constant_min_of_type(element::Type_t t);
/// \brief Returns a Constant storing scalar value equal to std::numeric_limits<t>::lowest()
NGRAPH_API std::shared_ptr<op::Constant> get_constant_lowest_of_type(element::Type_t t);
/// \brief Checks if size of HostTensorVector is the same as passed size attribute. Then checks
/// that all the HostTensorPtrs are not equal to nullptr
NGRAPH_API bool validate_host_tensor_vector(const HostTensorVector& v, const size_t& size);

View File

@ -1371,6 +1371,37 @@ shared_ptr<op::Constant> ngraph::get_constant_min_of_type(element::Type_t t) {
}
}
std::shared_ptr<op::Constant> ngraph::get_constant_lowest_of_type(element::Type_t t) {
#define NGRAPH_TYPE_TO_LOWEST_CONST(t) \
case t: \
return op::Constant::create(t, \
{}, \
{std::numeric_limits<typename element_type_traits<t>::value_type>::lowest()}); \
break
switch (t) {
NGRAPH_TYPE_TO_LOWEST_CONST(element::boolean);
NGRAPH_TYPE_TO_LOWEST_CONST(element::bf16);
NGRAPH_TYPE_TO_LOWEST_CONST(element::f16);
NGRAPH_TYPE_TO_LOWEST_CONST(element::f32);
NGRAPH_TYPE_TO_LOWEST_CONST(element::f64);
NGRAPH_TYPE_TO_LOWEST_CONST(element::i8);
NGRAPH_TYPE_TO_LOWEST_CONST(element::i16);
NGRAPH_TYPE_TO_LOWEST_CONST(element::i32);
NGRAPH_TYPE_TO_LOWEST_CONST(element::i64);
NGRAPH_TYPE_TO_LOWEST_CONST(element::u1);
NGRAPH_TYPE_TO_LOWEST_CONST(element::u8);
NGRAPH_TYPE_TO_LOWEST_CONST(element::u16);
NGRAPH_TYPE_TO_LOWEST_CONST(element::u32);
NGRAPH_TYPE_TO_LOWEST_CONST(element::u64);
case element::undefined:
case element::dynamic:
default:
return nullptr;
}
}
namespace {
HostTensorPtr equality_mask(const HostTensorPtr& tensor, const shared_ptr<op::Constant>& constant) {

View File

@ -9,6 +9,7 @@
#include "default_opset.hpp"
#include "ngraph/builder/make_constant.hpp"
#include "ngraph/validation_util.hpp"
#include "onnx_import/core/null_node.hpp"
namespace ngraph {
@ -36,19 +37,19 @@ OutputVector clip(const Node& node) {
Output<ngraph::Node> max;
// If second input is provided, assign to min input, otherwise set lowest
// numeric limit of double as min input.
// numeric limit of data type as min input.
if (inputs.size() > 1 && !ngraph::op::is_null(inputs.at(1))) {
min = inputs.at(1);
} else {
min = builder::make_constant_from_double(data_type, Shape{}, std::numeric_limits<double>::lowest());
min = ngraph::get_constant_lowest_of_type(data_type);
}
// If third input is provided, assign to max input, otherwise set maximum
// numeric limit of double as max input.
// numeric limit of data type as max input.
if (inputs.size() == 3 && !ngraph::op::is_null(inputs.at(2))) {
max = inputs.at(2);
} else {
max = builder::make_constant_from_double(data_type, Shape{}, std::numeric_limits<double>::max());
max = ngraph::get_constant_max_of_type(data_type);
}
const auto max_of_min_and_data = std::make_shared<default_opset::Maximum>(min, data);

View File

@ -0,0 +1,48 @@
ir_version: 8
producer_name: "onnx-importer-test"
graph {
node {
input: "X"
input: ""
input: ""
output: "Y"
op_type: "Clip"
}
name: "test-model-clip"
input {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
}
opset_import {
domain: ""
version: 12
}

View File

@ -0,0 +1,48 @@
ir_version: 8
producer_name: "onnx-importer-test"
graph {
node {
input: "X"
input: ""
input: ""
output: "Y"
op_type: "Clip"
}
name: "test-model-clip"
input {
name: "X"
type {
tensor_type {
elem_type: 7
shape {
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 7
shape {
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
}
opset_import {
domain: ""
version: 12
}

View File

@ -0,0 +1,58 @@
ir_version: 8
producer_name: "onnx-importer-test"
graph {
node {
input: "X"
input: ""
input: "MAX"
output: "Y"
op_type: "Clip"
}
name: "test-model-clip"
input {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
input {
name: "MAX"
type {
tensor_type {
elem_type: 1
shape {
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 3
}
}
}
}
}
}
opset_import {
domain: ""
version: 12
}

View File

@ -0,0 +1,58 @@
ir_version: 8
producer_name: "onnx-importer-test"
graph {
node {
input: "X"
input: ""
input: "MAX"
output: "Y"
op_type: "Clip"
}
name: "test-model-clip"
input {
name: "X"
type {
tensor_type {
elem_type: 7
shape {
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
input {
name: "MAX"
type {
tensor_type {
elem_type: 7
shape {
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 7
shape {
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
}
opset_import {
domain: ""
version: 12
}

View File

@ -0,0 +1,58 @@
ir_version: 8
producer_name: "onnx-importer-test"
graph {
node {
input: "X"
input: "MIN"
input: ""
output: "Y"
op_type: "Clip"
}
name: "test-model-clip"
input {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
input {
name: "MIN"
type {
tensor_type {
elem_type: 1
shape {
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
}
opset_import {
domain: ""
version: 12
}

View File

@ -0,0 +1,53 @@
ir_version: 8
producer_name: "onnx-importer-test"
graph {
node {
input: "X"
input: "MIN"
input: ""
output: "Y"
op_type: "Clip"
}
name: "test-model-clip"
initializer {
data_type: 1
float_data: -1.590000033378601
name: "MIN"
}
input {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
}
opset_import {
domain: ""
version: 12
}

View File

@ -0,0 +1,68 @@
ir_version: 8
producer_name: "onnx-importer-test"
graph {
node {
input: "X"
input: "MIN"
input: "MAX"
output: "Y"
op_type: "Clip"
}
name: "test-model-clip"
input {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
input {
name: "MIN"
type {
tensor_type {
elem_type: 1
shape {
}
}
}
}
input {
name: "MAX"
type {
tensor_type {
elem_type: 1
shape {
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
}
opset_import {
domain: ""
version: 12
}

View File

@ -0,0 +1,58 @@
ir_version: 8
producer_name: "onnx-importer-test"
graph {
node {
input: "X"
input: "MIN"
input: "MAX"
output: "Y"
op_type: "Clip"
}
name: "test-model-clip"
initializer {
data_type: 1
float_data: -1.590000033378601
name: "MIN"
}
initializer {
data_type: 1
float_data: 2.009999990463257
name: "MAX"
}
input {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
}
opset_import {
domain: ""
version: 12
}

View File

@ -3675,6 +3675,155 @@ NGRAPH_TEST(${BACKEND_NAME}, onnx_clip_inbounds) {
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_clip_no_min_no_max) {
auto function =
onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/clip_no_min_no_max.onnx"));
auto test_case = test::TestCase<TestEngine>(function);
const std::vector<float> data{-1.6, -0.1, 10., 0., -10., 1.99, 2.015, 3.};
test_case.add_input<float>(data);
test_case.add_expected_output<float>(Shape{2, 4}, data);
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_clip_no_min_no_max_inf) {
auto function =
onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/clip_no_min_no_max.onnx"));
auto test_case = test::TestCase<TestEngine>(function);
const std::vector<float> data{std::numeric_limits<float>::infinity(),
-std::numeric_limits<float>::infinity(),
static_cast<float>(std::numeric_limits<double>::max()),
std::numeric_limits<float>::min(),
std::numeric_limits<float>::max(),
std::numeric_limits<float>::lowest(),
0,
-1};
const std::vector<float> expected_output{std::numeric_limits<float>::max(),
std::numeric_limits<float>::lowest(),
std::numeric_limits<float>::max(),
std::numeric_limits<float>::min(),
std::numeric_limits<float>::max(),
std::numeric_limits<float>::lowest(),
0,
-1};
test_case.add_input<float>(data);
test_case.add_expected_output<float>(Shape{2, 4}, expected_output);
test_case.run_with_tolerance_as_fp(0);
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_clip_no_min_set_max) {
auto function =
onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/clip_no_min_set_max.onnx"));
auto test_case = test::TestCase<TestEngine>(function);
const std::vector<float> data{-1.6, -0.1, 10., 0., -10., 1.99, 2.015, 3.};
const std::vector<float> max_val{2.01};
const std::vector<float> output{-1.6, -0.1, 2.01, 0., -10., 1.99, 2.01, 2.01};
test_case.add_input<float>(data);
test_case.add_input<float>(max_val);
test_case.add_expected_output<float>(Shape{2, 4}, output);
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_clip_set_min_no_max) {
auto function =
onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/clip_set_min_no_max.onnx"));
auto test_case = test::TestCase<TestEngine>(function);
const std::vector<float> data{-1.6, -0.1, 10., 0., -10., 1.99, 2.015, 3.};
const std::vector<float> min_val{-1.59};
const std::vector<float> output{-1.59, -0.1, 10., 0., -1.59, 1.99, 2.015, 3.};
test_case.add_input<float>(data);
test_case.add_input<float>(min_val);
test_case.add_expected_output<float>(Shape{2, 4}, output);
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_clip_no_min_no_max_int64) {
auto function =
onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/clip_no_min_no_max_int64.onnx"));
auto test_case = test::TestCase<TestEngine>(function);
const std::vector<int64_t> data{INT64_MAX, INT64_MIN, INT32_MAX, INT32_MIN, 0, -1, 1, 0};
test_case.add_input<int64_t>(data);
test_case.add_expected_output<int64_t>(Shape{2, 4}, data);
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_clip_no_min_set_max_int64) {
auto function =
onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/clip_no_min_set_max_int64.onnx"));
auto test_case = test::TestCase<TestEngine>(function);
const std::vector<int64_t> data{INT64_MAX, INT64_MIN, INT32_MAX, INT32_MIN, 0, -1, 1, 0};
const std::vector<int64_t> max_val{INT32_MAX};
const std::vector<int64_t> output{INT32_MAX, INT64_MIN, INT32_MAX, INT32_MIN, 0, -1, 1, 0};
test_case.add_input<int64_t>(data);
test_case.add_input<int64_t>(max_val);
test_case.add_expected_output<int64_t>(Shape{2, 4}, output);
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_clip_set_min_no_max_initializers) {
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/clip_set_min_no_max_initializers.onnx"));
auto test_case = test::TestCase<TestEngine>(function);
const std::vector<float> data{-1.6, -0.1, 10., 0., -10., 1.99, 2.015, 3.};
const std::vector<float> output{-1.59, -0.1, 10., 0., -1.59, 1.99, 2.015, 3.};
test_case.add_input<float>(data);
test_case.add_expected_output<float>(Shape{2, 4}, output);
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_clip_set_min_set_max) {
auto function =
onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/clip_set_min_set_max.onnx"));
auto test_case = test::TestCase<TestEngine>(function);
const std::vector<float> data{-1.6, -0.1, 10., 0., -10., 1.99, 2.015, 3.};
const std::vector<float> min_val{-1.59};
const std::vector<float> max_val{2.01};
const std::vector<float> output{-1.59, -0.1, 2.01, 0., -1.59, 1.99, 2.01, 2.01};
test_case.add_input<float>(data);
test_case.add_input<float>(min_val);
test_case.add_input<float>(max_val);
test_case.add_expected_output<float>(Shape{2, 4}, output);
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_clip_set_min_set_max_initializers) {
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/clip_set_min_set_max_initializers.onnx"));
auto test_case = test::TestCase<TestEngine>(function);
const std::vector<float> data{-1.6, -0.1, 10., 0., -10., 1.99, 2.015, 3.};
const std::vector<float> output{-1.59, -0.1, 2.01, 0., -1.59, 1.99, 2.01, 2.01};
test_case.add_input<float>(data);
test_case.add_expected_output<float>(Shape{2, 4}, output);
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_mvn_v6) {
auto function = onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/mvn_v6.onnx"));

View File

@ -110,6 +110,10 @@ onnx_model_scatterND_param_i64_indices
IE_CPU.onnx_constant_sparse_tensor_int64_3x4
IE_CPU.onnx_constant_sparse_tensor_uint64_3x4
# I64 ONNX Clip Failing on ubuntu18/20_release CI
IE_CPU.onnx_clip_no_min_no_max_int64 # -2147483648 is not close to 2147483647 at index 2
IE_CPU.onnx_clip_no_min_set_max_int64 # -1 is not close to 2147483647 at index 0
# TopK Incorrect input data/index values precision
onnx_model_argmax_int32
onnx_model_argmin_int32