Unverified Commit ca075107 authored by Mateusz Bencer's avatar Mateusz Bencer Committed by GitHub

[ONNX] Replace arithmetic operators and builders into v1 ops (#4368)

* Replace arithmetic operators and builders into v1 ops

* Code review remarks introduced

* code review remarks introduced

* doc fixed
Co-authored-by: 's avatarScott Cyphers <diyessi@users.noreply.github.com>
parent 602b06e2
......@@ -26,6 +26,7 @@
#include "ngraph/op/sqrt.hpp"
#include "ngraph/op/subtract.hpp"
#include "ngraph/op/sum.hpp"
#include "ngraph/opsets/opset1.hpp"
#include "ngraph/util.hpp"
namespace ngraph
......@@ -111,5 +112,56 @@ namespace ngraph
return result->add_provenance_group_members_above({value});
}
std::shared_ptr<Node> builder::opset1::mean(const Output<Node>& value,
const AxisSet& reduction_axes,
bool keep_dims)
{
const auto xsum = std::make_shared<ngraph::opset1::ReduceSum>(
value,
ngraph::opset1::Constant::create(
element::i64, Shape{reduction_axes.size()}, reduction_axes.to_vector()),
keep_dims);
const auto N = get_num_elements(value.get_shape(), reduction_axes);
const auto& et = value.get_element_type();
const auto divisor = ngraph::opset1::Constant::create(et, Shape{}, {N});
return std::make_shared<ngraph::opset1::Divide>(xsum, divisor)
->add_provenance_group_members_above({value});
}
std::shared_ptr<Node> builder::opset1::variance(const Output<Node>& value,
const AxisSet& reduction_axes,
const bool bessel_correction)
{
const bool keep_dims = true;
std::shared_ptr<Node> mu = opset1::mean(value, reduction_axes, keep_dims);
Output<Node> diff = std::make_shared<ngraph::opset1::Subtract>(value, mu);
diff = std::make_shared<ngraph::opset1::ReduceSum>(
std::make_shared<ngraph::opset1::Multiply>(diff, diff),
ngraph::opset1::Constant::create(
element::i64, Shape{reduction_axes.size()}, reduction_axes.to_vector()),
false);
const auto& et = value.get_element_type();
const auto N = get_num_elements(value.get_shape(), reduction_axes);
std::shared_ptr<Node> result;
if (bessel_correction)
{
const auto N1const = ngraph::opset1::Constant::create(et, Shape{}, {N - 1});
result = std::make_shared<ngraph::opset1::Divide>(diff, N1const);
}
else
{
const auto Nconst = ngraph::opset1::Constant::create(et, Shape{}, {N});
result = std::make_shared<ngraph::opset1::Divide>(diff, Nconst);
}
return result->add_provenance_group_members_above({value});
}
} // namespace builder
} // namespace ngraph
......@@ -133,5 +133,66 @@ namespace ngraph
const AxisSet& reduction_axes,
const bool bessel_correction = false);
namespace opset1
{
// clang-format off
/// \brief Sum-based Mean of a Tensor.
///
/// Calculates
///
/// \f$\sum_{i=1}^{N} \frac{x_i}{N}\f$
///
/// Where `i` traverses all of the axes provided in `reduction_axes`
///
/// ## Inputs
///
/// | | Type | Description | |
/// | ---------------- | --------------------------------- | -------------------------------------------------------|
/// | `node` | \f$E[d_1,\dots,d_n]~(n \geq 0)\f$ | An input tensor of any shape |
/// | `reduction_axes` | AxesSet | The axes to eliminate through reduction (0 indexed). |
/// | `keep_dims` | bool | If set to true it holds reduced axes. |
///
/// ## Output
///
/// | Type | Description |
/// | ----------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
/// | \f$E[\textit{delete}(A,d_1,\dots,d_n)]\f$ | The tensor \f$T\f$, where \f$T\f$ is the input tensor with the `reduction_axes` \f$A\f$ eliminated by reduction. |
// clang-format on
std::shared_ptr<Node> mean(const Output<Node>& node,
const AxisSet& reduction_axes,
bool keep_dims = false);
// clang-format off
/// \brief Sum-based Variance of a Tensor.
///
/// If bessel_correct is true, calculates
///
/// \f$\frac{\sum_{i=1}^{N}\left(x_i-\bar{x}\right)^2}{N-1}\f$
///
/// else, calculates
///
/// \f$\frac{\sum_{i=1}^{N}\left(x_i-\bar{x}\right)^2}{N}\f$
///
/// Where `i` traverses all of the axes provided in `reduction_axes` and \f$\bar{x} = \sum_{i=1}^{N} \frac{x_i}{N}\f$
///
/// ## Inputs
///
/// | | Type | Description |
/// | ------------------- | --------------------------------- | ------------------------------------------------------------ |
/// | `value | \f$E[d_1,\dots,d_n]~(n \geq 0)\f$ | An input tensor of any shape |
/// | `reduction_axes` | AxesSet | The axes to eliminate through reduction (0 indexed). |
/// | `bessel_correction` | bool (default = false) | Enable Bessel's correction to std_dev for Small sample sizes |
///
/// ## Output
///
/// | Type | Description |
/// | ----------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
/// | \f$E[\textit{delete}(A,d_1,\dots,d_n)]\f$ | The tensor \f$T\f$, where \f$T\f$ is the input tensor with the `reduction_axes` \f$A\f$ eliminated by reduction. |
// clang-format on
std::shared_ptr<Node> variance(const Output<Node>& value,
const AxisSet& reduction_axes,
const bool bessel_correction = false);
}
} // namespace builder
} // namespace ngraph
......@@ -71,11 +71,11 @@ namespace ngraph
scale = ngraph::builder::opset1::make_broadcast(scale, data_shape, 1);
bias = ngraph::builder::opset1::make_broadcast(bias, data_shape, 1);
Output<ngraph::Node> mean = builder::mean(data, reduction_axes);
Output<ngraph::Node> mean = builder::opset1::mean(data, reduction_axes);
mean =
ngraph::builder::opset1::make_broadcast(mean, data_shape, reduction_axes);
Output<ngraph::Node> variance = builder::variance(data, reduction_axes);
Output<ngraph::Node> variance = builder::opset1::variance(data, reduction_axes);
variance = ngraph::builder::opset1::make_broadcast(
variance, data_shape, reduction_axes);
......
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