From 3c5f949ad73fff961a7d193c9275b73df13b8096 Mon Sep 17 00:00:00 2001 From: John Richardson Date: Wed, 4 Oct 2017 15:27:37 +0100 Subject: COMPMID-575: Port Magnitude to new validation Change-Id: I2600947bef30853d00adfa4b919dbcb860de9bfd Reviewed-on: http://mpd-gerrit.cambridge.arm.com/91717 Tested-by: Kaizen Reviewed-by: Anthony Barbier --- arm_compute/runtime/CL/functions/CLMagnitude.h | 3 +- arm_compute/runtime/NEON/functions/NEMagnitude.h | 3 +- src/core/NEON/kernels/NEMagnitudePhaseKernel.cpp | 7 +- src/runtime/CL/functions/CLMagnitude.cpp | 4 +- src/runtime/NEON/functions/NEMagnitude.cpp | 32 ++++-- tests/Utils.h | 14 +++ tests/validation/CL/Magnitude.cpp | 123 ++++++++++++++++++++++ tests/validation/CPP/Magnitude.cpp | 68 ++++++++++++ tests/validation/CPP/Magnitude.h | 43 ++++++++ tests/validation/NEON/Magnitude.cpp | 125 +++++++++++++++++++++++ tests/validation/fixtures/MagnitudeFixture.h | 123 ++++++++++++++++++++++ utils/TypePrinter.h | 25 +++++ 12 files changed, 555 insertions(+), 15 deletions(-) create mode 100644 tests/validation/CL/Magnitude.cpp create mode 100644 tests/validation/CPP/Magnitude.cpp create mode 100644 tests/validation/CPP/Magnitude.h create mode 100644 tests/validation/NEON/Magnitude.cpp create mode 100644 tests/validation/fixtures/MagnitudeFixture.h diff --git a/arm_compute/runtime/CL/functions/CLMagnitude.h b/arm_compute/runtime/CL/functions/CLMagnitude.h index dc5f9139b3..f9c7e5c14a 100644 --- a/arm_compute/runtime/CL/functions/CLMagnitude.h +++ b/arm_compute/runtime/CL/functions/CLMagnitude.h @@ -41,8 +41,9 @@ public: * @param[in] input2 Second tensor input. Data types supported: S16. * @param[out] output Output tensor. Data types supported: S16. * @param[in] mag_type (Optional) Magnitude calculation type. Default: L2NORM. + * @param[in] use_fp16 (Optional) If true the FP16 kernels will be used. If false F32 kernels are used. */ - void configure(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, MagnitudeType mag_type = MagnitudeType::L2NORM); + void configure(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, MagnitudeType mag_type = MagnitudeType::L2NORM, bool use_fp16 = false); }; } #endif /*__ARM_COMPUTE_CLMAGNITUDE_H__ */ diff --git a/arm_compute/runtime/NEON/functions/NEMagnitude.h b/arm_compute/runtime/NEON/functions/NEMagnitude.h index 6c1f988ef0..5bc3faf664 100644 --- a/arm_compute/runtime/NEON/functions/NEMagnitude.h +++ b/arm_compute/runtime/NEON/functions/NEMagnitude.h @@ -39,9 +39,10 @@ public: * @param[in] input1 First tensor input. Data type supported: S16. * @param[in] input2 Second tensor input. Data type supported: S16. * @param[out] output Output tensor. Data type supported: S16. + * @param[in] mag_type (Optional) Magnitude calculation type. Default: L2NORM. * @param[in] use_fp16 (Optional) If true the FP16 kernels will be used. If false F32 kernels are used. */ - void configure(const ITensor *input1, const ITensor *input2, ITensor *output, bool use_fp16 = false); + void configure(const ITensor *input1, const ITensor *input2, ITensor *output, MagnitudeType mag_type = MagnitudeType::L2NORM, bool use_fp16 = false); }; } #endif /*__ARM_COMPUTE_NEMAGNITUDE_H__ */ diff --git a/src/core/NEON/kernels/NEMagnitudePhaseKernel.cpp b/src/core/NEON/kernels/NEMagnitudePhaseKernel.cpp index 2424ec139e..646cb84444 100644 --- a/src/core/NEON/kernels/NEMagnitudePhaseKernel.cpp +++ b/src/core/NEON/kernels/NEMagnitudePhaseKernel.cpp @@ -143,7 +143,7 @@ inline float32x4_t sqrtv(float32x4_t x) inline int16x8_t magnitude_l1(int16x8_t input1, int16x8_t input2) { - return vqaddq_s16(vabsq_s16(input1), vabsq_s16(input2)); + return vqaddq_s16(vqabsq_s16(input1), vqabsq_s16(input2)); } inline int16x8_t magnitude_l2(int16x8_t input1, int16x8_t input2) @@ -575,11 +575,8 @@ inline int16x8_t magnitude_l2(int16x8_t input1, int16x8_t input2) inline int16x8_t magnitude_l1(int16x8_t input1, int16x8_t input2) { - int16x8_t gx_abs = vabsq_s16(input1); - int16x8_t gy_abs = vabsq_s16(input2); - /* Saturating add */ - return vqaddq_s16(gx_abs, gy_abs); + return vqaddq_s16(vqabsq_s16(input1), vqabsq_s16(input2)); } inline uint8x8_t phase_signed(int16x8_t input1, int16x8_t input2) diff --git a/src/runtime/CL/functions/CLMagnitude.cpp b/src/runtime/CL/functions/CLMagnitude.cpp index 68b8c3545a..9d6ac7a11a 100644 --- a/src/runtime/CL/functions/CLMagnitude.cpp +++ b/src/runtime/CL/functions/CLMagnitude.cpp @@ -30,8 +30,10 @@ using namespace arm_compute; -void CLMagnitude::configure(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, MagnitudeType mag_type) +void CLMagnitude::configure(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, MagnitudeType mag_type, bool use_fp16) { + ARM_COMPUTE_UNUSED(use_fp16); //TODO(COMPMID-644): Add half float support + auto k = arm_compute::support::cpp14::make_unique(); k->configure(input1, input2, output, nullptr, mag_type); _kernel = std::move(k); diff --git a/src/runtime/NEON/functions/NEMagnitude.cpp b/src/runtime/NEON/functions/NEMagnitude.cpp index 7877995d6b..f86505449f 100644 --- a/src/runtime/NEON/functions/NEMagnitude.cpp +++ b/src/runtime/NEON/functions/NEMagnitude.cpp @@ -31,18 +31,36 @@ using namespace arm_compute; -void NEMagnitude::configure(const ITensor *input1, const ITensor *input2, ITensor *output, bool use_fp16) +void NEMagnitude::configure(const ITensor *input1, const ITensor *input2, ITensor *output, MagnitudeType mag_type, bool use_fp16) { if(use_fp16) { - auto k = arm_compute::support::cpp14::make_unique>(); - k->configure(input1, input2, output, nullptr); - _kernel = std::move(k); + if(mag_type == MagnitudeType::L1NORM) + { + auto k = arm_compute::support::cpp14::make_unique>(); + k->configure(input1, input2, output, nullptr); + _kernel = std::move(k); + } + else + { + auto k = arm_compute::support::cpp14::make_unique>(); + k->configure(input1, input2, output, nullptr); + _kernel = std::move(k); + } } else { - auto k = arm_compute::support::cpp14::make_unique>(); - k->configure(input1, input2, output, nullptr); - _kernel = std::move(k); + if(mag_type == MagnitudeType::L1NORM) + { + auto k = arm_compute::support::cpp14::make_unique>(); + k->configure(input1, input2, output, nullptr); + _kernel = std::move(k); + } + else + { + auto k = arm_compute::support::cpp14::make_unique>(); + k->configure(input1, input2, output, nullptr); + _kernel = std::move(k); + } } } diff --git a/tests/Utils.h b/tests/Utils.h index 4745b8bbd7..465cba88ab 100644 --- a/tests/Utils.h +++ b/tests/Utils.h @@ -106,6 +106,10 @@ using promote_t = typename promote::type; template using make_signed_conditional_t = typename std::conditional::value, std::make_signed, std::common_type>::type; + +template +using make_unsigned_conditional_t = typename std::conditional::value, std::make_unsigned, std::common_type>::type; + // clang-format on // *INDENT-ON* } @@ -298,6 +302,16 @@ struct common_promoted_signed_type using intermediate_type = typename traits::make_signed_conditional_t::type; }; +/** Find the unsigned promoted common type. + */ +template +struct common_promoted_unsigned_type +{ + using common_type = typename std::common_type::type; + using promoted_type = traits::promote_t; + using intermediate_type = typename traits::make_unsigned_conditional_t::type; +}; + /** Convert a linear index into n-dimensional coordinates. * * @param[in] shape Shape of the n-dimensional tensor. diff --git a/tests/validation/CL/Magnitude.cpp b/tests/validation/CL/Magnitude.cpp new file mode 100644 index 0000000000..b002239e01 --- /dev/null +++ b/tests/validation/CL/Magnitude.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/runtime/CL/functions/CLMagnitude.h" +#include "tests/CL/CLAccessor.h" +#include "tests/PaddingCalculator.h" +#include "tests/datasets/ShapeDatasets.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Macros.h" +#include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Validation.h" +#include "tests/validation/fixtures/MagnitudeFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +template +AbsoluteTolerance tolerance(MagnitudeType magnitude_type) +{ + return AbsoluteTolerance((MagnitudeType::L1NORM == magnitude_type) ? 0 : 1); +} +} // namespace + +TEST_SUITE(CL) +TEST_SUITE(Magnitude) + +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), framework::dataset::make("DataType", { DataType::S16, DataType::S32 })), + shape, data_type) +{ + // Create tensors + CLTensor src1 = create_tensor(shape, data_type); + CLTensor src2 = create_tensor(shape, data_type); + CLTensor dst = create_tensor(shape, data_type); + + ARM_COMPUTE_EXPECT(src1.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(src2.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Create and configure function (default MagnitudeType::L2NORM) + CLMagnitude magnitude; + magnitude.configure(&src1, &src2, &dst); + + // Validate valid region + const ValidRegion valid_region = shape_to_valid_region(shape); + validate(dst.info()->valid_region(), valid_region); + + // Validate padding + const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding(); + + validate(src1.info()->padding(), padding); + validate(src2.info()->padding(), padding); + validate(dst.info()->padding(), padding); +} + +template +using CLMagnitudeFixture = MagnitudeValidationFixture; + +TEST_SUITE(S16) +FIXTURE_DATA_TEST_CASE(RunSmall, CLMagnitudeFixture, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::Small2DShapes(), framework::dataset::make("Format", Format::S16)), + framework::dataset::make("MagnitudeType", { MagnitudeType::L1NORM, MagnitudeType::L2NORM })), + framework::dataset::make("UseFP16", false))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance(_magnitude_type)); +} + +FIXTURE_DATA_TEST_CASE(RunLarge, CLMagnitudeFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::Large2DShapes(), framework::dataset::make("Format", Format::S16)), + framework::dataset::make("MagnitudeType", { MagnitudeType::L1NORM, MagnitudeType::L2NORM })), + framework::dataset::make("UseFP16", false))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance(_magnitude_type)); +} +TEST_SUITE_END() // S16 + +TEST_SUITE(S32) +FIXTURE_DATA_TEST_CASE(RunSmall, CLMagnitudeFixture, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::Small2DShapes(), framework::dataset::make("Format", Format::S32)), + framework::dataset::make("MagnitudeType", { MagnitudeType::L1NORM, MagnitudeType::L2NORM })), + framework::dataset::make("UseFP16", false))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance(_magnitude_type)); +} + +FIXTURE_DATA_TEST_CASE(RunLarge, CLMagnitudeFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::Large2DShapes(), framework::dataset::make("Format", Format::S32)), + framework::dataset::make("MagnitudeType", { MagnitudeType::L1NORM, MagnitudeType::L2NORM })), + framework::dataset::make("UseFP16", false))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance(_magnitude_type)); +} +TEST_SUITE_END() // S32 + +TEST_SUITE_END() +TEST_SUITE_END() +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/CPP/Magnitude.cpp b/tests/validation/CPP/Magnitude.cpp new file mode 100644 index 0000000000..f0002bfb33 --- /dev/null +++ b/tests/validation/CPP/Magnitude.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "Magnitude.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +SimpleTensor magnitude(const SimpleTensor &gx, const SimpleTensor &gy, MagnitudeType magnitude_type) +{ + SimpleTensor mag(gx.shape(), gx.data_type()); + + using intermediate_type = typename common_promoted_unsigned_type::intermediate_type; + + for(int i = 0; i < gx.num_elements(); ++i) + { + double val = 0.f; + + if(magnitude_type == MagnitudeType::L1NORM) + { + val = static_cast(std::abs(gx[i])) + static_cast(std::abs(gy[i])); + } + else // MagnitudeType::L2NORM + { + // Note: kernel saturates to uint32_t instead of intermediate_type for S32 format + auto sum = static_cast(gx[i] * gx[i]) + static_cast(gy[i] * gy[i]); + val = std::sqrt(sum) + 0.5f; + } + + mag[i] = saturate_cast(val); + } + + return mag; +} + +template SimpleTensor magnitude(const SimpleTensor &gx, const SimpleTensor &gy, MagnitudeType magnitude_type); +template SimpleTensor magnitude(const SimpleTensor &gx, const SimpleTensor &gy, MagnitudeType magnitude_type); +template SimpleTensor magnitude(const SimpleTensor &gx, const SimpleTensor &gy, MagnitudeType magnitude_type); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/CPP/Magnitude.h b/tests/validation/CPP/Magnitude.h new file mode 100644 index 0000000000..75620712e3 --- /dev/null +++ b/tests/validation/CPP/Magnitude.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_TEST_MAGNITUDE_H__ +#define __ARM_COMPUTE_TEST_MAGNITUDE_H__ + +#include "tests/SimpleTensor.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +SimpleTensor magnitude(const SimpleTensor &gx, const SimpleTensor &gy, MagnitudeType magnitude_type); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_MAGNITUDE_H__ */ diff --git a/tests/validation/NEON/Magnitude.cpp b/tests/validation/NEON/Magnitude.cpp new file mode 100644 index 0000000000..cdc29a58dc --- /dev/null +++ b/tests/validation/NEON/Magnitude.cpp @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/runtime/NEON/functions/NEMagnitude.h" +#include "tests/NEON/Accessor.h" +#include "tests/PaddingCalculator.h" +#include "tests/datasets/ShapeDatasets.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Macros.h" +#include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Validation.h" +#include "tests/validation/fixtures/MagnitudeFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +template +AbsoluteTolerance tolerance(MagnitudeType magnitude_type) +{ + return AbsoluteTolerance((MagnitudeType::L1NORM == magnitude_type) ? 0 : 1); +} + +#ifdef ARM_COMPUTE_AARCH64_V8_2 +template <> +AbsoluteTolerance tolerance(MagnitudeType magnitude_type) +{ + return AbsoluteTolerance((MagnitudeType::L1NORM == magnitude_type) ? half(0.0) : half(1.0)); +} +#endif /* ARM_COMPUTE_AARCH64_V8_2 */ + +} // namespace + +TEST_SUITE(NEON) +TEST_SUITE(Magnitude) + +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), framework::dataset::make("DataType", DataType::S16)), shape, data_type) +{ + // Create tensors + Tensor src1 = create_tensor(shape, data_type); + Tensor src2 = create_tensor(shape, data_type); + Tensor dst = create_tensor(shape, data_type); + + ARM_COMPUTE_EXPECT(src1.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(src2.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Create and configure function (default MagnitudeType::L2NORM) + NEMagnitude magnitude; + magnitude.configure(&src1, &src2, &dst); + + // Validate valid region + const ValidRegion valid_region = shape_to_valid_region(shape); + validate(dst.info()->valid_region(), valid_region); + + // Validate padding + const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding(); + validate(src1.info()->padding(), padding); + validate(src2.info()->padding(), padding); + validate(dst.info()->padding(), padding); +} + +template +using NEMagnitudeFixture = MagnitudeValidationFixture; + +TEST_SUITE(S16) +FIXTURE_DATA_TEST_CASE(RunSmall, NEMagnitudeFixture, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::Small2DShapes(), framework::dataset::make("Format", Format::S16)), + framework::dataset::make("MagnitudeType", { MagnitudeType::L1NORM, MagnitudeType::L2NORM })), + framework::dataset::make("UseFP16", false))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance(_magnitude_type)); +} + +FIXTURE_DATA_TEST_CASE(RunLarge, NEMagnitudeFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::Large2DShapes(), framework::dataset::make("Format", Format::S16)), + framework::dataset::make("MagnitudeType", { MagnitudeType::L1NORM, MagnitudeType::L2NORM })), + framework::dataset::make("UseFP16", false))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance(_magnitude_type)); +} +TEST_SUITE_END() // S16 + +#ifdef ARM_COMPUTE_AARCH64_V8_2 +TEST_SUITE(F16) +FIXTURE_DATA_TEST_CASE(RunSmall, NEMagnitudeFixture, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::Small2DShapes(), framework::dataset::make("Format", + Format::S16)), + framework::dataset::make("MagnitudeType", { MagnitudeType::L1NORM, MagnitudeType::L2NORM })), + framework::dataset::make("UseFP16", true))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance(_magnitude_type)); +} +TEST_SUITE_END() // F16 +#endif /* ARM_COMPUTE_AARCH64_V8_2 */ + +TEST_SUITE_END() +TEST_SUITE_END() +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/fixtures/MagnitudeFixture.h b/tests/validation/fixtures/MagnitudeFixture.h new file mode 100644 index 0000000000..dcd3da8dfe --- /dev/null +++ b/tests/validation/fixtures/MagnitudeFixture.h @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_MAGNITUDE_FIXTURE +#define ARM_COMPUTE_TEST_MAGNITUDE_FIXTURE + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" +#include "tests/Globals.h" +#include "tests/IAccessor.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Fixture.h" +#include "tests/validation/CPP/Magnitude.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template +class MagnitudeValidationFixture : public framework::Fixture +{ +public: + template + void setup(TensorShape shape, Format format, MagnitudeType magnitude_type, bool use_fp16) + { + _target = compute_target(shape, format, magnitude_type, use_fp16); + _reference = compute_reference(shape, format, magnitude_type); + _magnitude_type = magnitude_type; + } + +protected: + template + void fill(U &&tensor, std::random_device::result_type seed_offset) + { + library->fill_tensor_uniform(tensor, seed_offset); + } + + TensorType compute_target(const TensorShape &shape, Format format, MagnitudeType magnitude_type, bool use_fp16) + { + DataType data_type = data_type_from_format(format); + + // Create tensors + TensorType src1 = create_tensor(shape, data_type); + src1.info()->set_format(format); + + TensorType src2 = create_tensor(shape, data_type); + src2.info()->set_format(format); + + TensorType dst = create_tensor(shape, data_type); + dst.info()->set_format(format); + + // Create and configure function + FunctionType magnitude; + magnitude.configure(&src1, &src2, &dst, magnitude_type, use_fp16); + + ARM_COMPUTE_EXPECT(src1.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(src2.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + src1.allocator()->allocate(); + src2.allocator()->allocate(); + dst.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!src1.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!src2.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Fill tensors + fill(AccessorType(src1), 0); + fill(AccessorType(src2), 1); + + // Compute function + magnitude.run(); + + return dst; + } + + SimpleTensor compute_reference(const TensorShape &shape, Format format, MagnitudeType magnitude_type) + { + DataType data_type = data_type_from_format(format); + + // Create reference + SimpleTensor src1{ shape, data_type }; + SimpleTensor src2{ shape, data_type }; + + // Fill reference + fill(src1, 0); + fill(src2, 1); + + return reference::magnitude(src1, src2, magnitude_type); + } + + TensorType _target{}; + SimpleTensor _reference{}; + MagnitudeType _magnitude_type{}; +}; +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_MAGNITUDE_FIXTURE */ diff --git a/utils/TypePrinter.h b/utils/TypePrinter.h index ec373742ea..4fd3c0c027 100644 --- a/utils/TypePrinter.h +++ b/utils/TypePrinter.h @@ -739,5 +739,30 @@ inline std::string to_string(const arm_compute::PhaseType &type) str << type; return str.str(); } + +/** Formatted output of the MagnitudeType type. */ +inline ::std::ostream &operator<<(::std::ostream &os, const MagnitudeType &magnitude_type) +{ + switch(magnitude_type) + { + case MagnitudeType::L1NORM: + os << "L1NORM"; + break; + case MagnitudeType::L2NORM: + os << "L2NORM"; + break; + default: + ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); + } + + return os; +} + +inline std::string to_string(const arm_compute::MagnitudeType &type) +{ + std::stringstream str; + str << type; + return str.str(); +} } // namespace arm_compute #endif /* __ARM_COMPUTE_TEST_TYPE_PRINTER_H__ */ -- cgit v1.2.1