blob: c1a83d1f40b43ce4d908cf4f6a59871c566cda67 [file] [log] [blame]
John Kesapides8d942692019-02-26 14:52:12 +00001/*
ramy.elgammal@arm.coma2561f02023-06-16 20:45:48 +01002 * Copyright (c) 2019-2020, 2023 Arm Limited.
John Kesapides8d942692019-02-26 14:52:12 +00003 *
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
Anitha Rajc63f8b02023-11-14 13:23:24 +000025#ifndef ACL_TESTS_VALIDATE_EXAMPLES_GRAPH_VALIDATE_UTILS_H
26#define ACL_TESTS_VALIDATE_EXAMPLES_GRAPH_VALIDATE_UTILS_H
John Kesapides8d942692019-02-26 14:52:12 +000027
28#include "arm_compute/graph.h"
29
30#include "ValidateExample.h"
31#include "utils/command_line/CommandLineParser.h"
32
33namespace arm_compute
34{
35namespace utils
36{
37/*Available Padding modes */
38enum class ConvolutionPaddingMode
39{
40 Valid,
41 Same,
42 Manual
43};
44
45/** Stream Input operator for the ConvolutionPaddingMode type
46 *
47 * @param[in] stream Input stream.
48 * @param[out] Mode Convolution parameters to output
49 *
50 * @return input stream.
51 */
52inline ::std::istream &operator>>(::std::istream &stream, ConvolutionPaddingMode &Mode)
53{
54 static const std::map<std::string, ConvolutionPaddingMode> modes =
55 {
56 { "valid", ConvolutionPaddingMode::Valid },
57 { "same", ConvolutionPaddingMode::Same },
58 { "manual", ConvolutionPaddingMode::Manual }
59 };
60 std::string value;
61 stream >> value;
62#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
63 try
64 {
65#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
66 Mode = modes.at(arm_compute::utility::tolower(value));
67#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
68 }
69 catch(const std::out_of_range &)
70 {
71 throw std::invalid_argument(value);
72 }
73#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
74
75 return stream;
76}
77
78/** Formatted output of the ConvolutionPaddingMode type
79 *
80 * @param[out] os Output stream.
81 * @param[in] Mode ConvolutionPaddingMode to output
82 *
83 * @return Modified output stream.
84 */
85inline ::std::ostream &operator<<(::std::ostream &os, ConvolutionPaddingMode Mode)
86{
87 switch(Mode)
88 {
89 case ConvolutionPaddingMode::Valid:
90 os << "Valid";
91 break;
92 case ConvolutionPaddingMode::Same:
93 os << "Same";
94 break;
95 case ConvolutionPaddingMode::Manual:
96 os << "Manual";
97 break;
98 default:
99 throw std::invalid_argument("Unsupported padding mode format");
100 }
101
102 return os;
103}
104
105/** Structure holding all the input tensor graph parameters */
106struct TensorParams
107{
108 int width{ 1 };
109 int height{ 1 };
110 int fm{ 1 };
111 int batch{ 1 };
112 QuantizationInfo quant_info{ 1.0f, 0 };
113 std::string npy{};
114 uint64_t range_low{ 0 };
115 uint64_t range_high{ 16 };
116};
117
118/** Structure holding all the verification graph parameters */
119struct VerificationParams
120{
121 float absolute_tolerance{ -1.f };
122 float relative_tolerance{ -1.f };
123 float tolerance_number{ -1.f };
124};
125
126/** Structure holding all the common graph parameters */
127struct FrameworkParams
128{
129 bool help{ false };
130 int threads{ 0 };
131 arm_compute::graph::Target target{ arm_compute::graph::Target::NEON };
132};
133
134/** Structure holding all the graph Example parameters */
135struct CommonParams
136{
137 FrameworkParams common_params{};
138 TensorParams input{};
139 TensorParams weights{};
140 TensorParams bias{};
141 TensorParams output{};
142 VerificationParams verification{};
143 arm_compute::DataType data_type{ DataType::F32 };
144};
145
146/** Structure holding all the Convolution layer graph parameters */
147struct ConvolutionParams
148{
149 int depth_multiplier{ 1 };
150 /** Padding graph parameters */
151 int padding_top{ 0 };
152 int padding_bottom{ 0 };
153 int padding_left{ 0 };
154 int padding_right{ 0 };
155 int padding_stride_x{ 0 };
156 int padding_stride_y{ 0 };
157 ConvolutionPaddingMode padding_mode{ ConvolutionPaddingMode::Valid };
158 struct
159 {
160 struct
161 {
162 int X{ 0 };
163 int Y{ 0 };
164 } stride{};
165 ConvolutionPaddingMode mode{ ConvolutionPaddingMode::Valid };
166 } padding{};
167};
168
169/** Structure holding all the fully_connected layer graph parameters */
170struct FullyConnectedParams
171{
172 FullyConnectedLayerInfo info{};
173 int num_outputs{ 1 };
174};
175
176/** Structure holding all the graph Example parameters */
177struct ExampleParams : public CommonParams
178{
179 FullyConnectedParams fully_connected{};
180 ConvolutionParams convolution{};
181 arm_compute::graph::DepthwiseConvolutionMethod depth_convolution_method{ arm_compute::graph::DepthwiseConvolutionMethod::Default };
182 arm_compute::graph::ConvolutionMethod convolution_method{ arm_compute::graph::ConvolutionMethod::Default };
183 arm_compute::DataLayout data_layout{ DataLayout::NCHW };
184};
185
186/** Calculate stride information.
187 *
188 * Depending on the selected padding mode create the desired PadStrideInfo
189 *
190 * @param[in] params Convolution parameters supplied by the user.
191 *
192 * @return PadStrideInfo with the correct padding mode.
193 */
194inline PadStrideInfo calculate_convolution_padding(ExampleParams params)
195{
196 switch(params.convolution.padding_mode)
197 {
198 case ConvolutionPaddingMode::Manual:
199 {
200 return PadStrideInfo(params.convolution.padding_stride_x, params.convolution.padding_stride_y, params.convolution.padding_left, params.convolution.padding_right, params.convolution.padding_top,
201 params.convolution.padding_bottom, DimensionRoundingType::FLOOR);
202 }
203 case ConvolutionPaddingMode::Valid:
204 {
205 return PadStrideInfo();
206 }
207 case ConvolutionPaddingMode::Same:
208 {
209 return arm_compute::calculate_same_pad(TensorShape(params.input.width, params.input.height), TensorShape(params.weights.width, params.weights.height),
210 PadStrideInfo(params.convolution.padding_stride_x,
211 params.convolution.padding_stride_y));
212 }
213 default:
214 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
215 }
216}
217/** CommonGraphValidateOptions command line options used to configure the graph examples
218 *
219 * (Similar to common options)
220 * The options in this object get populated when "parse()" is called on the parser used to construct it.
221 * The expected workflow is:
222 *
223 * CommandLineParser parser;
224 * CommonOptions options( parser );
225 * parser.parse(argc, argv);
226 */
227class CommonGraphValidateOptions
228{
229public:
230 explicit CommonGraphValidateOptions(CommandLineParser &parser) noexcept
231 : help(parser.add_option<ToggleOption>("help")),
232 threads(parser.add_option<SimpleOption<int>>("threads")),
233 target(),
234 data_type(),
235 absolute_tolerance(parser.add_option<SimpleOption<float>>("abs_tolerance", -1.0f)),
236 relative_tolerance(parser.add_option<SimpleOption<float>>("rel_tolerance", -1.0f)),
237 tolerance_number(parser.add_option<SimpleOption<float>>("tolerance_num", -1.0f))
238 {
239 const std::set<arm_compute::graph::Target> supported_targets
240 {
241 arm_compute::graph::Target::NEON,
242 arm_compute::graph::Target::CL,
John Kesapides8d942692019-02-26 14:52:12 +0000243 };
244
245 const std::set<arm_compute::DataType> supported_data_types
246 {
247 DataType::F16,
248 DataType::F32,
249 DataType::QASYMM8,
250 };
251
252 target = parser.add_option<EnumOption<arm_compute::graph::Target>>("target", supported_targets, arm_compute::graph::Target::NEON);
253 data_type = parser.add_option<EnumOption<DataType>>("type", supported_data_types, DataType::F32);
254
255 target->set_help("Target to execute on");
256 data_type->set_help("Data type to use");
257 help->set_help("Show this help message");
258 absolute_tolerance->set_help("Absolute tolerance used for verification");
259 relative_tolerance->set_help("Absolute tolerance used for verification");
260 tolerance_number->set_help("Absolute tolerance used for verification");
John Kesapides8d942692019-02-26 14:52:12 +0000261 }
262
263 /** Prevent instances of this class from being copied (As this class contains pointers) */
264 CommonGraphValidateOptions(const CommonGraphValidateOptions &) = delete;
265 /** Prevent instances of this class from being copied (As this class contains pointers) */
266 CommonGraphValidateOptions &operator=(const CommonGraphValidateOptions &) = delete;
267 /** Allow instances of this class to be moved */
268 CommonGraphValidateOptions(CommonGraphValidateOptions &&) noexcept(true) = default;
269 /** Allow instances of this class to be moved */
270 CommonGraphValidateOptions &operator=(CommonGraphValidateOptions &&) noexcept(true) = default;
271 /** Default destructor */
272 virtual ~CommonGraphValidateOptions() = default;
273
274 void consume_common_parameters(CommonParams &common_params)
275 {
276 common_params.common_params.help = help->is_set() ? help->value() : false;
277 common_params.common_params.threads = threads->value();
278 common_params.common_params.target = target->value();
279
280 common_params.verification.absolute_tolerance = absolute_tolerance->value();
281 common_params.verification.relative_tolerance = relative_tolerance->value();
282 common_params.verification.tolerance_number = tolerance_number->value();
283 }
284
285 /** Formatted output of the ExampleParams type
286 *
287 * @param[out] os Output stream.
288 * @param[in] common_params Example parameters to output
289 *
John Kesapides8d942692019-02-26 14:52:12 +0000290 */
291 virtual void print_parameters(::std::ostream &os, const ExampleParams &common_params)
292 {
293 os << "Threads : " << common_params.common_params.threads << std::endl;
294 os << "Target : " << common_params.common_params.target << std::endl;
295 os << "Data type : " << common_params.data_type << std::endl;
296 }
297
298 ToggleOption *help; /**< show help message */
299 SimpleOption<int> *threads; /**< Number of threads option */
300 EnumOption<arm_compute::graph::Target> *target; /**< Graph execution target */
301 EnumOption<arm_compute::DataType> *data_type; /**< Graph data type */
302 SimpleOption<float> *absolute_tolerance; /**< Absolute tolerance used in verification */
303 SimpleOption<float> *relative_tolerance; /**< Relative tolerance used in verification */
304 SimpleOption<float> *tolerance_number; /**< Tolerance number used in verification */
305};
306
307/** Consumes the consume_common_graph_parameters graph options and creates a structure containing any information
308 *
309 * @param[in] options Options to consume
310 * @param[out] common_params params structure to consume.
John Kesapides8d942692019-02-26 14:52:12 +0000311 */
312void consume_common_graph_parameters(CommonGraphValidateOptions &options, CommonParams &common_params)
313{
314 common_params.common_params.help = options.help->is_set() ? options.help->value() : false;
315 common_params.common_params.threads = options.threads->value();
316 common_params.common_params.target = options.target->value();
317
318 common_params.verification.absolute_tolerance = options.absolute_tolerance->value();
319 common_params.verification.relative_tolerance = options.relative_tolerance->value();
320 common_params.verification.tolerance_number = options.tolerance_number->value();
321}
322
323/** Generates appropriate accessor according to the specified graph parameters
324 *
325 * @param[in] tensor Tensor parameters
326 * @param[in] lower Lower random values bound
327 * @param[in] upper Upper random values bound
328 * @param[in] seed Random generator seed
329 *
330 * @return An appropriate tensor accessor
331 */
332inline std::unique_ptr<graph::ITensorAccessor> get_accessor(const TensorParams &tensor, PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0)
333{
334 if(!tensor.npy.empty())
335 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000336 return std::make_unique<arm_compute::graph_utils::NumPyBinLoader>(tensor.npy);
John Kesapides8d942692019-02-26 14:52:12 +0000337 }
338 else
339 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000340 return std::make_unique<arm_compute::graph_utils::RandomAccessor>(lower, upper, seed);
John Kesapides8d942692019-02-26 14:52:12 +0000341 }
342}
343
344/** Graph example validation accessor class */
345template <typename D>
346class VerifyAccessor : public graph::ITensorAccessor
347{
348public:
349 using TBias = typename std::conditional<std::is_same<typename std::decay<D>::type, uint8_t>::value, int32_t, D>::type;
350 /** Constructor
351 *
352 * @param[in] params Convolution parameters
353 */
354 explicit VerifyAccessor(ExampleParams &params)
355 : _params(std::move(params))
356 {
357 }
358 // Inherited methods overriden:
359 bool access_tensor(ITensor &tensor) override
360 {
361 if(_params.output.npy.empty())
362 {
363 arm_compute::test::SimpleTensor<D> src;
364 arm_compute::test::SimpleTensor<D> weights;
365 arm_compute::test::SimpleTensor<TBias> bias;
366
367 //Create Input tensors
368 create_tensors(src, weights, bias, tensor);
369
370 //Fill the tensors with random values
371 fill_tensor(src, 0, static_cast<D>(_params.input.range_low), static_cast<D>(_params.input.range_high));
372 fill_tensor(weights, 1, static_cast<D>(_params.weights.range_low), static_cast<D>(_params.weights.range_high));
373 fill_tensor(bias, 2, static_cast<TBias>(_params.input.range_low), static_cast<TBias>(_params.input.range_high));
374
375 arm_compute::test::SimpleTensor<D> output = reference(src, weights, bias, output_shape(tensor));
376
377 validate(tensor, output);
378 }
379 else
380 {
381 //The user provided a reference file use an npy accessor to validate
382 arm_compute::graph_utils::NumPyAccessor(_params.output.npy, tensor.info()->tensor_shape(), tensor.info()->data_type()).access_tensor(tensor);
383 }
384 return false;
385 }
386
387 /** Create reference tensors.
388 *
389 * Validate the given tensor against the reference result.
390 *
391 * @param[out] src The tensor with the source data.
392 * @param[out] weights The tensor with the weigths data.
393 * @param[out] bias The tensor with the bias data.
394 * @param[in] tensor Tensor result of the actual operation passed into the Accessor.
395 *
John Kesapides8d942692019-02-26 14:52:12 +0000396 */
397 virtual void create_tensors(arm_compute::test::SimpleTensor<D> &src,
398 arm_compute::test::SimpleTensor<D> &weights,
399 arm_compute::test::SimpleTensor<TBias> &bias,
400 ITensor &tensor)
401 {
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100402 ARM_COMPUTE_UNUSED(tensor);
John Kesapides8d942692019-02-26 14:52:12 +0000403 //Create Input tensors
404 src = arm_compute::test::SimpleTensor<D> { TensorShape(_params.input.width, _params.input.height, _params.input.fm, _params.input.batch), _params.data_type, 1, _params.input.quant_info };
405 weights = arm_compute::test::SimpleTensor<D> { TensorShape(_params.weights.width, _params.weights.height, _params.weights.fm), _params.data_type, 1, _params.weights.quant_info };
406 bias = arm_compute::test::SimpleTensor<TBias> { TensorShape(_params.input.height), _params.data_type, 1, _params.input.quant_info };
407 }
408
409 /** Calculate reference output tensor shape.
410 *
411 * @param[in] tensor Tensor result of the actual operation passed into the Accessor.
412 *
413 * @return output tensor shape.
414 */
415 virtual TensorShape output_shape(ITensor &tensor)
416 {
417 return arm_compute::graph_utils::permute_shape(tensor.info()->tensor_shape(), _params.data_layout, DataLayout::NCHW);
418 }
419
420 /** Calculate reference tensor.
421 *
422 * Validate the given tensor against the reference result.
423 *
424 * @param[in] src The tensor with the source data.
425 * @param[in] weights The tensor with the weigths data.
426 * @param[in] bias The tensor with the bias data.
427 * @param[in] output_shape Shape of the output tensor.
428 *
429 * @return Tensor with the reference output.
430 */
431 virtual arm_compute::test::SimpleTensor<D> reference(arm_compute::test::SimpleTensor<D> &src,
432 arm_compute::test::SimpleTensor<D> &weights,
433 arm_compute::test::SimpleTensor<TBias> &bias,
434 const arm_compute::TensorShape &output_shape) = 0;
435
436 /** Fill QASYMM tensor with Random values.
437 *
438 * Validate the given tensor against the reference result.
439 *
440 * @param[out] tensor The tensor we want to file
441 * @param[in] seed seed for the randomization function
442 * @param[in] low lower bound for random values
443 * @param[in] high upper bound for random values
John Kesapides8d942692019-02-26 14:52:12 +0000444 */
445 void fill_tensor(arm_compute::test::SimpleTensor<uint8_t> &tensor, std::random_device::result_type seed, uint8_t low, uint8_t high)
446 {
447 ARM_COMPUTE_ERROR_ON(tensor.data_type() != arm_compute::DataType::QASYMM8);
448
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100449 const UniformQuantizationInfo qinfo = tensor.quantization_info().uniform();
John Kesapides8d942692019-02-26 14:52:12 +0000450
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100451 uint8_t qasymm8_low = quantize_qasymm8(low, qinfo);
452 uint8_t qasymm8_high = quantize_qasymm8(high, qinfo);
John Kesapides8d942692019-02-26 14:52:12 +0000453
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100454 std::mt19937 gen(seed);
John Kesapides8d942692019-02-26 14:52:12 +0000455 std::uniform_int_distribution<uint8_t> distribution(qasymm8_low, qasymm8_high);
456
457 for(int i = 0; i < tensor.num_elements(); ++i)
458 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100459 tensor[i] = quantize_qasymm8(distribution(gen), qinfo);
John Kesapides8d942692019-02-26 14:52:12 +0000460 }
461 }
462 /** Fill S32 tensor with Random values.
463 *
464 * Validate the given tensor against the reference result.
465 *
466 * @param[out] tensor The tensor we want to file
467 * @param[in] seed seed for the randomization function
468 * @param[in] low lower bound for random values
469 * @param[in] high upper bound for random values
John Kesapides8d942692019-02-26 14:52:12 +0000470 */
471 void fill_tensor(arm_compute::test::SimpleTensor<int32_t> &tensor, std::random_device::result_type seed, int32_t low, int32_t high)
472 {
473 std::mt19937 gen(seed);
474 std::uniform_int_distribution<int32_t> distribution(static_cast<int32_t>(low), static_cast<uint32_t>(high));
475
476 for(int i = 0; i < tensor.num_elements(); ++i)
477 {
478 tensor[i] = distribution(gen);
479 }
480 }
481 /** Fill F32 tensor with Random values.
482 *
483 * Validate the given tensor against the reference result.
484 *
485 * @param[out] tensor The tensor we want to file
486 * @param[in] seed seed for the randomization function
487 * @param[in] low lower bound for random values
488 * @param[in] high upper bound for random values
John Kesapides8d942692019-02-26 14:52:12 +0000489 */
490 void fill_tensor(arm_compute::test::SimpleTensor<float> &tensor, std::random_device::result_type seed, float low, float high)
491 {
492 ARM_COMPUTE_ERROR_ON(tensor.data_type() != arm_compute::DataType::F32);
493 std::mt19937 gen(seed);
494 std::uniform_real_distribution<float> distribution(low, high);
495
496 for(int i = 0; i < tensor.num_elements(); ++i)
497 {
498 tensor[i] = distribution(gen);
499 }
500 }
501 /** Fill F16 tensor with Random values.
502 *
503 * Validate the given tensor against the reference result.
504 *
505 * @param[out] tensor The tensor we want to file
506 * @param[in] seed seed for the randomization function
507 * @param[in] low lower bound for random values
508 * @param[in] high upper bound for random values
John Kesapides8d942692019-02-26 14:52:12 +0000509 */
510 void fill_tensor(arm_compute::test::SimpleTensor<half> &tensor, std::random_device::result_type seed, half low, half high)
511 {
512 ARM_COMPUTE_ERROR_ON(tensor.data_type() != arm_compute::DataType::F16);
513 std::mt19937 gen(seed);
514 std::uniform_real_distribution<float> distribution(static_cast<half>(low), static_cast<half>(high));
515
516 for(int i = 0; i < tensor.num_elements(); ++i)
517 {
518 tensor[i] = static_cast<half>(distribution(gen));
519 }
520 }
521
522 /** Select relative tolerance.
523 *
524 * Select relative tolerance if not supplied by user.
525 *
526 * @return Appropriate relative tolerance.
527 */
528 virtual float relative_tolerance() = 0;
529
530 /** Select absolute tolerance.
531 *
532 * Select absolute tolerance if not supplied by user.
533 *
534 * @return Appropriate absolute tolerance.
535 */
536 virtual float absolute_tolerance() = 0;
537
538 /** Select tolerance number.
539 *
540 * Select tolerance number if not supplied by user.
541 *
542 * @return Appropriate tolerance number.
543 */
544 virtual float tolerance_number() = 0;
545
546 /** Validate the output versus the reference.
547 *
548 * @param[in] tensor Tensor result of the actual operation passed into the Accessor.
549 * @param[in] output Tensor result of the reference implementation.
John Kesapides8d942692019-02-26 14:52:12 +0000550 */
551 void validate(ITensor &tensor, arm_compute::test::SimpleTensor<D> output)
552 {
553 float user_relative_tolerance = _params.verification.relative_tolerance;
554 float user_absolute_tolerance = _params.verification.absolute_tolerance;
555 float user_tolerance_num = _params.verification.tolerance_number;
556 /* If no user input was provided override with defaults. */
557 if(user_relative_tolerance == -1)
558 {
559 user_relative_tolerance = relative_tolerance();
560 }
561
562 if(user_absolute_tolerance == -1)
563 {
564 user_absolute_tolerance = absolute_tolerance();
565 }
566
567 if(user_tolerance_num == -1)
568 {
569 user_tolerance_num = tolerance_number();
570 }
571
572 const arm_compute::test::validation::RelativeTolerance<float> rel_tolerance(user_relative_tolerance); /**< Relative tolerance */
573 const arm_compute::test::validation::AbsoluteTolerance<float> abs_tolerance(user_absolute_tolerance); /**< Absolute tolerance */
574 const float tolerance_num(user_tolerance_num); /**< Tolerance number */
575
576 arm_compute::test::validation::validate(arm_compute::test::Accessor(tensor), output, rel_tolerance, tolerance_num, abs_tolerance);
577 }
578
579 ExampleParams _params;
580};
581
582/** Generates appropriate convolution verify accessor
583 *
584 * @param[in] params User supplied parameters for convolution.
585 *
586 * @return A convolution verify accessor for the requested datatype.
587 */
588template <template <typename D> class VerifyAccessorT>
589inline std::unique_ptr<graph::ITensorAccessor> get_verify_accessor(ExampleParams params)
590{
591 switch(params.data_type)
592 {
593 case DataType::QASYMM8:
594 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000595 return std::make_unique<VerifyAccessorT<uint8_t>>(
John Kesapides8d942692019-02-26 14:52:12 +0000596 params);
597 }
598 case DataType::F16:
599 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000600 return std::make_unique<VerifyAccessorT<half>>(
John Kesapides8d942692019-02-26 14:52:12 +0000601 params);
602 }
603 case DataType::F32:
604 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000605 return std::make_unique<VerifyAccessorT<float>>(
John Kesapides8d942692019-02-26 14:52:12 +0000606 params);
607 }
608 default:
609 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
610 }
611}
612
613template <typename LayerT, typename OptionsT, template <typename D> class VerifyAccessorT>
614class GraphValidateExample : public ValidateExample
615{
616public:
617 GraphValidateExample(std::string name)
618 : graph(0, name)
619 {
620 }
621
622 virtual LayerT GraphFunctionLayer(ExampleParams &params) = 0;
623
624 bool do_setup(int argc, char **argv) override
625 {
626 CommandLineParser parser;
627
628 OptionsT Options(parser);
629
630 parser.parse(argc, argv);
631
632 ExampleParams params;
633
634 Options.consume_common_parameters(params);
635 Options.consume_parameters(params);
636
637 if(params.common_params.help)
638 {
639 parser.print_help(argv[0]);
640 return false;
641 }
642
643 Options.print_parameters(std::cout, params);
644 // Create input descriptor
645 const TensorShape input_shape = arm_compute::graph_utils::permute_shape(TensorShape(params.input.width, params.input.height, params.input.fm, params.input.batch),
646 DataLayout::NCHW, params.data_layout);
647 arm_compute::graph::TensorDescriptor input_descriptor = arm_compute::graph::TensorDescriptor(input_shape, params.data_type, params.input.quant_info, params.data_layout);
648
649 const PixelValue lower = PixelValue(params.input.range_low, params.data_type, params.input.quant_info);
650 const PixelValue upper = PixelValue(params.input.range_high, params.data_type, params.input.quant_info);
651
652 graph << params.common_params.target
653 << params.convolution_method
654 << params.depth_convolution_method
655 << arm_compute::graph::frontend::InputLayer(input_descriptor, get_accessor(params.input, lower, upper, 0))
656 << GraphFunctionLayer(params)
657 << arm_compute::graph::frontend::OutputLayer(get_verify_accessor<VerifyAccessorT>(params));
658
659 arm_compute::graph::GraphConfig config;
660 config.num_threads = params.common_params.threads;
661
662 graph.finalize(params.common_params.target, config);
663
664 return true;
665 }
666
667 void do_run() override
668 {
669 graph.run();
670 }
671
672 void do_teardown() override
673 {
674 }
675
676 arm_compute::graph::frontend::Stream graph;
677};
678
679} // graph_validate_utils
680} // arm_compute
Anitha Rajc63f8b02023-11-14 13:23:24 +0000681#endif // ACL_TESTS_VALIDATE_EXAMPLES_GRAPH_VALIDATE_UTILS_H