[ONNX] Dynamic version of ONNX Size op (#3553)

This commit is contained in:
Katarzyna Mitrus 2020-12-16 11:16:06 +01:00 committed by GitHub
parent 679e4ae4d7
commit 9376f78994
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 401 additions and 7 deletions

View File

@ -34,13 +34,9 @@ namespace ngraph
OutputVector size(const Node& node)
{
auto data = node.get_ng_inputs().at(0);
std::int64_t tensor_elements_count{
static_cast<std::int64_t>(shape_size(data.get_shape()))};
return {std::make_shared<default_opset::Constant>(
ngraph::element::i64,
Shape{},
std::vector<std::int64_t>{tensor_elements_count})};
auto axes = default_opset::Constant::create(ngraph::element::i32, Shape{}, {0});
auto input_shape = std::make_shared<default_opset::ShapeOf>(data);
return {std::make_shared<default_opset::ReduceProd>(input_shape, axes)};
}
} // namespace set_1

View File

@ -0,0 +1,40 @@
ir_version: 7
producer_name: "onnx-importer-test"
graph {
node {
input: "X"
output: "Y"
op_type: "Size"
}
name: "test-model-size-op"
input {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_param: "?"
}
dim {
dim_value: 3
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 7
shape {
}
}
}
}
}
opset_import {
domain: ""
version: 12
}

View File

@ -0,0 +1,65 @@
ir_version: 7
producer_name: "onnx-importer-test"
graph {
node {
output: "N"
op_type: "Constant"
attribute {
name: "value"
t {
dims: 1
data_type: 1
float_data: 2.0
name: "const_tensor_N"
}
type: TENSOR
}
}
node {
input: "X"
output: "A"
op_type: "Relu"
}
node {
input: "A"
input: "N"
output: "B"
op_type: "Pow"
}
node {
input: "B"
output: "Y"
op_type: "Size"
}
name: "test-model"
input {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
dim {
dim_value: 4
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 7
shape {
}
}
}
}
}
opset_import {
domain: ""
version: 12
}

View File

@ -0,0 +1,80 @@
ir_version: 7
producer_name: "onnx-importer-test"
graph {
node {
output: "N"
op_type: "Constant"
attribute {
name: "value"
t {
dims: 1
data_type: 6
int32_data: 2
name: "const_tensor_N"
}
type: TENSOR
}
}
node {
input: "X"
output: "A"
op_type: "Relu"
}
node {
input: "A"
input: "N"
output: "B"
op_type: "Pow"
}
node {
input: "B"
output: "C"
op_type: "Size"
}
node {
input: "C"
output: "D"
op_type: "Cast"
attribute {
name: "to"
i: 1
type: INT
}
}
node {
input: "D"
output: "Y"
op_type: "Relu"
}
name: "test-model"
input {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
dim {
dim_value: 4
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
}
}
}
}
}
opset_import {
domain: ""
version: 12
}

View File

@ -0,0 +1,105 @@
ir_version: 7
producer_name: "onnx-importer-test"
graph {
node {
output: "N"
op_type: "Constant"
attribute {
name: "value"
t {
dims: 1
data_type: 6
int32_data: 1
name: "const_tensor_N"
}
type: TENSOR
}
}
node {
input: "X"
output: "A"
op_type: "Relu"
}
node {
input: "A"
input: "N"
output: "B"
op_type: "Pow"
}
node {
input: "X"
output: "C"
op_type: "Size"
}
node {
input: "C"
output: "D"
op_type: "Cast"
attribute {
name: "to"
i: 1
type: INT
}
}
node {
input: "D"
input: "B"
output: "Y"
op_type: "Add"
}
name: "test-model"
input {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
dim {
dim_value: 2
}
dim {
dim_value: 4
}
dim {
dim_value: 1
}
dim {
dim_value: 3
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
dim {
dim_value: 2
}
dim {
dim_value: 4
}
dim {
dim_value: 1
}
dim {
dim_value: 3
}
}
}
}
}
}
opset_import {
domain: ""
version: 12
}

View File

@ -0,0 +1,40 @@
ir_version: 7
producer_name: "onnx-importer-test"
graph {
node {
input: "X"
output: "Y"
op_type: "Size"
}
name: "test-model-size-op"
input {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 3
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 7
shape {
}
}
}
}
}
opset_import {
domain: ""
version: 12
}

View File

@ -2973,6 +2973,54 @@ NGRAPH_TEST(${BACKEND_NAME}, onnx_image_scaler)
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_size_op_single)
{
const auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/size_op_single.prototxt"));
auto test_case = test::TestCase<TestEngine>(function);
test_case.add_input<float>(Shape{2, 3}, {1.0, 2.0, 3.0, 4.0, 5.0, 6.0});
test_case.add_expected_output<int>(Shape{}, {6});
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_size_op_graph_end)
{
const auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/size_op_graph_end.prototxt"));
auto test_case = test::TestCase<TestEngine>(function);
test_case.add_input<float>({1.0, 2.0, 3.0, 4.0});
test_case.add_expected_output<int>(Shape{}, {4});
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_size_op_graph_middle)
{
const auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/size_op_graph_middle.prototxt"));
auto test_case = test::TestCase<TestEngine>(function);
test_case.add_input<float>({1.0, 2.0, 3.0, 4.0});
test_case.add_expected_output<float>(Shape{}, {4.0});
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_size_op_on_input_graph_middle)
{
const auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/size_op_on_input_graph_middle.prototxt"));
auto test_case = test::TestCase<TestEngine>(function);
test_case.add_input<float>(Shape{1, 2, 4, 1, 3},
{0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.});
test_case.add_expected_output<float>(
Shape{1, 2, 4, 1, 3}, {24., 24., 24., 24., 24., 24., 24., 24., 24., 24., 24., 24.,
24., 24., 24., 24., 24., 24., 24., 24., 24., 24., 24., 24.});
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_empty_initializers_handling)
{
// int this test the "scales" input of the Resize operator is set to an empty initializer

View File

@ -1286,6 +1286,17 @@ NGRAPH_TEST(${BACKEND_NAME}, onnx_dyn_shapes_reduce_max_dynamic_input_rank_negat
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_size_dyn_op)
{
const auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/dynamic_shapes/size_op_dyn.prototxt"));
auto test_case = test::TestCase<TestEngine, TestCaseType::DYNAMIC>(function);
test_case.add_input<float>(Shape{2, 3}, {1.0, 2.0, 3.0, 4.0, 5.0, 6.0});
test_case.add_expected_output<int>(Shape{}, {6});
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_model_max_pool_dyn_rank_without_default_attrs)
{
auto function = onnx_import::import_onnx_model(file_util::path_join(

View File

@ -248,6 +248,15 @@ IE_GPU.onnx_model_gru_fwd_mixed_seq_len_const
## Const layer has incorrect dimensions in the output data
IE_CPU.nothing_to_reverse
# Unsupported dynamic ops
onnx_size_dyn_op
# Constant network
# MKLDNNGraph::CreateGraph: No inputs for the topology
onnx_size_op_single
onnx_size_op_graph_end
onnx_size_op_graph_middle
#-------------------------------------------------------------------------------
#