Unverified Commit 54132cc9 authored by Mateusz Bencer's avatar Mateusz Bencer Committed by GitHub

[ONNX] norm builders should produce v1 ops (#4354)

parent d28fac61
This diff is collapsed.
...@@ -34,62 +34,65 @@ namespace ngraph ...@@ -34,62 +34,65 @@ namespace ngraph
MAX MAX
}; };
/// \brief Calculates L-0 norm of input tensor. namespace opset1
/// {
/// \note The L-0 norm represents the cardinality of elements different /// \brief Calculates L-0 norm of input tensor.
/// from zero. This actually is not a "true" norm. ///
/// /// \note The L-0 norm represents the cardinality of elements different
/// \param[in] value The input tensor. /// from zero. This actually is not a "true" norm.
/// \param[in] reduction_axes The axes along which we calculate norm. ///
/// /// \param[in] value The input tensor.
/// \return L-0 norm of value. /// \param[in] reduction_axes The axes along which we calculate norm.
/// ///
std::shared_ptr<Node> l0_norm(const Output<Node>& value, const AxisSet& reduction_axes); /// \return L-0 norm of value. The output sub-graph is composed of v1 ops.
///
std::shared_ptr<Node> l0_norm(const Output<Node>& value, const AxisSet& reduction_axes);
/// \brief Calculates L-1 norm of a value. /// \brief Calculates L-1 norm of a value.
/// ///
/// \note The L-1 norm represents the sum of absolute values. /// \note The L-1 norm represents the sum of absolute values.
/// ///
/// \param[in] value The input tensor. /// \param[in] value The input tensor.
/// \param[in] reduction_axes The axes along which we calculate norm. /// \param[in] reduction_axes The axes along which we calculate norm.
/// \param[in] bias The bias added to the calculated sum. /// \param[in] bias The bias added to the calculated sum.
/// ///
/// \return L-1 norm of value. /// \return L-1 norm of value. The output sub-graph is composed of v1 ops.
/// ///
std::shared_ptr<Node> std::shared_ptr<Node>
l1_norm(const Output<Node>& value, const AxisSet& reduction_axes, float bias = 0.f); l1_norm(const Output<Node>& value, const AxisSet& reduction_axes, float bias = 0.f);
/// \brief Calculates L-2 norm of input tensor. /// \brief Calculates L-2 norm of input tensor.
/// ///
/// \note The L-2 norm represents the square root of sum of squares of each /// \note The L-2 norm represents the square root of sum of squares of each
/// individual element. /// individual element.
/// ///
/// \param[in] value The input tensor. /// \param[in] value The input tensor.
/// \param[in] reduction_axes The axes along which we calculate norm. /// \param[in] reduction_axes The axes along which we calculate norm.
/// \param[in] bias The bias combined with calculated sum. /// \param[in] bias The bias combined with calculated sum.
/// \param[in] bias_mode The method of bias application. /// \param[in] bias_mode The method of bias application.
/// \param[in] keep_dims The flag indicates if axes will be removed or kept. /// \param[in] keep_dims The flag indicates if axes will be removed or kept.
/// ///
/// \return L-2 norm of value. /// \return L-2 norm of value. The output sub-graph is composed of v1 ops.
/// ///
std::shared_ptr<Node> l2_norm(const Output<Node>& value, std::shared_ptr<Node> l2_norm(const Output<Node>& value,
const AxisSet& reduction_axes, const AxisSet& reduction_axes,
float bias = 0.f, float bias = 0.f,
BiasMode bias_mode = BiasMode::ADD, BiasMode bias_mode = BiasMode::ADD,
bool keep_dims = false); bool keep_dims = false);
/// \brief Creates node which calculates L-p norm on input tensor. /// \brief Creates node which calculates L-p norm on input tensor.
/// ///
/// \param[in] value The input tensor. /// \param[in] value The input tensor.
/// \param[in] reduction_axes The axes along which we calculate norm. /// \param[in] reduction_axes The axes along which we calculate norm.
/// \param[in] p_norm The p norm to calculate. /// \param[in] p_norm The p norm to calculate.
/// \param[in] bias The bias added to the calculated sum. /// \param[in] bias The bias added to the calculated sum.
/// ///
/// \return L-p norm of value. /// \return L-p norm of value. The output sub-graph is composed of v1 ops.
/// ///
std::shared_ptr<Node> lp_norm(const Output<Node>& value, std::shared_ptr<Node> lp_norm(const Output<Node>& value,
const AxisSet& reduction_axes, const AxisSet& reduction_axes,
std::size_t p_norm = 2, std::size_t p_norm = 2,
float bias = 0.f); float bias = 0.f);
}
} // namespace builder } // namespace builder
} // namespace ngraph } // namespace ngraph
...@@ -55,7 +55,7 @@ namespace ngraph ...@@ -55,7 +55,7 @@ namespace ngraph
<< "Invalid `p` attribute value: " << p_norm << "Invalid `p` attribute value: " << p_norm
<< "Only normalization of 1st or 2nd order is supported."; << "Only normalization of 1st or 2nd order is supported.";
std::shared_ptr<ngraph::Node> norm = ngraph::builder::lp_norm( std::shared_ptr<ngraph::Node> norm = ngraph::builder::opset1::lp_norm(
data, AxisSet{normalize_axis}, static_cast<std::size_t>(p_norm)); data, AxisSet{normalize_axis}, static_cast<std::size_t>(p_norm));
const auto target_shape = default_opset::Constant::create( const auto target_shape = default_opset::Constant::create(
......
...@@ -55,7 +55,7 @@ namespace ngraph ...@@ -55,7 +55,7 @@ namespace ngraph
AxisSet reduction_axes{ AxisSet reduction_axes{
common::get_monotonic_range<std::size_t>(orig_shape.size(), 2)}; common::get_monotonic_range<std::size_t>(orig_shape.size(), 2)};
slice = ngraph::builder::lp_norm( slice = ngraph::builder::opset1::lp_norm(
slice, reduction_axes, static_cast<std::size_t>(p_norm)); slice, reduction_axes, static_cast<std::size_t>(p_norm));
// output shape is all ones except N channel // output shape is all ones except N channel
......
...@@ -97,7 +97,7 @@ namespace ngraph ...@@ -97,7 +97,7 @@ namespace ngraph
/// ///
inline NodeVector reduce_l1(const Node& node) inline NodeVector reduce_l1(const Node& node)
{ {
auto l1_norm_reduction = std::bind(ngraph::builder::l1_norm, auto l1_norm_reduction = std::bind(ngraph::builder::opset1::l1_norm,
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_2,
0.f); 0.f);
...@@ -119,7 +119,7 @@ namespace ngraph ...@@ -119,7 +119,7 @@ namespace ngraph
/// ///
inline NodeVector reduce_l2(const Node& node) inline NodeVector reduce_l2(const Node& node)
{ {
auto l2_norm_reduction = std::bind(ngraph::builder::l2_norm, auto l2_norm_reduction = std::bind(ngraph::builder::opset1::l2_norm,
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_2,
0.f, 0.f,
......
...@@ -68,7 +68,7 @@ NodeVector op::GRN::decompose_op() const ...@@ -68,7 +68,7 @@ NodeVector op::GRN::decompose_op() const
} }
// Calculate l2 norm across channels. // Calculate l2 norm across channels.
shared_ptr<Node> norm = builder::l2_norm(data, AxisSet{1}, m_bias); shared_ptr<Node> norm = builder::opset1::l2_norm(data, AxisSet{1}, m_bias);
// Get back reduced axis. // Get back reduced axis.
norm = std::make_shared<Broadcast>(norm, data.get_shape(), AxisSet{1}); norm = std::make_shared<Broadcast>(norm, data.get_shape(), AxisSet{1});
data = data / norm; data = data / norm;
......
...@@ -96,7 +96,8 @@ NodeVector op::NormalizeL2::decompose_op() const ...@@ -96,7 +96,8 @@ NodeVector op::NormalizeL2::decompose_op() const
// Calculate l2 norm across axes determined by axes input // Calculate l2 norm across axes determined by axes input
auto builder_bias_mode = auto builder_bias_mode =
(m_eps_mode == EpsMode::MAX) ? builder::BiasMode::MAX : builder::BiasMode::ADD; (m_eps_mode == EpsMode::MAX) ? builder::BiasMode::MAX : builder::BiasMode::ADD;
Output<Node> norm = builder::l2_norm(data, reduction_axes, m_eps, builder_bias_mode, true); Output<Node> norm =
builder::opset1::l2_norm(data, reduction_axes, m_eps, builder_bias_mode, true);
data = make_shared<op::Divide>(data, norm, AutoBroadcastSpec(AutoBroadcastType::NUMPY)); data = make_shared<op::Divide>(data, norm, AutoBroadcastSpec(AutoBroadcastType::NUMPY));
......
...@@ -389,7 +389,7 @@ TEST(provenance, builder) ...@@ -389,7 +389,7 @@ TEST(provenance, builder)
{ {
auto p1 = make_shared<op::Parameter>(element::i32, PartialShape{2, 3, 4}); auto p1 = make_shared<op::Parameter>(element::i32, PartialShape{2, 3, 4});
p1->add_provenance_tag("P1"); p1->add_provenance_tag("P1");
auto norm = builder::lp_norm(p1, {0}, 1, 0); auto norm = builder::opset1::lp_norm(p1, {0}, 1, 0);
norm->add_provenance_tag("norm"); norm->add_provenance_tag("norm");
for (auto node : topological_sort(NodeVector{norm})) for (auto node : topological_sort(NodeVector{norm}))
{ {
......
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