blob: 8bce60caf27979f7c801b4c260f339c933b60494 [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
2 * Copyright (c) 2017 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_DEPTHWISE_SEPARABLE_CONVOLUTION_LAYER_FIXTURE
25#define ARM_COMPUTE_TEST_DEPTHWISE_SEPARABLE_CONVOLUTION_LAYER_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010029#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010032#include "tests/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010034#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000035#include "tests/validation/reference/DepthwiseSeparableConvolutionLayer.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010036
37#include <random>
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
46class DepthwiseSeparableConvolutionValidationFixture : public framework::Fixture
47{
48public:
49 template <typename...>
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010050 void setup(TensorShape in_shape, TensorShape depthwise_weights_shape, TensorShape depthwise_biases_shape, TensorShape depthwise_out_shape, TensorShape pointwise_weights_shape,
51 TensorShape pointwise_biases_shape, TensorShape output_shape,
Giorgio Arena93a690e2017-08-01 16:09:33 +010052 PadStrideInfo pad_stride_depthwise_info, PadStrideInfo pad_stride_pointwise_info)
53 {
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010054 _target = compute_target(in_shape, depthwise_weights_shape, depthwise_biases_shape, depthwise_out_shape, pointwise_weights_shape, pointwise_biases_shape, output_shape, pad_stride_depthwise_info,
55 pad_stride_pointwise_info);
56 _reference = compute_reference(in_shape, depthwise_weights_shape, depthwise_biases_shape, depthwise_out_shape, pointwise_weights_shape, pointwise_biases_shape, output_shape, pad_stride_depthwise_info,
57 pad_stride_pointwise_info);
Giorgio Arena93a690e2017-08-01 16:09:33 +010058 }
59
60protected:
61 template <typename U>
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010062 void fill(U &&tensor, int i, bool zero_fill = false)
Giorgio Arena93a690e2017-08-01 16:09:33 +010063 {
64 switch(tensor.data_type())
65 {
66 case DataType::F32:
67 {
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010068 std::uniform_real_distribution<> distribution((zero_fill) ? 0.f : -1.0f, (zero_fill) ? 0.f : 1.0f);
Giorgio Arena93a690e2017-08-01 16:09:33 +010069 library->fill(tensor, distribution, i);
70 break;
71 }
72 default:
73 library->fill_tensor_uniform(tensor, i);
74 }
75 }
76
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010077 TensorType compute_target(const TensorShape &input_shape, const TensorShape &depthwise_weights_shape, const TensorShape &depthwise_biases_shape, const TensorShape &depthwise_out_shape,
78 const TensorShape &pointwise_weights_shape, const TensorShape &pointwise_biases_shape, const TensorShape &output_shape,
79 const PadStrideInfo &pad_stride_depthwise_info, const PadStrideInfo &pad_stride_pointwise_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +010080 {
81 // Create tensors
82 TensorType src = create_tensor<TensorType>(input_shape, DataType::F32);
83 TensorType depthwise_weights = create_tensor<TensorType>(depthwise_weights_shape, DataType::F32);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010084 TensorType depthwise_biases = create_tensor<TensorType>(depthwise_biases_shape, DataType::F32);
Giorgio Arena93a690e2017-08-01 16:09:33 +010085 TensorType depthwise_out = create_tensor<TensorType>(depthwise_out_shape, DataType::F32);
86 TensorType pointwise_weights = create_tensor<TensorType>(pointwise_weights_shape, DataType::F32);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010087 TensorType pointwise_biases = create_tensor<TensorType>(pointwise_biases_shape, DataType::F32);
Giorgio Arena93a690e2017-08-01 16:09:33 +010088 TensorType dst = create_tensor<TensorType>(output_shape, DataType::F32);
89
90 // Create Depthwise Separable Convolution Layer configure function
Michalis Spyrou53d8c5c2017-11-23 17:11:35 +000091 FunctionType depthwise_separable_convolution_layer;
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010092 depthwise_separable_convolution_layer.configure(&src, &depthwise_weights, &depthwise_biases, &depthwise_out, &pointwise_weights, &pointwise_biases, &dst, pad_stride_depthwise_info,
93 pad_stride_pointwise_info);
Giorgio Arena93a690e2017-08-01 16:09:33 +010094
95 // Allocate tensors
96 src.allocator()->allocate();
97 depthwise_weights.allocator()->allocate();
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010098 depthwise_biases.allocator()->allocate();
Giorgio Arena93a690e2017-08-01 16:09:33 +010099 depthwise_out.allocator()->allocate();
100 pointwise_weights.allocator()->allocate();
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100101 pointwise_biases.allocator()->allocate();
Giorgio Arena93a690e2017-08-01 16:09:33 +0100102 dst.allocator()->allocate();
103
104 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
105 ARM_COMPUTE_EXPECT(!depthwise_weights.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100106 ARM_COMPUTE_EXPECT(!depthwise_biases.info()->is_resizable(), framework::LogLevel::ERRORS);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100107 ARM_COMPUTE_EXPECT(!depthwise_out.info()->is_resizable(), framework::LogLevel::ERRORS);
108 ARM_COMPUTE_EXPECT(!pointwise_weights.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100109 ARM_COMPUTE_EXPECT(!pointwise_biases.info()->is_resizable(), framework::LogLevel::ERRORS);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100110 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
111
112 // Fill tensors
113 fill(AccessorType(src), 0);
114 fill(AccessorType(depthwise_weights), 1);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100115 fill(AccessorType(depthwise_biases), 2, true);
116 fill(AccessorType(pointwise_weights), 3);
117 fill(AccessorType(pointwise_biases), 4);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100118
119 // Compute function
120 depthwise_separable_convolution_layer.run();
121
122 return dst;
123 }
124
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100125 SimpleTensor<T> compute_reference(const TensorShape &in_shape, const TensorShape &depthwise_weights_shape, const TensorShape &depthwise_biases_shape, const TensorShape &depthwise_out_shape,
126 const TensorShape &pointwise_weights_shape, const TensorShape &pointwise_biases_shape, const TensorShape &dst_shape,
127 const PadStrideInfo &pad_stride_depthwise_info, const PadStrideInfo &pad_stride_pointwise_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100128 {
129 SimpleTensor<T> src(in_shape, DataType::F32);
130 SimpleTensor<T> depthwise_weights(depthwise_weights_shape, DataType::F32);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100131 SimpleTensor<T> depthwise_biases(depthwise_biases_shape, DataType::F32);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100132 SimpleTensor<T> pointwise_weights(pointwise_weights_shape, DataType::F32);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100133 SimpleTensor<T> pointwise_biases(pointwise_biases_shape, DataType::F32);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100134
135 fill(src, 0);
136 fill(depthwise_weights, 1);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100137 fill(depthwise_biases, 2, true);
138 fill(pointwise_weights, 3);
139 fill(pointwise_biases, 4);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100140
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100141 return reference::depthwise_separable_convolution_layer(src,
142 depthwise_weights, depthwise_biases, depthwise_out_shape,
143 pointwise_weights, pointwise_biases,
144 dst_shape,
145 pad_stride_depthwise_info, pad_stride_pointwise_info);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100146 }
147
148 TensorType _target{};
149 SimpleTensor<T> _reference{};
150};
151} // namespace validation
152} // namespace test
153} // namespace arm_compute
154#endif /* ARM_COMPUTE_TEST_DEPTHWISE_SEPARABLE_CONVOLUTION_LAYER_FIXTURE */