diff options
author | Giorgio Arena <gioare01@e108627-lin.cambridge.arm.com> | 2018-03-01 11:13:45 +0000 |
---|---|---|
committer | Anthony Barbier <anthony.barbier@arm.com> | 2018-11-02 16:49:16 +0000 |
commit | 1f9ca1d7737846c74053d68ee0844b448bae298b (patch) | |
tree | c8f8c6850b59899a01efcde3b0a2e294af40c5b5 /tests | |
parent | a9676118fd2a0e5bc916969af83ecee049bae76b (diff) | |
download | ComputeLibrary-1f9ca1d7737846c74053d68ee0844b448bae298b.tar.gz |
COMPMID-935 Implementing Convolution with Winograd on OpenCL (part 3)
Change-Id: I51f92f30602fb0a02314f344fa67061f448694bf
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/122793
Tested-by: Jenkins <bsgcomp@arm.com>
Reviewed-by: Giorgio Arena <giorgio.arena@arm.com>
Reviewed-by: Gian Marco Iodice <gianmarco.iodice@arm.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/datasets/WinogradInputTransformDataset.h | 140 | ||||
-rw-r--r-- | tests/validation/CL/Winograd.cpp | 135 | ||||
-rw-r--r-- | tests/validation/Helpers.cpp | 110 | ||||
-rwxr-xr-x | tests/validation/Helpers.h | 30 | ||||
-rw-r--r-- | tests/validation/fixtures/WinogradLayerFixture.h | 85 | ||||
-rw-r--r-- | tests/validation/reference/Winograd.cpp | 137 | ||||
-rw-r--r-- | tests/validation/reference/Winograd.h | 43 |
7 files changed, 678 insertions, 2 deletions
diff --git a/tests/datasets/WinogradInputTransformDataset.h b/tests/datasets/WinogradInputTransformDataset.h new file mode 100644 index 0000000000..07e41ebf7a --- /dev/null +++ b/tests/datasets/WinogradInputTransformDataset.h @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2018 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_WINOGRAD_INPUT_TRANSFORM_DATASET +#define ARM_COMPUTE_TEST_WINOGRAD_INPUT_TRANSFORM_DATASET + +#include "utils/TypePrinter.h" + +#include "arm_compute/core/Types.h" + +namespace arm_compute +{ +namespace test +{ +namespace datasets +{ +class WinogradInputTransformDataset +{ +public: + using type = std::tuple<TensorShape, PadStrideInfo, Size2D, bool>; + + struct iterator + { + iterator(std::vector<TensorShape>::const_iterator in_it, std::vector<PadStrideInfo>::const_iterator info_it, std::vector<Size2D>::const_iterator kernel_dims_it, + std::vector<bool>::const_iterator format_it) + : _in_it{ std::move(in_it) }, _info_it{ std::move(info_it) }, _kernel_dims_it{ std::move(kernel_dims_it) }, _format_it{ std::move(format_it) } + { + } + + std::string description() const + { + std::stringstream description; + description << "In=" << *_in_it << ":"; + description << "Info=" << *_info_it; + description << "KernelDims=" << *_kernel_dims_it; + description << "IsNCHW=" << *_format_it; + return description.str(); + } + + WinogradInputTransformDataset::type operator*() const + { + return std::make_tuple(*_in_it, *_info_it, *_kernel_dims_it, *_format_it); + } + + iterator &operator++() + { + ++_in_it; + ++_info_it; + ++_kernel_dims_it; + ++_format_it; + + return *this; + } + + private: + std::vector<TensorShape>::const_iterator _in_it; + std::vector<PadStrideInfo>::const_iterator _info_it; + std::vector<Size2D>::const_iterator _kernel_dims_it; + std::vector<bool>::const_iterator _format_it; + }; + + iterator begin() const + { + return iterator(_in_shapes.begin(), _infos.begin(), _kernel_dims.begin(), _format.begin()); + } + + int size() const + { + return std::min(_in_shapes.size(), std::min(_infos.size(), std::min(_kernel_dims.size(), _format.size()))); + } + + void add_config(TensorShape in, PadStrideInfo info, Size2D kernel_dims, bool format) + { + _in_shapes.emplace_back(std::move(in)); + _infos.emplace_back(std::move(info)); + _kernel_dims.emplace_back(std::move(kernel_dims)); + _format.emplace_back(std::move(format)); + } + +protected: + WinogradInputTransformDataset() = default; + WinogradInputTransformDataset(WinogradInputTransformDataset &&) = default; + +private: + std::vector<TensorShape> _in_shapes{}; + std::vector<PadStrideInfo> _infos{}; + std::vector<Size2D> _kernel_dims{}; + std::vector<bool> _format{}; +}; + +class SmallWinogradInputTransformDataset final : public WinogradInputTransformDataset +{ +public: + SmallWinogradInputTransformDataset() + { + add_config(TensorShape(9U, 9U), PadStrideInfo(1, 1, 1, 1), Size2D(3U, 3U), true); + add_config(TensorShape(27U, 13U, 2U), PadStrideInfo(1, 1, 0, 0), Size2D(3U, 3U), true); + add_config(TensorShape(128U, 64U, 1U, 3U), PadStrideInfo(1, 1, 1, 1), Size2D(3U, 3U), true); + add_config(TensorShape(9U, 9U, 3U, 4U), PadStrideInfo(1, 1, 0, 0), Size2D(3U, 3U), true); + add_config(TensorShape(27U, 13U, 2U, 4U), PadStrideInfo(1, 1, 1, 1), Size2D(3U, 3U), true); + add_config(TensorShape(9U, 9U, 3U, 5U), PadStrideInfo(1, 1, 0, 0), Size2D(3U, 3U), true); + add_config(TensorShape(14U, 14U, 512U, 2U), PadStrideInfo(1, 1, 1, 1), Size2D(3U, 3U), true); + } +}; + +class LargeWinogradInputTransformDataset final : public WinogradInputTransformDataset +{ +public: + LargeWinogradInputTransformDataset() + { + add_config(TensorShape(42U, 37U, 8U, 15U), PadStrideInfo(1, 1, 1, 1), Size2D(3U, 3U), true); + add_config(TensorShape(57U, 60U, 13U, 8U), PadStrideInfo(1, 1, 1, 1), Size2D(3U, 3U), true); + add_config(TensorShape(128U, 64U, 21U, 13U), PadStrideInfo(1, 1, 0, 0), Size2D(3U, 3U), true); + add_config(TensorShape(83U, 72U, 14U, 5U), PadStrideInfo(1, 1, 0, 0), Size2D(3U, 3U), true); + } +}; +} // namespace datasets +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_WINOGRAD_INPUT_TRANSFORM_DATASET */ diff --git a/tests/validation/CL/Winograd.cpp b/tests/validation/CL/Winograd.cpp new file mode 100644 index 0000000000..664b3f4ef8 --- /dev/null +++ b/tests/validation/CL/Winograd.cpp @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2018 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 CONCLCTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/core/Types.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include "arm_compute/runtime/CL/CLTensor.h" +#include "arm_compute/runtime/CL/CLTensorAllocator.h" +#include "arm_compute/runtime/CL/functions/CLWinogradInputTransform.h" +#include "tests/CL/CLAccessor.h" +#include "tests/datasets/WinogradInputTransformDataset.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/WinogradLayerFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +TEST_SUITE(CL) +TEST_SUITE(Winograd) + +TEST_SUITE(InputTransform) + +// *INDENT-OFF* +// clang-format off +DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip( + framework::dataset::make("InputInfo",{ + TensorInfo(TensorShape(53U, 21U, 5U, 3U), 1, DataType::F16), // F16 not supported + TensorInfo(TensorShape(53U, 21U, 5U, 3U), 1, DataType::QASYMM8), // QASYMM8 not supported + TensorInfo(TensorShape(53U, 21U, 5U, 3U), 1, DataType::F32), // Kernel size not supported + TensorInfo(TensorShape(53U, 21U, 5U, 3U), 1, DataType::F32), // Strides not supported + TensorInfo(TensorShape(53U, 33U, 4U), 1, DataType::F32), // valid + TensorInfo(TensorShape(34U, 42U, 7U, 3U), 1, DataType::F32), // valid + TensorInfo(TensorShape(31U, 37U, 37U), 1, DataType::F32) // valid + }), + framework::dataset::make("OutputInfo", { + TensorInfo(TensorShape(5U, 5U, 16U, 3U), 1, DataType::F16), + TensorInfo(TensorShape(5U, 5U, 16U, 3U), 1, DataType::QASYMM8), + TensorInfo(TensorShape(5U, 5U, 16U, 3U), 1, DataType::F32), + TensorInfo(TensorShape(5U, 1U, 16U, 3U), 1, DataType::F32), + TensorInfo(TensorShape(4U, 442U, 16U), 1, DataType::F32), + TensorInfo(TensorShape(7U, 320U, 16U, 3U), 1, DataType::F32), + TensorInfo(TensorShape(37U, 304U, 16U), 1, DataType::F32) + })), + framework::dataset::make("PadStrideInfo", { + PadStrideInfo(1, 1, 1, 0), + PadStrideInfo(1, 1, 0, 0), + PadStrideInfo(1, 1, 1, 1), + PadStrideInfo(2, 1, 1, 1), + PadStrideInfo(1, 1, 0, 1), + PadStrideInfo(1, 1, 0, 0), + PadStrideInfo(1, 1, 1, 1) + })), + framework::dataset::make("KernelDims", { + Size2D(3U, 3U), + Size2D(3U, 3U), + Size2D(5U, 5U), + Size2D(3U, 3U), + Size2D(3U, 3U), + Size2D(3U, 3U), + Size2D(3U, 3U) + })), + framework::dataset::make("Expected", { false, false, false, false, true, true, true })), + input_info, output_info, conv_info, kernel_dims, expected) +{ + ARM_COMPUTE_EXPECT(bool(CLWinogradInputTransform::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), conv_info, kernel_dims)) == expected, framework::LogLevel::ERRORS); +} +// clang-format on +// *INDENT-ON* + +using CLWinogradInputTransformFixture = WinogradInputTransformValidationFixture<CLTensor, CLAccessor, CLWinogradInputTransform, float>; + +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(framework::dataset::concat(datasets::SmallWinogradInputTransformDataset(), datasets::LargeWinogradInputTransformDataset()), + framework::dataset::make("DataType", { DataType::F32 })), + shape_in, conv_info, kernel_dims, is_nchw_format, data_type) +{ + ARM_COMPUTE_UNUSED(is_nchw_format); + + TensorShape shape_out = compute_winograd_input_transform_shape(TensorInfo(shape_in, 1, data_type), conv_info, kernel_dims); + + // Create tensors + CLTensor in = create_tensor<CLTensor>(shape_in, data_type); + CLTensor out = create_tensor<CLTensor>(shape_out, data_type); + + ARM_COMPUTE_EXPECT(in.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(out.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Create and configure function + CLWinogradInputTransform winograd_input_transform; + + // Configure the function + winograd_input_transform.configure(&in, &out, conv_info, kernel_dims); +} + +FIXTURE_DATA_TEST_CASE(RunSmall, CLWinogradInputTransformFixture, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallWinogradInputTransformDataset(), framework::dataset::make("DataType", { DataType::F32 }))) +{ + validate(CLAccessor(_target), _reference); +} + +FIXTURE_DATA_TEST_CASE(RunLarge, CLWinogradInputTransformFixture, framework::DatasetMode::NIGHTLY, combine(datasets::LargeWinogradInputTransformDataset(), framework::dataset::make("DataType", { DataType::F32 }))) +{ + validate(CLAccessor(_target), _reference); +} + +TEST_SUITE_END() + +TEST_SUITE_END() +TEST_SUITE_END() +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/Helpers.cpp b/tests/validation/Helpers.cpp index 313b059a8c..3d554f0d25 100644 --- a/tests/validation/Helpers.cpp +++ b/tests/validation/Helpers.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 ARM Limited. + * Copyright (c) 2017-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -23,6 +23,9 @@ */ #include "tests/validation/Helpers.h" +#include <algorithm> +#include <cmath> + namespace arm_compute { namespace test @@ -133,6 +136,111 @@ SimpleTensor<uint8_t> convert_to_asymmetric(const SimpleTensor<float> &src, cons } return dst; } + +void matrix_multiply(const SimpleTensor<float> &a, const SimpleTensor<float> &b, SimpleTensor<float> &out) +{ + ARM_COMPUTE_ERROR_ON(a.shape()[0] != b.shape()[1]); + ARM_COMPUTE_ERROR_ON(a.shape()[1] != out.shape()[1]); + ARM_COMPUTE_ERROR_ON(b.shape()[0] != out.shape()[0]); + + const int M = a.shape()[1]; // Rows + const int N = b.shape()[0]; // Cols + const int K = b.shape()[1]; + + for(int y = 0; y < M; ++y) + { + for(int x = 0; x < N; ++x) + { + float acc = 0.0f; + for(int k = 0; k < K; ++k) + { + acc += a[y * K + k] * b[x + k * N]; + } + + out[x + y * N] = acc; + } + } +} + +void transpose_matrix(const SimpleTensor<float> &in, SimpleTensor<float> &out) +{ + ARM_COMPUTE_ERROR_ON((in.shape()[0] != out.shape()[1]) || (in.shape()[1] != out.shape()[0])); + + const int width = in.shape()[0]; + const int height = in.shape()[1]; + + for(int y = 0; y < height; ++y) + { + for(int x = 0; x < width; ++x) + { + const float val = in[x + y * width]; + + out[x * height + y] = val; + } + } +} + +template <typename T> +void get_tile(const SimpleTensor<T> &in, SimpleTensor<T> &tile, const Coordinates &coord) +{ + ARM_COMPUTE_ERROR_ON(tile.shape().num_dimensions() != 2); + + const int w_tile = tile.shape()[0]; + const int h_tile = tile.shape()[1]; + + // Fill the tile with zeros + std::fill(tile.data() + 0, (tile.data() + (w_tile * h_tile)), static_cast<T>(0)); + + // Check if with the dimensions greater than 2 we could have out-of-bound reads + for(size_t d = 2; d < Coordinates::num_max_dimensions; ++d) + { + if(coord[d] < 0 || coord[d] >= static_cast<int>(in.shape()[d])) + { + ARM_COMPUTE_ERROR("coord[d] < 0 || coord[d] >= in.shape()[d] with d >= 2"); + } + } + + // Since we could have out-of-bound reads along the X and Y dimensions, + // we start calculating the input address with x = 0 and y = 0 + Coordinates start_coord = coord; + start_coord[0] = 0; + start_coord[1] = 0; + + // Get input and roi pointers + auto in_ptr = static_cast<const T *>(in(start_coord)); + auto roi_ptr = static_cast<T *>(tile.data()); + + const int x_in_start = std::max(0, coord[0]); + const int y_in_start = std::max(0, coord[1]); + const int x_in_end = std::min(static_cast<int>(in.shape()[0]), coord[0] + w_tile); + const int y_in_end = std::min(static_cast<int>(in.shape()[1]), coord[1] + h_tile); + + // Number of elements to copy per row + const int n = x_in_end - x_in_start; + + // Starting coordinates for the ROI + const int x_tile_start = coord[0] > 0 ? 0 : std::abs(coord[0]); + const int y_tile_start = coord[1] > 0 ? 0 : std::abs(coord[1]); + + // Update input pointer + in_ptr += x_in_start; + in_ptr += (y_in_start * in.shape()[0]); + + // Update ROI pointer + roi_ptr += x_tile_start; + roi_ptr += (y_tile_start * tile.shape()[0]); + + for(int y = y_in_start; y < y_in_end; ++y) + { + // Copy per row + std::copy(in_ptr, in_ptr + n, roi_ptr); + + in_ptr += in.shape()[0]; + roi_ptr += tile.shape()[0]; + } +} + +template void get_tile(const SimpleTensor<float> &in, SimpleTensor<float> &roi, const Coordinates &coord); } // namespace validation } // namespace test } // namespace arm_compute diff --git a/tests/validation/Helpers.h b/tests/validation/Helpers.h index ba45968392..b192f317b4 100755 --- a/tests/validation/Helpers.h +++ b/tests/validation/Helpers.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 ARM Limited. + * Copyright (c) 2017-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -232,6 +232,34 @@ SimpleTensor<float> convert_from_asymmetric(const SimpleTensor<uint8_t> &src); * @return Quantized tensor. */ SimpleTensor<uint8_t> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info); + +/** Matrix multiply between 2 float simple tensors + * + * @param[in] a Input tensor A + * @param[in] b Input tensor B + * @param[out] out Output tensor + * + */ +void matrix_multiply(const SimpleTensor<float> &a, const SimpleTensor<float> &b, SimpleTensor<float> &out); + +/** Transpose matrix + * + * @param[in] in Input tensor + * @param[out] out Output tensor + * + */ +void transpose_matrix(const SimpleTensor<float> &in, SimpleTensor<float> &out); + +/** Get a 2D tile from a tensor + * + * @note In case of out-of-bound reads, the tile will be filled with zeros + * + * @param[in] in Input tensor + * @param[out] tile Tile + * @param[in] coord Coordinates + */ +template <typename T> +void get_tile(const SimpleTensor<T> &in, SimpleTensor<T> &tile, const Coordinates &coord); } // namespace validation } // namespace test } // namespace arm_compute diff --git a/tests/validation/fixtures/WinogradLayerFixture.h b/tests/validation/fixtures/WinogradLayerFixture.h index d7f0cbfdf5..95e331560d 100644 --- a/tests/validation/fixtures/WinogradLayerFixture.h +++ b/tests/validation/fixtures/WinogradLayerFixture.h @@ -26,6 +26,7 @@ #include "arm_compute/core/TensorShape.h" #include "arm_compute/core/Types.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" #include "arm_compute/runtime/NEON/NEScheduler.h" #include "tests/AssetsLibrary.h" #include "tests/Globals.h" @@ -35,6 +36,7 @@ #include "tests/validation/Helpers.h" #include "tests/validation/reference/ConvolutionLayer.h" #include "tests/validation/reference/Utils.h" +#include "tests/validation/reference/Winograd.h" #include <random> @@ -46,6 +48,8 @@ namespace test { namespace validation { +using namespace arm_compute::misc::shape_calculator; + template <typename TensorType, typename AccessorType, typename FunctionType, typename T> class WinogradLayerValidationFixture : public framework::Fixture { @@ -139,6 +143,87 @@ protected: DataType _data_type{}; }; +template <typename TensorType, typename AccessorType, typename FunctionType, typename T> +class WinogradInputTransformValidationFixture : public framework::Fixture +{ +public: + template <typename...> + void setup(TensorShape input_shape, PadStrideInfo conv_info, Size2D kernel_dims, bool is_nchw_format, DataType data_type) + { + TensorShape output_shape = compute_winograd_input_transform_shape(TensorInfo(input_shape, 1, data_type), conv_info, kernel_dims); + + _target = compute_target(input_shape, output_shape, conv_info, kernel_dims, is_nchw_format, data_type); + _reference = compute_reference(input_shape, output_shape, conv_info, kernel_dims, is_nchw_format, data_type); + } + +protected: + template <typename U> + void fill(U &&tensor, int i, float min, float max) + { + switch(tensor.data_type()) + { + case DataType::F32: + { + std::uniform_real_distribution<> distribution(min, max); + library->fill(tensor, distribution, i); + break; + } + default: + { + ARM_COMPUTE_ERROR("Not supported"); + library->fill_tensor_uniform(tensor, i); + break; + } + } + } + + TensorType compute_target(const TensorShape &input_shape, const TensorShape &output_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims, bool is_nchw_format, DataType data_type) + { + ARM_COMPUTE_UNUSED(is_nchw_format); + + // Create tensors + TensorType src = create_tensor<TensorType>(input_shape, data_type); + TensorType dst = create_tensor<TensorType>(output_shape, data_type); + + // Create and configure function + FunctionType transf; + transf.configure(&src, &dst, conv_info, kernel_dims); + + ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + src.allocator()->allocate(); + dst.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Fill tensors + fill(AccessorType(src), 0, -1.f, 1.f); + + // Compute CLWinogradInputTransform function + transf.run(); + + return dst; + } + + SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims, bool is_nchw_format, DataType data_type) + { + ARM_COMPUTE_UNUSED(is_nchw_format); + + // Create reference + SimpleTensor<T> src{ input_shape, data_type }; + + // Fill reference + fill(src, 0, -1.f, 1.f); + + return reference::winograd_input_transform<T>(src, output_shape, conv_info, kernel_dims); + } + + TensorType _target{}; + SimpleTensor<T> _reference{}; +}; } // namespace validation } // namespace test } // namespace arm_compute diff --git a/tests/validation/reference/Winograd.cpp b/tests/validation/reference/Winograd.cpp new file mode 100644 index 0000000000..371bb6348e --- /dev/null +++ b/tests/validation/reference/Winograd.cpp @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2018 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 "Winograd.h" + +#include "tests/validation/Helpers.h" +#include "tests/validation/reference/Utils.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +namespace +{ +template <typename T> +void winograd_input_transform3x3(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const PadStrideInfo &conv_info) +{ + TensorShape shape4x4(4u, 4u); + + // Simple tensor for the 4x4 input tile + SimpleTensor<T> src_tile{ shape4x4, src.data_type() }; + + // Simple tensor for the 4x4 temporary tile + SimpleTensor<T> tmp_tile{ shape4x4, src.data_type() }; + + // Simple tensor for the 4x4 output tile + SimpleTensor<T> dst_tile{ shape4x4, src.data_type() }; + + // Simple tensor for the transformation matrix + SimpleTensor<T> matrix{ shape4x4, src.data_type() }; + + // Simple tensor for the transformation matrix transposed + SimpleTensor<T> matrix_transposed{ shape4x4, src.data_type() }; + + const float matrix_values[] = { 1.f, 0.f, -1.f, 0.f, + 0.f, 1.f, 1.f, 0.f, + 0.f, -1.f, 1.f, 0.f, + 0.f, 1.f, 0.f, -1.f + }; + + for(int i = 0; i < matrix.num_elements(); ++i) + { + matrix[i] = matrix_values[i]; + } + + transpose_matrix(matrix, matrix_transposed); + + const int in_w = src.shape().x(); + const int in_h = src.shape().y(); + const int in_d = src.shape().z(); + const int num_batches = src.shape().total_size() / (in_w * in_h * in_d); + const int num_tiles_x = std::ceil((in_w - 2 + conv_info.pad_left() + conv_info.pad_right()) / 2.0f); + const int num_tiles_y = std::ceil((in_h - 2 + conv_info.pad_top() + conv_info.pad_bottom()) / 2.0f); + + ARM_COMPUTE_ERROR_ON((num_tiles_x * num_tiles_y) != static_cast<int>(dst.shape().y())); + + for(int b = 0; b < num_batches; ++b) + { + for(int z = 0; z < in_d; ++z) + { + for(int y = 0; y < num_tiles_y; ++y) + { + for(int x = 0; x < num_tiles_x; ++x) + { + int xi = x * 2 - conv_info.pad_left(); + int yi = y * 2 - conv_info.pad_top(); + + // Get the 4x4 tile from the input tensor + get_tile(src, src_tile, Coordinates(xi, yi, z, b)); + + // Compute the transformation + matrix_multiply(matrix, src_tile, tmp_tile); + matrix_multiply(tmp_tile, matrix_transposed, dst_tile); + + // Store the 4x4 output tile across the 16 channels + for(int i = 0; i < 16; ++i) + { + int xo = z; + int yo = x + y * num_tiles_x; + dst[coords2index(dst.shape(), Coordinates(xo, yo, i, b))] = dst_tile[i]; + } + } + } + } + } +} +} // namespace + +template <typename T> +SimpleTensor<T> winograd_input_transform(const SimpleTensor<T> &src, const TensorShape &dst_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims) +{ + ARM_COMPUTE_ERROR_ON(kernel_dims.width != kernel_dims.height); + ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NCHW); + + SimpleTensor<T> dst{ dst_shape, src.data_type() }; + + switch(kernel_dims.width) + { + case 3: + winograd_input_transform3x3(src, dst, conv_info); + break; + default: + ARM_COMPUTE_ERROR("Only 3x3 kernels are supported"); + } + + return dst; +} + +template SimpleTensor<float> winograd_input_transform(const SimpleTensor<float> &src, const TensorShape &dst_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/reference/Winograd.h b/tests/validation/reference/Winograd.h new file mode 100644 index 0000000000..ed95239db3 --- /dev/null +++ b/tests/validation/reference/Winograd.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2018 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_WINOGRAD_H__ +#define __ARM_COMPUTE_TEST_WINOGRAD_H__ + +#include "tests/SimpleTensor.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template <typename T> +SimpleTensor<T> winograd_input_transform(const SimpleTensor<T> &src, const TensorShape &dst_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_WINOGRAD_H__ */ |