Add support for ONNX RandomUniform and RandomUniformLike ops (#7190)

This commit is contained in:
Michał Karzyński 2021-08-24 16:36:30 +02:00 committed by GitHub
parent 80c5f1a325
commit 9228200ce5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 291 additions and 0 deletions

View File

@ -0,0 +1,50 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "op/random_uniform.hpp"
#include "default_opset.hpp"
#include "exceptions.hpp"
#include "ngraph/op/constant.hpp"
#include "ngraph/opsets/opset8.hpp"
#include "ngraph/shape.hpp"
#include "utils/common.hpp"
namespace ngraph {
namespace onnx_import {
namespace op {
namespace set_1 {
OutputVector random_uniform(const Node& node) {
CHECK_VALID_NODE(node, node.has_attribute("shape"), "RandomUniform operator must specify a 'shape' attribute.");
const auto dtype =
node.get_attribute_value<int64_t>("dtype", static_cast<int64_t>(ONNX_NAMESPACE::TensorProto_DataType_FLOAT));
const auto high = node.get_attribute_value<float>("high", 1.0f);
const auto low = node.get_attribute_value<float>("low", 0.0f);
const auto seed = node.get_attribute_value<int64_t>("seed", 0);
const auto shape = node.get_attribute_value<std::vector<int64_t>>("shape");
const auto target_shape_const = default_opset::Constant::create(ngraph::element::i64, Shape{shape.size()}, shape);
const auto high_const = default_opset::Constant::create(ngraph::element::f32, Shape{1}, {high});
const auto low_const = default_opset::Constant::create(ngraph::element::f32, Shape{1}, {low});
const auto target_type = common::get_ngraph_element_type(dtype);
const uint64_t global_seed = 0;
return {std::make_shared<ngraph::opset8::RandomUniform>(target_shape_const,
low_const,
high_const,
target_type,
global_seed,
seed)};
}
} // namespace set_1
} // namespace op
} // namespace onnx_import
} // namespace ngraph

View File

@ -0,0 +1,20 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include "ngraph/node.hpp"
#include "onnx_import/core/node.hpp"
namespace ngraph {
namespace onnx_import {
namespace op {
namespace set_1 {
OutputVector random_uniform(const Node& node);
} // namespace set_1
} // namespace op
} // namespace onnx_import
} // namespace ngraph

View File

@ -0,0 +1,56 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "op/random_uniform_like.hpp"
#include "default_opset.hpp"
#include "exceptions.hpp"
#include "ngraph/op/constant.hpp"
#include "ngraph/opsets/opset8.hpp"
#include "ngraph/shape.hpp"
#include "utils/common.hpp"
namespace ngraph {
namespace onnx_import {
namespace op {
namespace set_1 {
OutputVector random_uniform_like(const Node& node) {
OutputVector inputs{node.get_ng_inputs()};
auto input = inputs.at(0);
ngraph::element::Type target_type;
if (node.has_attribute("dtype")) {
const auto dtype = node.get_attribute_value<int64_t>("dtype");
target_type = common::get_ngraph_element_type(dtype);
} else {
target_type = input.get_element_type();
}
const auto target_shape = std::make_shared<default_opset::ShapeOf>(input);
const auto high = node.get_attribute_value<float>("high", 1.0f);
const auto low = node.get_attribute_value<float>("low", 0.0f);
const auto seed = node.get_attribute_value<int64_t>("seed", 0);
const auto high_const = default_opset::Constant::create(ngraph::element::f32, Shape{1}, {high});
const auto low_const = default_opset::Constant::create(ngraph::element::f32, Shape{1}, {low});
const uint64_t global_seed = 0;
return {std::make_shared<ngraph::opset8::RandomUniform>(target_shape,
low_const,
high_const,
target_type,
global_seed,
seed)};
}
} // namespace set_1
} // namespace op
} // namespace onnx_import
} // namespace ngraph

View File

@ -0,0 +1,20 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include "ngraph/node.hpp"
#include "onnx_import/core/node.hpp"
namespace ngraph {
namespace onnx_import {
namespace op {
namespace set_1 {
OutputVector random_uniform_like(const Node& node);
} // namespace set_1
} // namespace op
} // namespace onnx_import
} // namespace ngraph

View File

@ -106,6 +106,8 @@
#include "op/org.openvinotoolkit/prior_box.hpp"
#include "op/org.openvinotoolkit/swish.hpp"
#include "op/quantize_linear.hpp"
#include "op/random_uniform.hpp"
#include "op/random_uniform_like.hpp"
#include "op/range.hpp"
#include "op/reciprocal.hpp"
#include "op/reduce.hpp"
@ -366,6 +368,8 @@ OperatorsBridge::OperatorsBridge() {
REGISTER_OPERATOR("QuantizeLinear", 1, quantize_linear);
REGISTER_OPERATOR("QuantizeLinear", 13, quantize_linear);
REGISTER_OPERATOR("Range", 1, range);
REGISTER_OPERATOR("RandomUniform", 1, random_uniform);
REGISTER_OPERATOR("RandomUniformLike", 1, random_uniform_like);
REGISTER_OPERATOR("Reciprocal", 1, reciprocal);
REGISTER_OPERATOR("ReduceLogSum", 1, reduce_log_sum);
REGISTER_OPERATOR("ReduceLogSumExp", 1, reduce_log_sum_exp);

View File

@ -0,0 +1,49 @@
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
output: "y"
op_type: "RandomUniform"
attribute {
name: "shape"
ints: 2
ints: 2
type: INTS
}
attribute {
name: "high"
f: 50
type: FLOAT
}
attribute {
name: "low"
f: 40
type: FLOAT
}
attribute {
name: "seed"
i: 100
type: INT
}
}
name: "test_model"
output {
name: "y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 2
}
}
}
}
}
}
opset_import {
version: 1
}

View File

@ -0,0 +1,60 @@
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "x"
output: "y"
op_type: "RandomUniformLike"
attribute {
name: "high"
f: 50
type: FLOAT
}
attribute {
name: "low"
f: 40
type: FLOAT
}
attribute {
name: "seed"
i: 100
type: INT
}
}
name: "test_model"
input {
name: "x"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 2
}
}
}
}
}
output {
name: "y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 2
}
}
}
}
}
}
opset_import {
version: 1
}

View File

@ -4022,3 +4022,27 @@ NGRAPH_TEST(${BACKEND_NAME}, onnx_float16_tensor_as_int32) {
// clang-format on
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_model_random_uniform) {
const auto function =
onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/random_uniform.onnx"));
auto test_case = test::TestCase<TestEngine>(function);
// These output values are unknown at this time as we don't have a reference implementation of random number
// generator
test_case.add_expected_output<ngraph::float16>(Shape{2, 2}, {41, 42, 43, 44});
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_model_random_uniform_like) {
const auto function =
onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/random_uniform_like.onnx"));
auto test_case = test::TestCase<TestEngine>(function);
test_case.add_expected_output<ngraph::float16>(Shape{2, 2}, {0, 0, 0, 0});
// These output values are unknown at this time as we don't have a reference implementation of random number
// generator
test_case.add_input<ngraph::float16>(Shape{2, 2}, {41, 42, 43, 44});
test_case.run();
}

View File

@ -41,6 +41,10 @@ onnx_model_matmul_integer_4d_no_zero_point
onnx_model_qlinear_matmul
onnx_model_qlinear_matmul_3d
# No support yet for RandomUniform
onnx_model_random_uniform
onnx_model_random_uniform_like
# Result mismatch
onnx_model_shape
onnx_model_split_equal_parts_default

View File

@ -153,3 +153,7 @@ onnx_model_deformable_conv_2d
# No support for unsigned types
INTERPRETER.zero_sized_negative
# No support yet for RandomUniform
INTERPRETER.onnx_model_random_uniform
INTERPRETER.onnx_model_random_uniform_like