From 4dfc235367e602a366952a8495679e339d7a7263 Mon Sep 17 00:00:00 2001 From: Moritz Pflanzer Date: Wed, 2 Aug 2017 14:51:36 +0100 Subject: COMPMID-415: Move GEMM to new validation Change-Id: Ie32f981c86fa1b01905d2776b0d0a0a47b228f0b Reviewed-on: http://mpd-gerrit.cambridge.arm.com/82538 Tested-by: Kaizen Reviewed-by: Anthony Barbier --- scripts/check_bad_style.sh | 7 + tests/datasets_new/LargeGEMMDataset.h | 54 ++++++ tests/datasets_new/SmallGEMMDataset.h | 54 ++++++ tests/validation/CL/GEMM.cpp | 201 ----------------------- tests/validation/NEON/GEMM.cpp | 219 ------------------------- tests/validation/Reference.cpp | 29 ---- tests/validation/Reference.h | 15 -- tests/validation/ReferenceCPP.cpp | 11 -- tests/validation/ReferenceCPP.h | 11 -- tests/validation/TensorOperations.h | 73 --------- tests/validation/TensorVisitors.h | 23 --- tests/validation_new/CL/GEMM.cpp | 167 +++++++++++++++++++ tests/validation_new/CL/NormalizationLayer.cpp | 6 +- tests/validation_new/CPP/GEMM.cpp | 122 ++++++++++++++ tests/validation_new/CPP/GEMM.h | 47 ++++++ tests/validation_new/NEON/GEMM.cpp | 170 +++++++++++++++++++ tests/validation_new/fixtures/GEMMFixture.h | 152 +++++++++++++++++ 17 files changed, 774 insertions(+), 587 deletions(-) create mode 100644 tests/datasets_new/LargeGEMMDataset.h create mode 100644 tests/datasets_new/SmallGEMMDataset.h delete mode 100644 tests/validation/CL/GEMM.cpp delete mode 100644 tests/validation/NEON/GEMM.cpp create mode 100644 tests/validation_new/CL/GEMM.cpp create mode 100644 tests/validation_new/CPP/GEMM.cpp create mode 100644 tests/validation_new/CPP/GEMM.h create mode 100644 tests/validation_new/NEON/GEMM.cpp create mode 100644 tests/validation_new/fixtures/GEMMFixture.h diff --git a/scripts/check_bad_style.sh b/scripts/check_bad_style.sh index 6a88b67cdc..0f0b657521 100755 --- a/scripts/check_bad_style.sh +++ b/scripts/check_bad_style.sh @@ -60,6 +60,13 @@ then exit -1 fi +grep -Hnir "ARM_COMPUTE_ENABLE_FP16" ./tests/validation_new/CL | tee bad_style.log +if [[ $(cat bad_style.log | wc -l) > 0 ]] +then + echo "" + echo "ERROR: Found ARM_COMPUTE_ENABLE_FP16 in CL tests though F16 is always supported for OpenCL" + exit -1 +fi spdx_missing=0 for f in $(find $DIRECTORIES -type f) diff --git a/tests/datasets_new/LargeGEMMDataset.h b/tests/datasets_new/LargeGEMMDataset.h new file mode 100644 index 0000000000..f757ef34c6 --- /dev/null +++ b/tests/datasets_new/LargeGEMMDataset.h @@ -0,0 +1,54 @@ +/* + * 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_LARGE_GEMM_DATASET +#define ARM_COMPUTE_TEST_LARGE_GEMM_DATASET + +#include "tests/datasets_new/GEMMDataset.h" + +#include "tests/TypePrinter.h" + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" + +namespace arm_compute +{ +namespace test +{ +namespace datasets +{ +class LargeGEMMDataset final : public GEMMDataset +{ +public: + LargeGEMMDataset() + { + add_config(TensorShape(923U, 429U), TensorShape(871U, 923U), TensorShape(871U, 429U), TensorShape(871U, 429U), 1.0f, 0.0f); + add_config(TensorShape(1021U, 1U), TensorShape(783U, 1021U), TensorShape(783U, 1U), TensorShape(783U, 1U), 1.0f, 0.0f); + add_config(TensorShape(681U, 1023U), TensorShape(213U, 681U), TensorShape(213U, 1023U), TensorShape(213U, 1023U), 0.2f, 1.2f); + add_config(TensorShape(941U, 1U), TensorShape(623U, 941U), TensorShape(623U, 1U), TensorShape(623U, 1U), 0.4f, 0.7f); + } +}; +} // namespace datasets +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_LARGE_GEMM_DATASET */ diff --git a/tests/datasets_new/SmallGEMMDataset.h b/tests/datasets_new/SmallGEMMDataset.h new file mode 100644 index 0000000000..7856ff9127 --- /dev/null +++ b/tests/datasets_new/SmallGEMMDataset.h @@ -0,0 +1,54 @@ +/* + * 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_SMALL_GEMM_DATASET +#define ARM_COMPUTE_TEST_SMALL_GEMM_DATASET + +#include "tests/datasets_new/GEMMDataset.h" + +#include "tests/TypePrinter.h" + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" + +namespace arm_compute +{ +namespace test +{ +namespace datasets +{ +class SmallGEMMDataset final : public GEMMDataset +{ +public: + SmallGEMMDataset() + { + add_config(TensorShape(21U, 13U), TensorShape(33U, 21U), TensorShape(33U, 13U), TensorShape(33U, 13U), 1.0f, 0.0f); + add_config(TensorShape(31U, 1U), TensorShape(23U, 31U), TensorShape(23U, 1U), TensorShape(23U, 1U), 1.0f, 0.0f); + add_config(TensorShape(38U, 12U), TensorShape(21U, 38U), TensorShape(21U, 12U), TensorShape(21U, 12U), 0.2f, 1.2f); + add_config(TensorShape(32U, 1U), TensorShape(17U, 32U), TensorShape(17U, 1U), TensorShape(17U, 1U), 0.4f, 0.7f); + } +}; +} // namespace datasets +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_SMALL_GEMM_DATASET */ diff --git a/tests/validation/CL/GEMM.cpp b/tests/validation/CL/GEMM.cpp deleted file mode 100644 index a39cdfd2e3..0000000000 --- a/tests/validation/CL/GEMM.cpp +++ /dev/null @@ -1,201 +0,0 @@ -/* - * 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 "AssetsLibrary.h" -#include "CL/CLAccessor.h" -#include "TypePrinter.h" -#include "dataset/GEMMDataset.h" -#include "tests/Globals.h" -#include "tests/Utils.h" -#include "validation/Datasets.h" -#include "validation/Reference.h" -#include "validation/Validation.h" - -#include "arm_compute/core/Helpers.h" -#include "arm_compute/core/Types.h" -#include "arm_compute/runtime/CL/functions/CLGEMM.h" -#include "arm_compute/runtime/Tensor.h" -#include "arm_compute/runtime/TensorAllocator.h" - -#include "boost_wrapper.h" - -#include -#include - -using namespace arm_compute; -using namespace arm_compute::test; -using namespace arm_compute::test::validation; - -namespace -{ -const float tolerance_f32 = 1e-03f; /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */ -const float tolerance_q = 1.0f; /**< Tolerance value for comparing reference's output against implementation's output for fixed point data types */ - -CLTensor compute_gemm(const TensorShape &src_shape1, const TensorShape &src_shape2, const TensorShape &src_shape3, - const TensorShape &out_shape, float alpha, float beta, DataType dt, int fixed_point_position = 0) -{ - // Create tensors - CLTensor src1 = create_tensor(src_shape1, dt, 1, fixed_point_position); - CLTensor src2 = create_tensor(src_shape2, dt, 1, fixed_point_position); - CLTensor src3 = create_tensor(src_shape3, dt, 1, fixed_point_position); - CLTensor dst = create_tensor(out_shape, dt, 1, fixed_point_position); - - // Create and configure function - CLGEMM gemm; - gemm.configure(&src1, &src2, &src3, &dst, alpha, beta); - - // Allocate tensors - src1.allocator()->allocate(); - src2.allocator()->allocate(); - src3.allocator()->allocate(); - dst.allocator()->allocate(); - - BOOST_TEST(!src1.info()->is_resizable()); - BOOST_TEST(!src2.info()->is_resizable()); - BOOST_TEST(!src3.info()->is_resizable()); - BOOST_TEST(!dst.info()->is_resizable()); - - // Fill tensors - if(dt == DataType::F32) - { - std::uniform_real_distribution<> distribution(-1.0f, 1.0f); - library->fill(CLAccessor(src1), distribution, 0); - library->fill(CLAccessor(src2), distribution, 1); - library->fill(CLAccessor(src3), distribution, 2); - } - else - { - library->fill_tensor_uniform(CLAccessor(src1), 0); - library->fill_tensor_uniform(CLAccessor(src2), 1); - library->fill_tensor_uniform(CLAccessor(src3), 2); - } - - // Compute function - gemm.run(); - - return dst; -} -} // namespace - -#ifndef DOXYGEN_SKIP_THIS -BOOST_AUTO_TEST_SUITE(CL) -BOOST_AUTO_TEST_SUITE(GEMM) - -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly")) -BOOST_DATA_TEST_CASE(Configuration, - SmallGEMMDataset() * boost::unit_test::data::make({ DataType::F32, DataType::QS8, DataType::QS16 }), - gemm_set, dt) -{ - // Set fixed point position data type allowed - int fixed_point_position = (dt == DataType::F32) ? 0 : 3; - - // Create tensors - CLTensor src1 = create_tensor(gemm_set.shape_a, dt, 1, fixed_point_position); - CLTensor src2 = create_tensor(gemm_set.shape_b, dt, 1, fixed_point_position); - CLTensor src3 = create_tensor(gemm_set.shape_c, dt, 1, fixed_point_position); - CLTensor dst = create_tensor(gemm_set.shape_d, dt, 1, fixed_point_position); - - BOOST_TEST(src1.info()->is_resizable()); - BOOST_TEST(src2.info()->is_resizable()); - BOOST_TEST(src3.info()->is_resizable()); - BOOST_TEST(dst.info()->is_resizable()); - - // Create and configure function - CLGEMM gemm; - gemm.configure(&src1, &src2, &src3, &dst, gemm_set.alpha, gemm_set.beta); - - // Validate valid region - const ValidRegion src1_valid_region = shape_to_valid_region(gemm_set.shape_a); - const ValidRegion src2_valid_region = shape_to_valid_region(gemm_set.shape_b); - const ValidRegion src3_valid_region = shape_to_valid_region(gemm_set.shape_c); - const ValidRegion dst_valid_region = shape_to_valid_region(gemm_set.shape_d); - - validate(src1.info()->valid_region(), src1_valid_region); - validate(src2.info()->valid_region(), src2_valid_region); - validate(src3.info()->valid_region(), src3_valid_region); - validate(dst.info()->valid_region(), dst_valid_region); -} - -BOOST_AUTO_TEST_SUITE(Float) -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) -BOOST_DATA_TEST_CASE(SmallGEMM, SmallGEMMDataset() * boost::unit_test::data::make(DataType::F32), - gemm_set, dt) -{ - // Compute reference - RawTensor ref_dst = Reference::compute_reference_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt); - - // Compute function - CLTensor dst = compute_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt); - - // Validate output - validate(CLAccessor(dst), ref_dst, tolerance_f32); -} - -BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly")) -BOOST_DATA_TEST_CASE(LargeGEMM, LargeGEMMDataset() * boost::unit_test::data::make(DataType::F32), - gemm_set, dt) -{ - // Compute reference - RawTensor ref_dst = Reference::compute_reference_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt); - - // Compute function - CLTensor dst = compute_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt); - - // Validate output - validate(CLAccessor(dst), ref_dst, tolerance_f32); -} -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE(Quantized) -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) -BOOST_DATA_TEST_CASE(SmallGEMM, SmallGEMMDataset() * boost::unit_test::data::make({ DataType::QS8, DataType::QS16 }) * boost::unit_test::data::xrange(4, 7), - gemm_set, dt, fixed_point_position) -{ - // Compute reference - RawTensor ref_dst = Reference::compute_reference_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt, fixed_point_position); - - // Compute function - CLTensor dst = compute_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt, fixed_point_position); - - // Validate output - validate(CLAccessor(dst), ref_dst, tolerance_q); -} - -BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly")) -BOOST_DATA_TEST_CASE(LargeGEMM, LargeGEMMDataset() * boost::unit_test::data::make({ DataType::QS8, DataType::QS16 }) * boost::unit_test::data::xrange(4, 7), - gemm_set, dt, fixed_point_position) -{ - // Compute reference - RawTensor ref_dst = Reference::compute_reference_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt, fixed_point_position); - - // Compute function - CLTensor dst = compute_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt, fixed_point_position); - - // Validate output - validate(CLAccessor(dst), ref_dst, tolerance_q); -} -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE_END() -BOOST_AUTO_TEST_SUITE_END() -#endif /* DOXYGEN_SKIP_THIS */ diff --git a/tests/validation/NEON/GEMM.cpp b/tests/validation/NEON/GEMM.cpp deleted file mode 100644 index 8747f0ab72..0000000000 --- a/tests/validation/NEON/GEMM.cpp +++ /dev/null @@ -1,219 +0,0 @@ -/* - * 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 "AssetsLibrary.h" -#include "Globals.h" -#include "NEON/Accessor.h" -#include "TypePrinter.h" -#include "Utils.h" -#include "dataset/GEMMDataset.h" -#include "validation/Datasets.h" -#include "validation/Reference.h" -#include "validation/Validation.h" - -#include "arm_compute/core/Helpers.h" -#include "arm_compute/core/Types.h" -#include "arm_compute/runtime/NEON/functions/NEGEMM.h" -#include "arm_compute/runtime/Tensor.h" -#include "arm_compute/runtime/TensorAllocator.h" - -#include "boost_wrapper.h" - -#include -#include - -using namespace arm_compute; -using namespace arm_compute::test; -using namespace arm_compute::test::validation; - -namespace -{ -const float tolerance_f32 = 1e-03f; /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */ -const float tolerance_q = 1.0f; /**< Tolerance value for comparing reference's output against implementation's output for fixed point data types */ - -Tensor compute_gemm(const TensorShape &src_shape1, const TensorShape &src_shape2, const TensorShape &src_shape3, - const TensorShape &out_shape, float alpha, float beta, DataType dt, int fixed_point_position = 0) -{ - // Create tensors - Tensor src1 = create_tensor(src_shape1, dt, 1, fixed_point_position); - Tensor src2 = create_tensor(src_shape2, dt, 1, fixed_point_position); - Tensor src3 = create_tensor(src_shape3, dt, 1, fixed_point_position); - Tensor dst = create_tensor(out_shape, dt, 1, fixed_point_position); - - // Create and configure function - NEGEMM gemm; - gemm.configure(&src1, &src2, &src3, &dst, alpha, beta); - - // Allocate tensors - src1.allocator()->allocate(); - src2.allocator()->allocate(); - src3.allocator()->allocate(); - dst.allocator()->allocate(); - - BOOST_TEST(!src1.info()->is_resizable()); - BOOST_TEST(!src2.info()->is_resizable()); - BOOST_TEST(!src3.info()->is_resizable()); - BOOST_TEST(!dst.info()->is_resizable()); - - // Fill tensors - if(dt == DataType::F16 || dt == DataType::F32) - { - std::uniform_real_distribution<> distribution(-1.0f, 1.0f); - library->fill(Accessor(src1), distribution, 0); - library->fill(Accessor(src2), distribution, 1); - library->fill(Accessor(src3), distribution, 2); - } - else - { - library->fill_tensor_uniform(Accessor(src1), 0); - library->fill_tensor_uniform(Accessor(src2), 1); - library->fill_tensor_uniform(Accessor(src3), 2); - } - - // Compute function - gemm.run(); - - return dst; -} -} // namespace - -#ifndef DOXYGEN_SKIP_THIS -BOOST_AUTO_TEST_SUITE(NEON) -BOOST_AUTO_TEST_SUITE(GEMM) - -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly")) -BOOST_DATA_TEST_CASE(Configuration, - SmallGEMMDataset() * boost::unit_test::data::make({ DataType::F32, DataType::QS8, DataType::QS16 }), - gemm_set, dt) -{ - // Set fixed point position data type allowed - int fixed_point_position = (dt == DataType::F32) ? 0 : 3; - - // Create tensors - Tensor src1 = create_tensor(gemm_set.shape_a, dt, 1, fixed_point_position); - Tensor src2 = create_tensor(gemm_set.shape_b, dt, 1, fixed_point_position); - Tensor src3 = create_tensor(gemm_set.shape_c, dt, 1, fixed_point_position); - Tensor dst = create_tensor(gemm_set.shape_d, dt, 1, fixed_point_position); - - BOOST_TEST(src1.info()->is_resizable()); - BOOST_TEST(src2.info()->is_resizable()); - BOOST_TEST(src3.info()->is_resizable()); - BOOST_TEST(dst.info()->is_resizable()); - - // Create and configure function - NEGEMM gemm; - gemm.configure(&src1, &src2, &src3, &dst, gemm_set.alpha, gemm_set.beta); - - // Validate valid region - const ValidRegion src1_valid_region = shape_to_valid_region(gemm_set.shape_a); - const ValidRegion src2_valid_region = shape_to_valid_region(gemm_set.shape_b); - const ValidRegion src3_valid_region = shape_to_valid_region(gemm_set.shape_c); - const ValidRegion dst_valid_region = shape_to_valid_region(gemm_set.shape_d); - - validate(src1.info()->valid_region(), src1_valid_region); - validate(src2.info()->valid_region(), src2_valid_region); - validate(src3.info()->valid_region(), src3_valid_region); - validate(dst.info()->valid_region(), dst_valid_region); -} - -#ifdef ARM_COMPUTE_ENABLE_FP16 -BOOST_AUTO_TEST_SUITE(Float16) -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) -BOOST_DATA_TEST_CASE(SmallGEMM, SmallGEMMDataset() * boost::unit_test::data::make(DataType::F16), - gemm_set, dt) -{ - // Compute reference - RawTensor ref_dst = Reference::compute_reference_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt); - - // Compute function - Tensor dst = compute_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt); - - // Validate output - validate(Accessor(dst), ref_dst, tolerance_f32); -} -BOOST_AUTO_TEST_SUITE_END() -#endif /* ARM_COMPUTE_ENABLE_FP16 */ - -BOOST_AUTO_TEST_SUITE(Float) -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) -BOOST_DATA_TEST_CASE(SmallGEMM, SmallGEMMDataset() * boost::unit_test::data::make(DataType::F32), - gemm_set, dt) -{ - // Compute reference - RawTensor ref_dst = Reference::compute_reference_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt); - - // Compute function - Tensor dst = compute_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt); - - // Validate output - validate(Accessor(dst), ref_dst, tolerance_f32); -} - -BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly")) -BOOST_DATA_TEST_CASE(LargeGEMM, LargeGEMMDataset() * boost::unit_test::data::make(DataType::F32), - gemm_set, dt) -{ - // Compute reference - RawTensor ref_dst = Reference::compute_reference_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt); - - // Compute function - Tensor dst = compute_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt); - - // Validate output - validate(Accessor(dst), ref_dst, tolerance_f32); -} -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE(Quantized) -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) -BOOST_DATA_TEST_CASE(SmallGEMM, SmallGEMMDataset() * boost::unit_test::data::make({ DataType::QS8, DataType::QS16 }) * boost::unit_test::data::xrange(1, 7), - gemm_set, dt, fixed_point_position) -{ - // Compute reference - RawTensor ref_dst = Reference::compute_reference_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt, fixed_point_position); - - // Compute function - Tensor dst = compute_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt, fixed_point_position); - - // Validate output - validate(Accessor(dst), ref_dst, tolerance_q); -} - -BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly")) -BOOST_DATA_TEST_CASE(LargeGEMM, LargeGEMMDataset() * boost::unit_test::data::make({ DataType::QS8, DataType::QS16 }) * boost::unit_test::data::xrange(1, 7), - gemm_set, dt, fixed_point_position) -{ - // Compute reference - RawTensor ref_dst = Reference::compute_reference_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt, fixed_point_position); - - // Compute function - Tensor dst = compute_gemm(gemm_set.shape_a, gemm_set.shape_b, gemm_set.shape_c, gemm_set.shape_d, gemm_set.alpha, gemm_set.beta, dt, fixed_point_position); - - // Validate output - validate(Accessor(dst), ref_dst, tolerance_q); -} -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE_END() -BOOST_AUTO_TEST_SUITE_END() -#endif /* DOXYGEN_SKIP_THIS */ diff --git a/tests/validation/Reference.cpp b/tests/validation/Reference.cpp index 99e3095007..e5bbdf2f2e 100644 --- a/tests/validation/Reference.cpp +++ b/tests/validation/Reference.cpp @@ -340,35 +340,6 @@ RawTensor Reference::compute_reference_gaussian5x5(const TensorShape &shape, Bor return ref_dst; } -RawTensor Reference::compute_reference_gemm(const TensorShape &src_shape1, const TensorShape &src_shape2, const TensorShape &src_shape3, - const TensorShape &dst_shape, float alpha, float beta, DataType dt, int fixed_point_position) -{ - RawTensor src1(src_shape1, dt, 1, fixed_point_position); - RawTensor src2(src_shape2, dt, 1, fixed_point_position); - RawTensor src3(src_shape3, dt, 1, fixed_point_position); - RawTensor dst(dst_shape, dt, 1, fixed_point_position); - - // Fill reference - if(dt == DataType::F16 || dt == DataType::F32) - { - std::uniform_real_distribution<> distribution(-1.0f, 1.0f); - library->fill(src1, distribution, 0); - library->fill(src2, distribution, 1); - library->fill(src3, distribution, 2); - } - else - { - library->fill_tensor_uniform(src1, 0); - library->fill_tensor_uniform(src2, 1); - library->fill_tensor_uniform(src3, 2); - } - - // Compute reference - ReferenceCPP::gemm(src1, src2, src3, dst, alpha, beta); - - return dst; -} - RawTensor Reference::compute_reference_non_linear_filter(const TensorShape &shape, NonLinearFilterFunction function, unsigned int mask_size, MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode, uint8_t constant_border_value) { diff --git a/tests/validation/Reference.h b/tests/validation/Reference.h index f3216fbaf9..9bcbdf7418 100644 --- a/tests/validation/Reference.h +++ b/tests/validation/Reference.h @@ -215,21 +215,6 @@ public: * @return Computed raw tensor. */ static RawTensor compute_reference_gaussian5x5(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value); - /** Compute matrix multiply function. - * - * @param[in] src_shape1 First input tensor shape - * @param[in] src_shape2 Second input tensor shape - * @param[in] src_shape3 Third input tensor shape - * @param[out] dst_shape Output tensor. - * @param[in] alpha Weight of the matrix product - * @param[in] beta Weight of the third matrix - * @param[in] dt Tensor's data type - * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers - * - * @return Computed output tensor. - */ - static RawTensor compute_reference_gemm(const TensorShape &src_shape1, const TensorShape &src_shape2, const TensorShape &src_shape3, - const TensorShape &dst_shape, float alpha, float beta, DataType dt, int fixed_point_position = 0); /** Compute reference non linear filter function * * @param[in] shape Shape of the input and output tensors.Data type supported: U8 diff --git a/tests/validation/ReferenceCPP.cpp b/tests/validation/ReferenceCPP.cpp index 6b902ae3f2..0364e75ce3 100644 --- a/tests/validation/ReferenceCPP.cpp +++ b/tests/validation/ReferenceCPP.cpp @@ -222,17 +222,6 @@ void ReferenceCPP::gaussian5x5(const RawTensor &src, RawTensor &dst, BorderMode tensor_operations::gaussian5x5(s, d, border_mode, constant_border_value); } -// GEMM -void ReferenceCPP::gemm(const RawTensor &src1, const RawTensor &src2, const RawTensor &src3, - RawTensor &dst, float alpha, float beta) -{ - const TensorVariant s1 = TensorFactory::get_tensor(src1); - const TensorVariant s2 = TensorFactory::get_tensor(src2); - const TensorVariant s3 = TensorFactory::get_tensor(src3); - TensorVariant d = TensorFactory::get_tensor(dst); - - boost::apply_visitor(tensor_visitors::gemm_visitor(s1, s2, s3, alpha, beta), d); -} // Non linear filter void ReferenceCPP::non_linear_filter(const RawTensor &src, RawTensor &dst, NonLinearFilterFunction function, unsigned int mask_size, MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode, uint8_t constant_border_value) diff --git a/tests/validation/ReferenceCPP.h b/tests/validation/ReferenceCPP.h index d289e8e57e..7ae45f0dac 100644 --- a/tests/validation/ReferenceCPP.h +++ b/tests/validation/ReferenceCPP.h @@ -190,17 +190,6 @@ public: * @param[in] constant_border_value Constant border value if @p border_mode is BorderMode::CONSTANT */ static void gaussian5x5(const RawTensor &src, RawTensor &dst, BorderMode border_mode, uint8_t constant_border_value); - /** Compute GEMM function. - * - * @param[in] src1 First input tensor - * @param[in] src2 Second input tensor - * @param[in] src3 Third input tensor - * @param[out] dst Output tensr - * @param[in] alpha Weight of the matrix product - * @param[in] beta Weight of the third matrix - */ - static void gemm(const RawTensor &src1, const RawTensor &src2, const RawTensor &src3, - RawTensor &dst, float alpha, float beta); /** Compute non linear filter function. * * @param[in] src First input tensor diff --git a/tests/validation/TensorOperations.h b/tests/validation/TensorOperations.h index f4d2110387..6e6d1f5115 100644 --- a/tests/validation/TensorOperations.h +++ b/tests/validation/TensorOperations.h @@ -592,79 +592,6 @@ void gaussian5x5(const Tensor &in, Tensor &out, BorderMode border_mode, T } } -// Matrix multiplication for floating point type -template ::value, int>::type * = nullptr> -void gemm(const Tensor &in1, const Tensor &in2, const Tensor &in3, Tensor &out, float alpha, float beta) -{ - const int M = out.shape().y(); - const int N = out.shape().x(); - const int K = in1.shape().x(); - - for(int r = 0; r < M; ++r) - { - for(int c = 0; c < N; ++c) - { - T acc(0); - - for(int k = 0; k < K; ++k) - { - const T a0 = in1[r * K + k]; - const T b0 = in2[k * N + c]; - - acc += a0 * b0; - } - - // Finalize the result: A * B * alpha + C * beta - const T c0 = in3[c + r * N]; - out[c + r * N] = alpha * acc + beta * c0; - } - } -} - -// Matrix multiplication for fixed point type -template ::value, int>::type * = nullptr> -void gemm(const Tensor &in1, const Tensor &in2, const Tensor &in3, Tensor &out, float alpha, float beta) -{ - using namespace fixed_point_arithmetic; - - using promoted_type = typename fixed_point_arithmetic::traits::promote::type; - - const int M = out.shape().y(); - const int N = out.shape().x(); - const int K = in1.shape().x(); - const int8_t fixed_point_position = static_cast(in1.fixed_point_position()); - - const fixed_point alpha_q(alpha, fixed_point_position); - const fixed_point beta_q(beta, fixed_point_position); - - for(int r = 0; r < M; ++r) - { - for(int c = 0; c < N; ++c) - { - fixed_point acc_q(0, fixed_point_position); - - for(int k = 0; k < K; ++k) - { - const fixed_point a0_q(in1[r * K + k], fixed_point_position, true); - const fixed_point b0_q(in2[k * N + c], fixed_point_position, true); - const fixed_point axb_q = a0_q * b0_q; - - acc_q = axb_q + acc_q; - } - - // Finalize the result: A * B * alpha + C * beta - const fixed_point c0_q(in3[c + r * N], fixed_point_position, true); - - fixed_point res_q(acc_q); - res_q = alpha_q * res_q; - res_q = (c0_q * beta_q) + res_q; - - // Store the result - out[c + r * N] = res_q.raw(); - } - } -} - // Non linear filter template void non_linear_filter(const Tensor &in, Tensor &out, NonLinearFilterFunction function, unsigned int mask_size, diff --git a/tests/validation/TensorVisitors.h b/tests/validation/TensorVisitors.h index 67f1d8a001..5ee7ae3a9f 100644 --- a/tests/validation/TensorVisitors.h +++ b/tests/validation/TensorVisitors.h @@ -132,29 +132,6 @@ private: ConvertPolicy _policy; uint32_t _shift; }; -// GEMM visitor -struct gemm_visitor : public boost::static_visitor<> -{ -public: - explicit gemm_visitor(const TensorVariant &in1, const TensorVariant &in2, const TensorVariant &in3, float alpha, float beta) - : _in1(in1), _in2(in2), _in3(in3), _alpha(alpha), _beta(beta) - { - } - - template - void operator()(Tensor &out) const - { - const Tensor &in1 = boost::get>(_in1); - const Tensor &in2 = boost::get>(_in2); - const Tensor &in3 = boost::get>(_in3); - tensor_operations::gemm(in1, in2, in3, out, _alpha, _beta); - } - -private: - const TensorVariant &_in1, &_in2, &_in3; - float _alpha; - float _beta; -}; // Pixel-wise Multiplication visitor struct pixel_wise_multiplication_visitor : public boost::static_visitor<> { diff --git a/tests/validation_new/CL/GEMM.cpp b/tests/validation_new/CL/GEMM.cpp new file mode 100644 index 0000000000..7da5da3ed9 --- /dev/null +++ b/tests/validation_new/CL/GEMM.cpp @@ -0,0 +1,167 @@ +/* + * 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/core/Types.h" +#include "arm_compute/runtime/CL/CLTensor.h" +#include "arm_compute/runtime/CL/CLTensorAllocator.h" +#include "arm_compute/runtime/CL/functions/CLGEMM.h" +#include "framework/Asserts.h" +#include "framework/Macros.h" +#include "framework/datasets/Datasets.h" +#include "tests/CL/CLAccessor.h" +#include "tests/PaddingCalculator.h" +#include "tests/datasets_new/LargeGEMMDataset.h" +#include "tests/datasets_new/SmallGEMMDataset.h" +#include "tests/validation_new/Validation.h" +#include "tests/validation_new/fixtures/GEMMFixture.h" +#include "tests/validation_new/half.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +constexpr AbsoluteTolerance tolerance_f32(0.001f); /**< Tolerance value for comparing reference's output against implementation's output for floating point data types */ +constexpr AbsoluteTolerance tolerance_f16(0.1f); /**< Tolerance value for comparing reference's output against implementation's output for floating point data types */ +constexpr AbsoluteTolerance tolerance_q(1.0f); /**< Tolerance value for comparing reference's output against implementation's output for fixed point data types */ + +/** CNN data types */ +const auto CNNDataTypes = framework::dataset::make("DataType", +{ + DataType::F16, + DataType::F32, + DataType::QS8, + DataType::QS16, +}); +} // namespace + +TEST_SUITE(CL) +TEST_SUITE(GEMM) + +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(framework::dataset::concat(datasets::SmallGEMMDataset(), datasets::LargeGEMMDataset()), CNNDataTypes), + shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type) +{ + // Set fixed point position data type allowed + const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0; + + // Create tensors + CLTensor a = create_tensor(shape_a, data_type, 1, fixed_point_position); + CLTensor b = create_tensor(shape_b, data_type, 1, fixed_point_position); + CLTensor c = create_tensor(shape_c, data_type, 1, fixed_point_position); + CLTensor dst = create_tensor(output_shape, data_type, 1, fixed_point_position); + + ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Create and configure function + CLGEMM gemm; + gemm.configure(&a, &b, &c, &dst, alpha, beta); + + //TODO(COMPMID-415): Validate valid region +} + +template +using CLGEMMFixture = GEMMValidationFixture; + +TEST_SUITE(Float) +TEST_SUITE(FP16) +FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMFixture, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallGEMMDataset(), framework::dataset::make("DataType", DataType::F16))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f16); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLGEMMFixture, framework::DatasetMode::NIGHTLY, combine(datasets::LargeGEMMDataset(), framework::dataset::make("DataType", + DataType::F16))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f16); +} +TEST_SUITE_END() + +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMFixture, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallGEMMDataset(), framework::dataset::make("DataType", DataType::F32))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f32); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLGEMMFixture, framework::DatasetMode::NIGHTLY, combine(datasets::LargeGEMMDataset(), framework::dataset::make("DataType", DataType::F32))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f32); +} +TEST_SUITE_END() +TEST_SUITE_END() + +template +using CLGEMMFixedPointFixture = GEMMValidationFixedPointFixture; + +TEST_SUITE(Quantized) +TEST_SUITE(QS8) +FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMFixedPointFixture, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallGEMMDataset(), + framework::dataset::make("DataType", + DataType::QS8)), + framework::dataset::make("FractionalBits", 1, 7))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_q); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLGEMMFixedPointFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeGEMMDataset(), + framework::dataset::make("DataType", + DataType::QS8)), + framework::dataset::make("FractionalBits", 1, 7))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_q); +} +TEST_SUITE_END() + +TEST_SUITE(QS16) +FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMFixedPointFixture, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallGEMMDataset(), + framework::dataset::make("DataType", + DataType::QS16)), + framework::dataset::make("FractionalBits", 1, 14))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_q); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLGEMMFixedPointFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeGEMMDataset(), + framework::dataset::make("DataType", + DataType::QS16)), + framework::dataset::make("FractionalBits", 1, 14))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_q); +} +TEST_SUITE_END() +TEST_SUITE_END() + +TEST_SUITE_END() +TEST_SUITE_END() +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation_new/CL/NormalizationLayer.cpp b/tests/validation_new/CL/NormalizationLayer.cpp index ebef18a8bd..37e77f3632 100644 --- a/tests/validation_new/CL/NormalizationLayer.cpp +++ b/tests/validation_new/CL/NormalizationLayer.cpp @@ -45,9 +45,7 @@ namespace validation namespace { /** Tolerance for float operations */ -#ifdef ARM_COMPUTE_ENABLE_FP16 -constexpr AbsoluteTolerance tolerance_f16(0.001f); -#endif /* ARM_COMPUTE_ENABLE_FP16 */ +constexpr AbsoluteTolerance tolerance_f16(0.01f); constexpr AbsoluteTolerance tolerance_f32(0.00001f); /** Tolerance for fixed point operations */ constexpr AbsoluteTolerance tolerance_qs8(2); @@ -68,7 +66,6 @@ template using CLNormalizationLayerFixture = NormalizationValidationFixture; TEST_SUITE(Float) -#ifdef ARM_COMPUTE_ENABLE_FP16 TEST_SUITE(FP16) FIXTURE_DATA_TEST_CASE(RunSmall, CLNormalizationLayerFixture, framework::DatasetMode::PRECOMMIT, combine(NormalizationDataset, framework::dataset::make("DataType", DataType::F16))) { @@ -81,7 +78,6 @@ FIXTURE_DATA_TEST_CASE(RunLarge, CLNormalizationLayerFixture, validate(CLAccessor(_target), _reference, tolerance_f16); } TEST_SUITE_END() -#endif /* ARM_COMPUTE_ENABLE_FP16 */ TEST_SUITE(FP32) FIXTURE_DATA_TEST_CASE(RunSmall, CLNormalizationLayerFixture, framework::DatasetMode::PRECOMMIT, combine(NormalizationDataset, framework::dataset::make("DataType", DataType::F32))) diff --git a/tests/validation_new/CPP/GEMM.cpp b/tests/validation_new/CPP/GEMM.cpp new file mode 100644 index 0000000000..424633f8e7 --- /dev/null +++ b/tests/validation_new/CPP/GEMM.cpp @@ -0,0 +1,122 @@ +/* + * 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 "GEMM.h" + +#include "tests/validation_new/FixedPoint.h" +#include "tests/validation_new/half.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template ::value, int>::type> +SimpleTensor gemm(const SimpleTensor &a, const SimpleTensor &b, const SimpleTensor &c, float alpha, float beta) +{ + // Create reference + SimpleTensor dst{ c.shape(), c.data_type(), 1, c.fixed_point_position() }; + + // Compute reference + const int M = dst.shape().y(); + const int N = dst.shape().x(); + const int K = a.shape().x(); + + for(int row = 0; row < M; ++row) + { + for(int col = 0; col < N; ++col) + { + T acc(0); + + for(int k = 0; k < K; ++k) + { + acc += a[row * K + k] * b[k * N + col]; + } + + // Finalize the result: alpha * A * B + beta * C + dst[col + row * N] = alpha * acc + beta * c[col + row * N]; + } + } + + return dst; +} + +template ::value, int>::type> +SimpleTensor gemm(const SimpleTensor &a, const SimpleTensor &b, const SimpleTensor &c, float alpha, float beta) +{ + using namespace fixed_point_arithmetic; + + // Create reference + SimpleTensor dst{ c.shape(), c.data_type(), 1, c.fixed_point_position() }; + + // Compute reference + using promoted_type = fixed_point_arithmetic::traits::promote_t; + + const int M = dst.shape().y(); + const int N = dst.shape().x(); + const int K = a.shape().x(); + const int fixed_point_position = a.fixed_point_position(); + + const fixed_point alpha_q(alpha, fixed_point_position); + const fixed_point beta_q(beta, fixed_point_position); + + for(int row = 0; row < M; ++row) + { + for(int col = 0; col < N; ++col) + { + fixed_point acc_q(0, fixed_point_position); + + for(int k = 0; k < K; ++k) + { + const fixed_point a0_q(a[row * K + k], fixed_point_position, true); + const fixed_point b0_q(b[k * N + col], fixed_point_position, true); + + acc_q = acc_q + (a0_q * b0_q); + } + + // Finalize the result: alpha * A * B + beta * C + const fixed_point c0_q(c[col + row * N], fixed_point_position, true); + + fixed_point res_q(acc_q); + res_q = alpha_q * res_q; + res_q = res_q + (beta_q * c0_q); + + // Store the result + dst[col + row * N] = res_q.raw(); + } + } + + return dst; +} + +template SimpleTensor gemm(const SimpleTensor &a, const SimpleTensor &b, const SimpleTensor &c, float alpha, float beta); +template SimpleTensor gemm(const SimpleTensor &a, const SimpleTensor &b, const SimpleTensor &c, float alpha, float beta); +template SimpleTensor gemm(const SimpleTensor &a, const SimpleTensor &b, const SimpleTensor &c, float alpha, float beta); +template SimpleTensor gemm(const SimpleTensor &a, const SimpleTensor &b, const SimpleTensor &c, float alpha, float beta); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation_new/CPP/GEMM.h b/tests/validation_new/CPP/GEMM.h new file mode 100644 index 0000000000..ff79c7b546 --- /dev/null +++ b/tests/validation_new/CPP/GEMM.h @@ -0,0 +1,47 @@ +/* + * 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_GEMM_H__ +#define __ARM_COMPUTE_TEST_GEMM_H__ + +#include "tests/validation_new/Helpers.h" +#include "tests/validation_new/SimpleTensor.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template ::value, int>::type = 0> +SimpleTensor gemm(const SimpleTensor &a, const SimpleTensor &b, const SimpleTensor &c, float alpha, float beta); + +template ::value, int>::type = 0> +SimpleTensor gemm(const SimpleTensor &a, const SimpleTensor &b, const SimpleTensor &c, float alpha, float beta); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_GEMM_H__ */ diff --git a/tests/validation_new/NEON/GEMM.cpp b/tests/validation_new/NEON/GEMM.cpp new file mode 100644 index 0000000000..453b183d1e --- /dev/null +++ b/tests/validation_new/NEON/GEMM.cpp @@ -0,0 +1,170 @@ +/* + * 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/core/Types.h" +#include "arm_compute/runtime/NEON/functions/NEGEMM.h" +#include "arm_compute/runtime/Tensor.h" +#include "arm_compute/runtime/TensorAllocator.h" +#include "framework/Asserts.h" +#include "framework/Macros.h" +#include "framework/datasets/Datasets.h" +#include "tests/NEON/Accessor.h" +#include "tests/PaddingCalculator.h" +#include "tests/datasets_new/LargeGEMMDataset.h" +#include "tests/datasets_new/SmallGEMMDataset.h" +#include "tests/validation_new/Validation.h" +#include "tests/validation_new/fixtures/GEMMFixture.h" +#include "tests/validation_new/half.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +constexpr AbsoluteTolerance tolerance_f(0.001f); /**< Tolerance value for comparing reference's output against implementation's output for floating point data types */ +constexpr AbsoluteTolerance tolerance_q(1.0f); /**< Tolerance value for comparing reference's output against implementation's output for fixed point data types */ + +/** CNN data types */ +const auto CNNDataTypes = framework::dataset::make("DataType", +{ +#ifdef ARM_COMPUTE_ENABLE_FP16 + DataType::F16, +#endif /* ARM_COMPUTE_ENABLE_FP16 */ + DataType::F32, + DataType::QS8, + DataType::QS16, +}); +} // namespace + +TEST_SUITE(NEON) +TEST_SUITE(GEMM) + +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(framework::dataset::concat(datasets::SmallGEMMDataset(), datasets::LargeGEMMDataset()), CNNDataTypes), + shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type) +{ + // Set fixed point position data type allowed + const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0; + + // Create tensors + Tensor a = create_tensor(shape_a, data_type, 1, fixed_point_position); + Tensor b = create_tensor(shape_b, data_type, 1, fixed_point_position); + Tensor c = create_tensor(shape_c, data_type, 1, fixed_point_position); + Tensor dst = create_tensor(output_shape, data_type, 1, fixed_point_position); + + ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Create and configure function + NEGEMM gemm; + gemm.configure(&a, &b, &c, &dst, alpha, beta); + + //TODO(COMPMID-415): Validate valid region +} + +template +using NEGEMMFixture = GEMMValidationFixture; + +TEST_SUITE(Float) +#ifdef ARM_COMPUTE_ENABLE_FP16 +TEST_SUITE(FP16) +FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMFixture, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallGEMMDataset(), framework::dataset::make("DataType", DataType::F16))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_f); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEGEMMFixture, framework::DatasetMode::NIGHTLY, combine(datasets::LargeGEMMDataset(), framework::dataset::make("DataType", + DataType::F16))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_f); +} +TEST_SUITE_END() +#endif /* ARM_COMPUTE_ENABLE_FP16 */ + +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMFixture, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallGEMMDataset(), framework::dataset::make("DataType", DataType::F32))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_f); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEGEMMFixture, framework::DatasetMode::NIGHTLY, combine(datasets::LargeGEMMDataset(), framework::dataset::make("DataType", DataType::F32))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_f); +} +TEST_SUITE_END() +TEST_SUITE_END() + +template +using NEGEMMFixedPointFixture = GEMMValidationFixedPointFixture; + +TEST_SUITE(Quantized) +TEST_SUITE(QS8) +FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMFixedPointFixture, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallGEMMDataset(), + framework::dataset::make("DataType", + DataType::QS8)), + framework::dataset::make("FractionalBits", 1, 7))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_q); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEGEMMFixedPointFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeGEMMDataset(), + framework::dataset::make("DataType", + DataType::QS8)), + framework::dataset::make("FractionalBits", 1, 7))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_q); +} +TEST_SUITE_END() + +TEST_SUITE(QS16) +FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMFixedPointFixture, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallGEMMDataset(), + framework::dataset::make("DataType", + DataType::QS16)), + framework::dataset::make("FractionalBits", 1, 14))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_q); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEGEMMFixedPointFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeGEMMDataset(), + framework::dataset::make("DataType", + DataType::QS16)), + framework::dataset::make("FractionalBits", 1, 14))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_q); +} +TEST_SUITE_END() +TEST_SUITE_END() + +TEST_SUITE_END() +TEST_SUITE_END() +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation_new/fixtures/GEMMFixture.h b/tests/validation_new/fixtures/GEMMFixture.h new file mode 100644 index 0000000000..a6ab518d74 --- /dev/null +++ b/tests/validation_new/fixtures/GEMMFixture.h @@ -0,0 +1,152 @@ +/* + * 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_GEMM_FIXTURE +#define ARM_COMPUTE_TEST_GEMM_FIXTURE + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" +#include "framework/Asserts.h" +#include "framework/Fixture.h" +#include "tests/AssetsLibrary.h" +#include "tests/Globals.h" +#include "tests/IAccessor.h" +#include "tests/validation_new/CPP/GEMM.h" +#include "tests/validation_new/Helpers.h" + +#include + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template +class GEMMValidationFixedPointFixture : public framework::Fixture +{ +public: + template + void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_c, TensorShape output_shape, float alpha, float beta, DataType data_type, int fractional_bits) + { + _fractional_bits = fractional_bits; + _data_type = data_type; + + _target = compute_target(shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type, fractional_bits); + _reference = compute_reference(shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type, fractional_bits); + } + +protected: + template + void fill(U &&tensor, int i) + { + switch(tensor.data_type()) + { + case DataType::F16: + case DataType::F32: + { + std::uniform_real_distribution<> distribution(-1.0f, 1.0f); + library->fill(tensor, distribution, i); + break; + } + default: + library->fill_tensor_uniform(tensor, i); + } + } + + TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, const TensorShape &output_shape, float alpha, float beta, + DataType data_type, int fixed_point_position) + { + // Create tensors + TensorType a = create_tensor(shape_a, data_type, 1, fixed_point_position); + TensorType b = create_tensor(shape_b, data_type, 1, fixed_point_position); + TensorType c = create_tensor(shape_c, data_type, 1, fixed_point_position); + TensorType dst = create_tensor(output_shape, data_type, 1, fixed_point_position); + + // Create and configure function + FunctionType gemm; + gemm.configure(&a, &b, &c, &dst, alpha, beta); + + ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + a.allocator()->allocate(); + b.allocator()->allocate(); + c.allocator()->allocate(); + dst.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Fill tensors + fill(AccessorType(a), 0); + fill(AccessorType(b), 1); + fill(AccessorType(c), 2); + + // Compute GEMM function + gemm.run(); + + return dst; + } + + SimpleTensor compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, const TensorShape &output_shape, float alpha, float beta, + DataType data_type, int fixed_point_position) + { + // Create reference + SimpleTensor a{ shape_a, data_type, 1, fixed_point_position }; + SimpleTensor b{ shape_b, data_type, 1, fixed_point_position }; + SimpleTensor c{ shape_c, data_type, 1, fixed_point_position }; + + // Fill reference + fill(a, 0); + fill(b, 1); + fill(c, 2); + + return reference::gemm(a, b, c, alpha, beta); + } + + TensorType _target{}; + SimpleTensor _reference{}; + int _fractional_bits{}; + DataType _data_type{}; +}; + +template +class GEMMValidationFixture : public GEMMValidationFixedPointFixture +{ +public: + template + void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_c, TensorShape output_shape, float alpha, float beta, DataType data_type) + { + GEMMValidationFixedPointFixture::setup(shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type, 0); + } +}; +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_GEMM_FIXTURE */ -- cgit v1.2.1