2 * Copyright (c) 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #ifndef ARM_COMPUTE_TEST_CONVOLUTION_LAYER_FIXTURE
25 #define ARM_COMPUTE_TEST_CONVOLUTION_LAYER_FIXTURE
27 #include "arm_compute/core/TensorShape.h"
28 #include "arm_compute/core/Types.h"
29 #include "arm_compute/runtime/NEON/NEScheduler.h"
30 #include "tests/AssetsLibrary.h"
31 #include "tests/Globals.h"
32 #include "tests/IAccessor.h"
33 #include "tests/framework/Asserts.h"
34 #include "tests/framework/Fixture.h"
35 #include "tests/validation/CPP/ConvolutionLayer.h"
36 #include "tests/validation/CPP/Utils.h"
37 #include "tests/validation/Helpers.h"
43 class NEConvolutionLayer;
49 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
50 class ConvolutionValidationFixedPointFixture : public framework::Fixture
53 template <typename...>
54 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, bool reshape_weights, DataType data_type, int fractional_bits)
56 _fractional_bits = fractional_bits;
57 _data_type = data_type;
59 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, reshape_weights, data_type, fractional_bits);
60 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, data_type, fractional_bits);
65 void fill(U &&tensor, int i)
67 switch(tensor.data_type())
72 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
73 library->fill(tensor, distribution, i);
77 library->fill_tensor_uniform(tensor, i);
81 TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info,
82 bool reshape_weights, DataType data_type, int fixed_point_position)
84 WeightsInfo weights_info(!reshape_weights, weights_shape.x(), weights_shape.y(), weights_shape[3]);
85 TensorShape reshaped_weights_shape(weights_shape);
89 // Check if its a "fully connected" convolution
90 const bool is_fully_connected_convolution = (output_shape.x() == 1 && output_shape.y() == 1);
91 bool is_optimised = false;
93 is_optimised = std::is_same<FunctionType, NEConvolutionLayer>::value && NEScheduler::get().cpu_info().CPU == CPUTarget::ARMV7 && data_type == DataType::F32;
94 #elif defined(__aarch64__)
95 is_optimised = std::is_same<FunctionType, NEConvolutionLayer>::value && NEScheduler::get().cpu_info().CPU >= CPUTarget::ARMV8 && data_type == DataType::F32;
96 #endif /* defined(__arm__) || defined(__aarch64__) */
98 reshaped_weights_shape.collapse(3);
100 if(bias_shape.total_size() > 0)
102 reshaped_weights_shape.set(0, reshaped_weights_shape.x() + 1);
105 if(is_fully_connected_convolution || is_optimised)
107 const size_t shape_x = reshaped_weights_shape.x();
108 reshaped_weights_shape.set(0, reshaped_weights_shape.y());
109 reshaped_weights_shape.set(1, shape_x);
113 const int interleave_width = 16 / data_size_from_type(data_type);
114 reshaped_weights_shape.set(0, reshaped_weights_shape.x() * interleave_width);
115 reshaped_weights_shape.set(1, static_cast<unsigned int>(std::ceil(reshaped_weights_shape.y() / static_cast<float>(interleave_width))));
120 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, fixed_point_position);
121 TensorType weights = create_tensor<TensorType>(reshaped_weights_shape, data_type, 1, fixed_point_position);
122 TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1, fixed_point_position);
123 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, fixed_point_position);
125 // Create and configure function
127 conv.configure(&src, &weights, &bias, &dst, info, weights_info);
129 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
130 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
131 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
132 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
135 src.allocator()->allocate();
136 weights.allocator()->allocate();
137 bias.allocator()->allocate();
138 dst.allocator()->allocate();
140 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
141 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
142 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
143 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
146 fill(AccessorType(src), 0);
150 const bool is_fully_connected_convolution = (output_shape.x() == 1 && output_shape.y() == 1);
151 bool is_optimised = false;
153 is_optimised = std::is_same<FunctionType, NEConvolutionLayer>::value && NEScheduler::get().cpu_info().CPU == CPUTarget::ARMV7 && data_type == DataType::F32;
154 #elif defined(__aarch64__)
155 is_optimised = std::is_same<FunctionType, NEConvolutionLayer>::value && NEScheduler::get().cpu_info().CPU >= CPUTarget::ARMV8 && data_type == DataType::F32;
156 #endif /* defined(__arm__) || defined(__aarch64__) */
158 TensorShape tmp_weights_shape(weights_shape);
159 SimpleTensor<T> tmp_weights(tmp_weights_shape, data_type, 1, fixed_point_position);
160 SimpleTensor<T> tmp_bias(bias_shape, data_type, 1, fixed_point_position);
162 // Fill with original shape
163 fill(tmp_weights, 1);
166 tmp_weights = linearise_weights(tmp_weights, &tmp_bias);
168 if(!is_fully_connected_convolution && !is_optimised)
170 // Transpose with interleave
171 const int interleave_size = 16 / tmp_weights.element_size();
172 tmp_weights = transpose(std::move(tmp_weights), interleave_size);
175 AccessorType weights_accessor(weights);
177 for(int i = 0; i < tmp_weights.num_elements(); ++i)
179 Coordinates coord = index2coord(tmp_weights.shape(), i);
180 std::copy_n(static_cast<const T *>(tmp_weights(coord)), 1, static_cast<T *>(weights_accessor(coord)));
185 fill(AccessorType(weights), 1);
186 fill(AccessorType(bias), 2);
189 // Compute NEConvolutionLayer function
195 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info,
196 DataType data_type, int fixed_point_position)
199 SimpleTensor<T> src{ input_shape, data_type, 1, fixed_point_position };
200 SimpleTensor<T> weights{ weights_shape, data_type, 1, fixed_point_position };
201 SimpleTensor<T> bias{ bias_shape, data_type, 1, fixed_point_position };
208 return reference::convolution_layer<T>(src, weights, bias, output_shape, info);
211 TensorType _target{};
212 SimpleTensor<T> _reference{};
213 int _fractional_bits{};
214 DataType _data_type{};
217 template <typename U>
218 SimpleTensor<U> linearise_weights(const SimpleTensor<U> &weights, const SimpleTensor<U> *biases = nullptr)
220 TensorShape dst_shape(weights.shape());
221 dst_shape.collapse(3);
223 if(biases != nullptr)
225 dst_shape.set(0, dst_shape.x() + 1);
228 const size_t shape_x = dst_shape.x();
229 dst_shape.set(0, dst_shape.y());
230 dst_shape.set(1, shape_x);
232 SimpleTensor<U> dst(dst_shape, weights.data_type());
234 // Don't iterate over biases yet
235 for(int weights_idx = 0; weights_idx < weights.num_elements(); ++weights_idx)
237 Coordinates weights_coord = index2coord(weights.shape(), weights_idx);
238 const int dst_row = weights_idx % weights.shape().total_size_lower(3);
239 Coordinates dst_coord{ weights_coord[3], dst_row, weights_coord[4] };
240 const int dst_idx = coord2index(dst.shape(), dst_coord);
242 dst[dst_idx] = weights[weights_idx];
245 if(biases != nullptr)
247 // Fill last row with biases
248 for(int bias_idx = 0; bias_idx < biases->num_elements(); ++bias_idx)
250 Coordinates bias_coord = index2coord(biases->shape(), bias_idx);
251 Coordinates dst_coord{ bias_coord.x(), static_cast<int>(dst.shape().y()) - 1, bias_coord.y() };
252 int dst_idx = coord2index(dst.shape(), dst_coord);
254 dst[dst_idx] = (*biases)[bias_idx];
262 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
263 class ConvolutionValidationFixture : public ConvolutionValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>
266 template <typename...>
267 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, bool reshape_weights, DataType data_type)
269 ConvolutionValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, reshape_weights, data_type, 0);
272 } // namespace validation
274 } // namespace arm_compute
275 #endif /* ARM_COMPUTE_TEST_CONVOLUTION_LAYER_FIXTURE */