xref: /aosp_15_r20/external/ComputeLibrary/arm_compute/graph/backends/ValidateHelpers.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-2021 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_GRAPH_BACKENDS_DETAIL_VALIDATE_HELPERS_H
25 #define ARM_COMPUTE_GRAPH_BACKENDS_DETAIL_VALIDATE_HELPERS_H
26 
27 #include "arm_compute/graph/Logger.h"
28 #include "arm_compute/graph/Tensor.h"
29 #include "arm_compute/graph/Types.h"
30 #include "arm_compute/graph/nodes/Nodes.h"
31 
32 #include "arm_compute/core/Error.h"
33 #include "arm_compute/core/Helpers.h"
34 #include "arm_compute/core/ITensorInfo.h"
35 
36 namespace arm_compute
37 {
38 namespace graph
39 {
40 namespace backends
41 {
42 namespace detail
43 {
44 /** Returns backing tensor info of a given tensor
45  *
46  * @param[in] tensor Tensor to extract the backing tensor from
47  *
48  * @return Backing tensor tensor info if present else nullptr
49  */
get_backing_tensor_info(arm_compute::graph::Tensor * tensor)50 inline arm_compute::ITensorInfo *get_backing_tensor_info(arm_compute::graph::Tensor *tensor)
51 {
52     return ((tensor == nullptr) || (tensor->handle() == nullptr)) ? nullptr : tensor->handle()->tensor().info();
53 }
54 
55 /** Validates a ArgMinMax layer node
56  *
57  * @tparam ArgMinMax layer function type
58  *
59  * @param[in] node Node to validate
60  *
61  * @return Status
62  */
63 template <typename ArgMinMaxLayer>
validate_arg_min_max_layer(ArgMinMaxLayerNode & node)64 Status validate_arg_min_max_layer(ArgMinMaxLayerNode &node)
65 {
66     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating ArgMinMaxLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
67     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 1);
68     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
69 
70     // Extract IO and info
71     arm_compute::ITensorInfo *input  = detail::get_backing_tensor_info(node.input(0));
72     arm_compute::ITensorInfo *output = get_backing_tensor_info(node.output(0));
73 
74     // Validate function
75     return ArgMinMaxLayer::validate(input, node.axis(), output, node.reduction_operation());
76 }
77 
78 /** Validates a Bounding Box Transform layer node
79  *
80  * @tparam BoundingBoxTransformLayer  Bounding Box Transform layer function type
81  *
82  * @param[in] node Node to validate
83  *
84  * @return Status
85  */
86 template <typename BoundingBoxTransformLayer>
validate_bounding_box_transform_layer(BoundingBoxTransformLayerNode & node)87 Status validate_bounding_box_transform_layer(BoundingBoxTransformLayerNode &node)
88 {
89     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating BoundingBoxTransformLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
90     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 2);
91     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
92 
93     // Extract IO and info
94     arm_compute::ITensorInfo      *input     = get_backing_tensor_info(node.input(0));
95     arm_compute::ITensorInfo      *deltas    = get_backing_tensor_info(node.input(1));
96     arm_compute::ITensorInfo      *output    = get_backing_tensor_info(node.output(0));
97     const BoundingBoxTransformInfo bbox_info = node.info();
98 
99     return BoundingBoxTransformLayer::validate(input, output, deltas, bbox_info);
100 }
101 
102 /** Validates a Channel Shuffle layer node
103  *
104  * @tparam ChannelShuffleLayer  Channel Shuffle layer function type
105  *
106  * @param[in] node Node to validate
107  *
108  * @return Status
109  */
110 template <typename ChannelShuffleLayer>
validate_channel_shuffle_layer(ChannelShuffleLayerNode & node)111 Status validate_channel_shuffle_layer(ChannelShuffleLayerNode &node)
112 {
113     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating ChannelShuffle node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
114     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 1);
115     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
116 
117     // Extract IO and info
118     arm_compute::ITensorInfo *input      = get_backing_tensor_info(node.input(0));
119     arm_compute::ITensorInfo *output     = get_backing_tensor_info(node.output(0));
120     const unsigned int        num_groups = node.num_groups();
121 
122     return ChannelShuffleLayer::validate(input, output, num_groups);
123 }
124 
125 /** Validates a Convolution layer node
126  *
127  * @tparam ConvolutionLayer          Default Convolution layer function type
128  * @tparam DirectConvolutionLayer    Direct Convolution layer function type
129  * @tparam GEMMConvolutionLayer      GEMM Convolution layer function type
130  * @tparam WinogradConvolutionLayer  Winograd Convolution layer function type
131  *
132  * @param[in] node Node to validate
133  *
134  * @return Status
135  */
136 template <typename ConvolutionLayer, typename DirectConvolutionLayer, typename GEMMConvolutionLayer, typename WinogradConvolutionLayer>
validate_convolution_layer(ConvolutionLayerNode & node)137 Status validate_convolution_layer(ConvolutionLayerNode &node)
138 {
139     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating ConvolutionLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
140     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 3);
141     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
142 
143     // Extract IO and info
144     arm_compute::ITensorInfo *input   = get_backing_tensor_info(node.input(0));
145     arm_compute::ITensorInfo *weights = get_backing_tensor_info(node.input(1));
146     arm_compute::ITensorInfo *biases  = get_backing_tensor_info(node.input(2));
147     arm_compute::ITensorInfo *output  = get_backing_tensor_info(node.output(0));
148 
149     if(is_data_type_quantized_asymmetric(input->data_type()))
150     {
151         biases->set_data_type(DataType::S32);
152     }
153 
154     const PadStrideInfo     conv_info      = node.convolution_info();
155     const ConvolutionMethod conv_algorithm = node.convolution_method();
156     const bool              fast_math      = node.fast_math_hint() == FastMathHint::Enabled;
157     const unsigned int      num_groups     = node.num_groups();
158 
159     // Validate function
160     Status status{};
161     switch(conv_algorithm)
162     {
163         case ConvolutionMethod::Direct:
164             ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups != 1, "DirectConvolutionLayer does not support grouping!");
165             status = DirectConvolutionLayer::validate(input, weights, biases, output, conv_info);
166             break;
167         case ConvolutionMethod::GEMM:
168             status = GEMMConvolutionLayer::validate(input, weights, biases, output, conv_info,
169                                                     WeightsInfo(), Size2D(1, 1), ActivationLayerInfo(), num_groups);
170             break;
171         case ConvolutionMethod::Winograd:
172             ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups != 1, "WinogradConvolutionLayer does not support grouping!");
173             status = WinogradConvolutionLayer::validate(input, weights, biases, output, conv_info, ActivationLayerInfo(), fast_math);
174             break;
175         case ConvolutionMethod::Default:
176             status = ConvolutionLayer::validate(input, weights, biases, output, conv_info,
177                                                 WeightsInfo(), Size2D(1, 1), ActivationLayerInfo(), fast_math, num_groups);
178             break;
179         default:
180             ARM_COMPUTE_RETURN_ERROR_MSG("Unsupported convolution method");
181     }
182 
183     return status;
184 }
185 
186 /** Validates a Convolution layer node
187  *
188  * @tparam GEMMConvolutionLayer      GEMM Convolution layer function type
189  *
190  * @param[in] node Node to validate
191  *
192  * @return Status
193  */
194 template <typename GEMMConvolutionLayer>
validate_fused_convolution_with_post_op(FusedConvolutionWithPostOpNode & node)195 Status validate_fused_convolution_with_post_op(FusedConvolutionWithPostOpNode &node)
196 {
197     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating fused ConvolutionLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
198     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 4);
199     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
200 
201     // Extract IO and info
202     arm_compute::ITensorInfo *input   = get_backing_tensor_info(node.input(0));
203     arm_compute::ITensorInfo *weights = get_backing_tensor_info(node.input(1));
204     arm_compute::ITensorInfo *biases  = get_backing_tensor_info(node.input(2));
205     arm_compute::ITensorInfo *output  = get_backing_tensor_info(node.output(0));
206 
207     if(is_data_type_quantized_asymmetric(input->data_type()))
208     {
209         biases->set_data_type(DataType::S32);
210     }
211 
212     const PadStrideInfo conv_info = node.convolution_info();
213     //const ConvolutionMethod conv_algorithm = node.convolution_method();
214     //const bool              fast_math      = node.fast_math_hint() == FastMathHint::Enabled;
215     const unsigned int num_groups = node.num_groups();
216 
217     // Validate function
218     return GEMMConvolutionLayer::validate(input, weights, biases, output, conv_info,
219                                           WeightsInfo(), Size2D(1, 1), ActivationLayerInfo(), num_groups);
220 }
221 
222 /** Validates a Depthwise Convolution layer node
223  *
224  * @tparam DepthwiseConvolutionLayer    Default Depthwise Convolution layer type
225  *
226  * @param[in] node Node to validate
227  *
228  * @return Status
229  */
230 template <typename DepthwiseConvolutionLayer>
validate_depthwise_convolution_layer(DepthwiseConvolutionLayerNode & node)231 Status validate_depthwise_convolution_layer(DepthwiseConvolutionLayerNode &node)
232 {
233     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating DepthwiseConvolutionLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
234     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 3);
235     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
236 
237     // Extract IO and info
238     arm_compute::ITensorInfo *input   = detail::get_backing_tensor_info(node.input(0));
239     arm_compute::ITensorInfo *weights = detail::get_backing_tensor_info(node.input(1));
240     arm_compute::ITensorInfo *biases  = get_backing_tensor_info(node.input(2));
241     arm_compute::ITensorInfo *output  = get_backing_tensor_info(node.output(0));
242 
243     const PadStrideInfo              conv_info        = node.convolution_info();
244     const DepthwiseConvolutionMethod dwc_algorithm    = node.depthwise_convolution_method();
245     const int                        depth_multiplier = node.depth_multiplier();
246 
247     // Validate function
248     Status status{};
249     switch(dwc_algorithm)
250     {
251         case DepthwiseConvolutionMethod::Default:
252         case DepthwiseConvolutionMethod::Optimized3x3:
253             status = DepthwiseConvolutionLayer::validate(input, weights, biases, output, conv_info, depth_multiplier);
254             break;
255         default:
256             ARM_COMPUTE_RETURN_ERROR_MSG("Unsupported depthwise convolution method");
257     }
258 
259     return status;
260 }
261 /** Validates a depth to space layer node
262  *
263  * @tparam DequantizationLayer Dequantize layer type
264  *
265  * @param[in] node Node to validate
266  *
267  * @return Status
268  */
269 template <typename DepthToSpaceLayer>
validate_depth_to_space_layer(DepthToSpaceLayerNode & node)270 Status validate_depth_to_space_layer(DepthToSpaceLayerNode &node)
271 {
272     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating DetectionOutputLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
273     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 1);
274     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
275 
276     // Extract IO and info
277     arm_compute::ITensorInfo *input  = get_backing_tensor_info(node.input(0));
278     arm_compute::ITensorInfo *output = get_backing_tensor_info(node.output(0));
279 
280     return DepthToSpaceLayer::validate(input, output, node.block_shape());
281 }
282 /** Validates a dequantize layer node
283  *
284  * @tparam DequantizationLayer Dequantize layer type
285  *
286  * @param[in] node Node to validate
287  *
288  * @return Status
289  */
290 template <typename DequantizationLayer>
validate_dequantization_layer(DequantizationLayerNode & node)291 Status validate_dequantization_layer(DequantizationLayerNode &node)
292 {
293     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating DetectionOutputLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
294     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 1);
295     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
296 
297     // Extract IO and info
298     arm_compute::ITensorInfo *input  = get_backing_tensor_info(node.input(0));
299     arm_compute::ITensorInfo *output = get_backing_tensor_info(node.output(0));
300 
301     return DequantizationLayer::validate(input, output);
302 }
303 /** Validates a detection output layer node
304  *
305  * @tparam DetectionOutputLayer DetectionOutput layer type
306  *
307  * @param[in] node Node to validate
308  *
309  * @return Status
310  */
311 template <typename DetectionOutputLayer>
validate_detection_output_layer(DetectionOutputLayerNode & node)312 Status validate_detection_output_layer(DetectionOutputLayerNode &node)
313 {
314     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating DetectionOutputLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
315     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 3);
316     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
317 
318     // Extract IO and info
319     arm_compute::ITensorInfo      *input0      = get_backing_tensor_info(node.input(0));
320     arm_compute::ITensorInfo      *input1      = get_backing_tensor_info(node.input(1));
321     arm_compute::ITensorInfo      *input2      = get_backing_tensor_info(node.input(2));
322     arm_compute::ITensorInfo      *output      = get_backing_tensor_info(node.output(0));
323     const DetectionOutputLayerInfo detect_info = node.detection_output_info();
324 
325     return DetectionOutputLayer::validate(input0, input1, input2, output, detect_info);
326 }
327 /** Validates a detection post process layer node
328  *
329  * @tparam DetectionPostProcessLayer DetectionOutput layer type
330  *
331  * @param[in] node Node to validate
332  *
333  * @return Status
334  */
335 template <typename DetectionPostProcessLayer>
validate_detection_post_process_layer(DetectionPostProcessLayerNode & node)336 Status validate_detection_post_process_layer(DetectionPostProcessLayerNode &node)
337 {
338     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating DetectionPostProcessLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
339     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 3);
340     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 4);
341 
342     // Extract IO and info
343     arm_compute::ITensorInfo           *input0      = get_backing_tensor_info(node.input(0));
344     arm_compute::ITensorInfo           *input1      = get_backing_tensor_info(node.input(1));
345     arm_compute::ITensorInfo           *input2      = get_backing_tensor_info(node.input(2));
346     arm_compute::ITensorInfo           *output0     = get_backing_tensor_info(node.output(0));
347     arm_compute::ITensorInfo           *output1     = get_backing_tensor_info(node.output(1));
348     arm_compute::ITensorInfo           *output2     = get_backing_tensor_info(node.output(2));
349     arm_compute::ITensorInfo           *output3     = get_backing_tensor_info(node.output(3));
350     const DetectionPostProcessLayerInfo detect_info = node.detection_post_process_info();
351 
352     return DetectionPostProcessLayer::validate(input0, input1, input2, output0, output1, output2, output3, detect_info);
353 }
354 
355 /** Validates a Generate Proposals layer node
356  *
357  * @tparam GenerateProposalsLayer Generate Proposals layer type
358  *
359  * @param[in] node Node to validate
360  *
361  * @return Status
362  */
363 template <typename GenerateProposalsLayer>
validate_generate_proposals_layer(GenerateProposalsLayerNode & node)364 Status validate_generate_proposals_layer(GenerateProposalsLayerNode &node)
365 {
366     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating GenerateProposalsLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
367     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 3);
368     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 3);
369 
370     // Extract IO and info
371     arm_compute::ITensorInfo   *scores              = detail::get_backing_tensor_info(node.input(0));
372     arm_compute::ITensorInfo   *deltas              = detail::get_backing_tensor_info(node.input(1));
373     arm_compute::ITensorInfo   *anchors             = detail::get_backing_tensor_info(node.input(2));
374     arm_compute::ITensorInfo   *proposals           = get_backing_tensor_info(node.output(0));
375     arm_compute::ITensorInfo   *scores_out          = get_backing_tensor_info(node.output(1));
376     arm_compute::ITensorInfo   *num_valid_proposals = get_backing_tensor_info(node.output(2));
377     const GenerateProposalsInfo info                = node.info();
378 
379     return GenerateProposalsLayer::validate(scores, deltas, anchors, proposals, scores_out, num_valid_proposals, info);
380 }
381 
382 /** Validates a L2Normalization layer node
383  *
384  * @tparam L2Normalization layer type
385  *
386  * @param[in] node Node to validate
387  *
388  * @return Status
389  */
390 template <typename L2NormalizeLayer>
validate_l2_normalize_layer(L2NormalizeLayerNode & node)391 Status validate_l2_normalize_layer(L2NormalizeLayerNode &node)
392 {
393     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating L2NormalizeLayerNode node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
394     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 1);
395     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
396 
397     // Extract IO and info
398     arm_compute::ITensorInfo *input   = detail::get_backing_tensor_info(node.input(0));
399     arm_compute::ITensorInfo *output  = get_backing_tensor_info(node.output(0));
400     int                       axis    = node.axis();
401     float                     epsilon = node.epsilon();
402 
403     // Validate function
404     return L2NormalizeLayer::validate(input, output, axis, epsilon);
405 }
406 
407 /** Validates a NormalizePlanarYUV layer node
408  *
409  * @tparam NormalizePlanarYUVLayer layer type
410  *
411  * @param[in] node Node to validate
412  *
413  * @return Status
414  */
415 template <typename NormalizePlanarYUVLayer>
validate_normalize_planar_yuv_layer(NormalizePlanarYUVLayerNode & node)416 Status validate_normalize_planar_yuv_layer(NormalizePlanarYUVLayerNode &node)
417 {
418     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating NormalizePlanarYUVLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
419     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 3);
420     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
421 
422     // Extract IO and info
423     arm_compute::ITensorInfo *input  = detail::get_backing_tensor_info(node.input(0));
424     arm_compute::ITensorInfo *mean   = detail::get_backing_tensor_info(node.input(1));
425     arm_compute::ITensorInfo *std    = detail::get_backing_tensor_info(node.input(2));
426     arm_compute::ITensorInfo *output = get_backing_tensor_info(node.output(0));
427 
428     // Validate function
429     return NormalizePlanarYUVLayer::validate(input, output, mean, std);
430 }
431 
432 /** Validates a pad layer node
433  *
434  * @tparam PadLayer Pad layer type
435  *
436  * @param[in] node Node to validate
437  *
438  * @return Status
439  */
440 template <typename PadLayer>
validate_pad_layer(PadLayerNode & node)441 Status validate_pad_layer(PadLayerNode &node)
442 {
443     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating PadLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
444     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 1);
445     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
446 
447     // Extract IO and info
448     arm_compute::ITensorInfo *input   = get_backing_tensor_info(node.input(0));
449     arm_compute::ITensorInfo *output  = get_backing_tensor_info(node.output(0));
450     const PaddingList        &padding = node.padding();
451 
452     return PadLayer::validate(input, output, padding);
453 }
454 
455 /** Validates a permute layer node
456  *
457  * @tparam PermuteLayer Permute layer type
458  *
459  * @param[in] node Node to validate
460  *
461  * @return Status
462  */
463 template <typename PermuteLayer>
validate_permute_layer(PermuteLayerNode & node)464 Status validate_permute_layer(PermuteLayerNode &node)
465 {
466     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating PermuteLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
467     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 1);
468     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
469 
470     // Extract IO and info
471     arm_compute::ITensorInfo *input  = get_backing_tensor_info(node.input(0));
472     arm_compute::ITensorInfo *output = get_backing_tensor_info(node.output(0));
473     const PermutationVector &perm   = node.permutation_vector();
474 
475     return PermuteLayer::validate(input, output, perm);
476 }
477 
478 /** Validates a PRelu layer node
479  *
480  * @tparam PReluLayer PRelu layer type
481  *
482  * @param[in] node Node to validate
483  *
484  * @return Status
485  */
486 template <typename PReluLayer>
validate_prelu_layer(PReluLayerNode & node)487 Status validate_prelu_layer(PReluLayerNode &node)
488 {
489     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating PRelu node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
490     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 2);
491     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
492 
493     // Extract IO and info
494     arm_compute::ITensorInfo *input  = get_backing_tensor_info(node.input(0));
495     arm_compute::ITensorInfo *alpha  = get_backing_tensor_info(node.input(1));
496     arm_compute::ITensorInfo *output = get_backing_tensor_info(node.output(0));
497 
498     return PReluLayer::validate(input, alpha, output);
499 }
500 
501 /** Validates a priorbox layer node
502  *
503  * @tparam PriorBoxLayer PriorBox layer type
504  *
505  * @param[in] node Node to validate
506  *
507  * @return Status
508  */
509 template <typename PriorBoxLayer>
validate_priorbox_layer(PriorBoxLayerNode & node)510 Status validate_priorbox_layer(PriorBoxLayerNode &node)
511 {
512     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating PriorBoxLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
513     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 2);
514     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
515 
516     // Extract IO and info
517     arm_compute::ITensorInfo *input0     = get_backing_tensor_info(node.input(0));
518     arm_compute::ITensorInfo *input1     = get_backing_tensor_info(node.input(1));
519     arm_compute::ITensorInfo *output     = get_backing_tensor_info(node.output(0));
520     const PriorBoxLayerInfo   prior_info = node.priorbox_info();
521 
522     return PriorBoxLayer::validate(input0, input1, output, prior_info);
523 }
524 
525 /** Validates a Quantization layer node
526  *
527  * @tparam QuantizationLayer Quantization layer type
528  *
529  * @param[in] node Node to validate
530  *
531  * @return Status
532  */
533 template <typename QuantizationLayer>
validate_quantization_layer(QuantizationLayerNode & node)534 Status validate_quantization_layer(QuantizationLayerNode &node)
535 {
536     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating QuantizationLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
537     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 1);
538     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
539 
540     // Extract input and output
541     arm_compute::ITensorInfo *input  = detail::get_backing_tensor_info(node.input(0));
542     arm_compute::ITensorInfo *output = get_backing_tensor_info(node.output(0));
543 
544     // Validate function
545     return QuantizationLayer::validate(input, output);
546 }
547 
548 /** Validates a Reduction operation layer node
549  *
550  * @tparam ReductionLayer Reduction layer type
551  *
552  * @param[in] node Node to validate
553  *
554  * @return Status
555  */
556 template <typename ReductionLayer>
validate_reduction_operation_layer(ReductionLayerNode & node)557 Status validate_reduction_operation_layer(ReductionLayerNode &node)
558 {
559     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating ReductionLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
560 
561     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 1);
562     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
563 
564     // Extract input and output
565     arm_compute::ITensorInfo *input  = detail::get_backing_tensor_info(node.input(0));
566     arm_compute::ITensorInfo *output = get_backing_tensor_info(node.output(0));
567 
568     // Validate function
569     return ReductionLayer::validate(input, output, node.axis(), node.op(), node.keep_dims());
570 }
571 
572 /** Validates a Reorg layer node
573  *
574  * @tparam ReorgLayer Reorg layer type
575  *
576  * @param[in] node Node to validate
577  *
578  * @return Status
579  */
580 template <typename ReorgLayer>
validate_reorg_layer(ReorgLayerNode & node)581 Status validate_reorg_layer(ReorgLayerNode &node)
582 {
583     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating ReorgLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
584     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 1);
585     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
586 
587     // Extract input and output
588     arm_compute::ITensorInfo *input  = detail::get_backing_tensor_info(node.input(0));
589     arm_compute::ITensorInfo *output = get_backing_tensor_info(node.output(0));
590 
591     // Validate function
592     return ReorgLayer::validate(input, output, node.stride());
593 }
594 
595 /** Validates a Reshape layer node
596  *
597  * @tparam ReshapeLayer Reshape layer type
598  *
599  * @param[in] node Node to validate
600  *
601  * @return Status
602  */
603 template <typename ReshapeLayer>
validate_reshape_layer(ReshapeLayerNode & node)604 Status validate_reshape_layer(ReshapeLayerNode &node)
605 {
606     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating ReshapeLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
607     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 1);
608     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
609 
610     // Extract input and output
611     arm_compute::ITensorInfo *input  = detail::get_backing_tensor_info(node.input(0));
612     arm_compute::ITensorInfo *output = detail::get_backing_tensor_info(node.output(0));
613 
614     // Validate function
615     return ReshapeLayer::validate(input, output);
616 }
617 
618 /** Validates a ROI Align layer node
619  *
620  * @tparam ROIAlignLayer ROIAlign layer type
621  *
622  * @param[in] node Node to validate
623  *
624  * @return Status
625  */
626 template <typename ROIAlignLayer>
validate_roi_align_layer(ROIAlignLayerNode & node)627 Status validate_roi_align_layer(ROIAlignLayerNode &node)
628 {
629     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating ROIAlignLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
630     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 2);
631     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
632 
633     // Extract input and output
634     arm_compute::ITensorInfo *input     = detail::get_backing_tensor_info(node.input(0));
635     arm_compute::ITensorInfo *rois      = detail::get_backing_tensor_info(node.input(1));
636     arm_compute::ITensorInfo *output    = detail::get_backing_tensor_info(node.output(0));
637     const ROIPoolingLayerInfo &pool_info = node.pooling_info();
638 
639     // Validate function
640     return ROIAlignLayer::validate(input, rois, output, pool_info);
641 }
642 
643 /** Validates a Slice layer node
644  *
645  * @tparam SliceLayer Slice layer function type
646  *
647  * @param[in] node Node to validate
648  *
649  * @return Status
650  */
651 template <typename SliceLayer>
validate_slice_layer(SliceLayerNode & node)652 Status validate_slice_layer(SliceLayerNode &node)
653 {
654     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating Slice node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
655     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 1);
656     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
657 
658     // Extract IO and info
659     arm_compute::ITensorInfo *input  = get_backing_tensor_info(node.input(0));
660     arm_compute::ITensorInfo *output = get_backing_tensor_info(node.output(0));
661     const Coordinates         starts = node.starts();
662     const Coordinates         ends   = node.ends();
663 
664     return SliceLayer::validate(input, output, starts, ends);
665 }
666 
667 /** Validates a Strided Slice layer node
668  *
669  * @tparam StridedSliceLayer Strided Slice layer function type
670  *
671  * @param[in] node Node to validate
672  *
673  * @return Status
674  */
675 template <typename StridedSliceLayer>
validate_strided_slice_layer(StridedSliceLayerNode & node)676 Status validate_strided_slice_layer(StridedSliceLayerNode &node)
677 {
678     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating StridedSlice node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
679     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 1);
680     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
681 
682     // Extract IO and info
683     arm_compute::ITensorInfo   *input   = get_backing_tensor_info(node.input(0));
684     arm_compute::ITensorInfo   *output  = get_backing_tensor_info(node.output(0));
685     const Coordinates           starts  = node.starts();
686     const Coordinates           ends    = node.ends();
687     const BiStrides             strides = node.strides();
688     const StridedSliceLayerInfo info    = node.strided_slice_info();
689 
690     return StridedSliceLayer::validate(input, output, starts, ends, strides, info.begin_mask(), info.end_mask(), info.shrink_axis_mask());
691 }
692 
693 /** Validates a element-wise layer node
694  *
695  * @param[in] node Node to validate
696  *
697  * @return Status
698  */
699 template <typename EltwiseLayerFunctions>
validate_eltwise_Layer(EltwiseLayerNode & node)700 Status validate_eltwise_Layer(EltwiseLayerNode &node)
701 {
702     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating EltwiseLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
703     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 2);
704     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
705 
706     // Extract input and output
707     const arm_compute::ITensorInfo *input1         = detail::get_backing_tensor_info(node.input(0));
708     const arm_compute::ITensorInfo *input2         = detail::get_backing_tensor_info(node.input(1));
709     const arm_compute::ITensorInfo *output         = get_backing_tensor_info(node.output(0));
710     const EltwiseOperation          eltwise_op     = node.eltwise_operation();
711     const ConvertPolicy             convert_policy = node.convert_policy();
712     const RoundingPolicy            round_policy   = node.rounding_policy();
713     const ActivationLayerInfo       act_info       = node.fused_activation();
714     const QuantizationInfo          quant_info     = node.output_quant_info();
715 
716     // Validate function
717     if(eltwise_op == EltwiseOperation::Add)
718     {
719         return EltwiseLayerFunctions::ArithmeticAddition::validate(input1, input2, output, convert_policy, act_info);
720     }
721     else if(eltwise_op == EltwiseOperation::Sub)
722     {
723         return EltwiseLayerFunctions::ArithmeticSubtraction::validate(input1, input2, output, convert_policy, act_info);
724     }
725     else if(eltwise_op == EltwiseOperation::Mul)
726     {
727         return EltwiseLayerFunctions::PixelWiseMultiplication::validate(input1, input2, output, 1.0f, convert_policy, round_policy, act_info);
728     }
729     else if(eltwise_op == EltwiseOperation::Max)
730     {
731         return EltwiseLayerFunctions::ElementwiseMax::validate(input1, input2, output, act_info);
732     }
733     else if(eltwise_op == EltwiseOperation::Div)
734     {
735         return EltwiseLayerFunctions::ArithmeticDivision::validate(input1, input2, output, act_info);
736     }
737     else
738     {
739         ARM_COMPUTE_ERROR("Unsupported element-wise operation!");
740     }
741     return Status{};
742 }
743 /** Validates a unary element-wise layer node
744  *
745  * @param[in] node Node to validate
746  *
747  * @return Status
748  */
749 template <typename UnaryEltwiseLayerFunctions>
validate_unary_eltwise_layer(UnaryEltwiseLayerNode & node)750 Status validate_unary_eltwise_layer(UnaryEltwiseLayerNode &node)
751 {
752     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating EltwiseLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
753     ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 1);
754     ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
755 
756     // Extract input and output
757     arm_compute::ITensorInfo   *input      = detail::get_backing_tensor_info(node.input(0));
758     arm_compute::ITensorInfo   *output     = get_backing_tensor_info(node.output(0));
759     const UnaryEltwiseOperation eltwise_op = node.eltwise_descriptor().op;
760 
761     // Validate function
762     if(eltwise_op == UnaryEltwiseOperation::Exp)
763     {
764         return UnaryEltwiseLayerFunctions::ExpLayer::validate(input, output);
765     }
766     else
767     {
768         ARM_COMPUTE_ERROR("Unsupported unary element-wise operation!");
769     }
770 
771     return Status{};
772 }
773 } // namespace detail
774 } // namespace backends
775 } // namespace graph
776 } // namespace arm_compute
777 
778 #endif /* ARM_COMPUTE_GRAPH_BACKENDS_DETAIL_VALIDATE_HELPERS_H */
779