Revise reduce mean (#3786)

* Update spec

* create type_prop tests

* add reduce_mean type_prop tests to CMakeList

* Update spec

* fix typo

* Add dynamic type_prop tests

* style fix
This commit is contained in:
Piotr Szmelczynski 2021-01-22 12:21:39 +01:00 committed by GitHub
parent e346bdde14
commit d2ef8bf2f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 3 deletions

View File

@ -33,12 +33,11 @@
**Detailed Description**
Each element in the output is the result of reduction with finding the arithmetic mean operation along dimensions specified by the 2nd input:
Each element in the output is the result of arithmetic mean reduction operation along dimensions specified by the 2nd input:
output[i0, i1, ..., iN] = mean[j0,..., jN](x[j0, ..., jN]))
Where indices i0, ..., iN run through all valid indices for the 1st input and finding the arithmetic mean `mean[j0, ..., jN]` have `jk = ik` for those dimensions `k` that are not in the set of indices specified by the 2nd input of the operation.
Corner cases:
Where indices i0, ..., iN run through all valid indices for the 1st input and finding the arithmetic mean `mean[j0, ..., jN]` have `jk = ik` for those dimensions `k` that are not in the set of indices specified by the 2nd input of the operation. Corner cases:
1. When the 2nd input is an empty list, then this operation does nothing, it is an identity.
2. When the 2nd input contains all dimensions of the 1st input, this means that a single reduction value is calculated for entire input tensor.

View File

@ -196,6 +196,7 @@ set(SRC
type_prop/squared_difference.cpp
type_prop/squeeze.cpp
type_prop/swish.cpp
type_prop/reduce_mean.cpp
type_prop/reduce_prod.cpp
type_prop/reduce_sum.cpp
type_prop/ti.cpp

View File

@ -0,0 +1,84 @@
//*****************************************************************************
// Copyright 2017-2020 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#include "gtest/gtest.h"
#include "ngraph/ngraph.hpp"
#include "util/type_prop.hpp"
using namespace std;
using namespace ngraph;
TEST(type_prop, reduce_mean_v1_axis_out_of_range)
{
auto arg = make_shared<op::Parameter>(element::f32, Shape{1, 2, 3});
auto axes = make_shared<op::Constant>(element::i64, Shape{2}, vector<int64_t>{2, 3});
try
{
auto reduce_sum = make_shared<op::v1::ReduceMean>(arg, axes);
// Should have thrown, so fail if it didn't
FAIL() << "Incorrect axes values exception not thrown";
}
catch (const NodeValidationFailure& error)
{
EXPECT_HAS_SUBSTRING(error.what(), std::string("Reduction axis ("));
}
catch (...)
{
FAIL() << "Deduced type check failed for unexpected reason";
}
}
TEST(type_prop, reduce_mean_v1_shape_if_keep_dims)
{
auto arg = make_shared<op::Parameter>(element::f32, Shape{3, 4, 5});
auto axes = make_shared<op::Constant>(element::i64, Shape{2}, vector<int64_t>{1, 2});
auto keep_dims = true;
auto reduce_prod = make_shared<op::v1::ReduceMean>(arg, axes, keep_dims);
ASSERT_TRUE(reduce_prod->get_output_partial_shape(0).compatible(PartialShape{3, 1, 1}));
}
TEST(type_prop, reduce_mean_v1_shape_if_not_keep_dims)
{
auto arg = make_shared<op::Parameter>(element::f32, Shape{3, 4, 5});
auto axes = make_shared<op::Constant>(element::i64, Shape{2}, vector<int64_t>{1, 2});
auto keep_dims = false;
auto reduce_prod = make_shared<op::v1::ReduceMean>(arg, axes, keep_dims);
ASSERT_TRUE(reduce_prod->get_output_partial_shape(0).compatible(PartialShape{3}));
}
TEST(type_prop, reduce_mean_dynamic_shape)
{
auto arg =
make_shared<op::Parameter>(element::f32, PartialShape{3, 4, 5, Dimension::dynamic()});
auto axes = make_shared<op::Constant>(element::i64, Shape{2}, vector<int64_t>{1, 2});
auto keep_dims = true;
auto reduce_prod = make_shared<op::v1::ReduceMean>(arg, axes, keep_dims);
ASSERT_TRUE(reduce_prod->get_output_partial_shape(0).compatible(
PartialShape{3, 1, 1, Dimension::dynamic()}));
}
TEST(type_prop, reduce_mean_reduce_dynamic_shape)
{
auto arg =
make_shared<op::Parameter>(element::f32, PartialShape{3, 4, 5, Dimension::dynamic()});
auto axes = make_shared<op::Constant>(element::i64, Shape{2}, vector<int64_t>{1, 3});
auto keep_dims = true;
auto reduce_prod = make_shared<op::v1::ReduceMean>(arg, axes, keep_dims);
ASSERT_TRUE(reduce_prod->get_output_partial_shape(0).compatible(
PartialShape{3, 1, 5, Dimension::dynamic()}));
}