aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPablo Tello <pablo.tello@arm.com>2019-11-08 13:47:53 +0000
committerSiCongLi <sicong.li@arm.com>2019-12-11 18:52:46 +0000
commit6f58d1ec864b9a4960181ceeab177cd0db54e2e1 (patch)
tree14c8435126da52243f2c6eb4b39317fd37e6260c
parente6c3344f2a76b0724172200b1d5a443393045336 (diff)
downloadComputeLibrary-6f58d1ec864b9a4960181ceeab177cd0db54e2e1.tar.gz
COMPMID-2855: NEReduceMean throws error for invalid configs
Change-Id: I600507d0de19d7da6c1a13edcfff0a11ea6b5264 Signed-off-by: Pablo Tello <pablo.tello@arm.com> Reviewed-on: https://review.mlplatform.org/c/2254 Comments-Addressed: Arm Jenkins <bsgcomp@arm.com> Tested-by: Arm Jenkins <bsgcomp@arm.com> Reviewed-by: Michele Di Giorgio <michele.digiorgio@arm.com> Reviewed-by: Giorgio Arena <giorgio.arena@arm.com> Reviewed-by: Giuseppe Rossini <giuseppe.rossini@arm.com> Reviewed-by: Michalis Spyrou <michalis.spyrou@arm.com> Reviewed-by: Manuel Bottini <manuel.bottini@arm.com> Reviewed-by: SiCong Li <sicong.li@arm.com>
-rw-r--r--arm_compute/core/Helpers.h14
-rw-r--r--arm_compute/runtime/NEON/functions/NEReduceMean.h2
-rw-r--r--src/runtime/NEON/functions/NEReduceMean.cpp85
-rw-r--r--tests/validation/NEON/ReduceMean.cpp22
4 files changed, 76 insertions, 47 deletions
diff --git a/arm_compute/core/Helpers.h b/arm_compute/core/Helpers.h
index 87b1fdf64c..8d526e96c0 100644
--- a/arm_compute/core/Helpers.h
+++ b/arm_compute/core/Helpers.h
@@ -766,6 +766,20 @@ inline T wrap_around(T x, T m)
return x >= 0 ? x % m : (x % m + m) % m;
}
+/** Convert negative coordinates to positive in the range [0, num_dims_input]
+ *
+ * @param[out] coords Array of coordinates to be converted.
+ * @param[in] max_value Maximum value to be used when wrapping the negative values in coords
+ */
+inline Coordinates &convert_negative_axis(Coordinates &coords, int max_value)
+{
+ for(unsigned int i = 0; i < coords.num_dimensions(); ++i)
+ {
+ coords[i] = wrap_around(coords[i], max_value);
+ }
+ return coords;
+}
+
/** Given an integer value, this function returns the next power of two
*
* @param[in] x Input value
diff --git a/arm_compute/runtime/NEON/functions/NEReduceMean.h b/arm_compute/runtime/NEON/functions/NEReduceMean.h
index fdd8edfe87..245f7577ce 100644
--- a/arm_compute/runtime/NEON/functions/NEReduceMean.h
+++ b/arm_compute/runtime/NEON/functions/NEReduceMean.h
@@ -72,7 +72,7 @@ private:
std::vector<NEReductionOperation> _reduction_kernels;
std::vector<Tensor> _reduced_outs;
NEReshapeLayer _reshape;
- unsigned int _reduction_ops;
+ int _reduction_ops;
bool _keep_dims;
};
} // namespace arm_compute
diff --git a/src/runtime/NEON/functions/NEReduceMean.cpp b/src/runtime/NEON/functions/NEReduceMean.cpp
index 0b145f034d..4547a1f9b0 100644
--- a/src/runtime/NEON/functions/NEReduceMean.cpp
+++ b/src/runtime/NEON/functions/NEReduceMean.cpp
@@ -24,6 +24,7 @@
#include "arm_compute/runtime/NEON/functions/NEReduceMean.h"
#include "arm_compute/core/CPP/Validate.h"
+#include "arm_compute/core/Error.h"
#include "arm_compute/core/Helpers.h"
#include "arm_compute/runtime/NEON/NEScheduler.h"
@@ -34,49 +35,64 @@ NEReduceMean::NEReduceMean(std::shared_ptr<IMemoryManager> memory_manager)
{
}
-Status NEReduceMean::validate(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)
+Status validate_config(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)
{
ARM_COMPUTE_UNUSED(keep_dims);
- ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
+ ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
+ ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() < 1);
ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions());
- TensorShape out_shape = input->tensor_shape();
const unsigned int reduction_ops = reduction_axis.num_dimensions();
const int input_dims = input->num_dimensions();
Coordinates axis_local = reduction_axis;
- // Convert negative axis
- for(unsigned int i = 0; i < reduction_ops; ++i)
+ for(unsigned int i = 0; i < axis_local.num_dimensions(); ++i)
{
- axis_local[i] = wrap_around(axis_local[i], input_dims);
+ //axis: The dimensions to reduce. Must be in the range [-rank(input_tensor), rank(input_tensor)).
+ ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] < (-static_cast<int>(input->num_dimensions())));
+ ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] >= static_cast<int>(input->num_dimensions()));
}
- std::sort(axis_local.begin(), axis_local.begin() + reduction_ops);
- for(unsigned int i = 0; i < reduction_ops; ++i)
+ if(output->tensor_shape().total_size() != 0)
{
- ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] > 3);
- ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(axis_local[i]) > input->num_dimensions() - 1);
- if(output->total_size() > 0 && keep_dims)
- {
- ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(axis_local[i]) != 1);
- }
- if(keep_dims)
+ // Only validate if not using auto_init for the output tensor
+ TensorShape out_shape = input->tensor_shape();
+ // Validate output_shape only if not using auto_init
+ convert_negative_axis(axis_local, input_dims);
+ std::sort(axis_local.begin(), axis_local.begin() + reduction_ops);
+ for(unsigned int i = 0; i < reduction_ops; ++i)
{
- out_shape.set(axis_local[i], 1);
- }
- else
- {
- out_shape.remove_dimension(axis_local[i] - i);
+ ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] > 3);
+ ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(axis_local[i]) > input->num_dimensions() - 1);
+ if(output->total_size() > 0 && keep_dims)
+ {
+ ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(axis_local[i]) != 1);
+ }
+ if(keep_dims)
+ {
+ out_shape.set(axis_local[i], 1);
+ }
+ else
+ {
+ ARM_COMPUTE_RETURN_ERROR_ON(i > static_cast<unsigned int>(axis_local[i]));
+ const unsigned int remove_index = axis_local[i] - i;
+ ARM_COMPUTE_RETURN_ERROR_ON(remove_index >= out_shape.num_dimensions());
+ out_shape.remove_dimension(remove_index);
+ }
}
+ const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape);
+ ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info);
}
- const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape);
- ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info);
-
return Status{};
}
+Status NEReduceMean::validate(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)
+{
+ return validate_config(input, reduction_axis, keep_dims, output);
+}
+
void NEReduceMean::configure(ITensor *input, const Coordinates &reduction_axis, bool keep_dims, ITensor *output)
{
ARM_COMPUTE_ERROR_ON_NULLPTR(input);
@@ -86,18 +102,13 @@ void NEReduceMean::configure(ITensor *input, const Coordinates &reduction_axis,
_reduced_outs.resize(_reduction_ops - (keep_dims ? 1 : 0));
_keep_dims = keep_dims;
- Coordinates axis_local = reduction_axis;
- const int input_dims = input->info()->num_dimensions();
- const unsigned int reduction_ops = reduction_axis.num_dimensions();
+ Coordinates axis_local = reduction_axis;
+ const int input_dims = input->info()->num_dimensions();
- // Convert negative axis
- for(unsigned int i = 0; i < reduction_ops; ++i)
- {
- axis_local[i] = wrap_around(axis_local[i], input_dims);
- }
+ convert_negative_axis(axis_local, input_dims);
// Perform reduction for every axis
- for(unsigned int i = 0; i < _reduction_ops; ++i)
+ for(int i = 0; i < _reduction_ops; ++i)
{
TensorShape out_shape = i == 0 ? input->info()->tensor_shape() : (&_reduced_outs[i - 1])->info()->tensor_shape();
out_shape.set(axis_local[i], 1);
@@ -116,7 +127,7 @@ void NEReduceMean::configure(ITensor *input, const Coordinates &reduction_axis,
}
// Allocate intermediate tensors
- for(unsigned int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i)
+ for(int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i)
{
_reduced_outs[i].allocator()->allocate();
}
@@ -125,11 +136,10 @@ void NEReduceMean::configure(ITensor *input, const Coordinates &reduction_axis,
if(!keep_dims)
{
TensorShape out_shape = input->info()->tensor_shape();
-
// We have to sort the reduction axis vectors in order for remove_dimension
// to work properly
std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops);
- for(unsigned int i = 0; i < _reduction_ops; ++i)
+ for(int i = 0; i < _reduction_ops; ++i)
{
out_shape.remove_dimension(axis_local[i] - i);
}
@@ -141,10 +151,9 @@ void NEReduceMean::configure(ITensor *input, const Coordinates &reduction_axis,
void NEReduceMean::run()
{
MemoryGroupResourceScope scope_mg(_memory_group);
-
- for(unsigned int i = 0; i < _reduction_ops; ++i)
+ for(auto &kernel : _reduction_kernels)
{
- _reduction_kernels[i].run();
+ kernel.run();
}
if(!_keep_dims)
diff --git a/tests/validation/NEON/ReduceMean.cpp b/tests/validation/NEON/ReduceMean.cpp
index 3cd7ce362e..6d0caf7160 100644
--- a/tests/validation/NEON/ReduceMean.cpp
+++ b/tests/validation/NEON/ReduceMean.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2018 ARM Limited.
+ * Copyright (c) 2018-2019 ARM Limited.
*
* SPDX-License-Identifier: MIT
*
@@ -57,20 +57,26 @@ TEST_SUITE(ReduceMean)
// *INDENT-OFF*
// clang-format off
-DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
+DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(
framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 3U, 16U, 2U), 1, DataType::F32), // Invalid axis
TensorInfo(TensorShape(27U, 3U, 16U, 2U), 1, DataType::F32), // Invalid output shape
- TensorInfo(TensorShape(32U, 16U, 16U, 2U), 1, DataType::F32)
+ TensorInfo(TensorShape(32U, 16U, 16U, 2U), 1, DataType::F32),// OK
+ TensorInfo(TensorShape{228U, 19U, 2U, 2U}, 1, DataType::F32),// OK
+ TensorInfo(TensorShape{228U, 19U, 2U, 1U}, 1, DataType::F32) // Cannot support axis 3 not valid
}),
framework::dataset::make("OutputInfo", { TensorInfo(TensorShape(27U, 3U, 1U, 2U), 1, DataType::F32),
TensorInfo(TensorShape(27U, 3U, 1U, 2U), 1, DataType::F32),
- TensorInfo(TensorShape(32U, 16U, 1U, 2U), 1, DataType::F32)
+ TensorInfo(TensorShape(32U, 16U, 1U, 2U), 1, DataType::F32),
+ TensorInfo(TensorShape(19U), 1, DataType::F32),
+ TensorInfo(TensorShape(19U), 1, DataType::F32)
+
})),
- framework::dataset::make("Axis", { Coordinates(4), Coordinates(0,2), Coordinates(2) })),
- framework::dataset::make("Expected", { false, false, true })),
- input_info, output_info, axis, expected)
+ framework::dataset::make("Axis", { Coordinates(4), Coordinates(0,2), Coordinates(2), Coordinates(3,2,0), Coordinates(3,2,0) })),
+ framework::dataset::make("Keep", { true, true, true, false, false })),
+ framework::dataset::make("Expected", { false, false, true, true, false })),
+ input_info, output_info, axis, keep, expected)
{
- const Status status = NEReduceMean::validate(&input_info.clone()->set_is_resizable(false), axis, true, &output_info.clone()->set_is_resizable(false));
+ const Status status = NEReduceMean::validate(&input_info.clone()->set_is_resizable(false), axis, keep, &output_info.clone()->set_is_resizable(false));
ARM_COMPUTE_EXPECT(bool(status) == expected, framework::LogLevel::ERRORS);
}
// clang-format on