blob: 5428154a2b528331dd5bc6bc872231a2f2304a29 [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
Georgios Pinitasf72f9362018-01-12 16:29:45 +00002 * Copyright (c) 2017-2018 ARM Limited.
Giorgio Arena93a690e2017-08-01 16:09:33 +01003 *
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_DEPTHWISE_CONVOLUTION_FIXTURE
25#define ARM_COMPUTE_TEST_DEPTHWISE_CONVOLUTION_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
Giorgio Arena76572242018-04-04 17:44:26 +010029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010030#include "tests/AssetsLibrary.h"
31#include "tests/Globals.h"
32#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010033#include "tests/framework/Asserts.h"
34#include "tests/framework/Fixture.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010035#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000036#include "tests/validation/reference/DepthwiseConvolutionLayer.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010037
Georgios Pinitasf72f9362018-01-12 16:29:45 +000038#include "utils/Utils.h"
39
Giorgio Arena93a690e2017-08-01 16:09:33 +010040#include <random>
41
42namespace arm_compute
43{
44namespace test
45{
46namespace validation
47{
Giorgio Arena76572242018-04-04 17:44:26 +010048using namespace arm_compute::misc::shape_calculator;
49
Giorgio Arena93a690e2017-08-01 16:09:33 +010050template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000051class DepthwiseConvolutionLayerValidationGenericFixture : public framework::Fixture
Giorgio Arena93a690e2017-08-01 16:09:33 +010052{
53public:
Dmitry Savenkod7295b72017-11-20 22:00:08 +070054 using TBias = typename std::conditional<std::is_same<typename std::decay<T>::type, uint8_t>::value, int32_t, T>::type;
55
56public:
Giorgio Arena93a690e2017-08-01 16:09:33 +010057 template <typename...>
Giorgio Arena76572242018-04-04 17:44:26 +010058 void setup(TensorShape in_shape, Size2D kernel_size, PadStrideInfo pad_stride_info, unsigned int depth_multiplier, DataType data_type, QuantizationInfo quantization_info, DataLayout data_layout)
Giorgio Arena93a690e2017-08-01 16:09:33 +010059 {
Giorgio Arena76572242018-04-04 17:44:26 +010060 _quantization_info = quantization_info;
61 _data_type = data_type;
62 const DataType bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type;
63
64 TensorShape weights_shape(kernel_size.width, kernel_size.height);
65
66 const TensorInfo in_info(in_shape, 1, data_type);
67 const TensorInfo we_info(weights_shape, 1, data_type);
68 TensorShape out_shape = compute_depthwise_convolution_shape(in_info, we_info, pad_stride_info, depth_multiplier);
69
70 weights_shape.set(2, out_shape.z());
Pablo Tello941cd702017-12-12 14:35:00 +000071 const TensorShape biases_shape(weights_shape[2]);
Dmitry Savenkod7295b72017-11-20 22:00:08 +070072
Giorgio Arena76572242018-04-04 17:44:26 +010073 _target = compute_target(in_shape, weights_shape, biases_shape, out_shape, pad_stride_info, depth_multiplier, data_type, bias_data_type, quantization_info, data_layout);
Giorgio Arena563494c2018-04-30 17:29:41 +010074 _reference = compute_reference(in_shape, weights_shape, biases_shape, out_shape, pad_stride_info, depth_multiplier, data_type, bias_data_type, quantization_info);
Giorgio Arena93a690e2017-08-01 16:09:33 +010075 }
76
77protected:
78 template <typename U>
79 void fill(U &&tensor, int i)
80 {
81 switch(tensor.data_type())
82 {
Dmitry Savenkod7295b72017-11-20 22:00:08 +070083 case DataType::QASYMM8:
84 {
85 std::uniform_int_distribution<uint8_t> distribution(0, 10);
86 library->fill(tensor, distribution, i);
87 break;
88 }
Giorgio Arena93a690e2017-08-01 16:09:33 +010089 case DataType::F32:
Frank Lei8cdfdb82018-01-02 16:49:33 +080090 case DataType::F16:
Giorgio Arena93a690e2017-08-01 16:09:33 +010091 {
92 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
93 library->fill(tensor, distribution, i);
94 break;
95 }
Dmitry Savenkod7295b72017-11-20 22:00:08 +070096 case DataType::S32:
97 {
Georgios Pinitasf72f9362018-01-12 16:29:45 +000098 std::uniform_int_distribution<int32_t> distribution(-100, 100);
Dmitry Savenkod7295b72017-11-20 22:00:08 +070099 library->fill(tensor, distribution, i);
100 break;
101 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100102 default:
103 library->fill_tensor_uniform(tensor, i);
104 }
105 }
106
Giorgio Arena563494c2018-04-30 17:29:41 +0100107 TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, TensorShape biases_shape, TensorShape output_shape, PadStrideInfo &pad_stride_info, unsigned int depth_multiplier,
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100108 const DataType data_type, const DataType bias_data_type, const QuantizationInfo quantization_info, const DataLayout data_layout)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100109 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100110 if(data_layout == DataLayout::NHWC)
111 {
112 permute(input_shape, PermutationVector(2U, 0U, 1U));
113 permute(weights_shape, PermutationVector(2U, 0U, 1U));
114 permute(output_shape, PermutationVector(2U, 0U, 1U));
115 }
116
Giorgio Arena93a690e2017-08-01 16:09:33 +0100117 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100118 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, quantization_info, data_layout);
119 TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, quantization_info, data_layout);
120 TensorType biases = create_tensor<TensorType>(biases_shape, bias_data_type, 1, quantization_info, data_layout);
121 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, quantization_info, data_layout);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100122
123 // Create Depthwise Convolution configure function
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700124 FunctionType dwc;
Giorgio Arena76572242018-04-04 17:44:26 +0100125 dwc.configure(&src, &weights, &biases, &dst, pad_stride_info, depth_multiplier);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700126
127 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
128 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
129 ARM_COMPUTE_EXPECT(biases.info()->is_resizable(), framework::LogLevel::ERRORS);
130 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100131
132 // Allocate tensors
133 src.allocator()->allocate();
134 weights.allocator()->allocate();
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100135 biases.allocator()->allocate();
Giorgio Arena93a690e2017-08-01 16:09:33 +0100136 dst.allocator()->allocate();
137
138 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
139 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100140 ARM_COMPUTE_EXPECT(!biases.info()->is_resizable(), framework::LogLevel::ERRORS);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100141 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
142
143 // Fill tensors
144 fill(AccessorType(src), 0);
145 fill(AccessorType(weights), 1);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100146 fill(AccessorType(biases), 2);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100147
148 // Compute function
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700149 dwc.run();
Giorgio Arena93a690e2017-08-01 16:09:33 +0100150
151 return dst;
152 }
153
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700154 SimpleTensor<T> compute_reference(const TensorShape &in_shape, const TensorShape &weights_shape, const TensorShape &biases_shape, const TensorShape &out_shape, const PadStrideInfo &pad_stride_info,
Giorgio Arena76572242018-04-04 17:44:26 +0100155 unsigned int depth_multiplier,
Giorgio Arena563494c2018-04-30 17:29:41 +0100156 const DataType data_type, const DataType bias_data_type, const QuantizationInfo quantization_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100157 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100158 SimpleTensor<T> src{ in_shape, data_type, 1, quantization_info };
159 SimpleTensor<T> weights{ weights_shape, data_type, 1, quantization_info };
160 SimpleTensor<TBias> biases{ biases_shape, bias_data_type, 1, quantization_info };
Giorgio Arena93a690e2017-08-01 16:09:33 +0100161
162 fill(src, 0);
163 fill(weights, 1);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100164 fill(biases, 2);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100165
Giorgio Arena76572242018-04-04 17:44:26 +0100166 return reference::depthwise_convolution(src, weights, biases, out_shape, pad_stride_info, depth_multiplier);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100167 }
168
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700169 TensorType _target{};
170 SimpleTensor<T> _reference{};
171 DataType _data_type{};
172 QuantizationInfo _quantization_info{};
173};
174
175template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000176class DepthwiseConvolutionLayerValidationFixture : public DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700177{
178public:
179 template <typename...>
Giorgio Arena76572242018-04-04 17:44:26 +0100180 void setup(TensorShape in_shape, Size2D kernel_size, PadStrideInfo pad_stride_info, unsigned int depth_multiplier, DataType data_type, DataLayout data_layout)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700181 {
Giorgio Arena76572242018-04-04 17:44:26 +0100182 DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(in_shape, kernel_size, pad_stride_info, depth_multiplier,
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100183 data_type, QuantizationInfo(), data_layout);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700184 }
185};
186
187template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000188class DepthwiseConvolutionLayerValidationQuantizedFixture : public DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700189{
190public:
191 template <typename...>
Giorgio Arena76572242018-04-04 17:44:26 +0100192 void setup(TensorShape in_shape, Size2D kernel_size, PadStrideInfo pad_stride_info, unsigned int depth_multiplier, DataType data_type, QuantizationInfo quantization_info, DataLayout data_layout)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700193 {
Giorgio Arena76572242018-04-04 17:44:26 +0100194 DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(in_shape, kernel_size, pad_stride_info, depth_multiplier,
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000195 data_type, quantization_info, data_layout);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700196 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100197};
198} // namespace validation
199} // namespace test
200} // namespace arm_compute
201#endif /* ARM_COMPUTE_TEST_DEPTHWISE_CONVOLUTION_FIXTURE */