blob: cdad404dfa7e649ac18c347e5f90317d8a65d4bc [file] [log] [blame]
John Kesapides8d942692019-02-26 14:52:12 +00001/*
2 * Copyright (c) 2019 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#include "arm_compute/graph.h"
25
26#include "support/ToolchainSupport.h"
27
28#include "tests/NEON/Accessor.h"
29#include "tests/validation/Validation.h"
30#include "tests/validation/reference/DepthwiseConvolutionLayer.h"
31#include "tests/validation/reference/Permute.h"
32
33#include "utils/CommonGraphOptions.h"
34#include "utils/GraphUtils.h"
35#include "utils/Utils.h"
36
37#include "ValidateExample.h"
38#include "graph_validate_utils.h"
39
40#include <utility>
41
42using namespace arm_compute::utils;
43using namespace arm_compute::graph::frontend;
44using namespace arm_compute::graph_utils;
45using namespace arm_compute::graph;
46using namespace arm_compute;
47using namespace arm_compute::test;
48using namespace arm_compute::test::validation;
49
50namespace
51{
52/** Depthwise Convolution command line options used to configure the graph examples
53 *
54 * (Similar to common options)
55 * The options in this object get populated when "parse()" is called on the parser used to construct it.
56 * The expected workflow is:
57 *
58 * CommandLineParser parser;
59 * CommonOptions options( parser );
60 * parser.parse(argc, argv);
61 */
62class DepthConvolutionOptions final : public CommonGraphValidateOptions
63{
64public:
65 explicit DepthConvolutionOptions(CommandLineParser &parser) noexcept
66 : CommonGraphValidateOptions(parser),
67 width(parser.add_option<SimpleOption<int>>("width", 9)),
68 height(parser.add_option<SimpleOption<int>>("height", 9)),
69 channels(parser.add_option<SimpleOption<int>>("channels", 1)),
70 batch(parser.add_option<SimpleOption<int>>("batch", 1)),
71 weights_width(parser.add_option<SimpleOption<int>>("weights_width", 3)),
72 weights_height(parser.add_option<SimpleOption<int>>("weights_height", 3)),
73 padding_top(parser.add_option<SimpleOption<int>>("padding_top", 0)),
74 padding_left(parser.add_option<SimpleOption<int>>("padding_left", 0)),
75 padding_bottom(parser.add_option<SimpleOption<int>>("padding_bottom", 0)),
76 padding_right(parser.add_option<SimpleOption<int>>("padding_right", 0)),
77 stride_x(parser.add_option<SimpleOption<int>>("stride_x", 1)),
78 stride_y(parser.add_option<SimpleOption<int>>("stride_y", 1)),
79 padding_mode(),
80 conv_mode(),
81 depth_multiplier(parser.add_option<SimpleOption<int>>("depth_multiplier", 1)),
82 data_layout(),
83 scale(parser.add_option<SimpleOption<float>>("scale", 1.0f)),
84 offset(parser.add_option<SimpleOption<int>>("offset", 0)),
85 weights_scale(parser.add_option<SimpleOption<float>>("weights_scale", 1.0f)),
86 weights_offset(parser.add_option<SimpleOption<int>>("weights_offset", 0)),
87 output_scale(parser.add_option<SimpleOption<float>>("output_scale", 1.0f)),
88 output_offset(parser.add_option<SimpleOption<int>>("output_offset", 0)),
89 input_range_low(parser.add_option<SimpleOption<uint64_t>>("input_range_low")),
90 input_range_high(parser.add_option<SimpleOption<uint64_t>>("input_range_high")),
91 weights_range_low(parser.add_option<SimpleOption<uint64_t>>("weights_range_low")),
92 weights_range_high(parser.add_option<SimpleOption<uint64_t>>("weights_range_high")),
93 input_npy(parser.add_option<SimpleOption<std::string>>("input_image")),
94 output_npy(parser.add_option<SimpleOption<std::string>>("reference_image")),
95 weights_npy(parser.add_option<SimpleOption<std::string>>("weights_npy")),
96 bias_npy(parser.add_option<SimpleOption<std::string>>("bias_image"))
97 {
98 const std::set<ConvolutionPaddingMode> available_padding_modes
99 {
100 ConvolutionPaddingMode::Valid,
101 ConvolutionPaddingMode::Same
102 };
103
104 const std::set<arm_compute::graph::DepthwiseConvolutionMethod> supported_convolution_methods
105 {
106 arm_compute::graph::DepthwiseConvolutionMethod::Default,
107 arm_compute::graph::DepthwiseConvolutionMethod::GEMV,
108 arm_compute::graph::DepthwiseConvolutionMethod::Optimized3x3,
109 };
110
111 const std::set<DataLayout> supported_data_layouts
112 {
113 DataLayout::NHWC,
114 DataLayout::NCHW,
115 };
116
117 padding_mode = parser.add_option<EnumOption<ConvolutionPaddingMode>>("padding_mode", available_padding_modes, ConvolutionPaddingMode::Valid);
118 conv_mode = parser.add_option<EnumOption<arm_compute::graph::DepthwiseConvolutionMethod>>("convolution_method", supported_convolution_methods,
119 arm_compute::graph::DepthwiseConvolutionMethod::Default);
120 data_layout = parser.add_option<EnumOption<DataLayout>>("layout", supported_data_layouts, DataLayout::NHWC);
121
122 padding_mode->set_help("Set padding mode");
123 width->set_help("Set Input dimension width");
124 height->set_help("Set Input dimension height");
125 channels->set_help("Set Input dimension channels");
126 batch->set_help("Set Input dimension batch");
127 weights_width->set_help("Set weights_dimensions width");
128 weights_height->set_help("Set weights_dimensions height");
129 padding_top->set_help("Set padding top");
130 padding_bottom->set_help("Set padding bottom");
131 padding_left->set_help("Set padding left");
132 padding_right->set_help("Set padding right");
133 stride_x->set_help("Set padding stride x");
134 stride_y->set_help("Set padding stride y");
135 conv_mode->set_help("Set convolution method");
136 data_layout->set_help("Data layout to use");
137 scale->set_help("Quantization scale from QASYMM8");
138 offset->set_help("Quantization offset from QASYMM8");
139 output_scale->set_help("Quantization scale from QASYMM8");
140 output_offset->set_help("Quantization offset from QASYMM8");
141 input_npy->set_help("Use input .npy instead");
142 output_npy->set_help("Use .npy as a reference");
143 input_range_low->set_help("Lower bound for input randomization range");
144 input_range_high->set_help("Lower bound for input randomization range");
145 weights_scale->set_help("Quantization scale from QASYMM8");
146 weights_offset->set_help("Quantization offset from QASYMM8");
147 weights_range_low->set_help("Lower bound for input randomization range");
148 weights_range_high->set_help("Lower bound for input randomization range");
149 depth_multiplier->set_help("Depth multiplier");
150 }
151
152 /** Fill out the supplied parameters with user supplied parameters
153 *
154 * @param[out] os Output stream.
155 * @param[in] common_params Example parameters to output
156 *
157 * @return None.
158 */
159 void consume_parameters(ExampleParams &common_params)
160 {
161 common_params.input.width = width->value();
162 common_params.input.height = height->value();
163 common_params.input.fm = channels->value();
164 common_params.input.batch = batch->value();
165 common_params.input.quant_info.scale = scale->value();
166 common_params.input.quant_info.offset = offset->value();
167 common_params.input.npy = input_npy->value();
168 common_params.input.range_low = input_range_low->value();
169 common_params.input.range_high = input_range_high->value();
170
171 common_params.weights.width = weights_width->value();
172 common_params.weights.height = weights_height->value();
173 common_params.weights.npy = weights_npy->value();
174 common_params.weights.range_low = weights_range_low->value();
175 common_params.weights.range_high = weights_range_high->value();
176 common_params.weights.quant_info.scale = weights_scale->value();
177 common_params.weights.quant_info.offset = weights_offset->value();
178
179 common_params.bias.npy = bias_npy->value();
180
181 common_params.output.quant_info.scale = output_scale->value();
182 common_params.output.quant_info.offset = output_offset->value();
183 common_params.output.npy = output_npy->value();
184
185 common_params.convolution.padding_mode = padding_mode->value();
186 common_params.convolution.padding_top = padding_top->value();
187 common_params.convolution.padding_bottom = padding_bottom->value();
188 common_params.convolution.padding_left = padding_left->value();
189 common_params.convolution.padding_right = padding_right->value();
190 common_params.convolution.padding_stride_x = stride_x->value();
191 common_params.convolution.padding_stride_y = stride_y->value();
192 common_params.convolution.depth_multiplier = depth_multiplier->value();
193
194 common_params.data_type = data_type->value();
195 common_params.data_layout = data_layout->value();
196 common_params.depth_convolution_method = conv_mode->value();
197 }
198
199 void print_parameters(::std::ostream &os, const ExampleParams &common_params) override
200 {
201 os << "Threads : " << common_params.common_params.threads << std::endl;
202 os << "Target : " << common_params.common_params.target << std::endl;
203 os << "Data type : " << common_params.data_type << std::endl;
204 os << "Input dimensions(X,Y, Channels, Batch) : (" << common_params.input.width << "," << common_params.input.height << "," << common_params.input.fm << "," << common_params.input.batch << ")"
205 << std::endl;
206 os << "Weight dimensions(X,Y, Channels(same as input)) : (" << common_params.weights.width << "," << common_params.weights.height << "," << common_params.input.fm << ","
207 << ")" << std::endl;
208 os << "Padding(top, bottom, left, right) (stride x, stride y) : (" << common_params.convolution.padding_top << "," << common_params.convolution.padding_bottom << "," <<
209 common_params.convolution.padding_left << "," << common_params.convolution.padding_right << ") (" << common_params.convolution.padding_stride_x << "," << common_params.convolution.padding_stride_y <<
210 ")" << std::endl;
211 os << "Padding Mode: " << common_params.convolution.padding_mode << std::endl;
212 os << "Convolution Method: " << common_params.depth_convolution_method << std::endl;
213 os << "Depth multiplier: " << common_params.convolution.depth_multiplier;
214 }
215
216 /** Prevent instances of this class from being copied (As this class contains pointers) */
217 DepthConvolutionOptions(const DepthConvolutionOptions &) = delete;
218 /** Prevent instances of this class from being copied (As this class contains pointers) */
219 DepthConvolutionOptions &operator=(const DepthConvolutionOptions &) = delete;
220 /** Allow instances of this class to be moved */
221 DepthConvolutionOptions(DepthConvolutionOptions &&) noexcept(true) = default;
222 /** Allow instances of this class to be moved */
223 DepthConvolutionOptions &operator=(DepthConvolutionOptions &&) noexcept(true) = default;
224 /** Default destructor */
225 ~DepthConvolutionOptions() override = default;
226
227 SimpleOption<int> *width; /**< Input width */
228 SimpleOption<int> *height; /**< Input height */
229 SimpleOption<int> *channels; /**< Input channels */
230 SimpleOption<int> *batch; /**< Input batch */
231 SimpleOption<int> *weights_width; /**< weights width */
232 SimpleOption<int> *weights_height; /**< weights height */
233 SimpleOption<int> *padding_top; /**< Padding top */
234 SimpleOption<int> *padding_left; /**< Padding left */
235 SimpleOption<int> *padding_bottom; /**< Padding bottom */
236 SimpleOption<int> *padding_right; /**< Padding right */
237 SimpleOption<int> *stride_x; /**< Padding stride x */
238 SimpleOption<int> *stride_y; /**< Padding stride y */
239 EnumOption<ConvolutionPaddingMode> *padding_mode; /**< Padding mode */
240 EnumOption<arm_compute::graph::DepthwiseConvolutionMethod> *conv_mode; /**< Convolution method */
241 SimpleOption<int> *depth_multiplier; /**< Depth multiplier */
242 EnumOption<arm_compute::DataLayout> *data_layout; /**< Graph data layout */
243 SimpleOption<float> *scale; /**< Input Quantization scale from QASYMM8 */
244 SimpleOption<int> *offset; /**< Input Quantization offset from QASYMM8 */
245 SimpleOption<float> *weights_scale; /**< Weights Quantization scale from QASYMM8 */
246 SimpleOption<int> *weights_offset; /**< Weights Quantization offset from QASYMM8 */
247 SimpleOption<float> *output_scale; /**< Output Quantization scale from QASYMM8 */
248 SimpleOption<int> *output_offset; /**< Output Quantization offset from QASYMM8 */
249 SimpleOption<uint64_t> *input_range_low; /**< Lower bound for input randomization range */
250 SimpleOption<uint64_t> *input_range_high; /**< Upper bound for input randomization range */
251 SimpleOption<uint64_t> *weights_range_low; /**< Lower bound for weights randomization range */
252 SimpleOption<uint64_t> *weights_range_high; /**< Upper bound for weights randomization range */
253
254 SimpleOption<std::string> *input_npy; /**< Use input .npy image */
255 SimpleOption<std::string> *output_npy; /**< Use output .npy image to verify*/
256 SimpleOption<std::string> *weights_npy; /**< Use weights .npy image */
257 SimpleOption<std::string> *bias_npy; /**< Use bias .npy image */
258};
259
260/** DepthwiseConvolutionLayer Graph example validation accessor class */
261template <typename D>
262class DepthConvolutionVerifyAccessor final : public VerifyAccessor<D>
263{
264public:
265 using BaseClassType = VerifyAccessor<D>;
266 using BaseClassType::BaseClassType;
267 using BaseClassType::_params;
268 using TBias = typename std::conditional<std::is_same<typename std::decay<D>::type, uint8_t>::value, int32_t, D>::type;
269
270public:
271 SimpleTensor<D> reference(SimpleTensor<D> &src, SimpleTensor<D> &weights, SimpleTensor<TBias> &bias, const TensorShape &output_shape) override
272 {
273 // Calculate padding information
274 const PadStrideInfo padding_info = calculate_convolution_padding(_params);
275
276 //Calculate reference
277 return reference::depthwise_convolution<D>(src, weights, bias, output_shape, padding_info,
278 _params.convolution.depth_multiplier,
279 Size2D(1U, 1U),
280 _params.output.quant_info);
281 }
282
283 float relative_tolerance() override
284 {
285 const std::map<arm_compute::graph::Target, const std::map<DataType, float>> relative_tolerance
286 {
287 {
288 arm_compute::graph::Target::CL,
289 { { DataType::F16, 0.01f },
290 { DataType::F32, 0.01f },
291 { DataType::QASYMM8, 0.0f }
292 }
293 },
294 {
295 arm_compute::graph::Target::NEON,
296 { { DataType::F16, 0.01f },
297 { DataType::F32, 0.01f },
298 { DataType::QASYMM8, 1.0f }
299 }
300 }
301 };
302
303 return relative_tolerance.at(_params.common_params.target).at(_params.data_type);
304 }
305
306 float absolute_tolerance() override
307 {
308 const std::map<Target, const std::map<DataType, float>> absolute_tolerance
309 {
310 {
311 Target::CL,
312 { { DataType::F16, 0.0f },
313 { DataType::F32, 0.0000f },
314 { DataType::QASYMM8, 0.0f }
315 }
316 },
317 {
318 Target::NEON,
319 { { DataType::F16, 0.2f },
320 { DataType::F32, 0.002f },
321 { DataType::QASYMM8, 0.0f }
322 }
323 }
324 };
325
326 return absolute_tolerance.at(_params.common_params.target).at(_params.data_type);
327 }
328
329 float tolerance_number() override
330 {
331 const std::map<Target, const std::map<DataType, float>> absolute_tolerance
332 {
333 {
334 Target::CL,
335 { { DataType::F16, 0.05f },
336 { DataType::F32, 0.00f },
337 { DataType::QASYMM8, 0.0f }
338 }
339 },
340 {
341 Target::NEON,
342 { { DataType::F16, 0.05f },
343 { DataType::F32, 0.0f },
344 { DataType::QASYMM8, 0.0f }
345 }
346 }
347 };
348
349 return absolute_tolerance.at(_params.common_params.target).at(_params.data_type);
350 }
351};
352
353} // namespace
354
355class GraphDepthwiseConvolutionValidateExample final : public GraphValidateExample<DepthwiseConvolutionLayer, DepthConvolutionOptions, DepthConvolutionVerifyAccessor>
356{
357 using GraphValidateExample::graph;
358
359public:
360 GraphDepthwiseConvolutionValidateExample()
361 : GraphValidateExample("DepthWiseConvolution Graph example")
362 {
363 }
364
365 DepthwiseConvolutionLayer GraphFunctionLayer(ExampleParams &params) override
366 {
367 const PixelValue lower = PixelValue(params.input.range_low, params.data_type, params.input.quant_info);
368 const PixelValue upper = PixelValue(params.input.range_high, params.data_type, params.input.quant_info);
369
370 const PixelValue weights_lower = PixelValue(params.weights.range_low, params.data_type, params.weights.quant_info);
371 const PixelValue weights_upper = PixelValue(params.weights.range_high, params.data_type, params.weights.quant_info);
372
373 // Calculate padding information
374 const PadStrideInfo padding_info = calculate_convolution_padding(params);
375
376 return DepthwiseConvolutionLayer(params.weights.width, params.weights.height,
377 get_accessor(params.weights, weights_lower, weights_upper, 1),
378 get_accessor(params.bias, lower, upper, 2),
379 padding_info, params.convolution.depth_multiplier, params.weights.quant_info, params.output.quant_info);
380 }
381};
382
383/** Main program for Graph Depthwise Convolution test
384 *
385 * @param[in] argc Number of arguments
386 * @param[in] argv Arguments ( Input dimensions [width, height, channels, batch]
387 * Weights dimensions [width, height, channels]
388 * Padding [top,bottom,left,right, Stride x, Stride y, mode [Valid / Same / Manual] )
389 * Convolution Method[ Default/GEMV/Optimized3x3]
390 * Verification[tolerance_number,absolute_tolerance,relative_tolerance] )
391 *
392 */
393int main(int argc, char **argv)
394{
395 return arm_compute::utils::run_example<GraphDepthwiseConvolutionValidateExample>(argc, argv);
396}