Unverified Commit 3042c8cc authored by Katarzyna Mitrus's avatar Katarzyna Mitrus Committed by GitHub

[ONNX] Inverse hyperbolic functions - dynamic shape support (#4301)

* Asinh dynamic shape support prototype

* Asinh static shape test

* Asinh dynamic shapes test

* Unified asinh implementation for static and dynamic shapes

* Atanh dynamic shapes support

* Style apply

* Acosh dynamic shape support

* Tests update

* Style apply

* Update PlaidML manifest

* Use default tolerance bits

* Style apply

* Def tolerance bits for dynamic tests

* Rename dim_param

* Update acosh.prototxt

* Update acosh_dyn_shape.prototxt
Co-authored-by: 's avatarRobert Kimball <robert.kimball@intel.com>
parent 2f44b758
......@@ -40,15 +40,15 @@ namespace ngraph
// arccosh(x) = ln(x + sqrt(x^2 - 1))
//
std::shared_ptr<ngraph::Node> one_node{default_opset::Constant::create(
data->get_element_type(),
data->get_shape(),
std::vector<float>(ngraph::shape_size(data->get_shape()), 1.f))};
const auto one =
default_opset::Constant::create(data->get_element_type(), {}, {1.f});
std::shared_ptr<ngraph::Node> sqrt_node{
std::make_shared<default_opset::Sqrt>(data * data - one_node)};
const auto x_square = std::make_shared<default_opset::Multiply>(data, data);
const auto sqrt_args = std::make_shared<default_opset::Subtract>(x_square, one);
const auto sqrt_node = std::make_shared<default_opset::Sqrt>(sqrt_args);
const auto log_args = std::make_shared<default_opset::Add>(data, sqrt_node);
return {std::make_shared<default_opset::Log>(data + sqrt_node)};
return {std::make_shared<default_opset::Log>(log_args)};
}
} // namespace set_1
......
......@@ -39,15 +39,15 @@ namespace ngraph
// asinh(x) = ln(x + sqrt(x^2 + 1))
//
std::shared_ptr<ngraph::Node> one_node{default_opset::Constant::create(
data->get_element_type(),
data->get_shape(),
std::vector<float>(ngraph::shape_size(data->get_shape()), 1.f))};
const auto one =
default_opset::Constant::create(data->get_element_type(), {}, {1.f});
std::shared_ptr<ngraph::Node> sqrt_node{
std::make_shared<default_opset::Sqrt>(data * data + one_node)};
const auto x_square = std::make_shared<default_opset::Multiply>(data, data);
const auto sqrt_args = std::make_shared<default_opset::Add>(x_square, one);
const auto sqrt_node = std::make_shared<default_opset::Sqrt>(sqrt_args);
const auto log_args = std::make_shared<default_opset::Add>(data, sqrt_node);
return {std::make_shared<default_opset::Log>(data + sqrt_node)};
return {std::make_shared<default_opset::Log>(log_args)};
}
} // namespace set_1
......
......@@ -38,21 +38,21 @@ namespace ngraph
// Define inverse hyperbolic tangent in terms of natural logarithm:
//
// atanh(x) = 0.5 * ln((1 + x) / (1 - x))
// = 0.5 * (ln(1 + x) - ln(1 - x))
//
std::shared_ptr<ngraph::Node> one_node{default_opset::Constant::create(
data->get_element_type(),
data->get_shape(),
std::vector<float>(ngraph::shape_size(data->get_shape()), 1.f))};
const auto one =
default_opset::Constant::create(data->get_element_type(), {}, {1.f});
std::shared_ptr<ngraph::Node> half_node{default_opset::Constant::create(
data->get_element_type(),
data->get_shape(),
std::vector<float>(ngraph::shape_size(data->get_shape()), 0.5f))};
const auto half =
default_opset::Constant::create(data->get_element_type(), {}, {0.5f});
return {half_node * (std::make_shared<default_opset::Log>(one_node + data) -
std::make_shared<default_opset::Log>(one_node - data))};
const auto one_plus_x = std::make_shared<default_opset::Add>(one, data);
const auto one_minus_x = std::make_shared<default_opset::Subtract>(one, data);
const auto log_args =
std::make_shared<default_opset::Divide>(one_plus_x, one_minus_x);
const auto log_node = std::make_shared<default_opset::Log>(log_args);
return {std::make_shared<default_opset::Multiply>(half, log_node)};
}
} // namespace set_1
......
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "x"
output: "y"
op_type: "Acosh"
}
name: "acosh_graph"
input {
name: "x"
type {
tensor_type {
elem_type: 1
shape {
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: 3
}
}
}
}
}
}
opset_import {
version: 9
}
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "x"
output: "y"
op_type: "Asinh"
}
name: "asinh_graph"
input {
name: "x"
type {
tensor_type {
elem_type: 1
shape {
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: 3
}
}
}
}
}
}
opset_import {
version: 9
}
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "x"
output: "y"
op_type: "Atanh"
}
name: "atanh_graph"
input {
name: "x"
type {
tensor_type {
elem_type: 1
shape {
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: 3
}
}
}
}
}
}
opset_import {
version: 9
}
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "x"
output: "y"
op_type: "Acosh"
}
name: "acosh_graph"
input {
name: "x"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_param: "dynamic_1"
}
dim {
dim_param: "dynamic_2"
}
}
}
}
}
output {
name: "y"
type {
tensor_type {
elem_type: 1
shape {
}
}
}
}
}
opset_import {
version: 9
}
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "x"
output: "y"
op_type: "Asinh"
}
name: "asinh_graph"
input {
name: "x"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_param: "dynamic_1"
}
dim {
dim_param: "dynamic_2"
}
}
}
}
}
output {
name: "y"
type {
tensor_type {
elem_type: 1
shape {
}
}
}
}
}
opset_import {
version: 9
}
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "x"
output: "y"
op_type: "Atanh"
}
name: "atanh_graph"
input {
name: "x"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_param: "dynamic_1"
}
dim {
dim_param: "dynamic_2"
}
}
}
}
}
output {
name: "y"
type {
tensor_type {
elem_type: 1
shape {
}
}
}
}
}
opset_import {
version: 9
}
......@@ -25,6 +25,15 @@
#include <stdexcept>
#include <vector>
// clang-format off
#ifdef ${BACKEND_NAME}_FLOAT_TOLERANCE_BITS
#define DEFAULT_FLOAT_TOLERANCE_BITS ${BACKEND_NAME}_FLOAT_TOLERANCE_BITS
#endif
#ifdef ${BACKEND_NAME}_DOUBLE_TOLERANCE_BITS
#define DEFAULT_DOUBLE_TOLERANCE_BITS ${BACKEND_NAME}_DOUBLE_TOLERANCE_BITS
#endif
// clang-format on
#include "gtest/gtest.h"
#include "ngraph/frontend/onnx_import/onnx.hpp"
#include "ngraph/ngraph.hpp"
......@@ -1464,6 +1473,42 @@ NGRAPH_TEST(onnx_${BACKEND_NAME}, top_k_opset_11_const_k_smallest)
test_case.run();
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_acosh)
{
auto function =
onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/acosh.prototxt"));
auto test_case = ngraph::test::NgraphTestCase(function, "${BACKEND_NAME}");
test_case.add_input<float>(Shape{1, 3}, {1.0f, 2.5f, 4.3f});
test_case.add_expected_output<float>(Shape{1, 3}, {0.0f, 1.5667993f, 2.13795861f});
test_case.run();
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_asinh)
{
auto function =
onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/asinh.prototxt"));
auto test_case = ngraph::test::NgraphTestCase(function, "${BACKEND_NAME}");
test_case.add_input<float>(Shape{1, 3}, {-1.0f, 0.0f, 1.0f});
test_case.add_expected_output<float>(Shape{1, 3}, {-0.88137358f, 0.0f, 0.88137358f});
test_case.run();
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_atanh)
{
auto function =
onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/atanh.prototxt"));
auto test_case = ngraph::test::NgraphTestCase(function, "${BACKEND_NAME}");
test_case.add_input<float>(Shape{1, 3}, {-0.9f, 0.0f, 0.9f});
test_case.add_expected_output<float>(Shape{1, 3}, {-1.4722194f, 0.0f, 1.4722194f});
test_case.run();
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_sinh)
{
auto function =
......
......@@ -14,6 +14,15 @@
// limitations under the License.
//*****************************************************************************
// clang-format off
#ifdef ${BACKEND_NAME}_FLOAT_TOLERANCE_BITS
#define DEFAULT_FLOAT_TOLERANCE_BITS ${BACKEND_NAME}_FLOAT_TOLERANCE_BITS
#endif
#ifdef ${BACKEND_NAME}_DOUBLE_TOLERANCE_BITS
#define DEFAULT_DOUBLE_TOLERANCE_BITS ${BACKEND_NAME}_DOUBLE_TOLERANCE_BITS
#endif
// clang-format on
#include "gtest/gtest.h"
#include "ngraph/file_util.hpp"
#include "ngraph/frontend/onnx_import/default_opset.hpp"
......@@ -175,3 +184,78 @@ NGRAPH_TEST(onnx_dyn_shapes_${BACKEND_NAME}, dynamic_rank_input_inference)
test_case.run();
}
}
NGRAPH_TEST(onnx_dyn_shapes_${BACKEND_NAME}, model_acosh_1_3)
{
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/dynamic_shapes/acosh_dyn_shape.prototxt"));
auto test_case = NgraphTestCase(function, "${BACKEND_NAME}", BackendMode::DYNAMIC);
test_case.add_input<float>(Shape{1, 3}, {1.0f, 2.5f, 4.3f});
test_case.add_expected_output<float>(Shape{1, 3}, {0.0f, 1.5667993f, 2.1379586f});
test_case.run();
}
NGRAPH_TEST(onnx_dyn_shapes_${BACKEND_NAME}, model_acosh_3_2)
{
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/dynamic_shapes/acosh_dyn_shape.prototxt"));
auto test_case = NgraphTestCase(function, "${BACKEND_NAME}", BackendMode::DYNAMIC);
test_case.add_input<float>(Shape{3, 2}, {1.0f, 2.5f, 4.3f, 1.0f, 2.5f, 4.3f});
test_case.add_expected_output<float>(
Shape{3, 2}, {0.0f, 1.5667993f, 2.1379586f, 0.0f, 1.5667993f, 2.1379586f});
test_case.run();
}
NGRAPH_TEST(onnx_dyn_shapes_${BACKEND_NAME}, model_asinh_1_3)
{
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/dynamic_shapes/asinh_dyn_shape.prototxt"));
auto test_case = NgraphTestCase(function, "${BACKEND_NAME}", BackendMode::DYNAMIC);
test_case.add_input<float>(Shape{1, 3}, {-1.5f, 0.0f, 1.5f});
test_case.add_expected_output<float>(Shape{1, 3}, {-1.1947632f, 0.0f, 1.1947632f});
test_case.run();
}
NGRAPH_TEST(onnx_dyn_shapes_${BACKEND_NAME}, model_asinh_3_2)
{
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/dynamic_shapes/asinh_dyn_shape.prototxt"));
auto test_case = NgraphTestCase(function, "${BACKEND_NAME}", BackendMode::DYNAMIC);
test_case.add_input<float>(Shape{3, 2}, {-1.5f, 0.0f, 1.5f, -1.5f, 0.0f, 1.5f});
test_case.add_expected_output<float>(
Shape{3, 2}, {-1.1947632f, 0.0f, 1.1947632f, -1.1947632, 0.0f, 1.1947632f});
test_case.run();
}
NGRAPH_TEST(onnx_dyn_shapes_${BACKEND_NAME}, model_atanh_1_3)
{
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/dynamic_shapes/atanh_dyn_shape.prototxt"));
auto test_case = NgraphTestCase(function, "${BACKEND_NAME}", BackendMode::DYNAMIC);
test_case.add_input<float>(Shape{1, 3}, {-0.9f, 0.0f, 0.9f});
test_case.add_expected_output<float>(Shape{1, 3}, {-1.47221948f, 0.0f, 1.47221948f});
test_case.run();
}
NGRAPH_TEST(onnx_dyn_shapes_${BACKEND_NAME}, model_atanh_3_2)
{
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/dynamic_shapes/atanh_dyn_shape.prototxt"));
auto test_case = NgraphTestCase(function, "${BACKEND_NAME}", BackendMode::DYNAMIC);
test_case.add_input<float>(Shape{3, 2}, {-0.9f, 0.0f, 0.9f, -0.9f, 0.0f, 0.9f});
test_case.add_expected_output<float>(
Shape{3, 2}, {-1.47221948f, 0.0f, 1.47221948f, -1.47221948f, 0.0f, 1.47221948f});
test_case.run();
}
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