Compute Library
 22.05
graph_convolution.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2020 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 "tests/NEON/Accessor.h"
30 
32 #include "utils/GraphUtils.h"
33 #include "utils/Utils.h"
34 
35 #include "ValidateExample.h"
36 #include "graph_validate_utils.h"
37 
38 #include <utility>
39 
40 using namespace arm_compute::utils;
41 using namespace arm_compute::graph::frontend;
42 using namespace arm_compute::graph_utils;
43 using namespace arm_compute::graph;
44 using namespace arm_compute;
45 using namespace arm_compute::test;
46 using namespace arm_compute::test::validation;
47 
48 namespace
49 {
50 /** Convolution command line options used to configure the graph examples
51  *
52  * (Similar to common options)
53  * The options in this object get populated when "parse()" is called on the parser used to construct it.
54  * The expected workflow is:
55  *
56  * CommandLineParser parser;
57  * CommonOptions options( parser );
58  * parser.parse(argc, argv);
59  */
60 class ConvolutionOptions final : public CommonGraphValidateOptions
61 {
62 public:
63  explicit ConvolutionOptions(CommandLineParser &parser) noexcept
65  width(parser.add_option<SimpleOption<int>>("width", 9)),
66  height(parser.add_option<SimpleOption<int>>("height", 9)),
67  channels(parser.add_option<SimpleOption<int>>("channels", 1)),
68  batch(parser.add_option<SimpleOption<int>>("batch", 1)),
69  weights_width(parser.add_option<SimpleOption<int>>("weights_width", 3)),
70  weights_height(parser.add_option<SimpleOption<int>>("weights_height", 3)),
71  OFM(parser.add_option<SimpleOption<int>>("OFM", 1)),
72  padding_top(parser.add_option<SimpleOption<int>>("padding_top", 0)),
73  padding_left(parser.add_option<SimpleOption<int>>("padding_left", 0)),
74  padding_bottom(parser.add_option<SimpleOption<int>>("padding_bottom", 0)),
75  padding_right(parser.add_option<SimpleOption<int>>("padding_right", 0)),
76  stride_x(parser.add_option<SimpleOption<int>>("stride_x", 1)),
77  stride_y(parser.add_option<SimpleOption<int>>("stride_y", 1)),
78  padding_mode(),
79  conv_mode(),
80  data_layout(),
81  scale(parser.add_option<SimpleOption<float>>("scale", 1.0f)),
82  offset(parser.add_option<SimpleOption<int>>("offset", 0)),
83  weights_scale(parser.add_option<SimpleOption<float>>("weights_scale", 1.0f)),
84  weights_offset(parser.add_option<SimpleOption<int>>("weights_offset", 0)),
85  output_scale(parser.add_option<SimpleOption<float>>("output_scale", 1.0f)),
86  output_offset(parser.add_option<SimpleOption<int>>("output_offset", 0)),
87  input_range_low(parser.add_option<SimpleOption<uint64_t>>("input_range_low")),
88  input_range_high(parser.add_option<SimpleOption<uint64_t>>("input_range_high")),
89  weights_range_low(parser.add_option<SimpleOption<uint64_t>>("weights_range_low")),
90  weights_range_high(parser.add_option<SimpleOption<uint64_t>>("weights_range_high")),
91  input_npy(parser.add_option<SimpleOption<std::string>>("input_image")),
92  output_npy(parser.add_option<SimpleOption<std::string>>("reference_image")),
93  weights_npy(parser.add_option<SimpleOption<std::string>>("weights_npy")),
94  bias_npy(parser.add_option<SimpleOption<std::string>>("bias_image"))
95  {
96  const std::set<ConvolutionPaddingMode> available_padding_modes
97  {
100  };
101 
102  const std::set<arm_compute::graph::ConvolutionMethod> supported_convolution_methods
103  {
108  };
109 
110  const std::set<DataLayout> supported_data_layouts
111  {
114  };
115 
116  padding_mode = parser.add_option<EnumOption<ConvolutionPaddingMode>>("padding_mode", available_padding_modes, ConvolutionPaddingMode::Valid);
117  conv_mode = parser.add_option<EnumOption<arm_compute::graph::ConvolutionMethod>>("convolution_method", supported_convolution_methods, arm_compute::graph::ConvolutionMethod::Default);
118  data_layout = parser.add_option<EnumOption<DataLayout>>("layout", supported_data_layouts, DataLayout::NHWC);
119 
120  padding_mode->set_help("Set padding mode");
121  help->set_help("Show this help message");
122  width->set_help("Set Input dimension width");
123  height->set_help("Set Input dimension height");
124  channels->set_help("Set Input dimension channels");
125  batch->set_help("Set Input dimension batch");
126  weights_width->set_help("Set weights_dimensions width");
127  weights_height->set_help("Set weights_dimensions height");
128  OFM->set_help("Set OFM");
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  scale->set_help("Quantization scale from QASYMM8");
137  offset->set_help("Quantization offset from QASYMM8");
138  weights_scale->set_help("Quantization scale from QASYMM8");
139  weights_offset->set_help("Quantization offset from QASYMM8");
140  output_scale->set_help("Quantization scale from QASYMM8");
141  output_offset->set_help("Quantization offset from QASYMM8");
142  input_npy->set_help("Use input .npy instead");
143  output_npy->set_help("Use .npy as a reference");
144  input_range_low->set_help("Lower bound for input randomization range");
145  input_range_high->set_help("Lower bound for input randomization range");
146  weights_range_low->set_help("Lower bound for input randomization range");
147  weights_range_high->set_help("Lower bound for input randomization range");
148  }
149 
150  /** Fill out the supplied parameters with user supplied parameters
151  *
152  * @param[out] os Output stream.
153  * @param[in] common_params Example parameters to output
154  *
155  * @return None.
156  */
157  void consume_parameters(ExampleParams &common_params)
158  {
159  common_params.input.width = width->value();
160  common_params.input.height = height->value();
161  common_params.input.fm = channels->value();
162  common_params.input.batch = batch->value();
163  common_params.input.quant_info = QuantizationInfo(scale->value(), offset->value());
164  common_params.input.npy = input_npy->value();
165  common_params.input.range_low = input_range_low->value();
166  common_params.input.range_high = input_range_high->value();
167 
168  common_params.weights.width = weights_width->value();
169  common_params.weights.height = weights_height->value();
170  common_params.weights.fm = OFM->value();
171  common_params.weights.npy = weights_npy->value();
172  common_params.weights.quant_info = QuantizationInfo(weights_scale->value(), weights_offset->value());
173  common_params.weights.range_low = weights_range_low->value();
174  common_params.weights.range_high = weights_range_high->value();
175 
176  common_params.bias.npy = bias_npy->value();
177 
178  common_params.output.quant_info = QuantizationInfo(output_scale->value(), output_offset->value());
179  common_params.output.npy = output_npy->value();
180 
181  common_params.convolution.padding_mode = padding_mode->value();
182  common_params.convolution.padding_top = padding_top->value();
183  common_params.convolution.padding_bottom = padding_bottom->value();
184  common_params.convolution.padding_left = padding_left->value();
185  common_params.convolution.padding_right = padding_right->value();
186  common_params.convolution.padding_stride_x = stride_x->value();
187  common_params.convolution.padding_stride_y = stride_y->value();
188 
189  common_params.data_type = data_type->value();
190  common_params.data_layout = data_layout->value();
191  common_params.convolution_method = conv_mode->value();
192  }
193 
194  void print_parameters(::std::ostream &os, const ExampleParams &common_params) override
195  {
196  os << "Threads : " << common_params.common_params.threads << std::endl;
197  os << "Target : " << common_params.common_params.target << std::endl;
198  os << "Data type : " << common_params.data_type << std::endl;
199  os << "Input dimensions(X,Y, Channels, Batch) : (" << common_params.input.width << "," << common_params.input.height << "," << common_params.input.fm << "," << common_params.input.batch << ")"
200  << std::endl;
201  os << "Weight dimensions(X,Y, Channels(same as input), OFM) : (" << common_params.weights.width << "," << common_params.weights.height << "," << common_params.input.fm << "," <<
202  common_params.weights.fm << ")" << std::endl;
203  os << "Padding(top, bottom, left, right) (stride x, stride y) : (" << common_params.convolution.padding_top << "," << common_params.convolution.padding_bottom << "," <<
204  common_params.convolution.padding_left << "," << common_params.convolution.padding_right << ") (" << common_params.convolution.padding_stride_x << "," << common_params.convolution.padding_stride_y <<
205  ")" << std::endl;
206  os << "Padding Mode: " << common_params.convolution.padding_mode << std::endl;
207  os << "Convolution Method: " << common_params.convolution_method << std::endl;
208  }
209 
210  /** Prevent instances of this class from being copied (As this class contains pointers) */
211  ConvolutionOptions(const ConvolutionOptions &) = delete;
212  /** Prevent instances of this class from being copied (As this class contains pointers) */
213  ConvolutionOptions &operator=(const ConvolutionOptions &) = delete;
214  /** Allow instances of this class to be moved */
215  ConvolutionOptions(ConvolutionOptions &&) noexcept(true) = default;
216  /** Allow instances of this class to be moved */
217  ConvolutionOptions &operator=(ConvolutionOptions &&) noexcept(true) = default;
218  /** Default destructor */
219  ~ConvolutionOptions() override = default;
220 
221 private:
222  SimpleOption<int> *width; /**< Input width */
223  SimpleOption<int> *height; /**< Input height */
224  SimpleOption<int> *channels; /**< Input channels */
225  SimpleOption<int> *batch; /**< Input batch */
226  SimpleOption<int> *weights_width; /**< weights width */
227  SimpleOption<int> *weights_height; /**< weights height */
228  SimpleOption<int> *OFM; /**< Output Feature Map */
229  SimpleOption<int> *padding_top; /**< Padding top */
230  SimpleOption<int> *padding_left; /**< Padding left */
231  SimpleOption<int> *padding_bottom; /**< Padding bottom */
232  SimpleOption<int> *padding_right; /**< Padding right */
233  SimpleOption<int> *stride_x; /**< Padding stride x */
234  SimpleOption<int> *stride_y; /**< Padding stride y */
235  EnumOption<ConvolutionPaddingMode> *padding_mode; /**< Padding mode */
236  EnumOption<arm_compute::graph::ConvolutionMethod> *conv_mode; /**< Convolution method */
237  EnumOption<arm_compute::DataLayout> *data_layout; /**< Graph data layout */
238  SimpleOption<float> *scale; /**< Input Quantization scale from QASYMM8 */
239  SimpleOption<int> *offset; /**< Input Quantization offset from QASYMM8 */
240  SimpleOption<float> *weights_scale; /**< Weights Quantization scale from QASYMM8 */
241  SimpleOption<int> *weights_offset; /**< Weights Quantization offset from QASYMM8 */
242  SimpleOption<float> *output_scale; /**< Output Quantization scale from QASYMM8 */
243  SimpleOption<int> *output_offset; /**< Output Quantization offset from QASYMM8 */
244  SimpleOption<uint64_t> *input_range_low; /**< Lower bound for input randomization range */
245  SimpleOption<uint64_t> *input_range_high; /**< Upper bound for input randomization range */
246  SimpleOption<uint64_t> *weights_range_low; /**< Lower bound for weights randomization range */
247  SimpleOption<uint64_t> *weights_range_high; /**< Upper bound for weights randomization range */
248 
249  SimpleOption<std::string> *input_npy; /**< Use input .npy image */
250  SimpleOption<std::string> *output_npy; /**< Use output .npy image to verify*/
251  SimpleOption<std::string> *weights_npy; /**< Use weights .npy image */
252  SimpleOption<std::string> *bias_npy; /**< Use bias .npy image */
253 };
254 
255 /** ConvolutionLayer Graph example validation accessor class */
256 template <typename D>
257 class ConvolutionVerifyAccessor final : public VerifyAccessor<D>
258 {
259  using BaseClassType = VerifyAccessor<D>;
260  using BaseClassType::BaseClassType;
261  using BaseClassType::_params;
262  using TBias = typename std::conditional<std::is_same<typename std::decay<D>::type, uint8_t>::value, int32_t, D>::type;
263 
265  {
266  // Calculate padding information
267  const PadStrideInfo padding_info = calculate_convolution_padding(_params);
268 
269  //Calculate reference
270  return reference::convolution_layer<D>(src, weights, bias, output_shape, padding_info, Size2D(1, 1),
271  1, _params.output.quant_info);
272  }
273 
274  float relative_tolerance() override
275  {
276  const std::map<arm_compute::graph::Target, const std::map<DataType, float>> relative_tolerance
277  {
278  {
280  { { DataType::F16, 0.2f },
281  { DataType::F32, 0.5f },
282  { DataType::QASYMM8, 1.0f }
283  }
284  },
285  {
287  { { DataType::F16, 0.2f },
288  { DataType::F32, 0.01f },
289  { DataType::QASYMM8, 0.0f }
290  }
291  }
292  };
293 
294  if(_params.convolution_method == arm_compute::graph::ConvolutionMethod::Winograd
295  && _params.data_type == DataType::F32
296  && _params.common_params.target == arm_compute::graph::Target::NEON)
297  {
298  return 0.05f;
299  }
300  else
301  {
302  return relative_tolerance.at(_params.common_params.target).at(_params.data_type);
303  }
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.0001f },
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.07f },
336  { DataType::F32, 0.07f },
337  { DataType::QASYMM8, 0.0f }
338  }
339  },
340  {
341  Target::NEON,
342  { { DataType::F16, 0.07f },
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 
355 class GraphConvolutionValidateExample final : public GraphValidateExample<ConvolutionLayer, ConvolutionOptions, ConvolutionVerifyAccessor>
356 {
358 
359 public:
360  GraphConvolutionValidateExample()
361  : GraphValidateExample("Convolution Graph example")
362  {
363  }
364 
365  ConvolutionLayer 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 ConvolutionLayer(params.weights.width, params.weights.height, params.weights.fm,
377  get_accessor(params.weights, weights_lower, weights_upper, 1),
378  get_accessor(params.bias, lower, upper, 2),
379  padding_info, 1, params.weights.quant_info, params.output.quant_info);
380  }
381 };
382 
383 /** Main program for Graph 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, OFM]
388  * Padding [top,bottom,left,right, Stride x, Stride y, mode [Valid / Same / Manual] )
389  * Convolution Method[ Auto/GEMM/Winograd/Direct]
390  * Verification[tolerance_number,absolute_tolerance,relative_tolerance] )
391  *
392  */
393 int main(int argc, char **argv)
394 {
395  return arm_compute::utils::run_example<GraphConvolutionValidateExample>(argc, argv);
396 }
int padding_top
Padding graph parameters.
PadStrideInfo calculate_convolution_padding(ExampleParams params)
Calculate stride information.
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:1083
Arm® Neon™ capable target device.
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:34
Shape of a tensor.
Definition: TensorShape.h:39
arm_compute::graph::ConvolutionMethod convolution_method
1 channel, 1 F32 per channel
CommonGraphValidateOptions command line options used to configure the graph examples.
Includes all the Graph headers at once.
Class to parse command line arguments.
ConvolutionMethod
Available ConvolutionMethod.
Definition: Types.h:134
decltype(strategy::transforms) typedef type
std::unique_ptr< graph::ITensorAccessor > get_accessor(const TensorParams &tensor, PixelValue lower, PixelValue upper, const std::random_device::result_type seed=0)
Generates appropriate accessor according to the specified graph parameters.
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2022 Arm Limited.
1 channel, 1 F16 per channel
Quantization information.
quantized, asymmetric fixed-point 8-bit number unsigned
Structure holding all the graph Example parameters.
Padding and stride information class.
Definition: Types.h:669
Num samples, channels, height, width.
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
Graph example validation accessor class.
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
Num samples, height, width, channels.
Implementation of a simple option that accepts a value from a fixed set.
Definition: EnumOption.h:40
const size_t weights_width
Definition: impl.cpp:53
const size_t weights_height
Definition: impl.cpp:54
Default approach using internal heuristics.
const T & value() const
Get the selected value.
arm_compute::graph::frontend::Stream graph
OpenCL capable target device.
DataLayout
[DataLayout enum definition]
Definition: Types.h:113
int main(int argc, char **argv)
Main program for Graph Convolution test.
void set_help(std::string help)
Set the help message for the option.
Definition: Option.h:125
const int32_t * bias