From bdcb4c148ee2fdeaaddf4cf1e57bbb0de02bb894 Mon Sep 17 00:00:00 2001 From: Adnan AlSinan Date: Mon, 18 Sep 2023 14:49:45 +0100 Subject: Implement tflite compliant reverse for CPU - Add support for negative axis values. - Add option to use opposite ACL convention for dimension addressing. - Add validation tests for the mentioned additions. Resolves COMPMID-6497 Change-Id: I9174b201c3adc070766cc6cffcbe4ec1fe5ec1c3 Signed-off-by: Adnan AlSinan Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/10335 Comments-Addressed: Arm Jenkins Tested-by: Arm Jenkins Reviewed-by: SiCong Li Benchmark: Arm Jenkins --- tests/validation/fixtures/ReverseFixture.h | 74 +++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 17 deletions(-) (limited to 'tests/validation/fixtures') diff --git a/tests/validation/fixtures/ReverseFixture.h b/tests/validation/fixtures/ReverseFixture.h index 509fd93abf..8ff8cf9421 100644 --- a/tests/validation/fixtures/ReverseFixture.h +++ b/tests/validation/fixtures/ReverseFixture.h @@ -21,12 +21,15 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef ARM_COMPUTE_TEST_REVERSE_FIXTURE -#define ARM_COMPUTE_TEST_REVERSE_FIXTURE +#ifndef ACL_TESTS_VALIDATION_FIXTURES_REVERSEFIXTURE_H +#define ACL_TESTS_VALIDATION_FIXTURES_REVERSEFIXTURE_H #include "arm_compute/core/Helpers.h" #include "arm_compute/core/TensorShape.h" #include "arm_compute/core/Types.h" +#ifdef ARM_COMPUTE_OPENCL_ENABLED +#include "arm_compute/runtime/CL/functions/CLReverse.h" +#endif // ARM_COMPUTE_OPENCL_ENABLED #include "arm_compute/runtime/Tensor.h" #include "tests/AssetsLibrary.h" #include "tests/Globals.h" @@ -41,14 +44,40 @@ namespace test { namespace validation { +namespace +{ +template +#ifdef ARM_COMPUTE_OPENCL_ENABLED +std::enable_if_t < !std::is_same::value, void > +#else // ARM_COMPUTE_OPENCL_ENABLED +void +#endif // ARM_COMPUTE_OPENCL_ENABLED +configureReverse(ReverseFunction &func, TensorType &src, TensorType &axis, TensorType &dst, bool use_inverted_axis) +{ + func.configure(&src, &dst, &axis, use_inverted_axis); +} + +#ifdef ARM_COMPUTE_OPENCL_ENABLED +template +std::enable_if_t::value, void> +configureReverse(ReverseFunction &func, TensorType &src, TensorType &axis, TensorType &dst, bool use_inverted_axis) +{ + ARM_COMPUTE_UNUSED(use_inverted_axis); + func.configure(&src, &dst, &axis); +} + +#endif // ARM_COMPUTE_OPENCL_ENABLED +} //namespace + template class ReverseValidationFixture : public framework::Fixture { public: - void setup(TensorShape shape, TensorShape axis_shape, DataType data_type) + void setup(TensorShape shape, TensorShape axis_shape, DataType data_type, bool use_negative_axis = false, bool use_inverted_axis = false) { - _target = compute_target(shape, axis_shape, data_type); - _reference = compute_reference(shape, axis_shape, data_type); + _num_dims = shape.num_dimensions(); + _target = compute_target(shape, axis_shape, data_type, use_negative_axis, use_inverted_axis); + _reference = compute_reference(shape, axis_shape, data_type, use_negative_axis, use_inverted_axis); } protected: @@ -57,16 +86,25 @@ protected: { library->fill_tensor_uniform(tensor, 0); } - std::vector generate_random_axis() + std::vector generate_random_axis(bool use_negative = false) { - std::vector axis_v = { 0, 1, 2, 3 }; - std::mt19937 g(0); + std::vector axis_v; + if(use_negative) + { + axis_v = { -1, -2, -3, -4 }; + } + else + { + axis_v = { 0, 1, 2, 3 }; + } + axis_v = std::vector(axis_v.begin(), axis_v.begin() + _num_dims); + std::mt19937 g(library->seed()); std::shuffle(axis_v.begin(), axis_v.end(), g); return axis_v; } - TensorType compute_target(const TensorShape &shape, const TensorShape &axis_shape, DataType data_type) + TensorType compute_target(const TensorShape &shape, const TensorShape &axis_shape, DataType data_type, bool use_negative_axis, bool use_inverted_axis = false) { // Create tensors TensorType src = create_tensor(shape, data_type, 1); @@ -75,7 +113,8 @@ protected: // Create and configure function FunctionType reverse_func; - reverse_func.configure(&src, &dst, &axis); + + configureReverse(reverse_func, src, axis, dst, use_inverted_axis); ARM_COMPUTE_ASSERT(src.info()->is_resizable()); ARM_COMPUTE_ASSERT(axis.info()->is_resizable()); @@ -94,7 +133,7 @@ protected: fill(AccessorType(src)); { auto axis_data = AccessorType(axis); - auto axis_v = generate_random_axis(); + auto axis_v = generate_random_axis(use_negative_axis); std::copy(axis_v.begin(), axis_v.begin() + axis_shape.x(), static_cast(axis_data.data())); } @@ -104,24 +143,25 @@ protected: return dst; } - SimpleTensor compute_reference(const TensorShape &shape, const TensorShape &axis_shape, DataType data_type) + SimpleTensor compute_reference(const TensorShape &shape, const TensorShape &axis_shape, DataType data_type, bool use_negative_axis, bool use_inverted_axis = false) { // Create reference - SimpleTensor src{ shape, data_type }; - SimpleTensor axis{ axis_shape, DataType::U32 }; + SimpleTensor src{ shape, data_type }; + SimpleTensor axis{ axis_shape, DataType::S32 }; // Fill reference fill(src); - auto axis_v = generate_random_axis(); + auto axis_v = generate_random_axis(use_negative_axis); std::copy(axis_v.begin(), axis_v.begin() + axis_shape.x(), axis.data()); - return reference::reverse(src, axis); + return reference::reverse(src, axis, use_inverted_axis); } TensorType _target{}; SimpleTensor _reference{}; + unsigned int _num_dims{}; }; } // namespace validation } // namespace test } // namespace arm_compute -#endif /* ARM_COMPUTE_TEST_REVERSE_FIXTURE */ +#endif // ACL_TESTS_VALIDATION_FIXTURES_REVERSEFIXTURE_H -- cgit v1.2.1