Skip to content

Commit

Permalink
Add support for ReduceL1 and ReduceL2 to ONNX Importer (#1838)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartosz Lesniewski authored Aug 27, 2020
1 parent c147f03 commit d368241
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
1 change: 0 additions & 1 deletion ngraph/core/include/ngraph/op/reduce_l1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ namespace ngraph
///
/// \param arg The tensor to be reduced.
/// \param reduction_axes The axis positions (0-based) to be eliminated.
/// \param p The scalar defining the order of normalization.
/// \param keep_dims If set to true it holds axes that are used for reduction.
ReduceL1(const Output<Node>& arg,
const Output<Node>& reduction_axes,
Expand Down
28 changes: 12 additions & 16 deletions ngraph/frontend/onnx_import/src/op/reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,24 @@ namespace ngraph

OutputVector reduce_l1(const Node& node)
{
auto l1_norm_reduction = [](const Output<ngraph::Node>& node,
const ngraph::AxisSet& axis_set) {
const auto axis_set_const = default_opset::Constant::create(
element::i64, {axis_set.size()}, axis_set.to_vector());
return ngraph::builder::opset1::l1_norm(node, axis_set_const, 0.f);
};

return {reduction::make_ng_reduction_op(
node, node.get_ng_inputs().at(0), l1_norm_reduction)};
node,
node.get_ng_inputs().at(0),
std::make_shared<default_opset::ReduceL1,
const Output<ngraph::Node>&,
const Output<ngraph::Node>&,
bool>)};
}

OutputVector reduce_l2(const Node& node)
{
auto l2_norm_reduction = [](const Output<ngraph::Node>& node,
const ngraph::AxisSet& axis_set) {
const auto axis_set_const = default_opset::Constant::create(
element::i64, {axis_set.size()}, axis_set.to_vector());
return ngraph::builder::opset1::l2_norm(
node, axis_set_const, 0.f, ngraph::builder::BiasMode::ADD, false);
};
return {reduction::make_ng_reduction_op(
node, node.get_ng_inputs().at(0), l2_norm_reduction)};
node,
node.get_ng_inputs().at(0),
std::make_shared<default_opset::ReduceL2,
const Output<ngraph::Node>&,
const Output<ngraph::Node>&,
bool>)};
}

OutputVector reduce_max(const Node& node)
Expand Down
6 changes: 2 additions & 4 deletions ngraph/python/tests/test_onnx/test_ops_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ def test_reduce_l1(reduction_axes):
assert np.allclose(expected, ng_result)


@xfail_issue_35925
def test_reduce_l1_default_axes():
shape = [2, 4, 3, 2]
np.random.seed(133391)
Expand All @@ -108,7 +107,7 @@ def test_reduce_l1_default_axes():
assert np.array_equal(expected.shape, ng_result.shape)
assert np.allclose(expected, ng_result)

expected = np.sum(np.abs(input_data), keepdims=False)
expected = np.array([np.sum(np.abs(input_data), keepdims=False)])
node = onnx.helper.make_node("ReduceL1", inputs=["x"], outputs=["y"], keepdims=0)
ng_result = np.array(run_node(node, [input_data]).pop())
assert np.array_equal(expected.shape, ng_result.shape)
Expand All @@ -135,7 +134,6 @@ def test_reduce_l2(reduction_axes):
assert np.allclose(expected, ng_result)


@xfail_issue_35925
def test_reduce_l2_default_axes():
shape = [2, 4, 3, 2]
np.random.seed(133391)
Expand All @@ -147,7 +145,7 @@ def test_reduce_l2_default_axes():
assert np.array_equal(expected.shape, ng_result.shape)
assert np.allclose(expected, ng_result)

expected = np.sqrt(np.sum(np.square(input_data), keepdims=False))
expected = np.array([np.sqrt(np.sum(np.square(input_data), keepdims=False))])
node = onnx.helper.make_node("ReduceL2", inputs=["x"], outputs=["y"], keepdims=0)
ng_result = np.array(run_node(node, [input_data]).pop())
assert np.array_equal(expected.shape, ng_result.shape)
Expand Down

0 comments on commit d368241

Please sign in to comment.