John Richardson | 32af1f8 | 2018-06-05 12:47:20 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef ARM_COMPUTE_TEST_CONVOLUTIONFIXTURE |
| 25 | #define ARM_COMPUTE_TEST_CONVOLUTIONFIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "tests/Globals.h" |
| 30 | #include "tests/Utils.h" |
| 31 | #include "tests/framework/Fixture.h" |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace test |
| 36 | { |
| 37 | namespace benchmark |
| 38 | { |
| 39 | /** Parent fixture that can be used for NEON and CL */ |
| 40 | template <typename TensorType, typename Function, typename Accessor> |
| 41 | class ConvolutionFixture : public framework::Fixture |
| 42 | { |
| 43 | public: |
| 44 | template <typename...> |
| 45 | void setup(TensorShape src_shape, DataType output_data_type, BorderMode border_mode, unsigned int width, unsigned int height, bool is_separable = false) |
| 46 | { |
| 47 | std::mt19937 gen(library->seed()); |
| 48 | const uint8_t constant_border_value = 0; |
| 49 | |
| 50 | // Generate random scale value between 1 and 255. |
| 51 | std::uniform_int_distribution<uint8_t> distribution_scale(1, 255); |
| 52 | const uint32_t scale = distribution_scale(gen); |
| 53 | |
| 54 | ARM_COMPUTE_ERROR_ON(3 != width && 5 != width && 7 != width && 9 != width); |
| 55 | ARM_COMPUTE_ERROR_ON(3 != height && 5 != height && 7 != height && 9 != height); |
| 56 | |
| 57 | std::vector<int16_t> conv(width * height); |
| 58 | |
| 59 | _width = width; |
| 60 | _height = height; |
| 61 | |
| 62 | if(is_separable) |
| 63 | { |
| 64 | init_separable_conv(conv.data(), width, height, seed); |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | init_conv(conv.data(), width, height, seed); |
| 69 | } |
| 70 | |
| 71 | // Create tensors |
| 72 | src = create_tensor<TensorType>(src_shape, DataType::U8); |
| 73 | dst = create_tensor<TensorType>(src_shape, output_data_type); |
| 74 | |
| 75 | // Configure function |
| 76 | configure_target(convolution_func, src, dst, conv.data(), scale, border_mode, constant_border_value); |
| 77 | |
| 78 | // Allocate tensors |
| 79 | src.allocator()->allocate(); |
| 80 | dst.allocator()->allocate(); |
| 81 | |
| 82 | // Fill tensors |
| 83 | library->fill_tensor_uniform(Accessor(src), 0); |
| 84 | library->fill_tensor_uniform(Accessor(dst), 1); |
| 85 | } |
| 86 | |
| 87 | void run() |
| 88 | { |
| 89 | convolution_func.run(); |
| 90 | } |
| 91 | |
| 92 | void sync() |
| 93 | { |
| 94 | sync_if_necessary<TensorType>(); |
| 95 | sync_tensor_if_necessary<TensorType>(dst); |
| 96 | } |
| 97 | |
| 98 | protected: |
| 99 | virtual void configure_target(Function &func, TensorType &src, TensorType &dst, const int16_t *conv, uint32_t scale, |
| 100 | BorderMode border_mode, uint8_t border_value) = 0; |
| 101 | |
| 102 | protected: |
| 103 | unsigned int _width{}; |
| 104 | unsigned int _height{}; |
| 105 | Function convolution_func{}; |
| 106 | |
| 107 | private: |
| 108 | const std::random_device::result_type seed = 0; |
| 109 | TensorType src{}; |
| 110 | TensorType dst{}; |
| 111 | }; |
| 112 | |
| 113 | /** Child fixture used for square convolutions */ |
| 114 | template <typename TensorType, typename Function, typename Accessor> |
| 115 | class ConvolutionSquareFixture : public ConvolutionFixture<TensorType, Function, Accessor> |
| 116 | { |
| 117 | public: |
| 118 | template <typename...> |
| 119 | void setup(TensorShape src_shape, DataType output_data_type, BorderMode border_mode, unsigned int width) |
| 120 | { |
| 121 | ConvolutionFixture<TensorType, Function, Accessor>::setup(src_shape, output_data_type, border_mode, width, width); |
| 122 | } |
| 123 | |
| 124 | protected: |
| 125 | void configure_target(Function &func, TensorType &src, TensorType &dst, const int16_t *conv, uint32_t scale, |
| 126 | BorderMode border_mode, uint8_t constant_border_value) |
| 127 | { |
| 128 | this->convolution_func.configure(&src, &dst, conv, scale, border_mode, constant_border_value); |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | /** Child fixture used for rectangular convolutions */ |
| 133 | template <typename TensorType, typename Function, typename Accessor> |
| 134 | class ConvolutionRectangleFixture : public ConvolutionFixture<TensorType, Function, Accessor> |
| 135 | { |
| 136 | public: |
| 137 | template <typename...> |
| 138 | void setup(TensorShape src_shape, DataType output_data_type, BorderMode border_mode, unsigned int width, unsigned int height) |
| 139 | { |
| 140 | ConvolutionFixture<TensorType, Function, Accessor>::setup(src_shape, output_data_type, border_mode, width, height); |
| 141 | } |
| 142 | |
| 143 | protected: |
| 144 | void configure_target(Function &func, TensorType &src, TensorType &dst, const int16_t *conv, uint32_t scale, |
| 145 | BorderMode border_mode, uint8_t constant_border_value) |
| 146 | { |
| 147 | this->convolution_func.configure(&src, &dst, conv, this->_width, this->_height, scale, border_mode, constant_border_value); |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | /** Child fixture used for separable convolutions */ |
| 152 | template <typename TensorType, typename Function, typename Accessor> |
| 153 | class ConvolutionSeperableFixture : public ConvolutionFixture<TensorType, Function, Accessor> |
| 154 | { |
| 155 | public: |
| 156 | template <typename...> |
| 157 | void setup(TensorShape src_shape, DataType output_data_type, BorderMode border_mode, unsigned int width) |
| 158 | { |
| 159 | ConvolutionFixture<TensorType, Function, Accessor>::setup(src_shape, output_data_type, border_mode, width, width, true); |
| 160 | } |
| 161 | |
| 162 | protected: |
| 163 | void configure_target(Function &func, TensorType &src, TensorType &dst, const int16_t *conv, uint32_t scale, |
| 164 | BorderMode border_mode, uint8_t constant_border_value) |
| 165 | { |
| 166 | this->convolution_func.configure(&src, &dst, conv, scale, border_mode, constant_border_value); |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | } // namespace benchmark |
| 171 | } // namespace test |
| 172 | } // namespace arm_compute |
| 173 | #endif /* ARM_COMPUTE_TEST_CONVOLUTIONFIXTURE */ |