blob: ae52593b03d8148b7ed6524e24f52626e7057a3c [file] [log] [blame]
Georgios Pinitas28705162018-03-21 20:10:53 +00001/*
2 * Copyright (c) 2018 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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#ifndef __ARM_COMPUTE_GRAPH_BACKENDS_DETAIL_VALIDATE_HELPERS_H__
25#define __ARM_COMPUTE_GRAPH_BACKENDS_DETAIL_VALIDATE_HELPERS_H__
Georgios Pinitas28705162018-03-21 20:10:53 +000026
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010027#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"
Georgios Pinitas28705162018-03-21 20:10:53 +000031
32#include "arm_compute/core/Error.h"
Georgios Pinitascac13b12018-04-27 19:07:19 +010033#include "arm_compute/core/Helpers.h"
Georgios Pinitas28705162018-03-21 20:10:53 +000034#include "arm_compute/core/ITensorInfo.h"
35
36namespace arm_compute
37{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010038namespace graph
Georgios Pinitas28705162018-03-21 20:10:53 +000039{
40namespace backends
41{
42namespace 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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010050inline arm_compute::ITensorInfo *get_backing_tensor_info(arm_compute::graph::Tensor *tensor)
Georgios Pinitas28705162018-03-21 20:10:53 +000051{
52 return ((tensor == nullptr) || (tensor->handle() == nullptr)) ? nullptr : tensor->handle()->tensor().info();
53}
54
Georgios Pinitas087eaf62018-05-16 15:52:35 +010055/** Validates a Channel Shuffle layer node
56 *
57 * @tparam ChannelShuffleLayer Channel Shuffle layer function type
58 *
59 * @param[in] node Node to validate
60 *
61 * @return Status
62 */
63template <typename ChannelShuffleLayer>
64Status validate_channel_shuffle_layer(ChannelShuffleLayerNode &node)
65{
66 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating ChannelShuffle 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 = get_backing_tensor_info(node.input(0));
72 arm_compute::ITensorInfo *output = get_backing_tensor_info(node.output(0));
73 const unsigned int num_groups = node.num_groups();
74
75 return ChannelShuffleLayer::validate(input, output, num_groups);
76}
77
Georgios Pinitas28705162018-03-21 20:10:53 +000078/** Validates a Convolution layer node
79 *
80 * @tparam ConvolutionLayer Default Convolution layer function type
81 * @tparam DirectConvolutionLayer Direct Convolution layer function type
82 * @tparam GEMMConvolutionLayer GEMM Convolution layer function type
83 * @tparam WinogradConvolutionLayer Winograd Convolution layer function type
84 *
85 * @param[in] node Node to validate
86 *
87 * @return Status
88 */
89template <typename ConvolutionLayer, typename DirectConvolutionLayer, typename GEMMConvolutionLayer, typename WinogradConvolutionLayer>
90Status validate_convolution_layer(ConvolutionLayerNode &node)
91{
92 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating ConvolutionLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
93 ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 3);
94 ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
95
96 // Extract IO and info
Giorgio Arenabb54e4e2018-04-05 17:20:34 +010097 arm_compute::ITensorInfo *input = get_backing_tensor_info(node.input(0));
98 arm_compute::ITensorInfo *weights = get_backing_tensor_info(node.input(1));
99 arm_compute::ITensorInfo *biases = get_backing_tensor_info(node.input(2));
100 arm_compute::ITensorInfo *output = get_backing_tensor_info(node.output(0));
101
102 if(is_data_type_quantized_asymmetric(input->data_type()))
103 {
104 biases->set_data_type(DataType::S32);
105 }
106
107 const PadStrideInfo conv_info = node.convolution_info();
108 const ConvolutionMethod conv_algorithm = node.convolution_method();
Georgios Pinitase2220552018-07-20 13:23:44 +0100109 const bool fast_math = node.fast_math_hint() == FastMathHint::Enabled;
Georgios Pinitas28705162018-03-21 20:10:53 +0000110
111 // Validate function
112 Status status{};
113 switch(conv_algorithm)
114 {
Georgios Pinitase2220552018-07-20 13:23:44 +0100115 case ConvolutionMethod::Direct:
Georgios Pinitas28705162018-03-21 20:10:53 +0000116 status = DirectConvolutionLayer::validate(input, weights, biases, output, conv_info);
117 break;
118 case ConvolutionMethod::GEMM:
119 status = GEMMConvolutionLayer::validate(input, weights, biases, output, conv_info);
120 break;
Georgios Pinitase2220552018-07-20 13:23:44 +0100121 case ConvolutionMethod::Winograd:
122 status = WinogradConvolutionLayer::validate(input, weights, biases, output, conv_info, ActivationLayerInfo(), fast_math);
Georgios Pinitas28705162018-03-21 20:10:53 +0000123 break;
Georgios Pinitase2220552018-07-20 13:23:44 +0100124 case ConvolutionMethod::Default:
Georgios Pinitas54d6fae2018-05-10 15:50:14 +0100125 status = ConvolutionLayer::validate(input, weights, biases, output, conv_info);
126 break;
Georgios Pinitas28705162018-03-21 20:10:53 +0000127 default:
128 break;
129 }
130
131 // If validation fails try the Default approach
Georgios Pinitas54d6fae2018-05-10 15:50:14 +0100132 if(!bool(status))
Georgios Pinitas28705162018-03-21 20:10:53 +0000133 {
Giorgio Arena59631a12018-05-02 13:59:04 +0100134 status = ConvolutionLayer::validate(input, weights, biases, output, conv_info /*, fast_math*/);
Georgios Pinitas28705162018-03-21 20:10:53 +0000135 if(bool(status))
136 {
137 ARM_COMPUTE_LOG_GRAPH_INFO("Switched ConvolutionLayer method of node with ID : "
138 << node.id() << " and Name: " << node.name() << std::endl);
Georgios Pinitase2220552018-07-20 13:23:44 +0100139 node.set_convolution_method(ConvolutionMethod::Default);
Georgios Pinitas28705162018-03-21 20:10:53 +0000140 }
141 }
142
143 return status;
144}
145
146/** Validates a Depthwise Convolution layer node
147 *
148 * @tparam DepthwiseConvolutionLayer Default Depthwise Convolution layer type
149 * @tparam DepthwiseConvolutionLayer3x3 Optimized 3x3 Depthwise Convolution layer type
150 *
151 * @param[in] node Node to validate
152 *
153 * @return Status
154 */
155template <typename DepthwiseConvolutionLayer, typename DepthwiseConvolutionLayer3x3>
156Status validate_depthwise_convolution_layer(DepthwiseConvolutionLayerNode &node)
157{
158 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating DepthwiseConvolutionLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
159 ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 3);
160 ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1);
161
162 // Extract IO and info
163 arm_compute::ITensorInfo *weights = detail::get_backing_tensor_info(node.input(1));
164 const DepthwiseConvolutionMethod dwc_algorithm = node.depthwise_convolution_method();
165 ARM_COMPUTE_ERROR_ON(weights == nullptr);
166
167 // TODO (geopin01) : Switch when validation is implemented
168 // Validate function
Georgios Pinitase2220552018-07-20 13:23:44 +0100169 if((dwc_algorithm == DepthwiseConvolutionMethod::Optimized3x3) && (weights->tensor_shape()[get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::WIDTH)] != 3))
Georgios Pinitas28705162018-03-21 20:10:53 +0000170 {
171 ARM_COMPUTE_LOG_GRAPH_INFO("Switched DepthwiseConvolutionLayer method of node with ID : "
172 << node.id() << " and Name: " << node.name() << std::endl);
Georgios Pinitase2220552018-07-20 13:23:44 +0100173 node.set_depthwise_convolution_method(DepthwiseConvolutionMethod::Default);
Georgios Pinitas28705162018-03-21 20:10:53 +0000174 }
175
176 return Status{};
177}
178} // namespace detail
179} // namespace backends
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100180} // namespace graph
Georgios Pinitas28705162018-03-21 20:10:53 +0000181} // namespace arm_compute
182
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100183#endif /* __ARM_COMPUTE_GRAPH_BACKENDS_DETAIL_VALIDATE_HELPERS_H__ */