• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017-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/runtime/NEON/functions/NEConvolutionLayer.h"
25 
26 #include "arm_compute/core/PixelValue.h"
27 #include "arm_compute/core/Utils.h"
28 #include "arm_compute/core/Validate.h"
29 #include "arm_compute/runtime/NEON/NEScheduler.h"
30 #include "arm_compute/runtime/NEON/functions/NEDirectConvolutionLayer.h"
31 #include "arm_compute/runtime/NEON/functions/NEFFTConvolutionLayer.h"
32 #include "arm_compute/runtime/NEON/functions/NEGEMMConv2d.h"
33 #include "arm_compute/runtime/NEON/functions/NEGEMMConvolutionLayer.h"
34 #include "arm_compute/runtime/NEON/functions/NEWinogradConvolutionLayer.h"
35 
36 #include "support/MemorySupport.h"
37 
38 #include <cmath>
39 #include <tuple>
40 #include <utility>
41 
42 namespace arm_compute
43 {
NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)44 NEConvolutionLayer::NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) //NOLINT
45     : _memory_manager(std::move(memory_manager)),
46       _function()
47 {
48 }
49 
configure(ITensor * input,const ITensor * weights,const ITensor * biases,ITensor * output,const PadStrideInfo & conv_info,const WeightsInfo & weights_info,const Size2D & dilation,const ActivationLayerInfo & act_info,bool enable_fast_math,unsigned int num_groups)50 void NEConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
51                                    const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups)
52 {
53     // Perform validate step
54     ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
55     ARM_COMPUTE_UNUSED(num_groups);
56     ARM_COMPUTE_ERROR_THROW_ON(NEConvolutionLayer::validate(input->info(), weights->info(), ((biases != nullptr) ? biases->info() : nullptr), output->info(), conv_info, weights_info, dilation, act_info,
57                                                             enable_fast_math, num_groups));
58 
59     const Conv2dInfo info(conv_info, dilation, act_info, enable_fast_math, num_groups);
60     switch(NEConvolutionLayer::get_convolution_method(input->info(), weights->info(), output->info(), conv_info, weights_info, dilation, act_info, enable_fast_math))
61     {
62         case ConvolutionMethod::WINOGRAD:
63         {
64             auto f = arm_compute::support::cpp14::make_unique<NEWinogradConvolutionLayer>(_memory_manager);
65             f->configure(input, weights, biases, output, conv_info, act_info, enable_fast_math);
66             _function = std::move(f);
67             break;
68         }
69         case ConvolutionMethod::GEMM:
70         {
71             auto f = arm_compute::support::cpp14::make_unique<NEGEMMConvolutionLayer>(_memory_manager);
72             f->configure(input, weights, biases, output, conv_info, weights_info, dilation, act_info);
73             _function = std::move(f);
74             break;
75         }
76         case ConvolutionMethod::GEMM_CONV2D:
77         {
78             auto f = arm_compute::support::cpp14::make_unique<NEGEMMConv2d>(_memory_manager);
79             f->configure(input, weights, biases, output, info);
80             _function = std::move(f);
81             break;
82         }
83         case ConvolutionMethod::DIRECT:
84         {
85             auto f = arm_compute::support::cpp14::make_unique<NEDirectConvolutionLayer>(_memory_manager);
86             f->configure(input, weights, biases, output, conv_info, act_info);
87             _function = std::move(f);
88             break;
89         }
90         case ConvolutionMethod::FFT:
91         {
92             auto f = arm_compute::support::cpp14::make_unique<NEFFTConvolutionLayer>(_memory_manager);
93             f->configure(input, weights, biases, output, conv_info, act_info);
94             _function = std::move(f);
95             break;
96         }
97         default:
98             ARM_COMPUTE_ERROR("Not supported.");
99             break;
100     }
101 }
102 
validate(const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * biases,const ITensorInfo * output,const PadStrideInfo & conv_info,const WeightsInfo & weights_info,const Size2D & dilation,const ActivationLayerInfo & act_info,bool enable_fast_math,unsigned int num_groups)103 Status NEConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
104                                     const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups)
105 {
106     ARM_COMPUTE_RETURN_ERROR_ON_MSG((num_groups != 1), "Grouping (num_groups != 1) is not supported on NEON");
107 
108     const Conv2dInfo info(conv_info, dilation, act_info, enable_fast_math, num_groups);
109     switch(NEConvolutionLayer::get_convolution_method(input, weights, output, conv_info, weights_info, dilation, act_info, enable_fast_math))
110     {
111         case ConvolutionMethod::WINOGRAD:
112             ARM_COMPUTE_RETURN_ON_ERROR(NEWinogradConvolutionLayer::validate(input, weights, biases, output, conv_info, act_info, enable_fast_math));
113             break;
114         case ConvolutionMethod::GEMM:
115             ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMConvolutionLayer::validate(input, weights, biases, output, conv_info, weights_info, dilation, act_info));
116             break;
117         case ConvolutionMethod::GEMM_CONV2D:
118             ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMConv2d::validate(input, weights, biases, output, info));
119             break;
120         case ConvolutionMethod::DIRECT:
121             ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayer::validate(input, weights, biases, output, conv_info, act_info));
122             break;
123         case ConvolutionMethod::FFT:
124             ARM_COMPUTE_RETURN_ON_ERROR(NEFFTConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info));
125             break;
126         default:
127             ARM_COMPUTE_ERROR("Not supported.");
128             break;
129     }
130 
131     return Status{};
132 }
133 
get_convolution_method(const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * output,const PadStrideInfo & conv_info,const WeightsInfo & weights_info,const Size2D & dilation,const ActivationLayerInfo & act_info,bool enable_fast_math)134 ConvolutionMethod NEConvolutionLayer::get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights,
135                                                              const ITensorInfo *output, const PadStrideInfo &conv_info,
136                                                              const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math)
137 {
138     ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, weights);
139     ARM_COMPUTE_UNUSED(weights_info);
140 
141     const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
142     const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
143     const size_t idx_c = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
144 
145     const Conv2dInfo info(conv_info, dilation, act_info, enable_fast_math, 1);
146 
147     /* Input spatial dims, kernel size, IFM/OFM, conv info*/
148     using ConvolutionConfiguration = std::tuple<Size2D, Size2D, Size2D, PadStrideInfo>;
149     using ConfigurationMethod      = std::pair<ConvolutionConfiguration, ConvolutionMethod>;
150 
151     const std::vector<ConfigurationMethod> known_configs =
152     {
153         // Alexnet
154         ConfigurationMethod(ConvolutionConfiguration(Size2D(27U, 27U), Size2D(5U, 5U), Size2D(48U, 128U), PadStrideInfo(1U, 1U, 2U, 2U)), ConvolutionMethod::GEMM),
155         // VGG16 / VGG19
156         ConfigurationMethod(ConvolutionConfiguration(Size2D(224U, 224U), Size2D(3U, 3U), Size2D(3U, 64U), PadStrideInfo(1U, 1U, 1U, 1U)), ConvolutionMethod::GEMM),
157         // Mobilenet 224
158         ConfigurationMethod(ConvolutionConfiguration(Size2D(224U, 224U), Size2D(3U, 3U), Size2D(3U, 32U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR)), ConvolutionMethod::GEMM),
159         // Mobilenet 160
160         ConfigurationMethod(ConvolutionConfiguration(Size2D(160U, 160U), Size2D(3U, 3U), Size2D(3U, 24U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR)), ConvolutionMethod::GEMM)
161     };
162 
163     const auto find_config = [&](ConfigurationMethod c)
164     {
165         const ConvolutionConfiguration config = c.first;
166         const PadStrideInfo            info   = std::get<3>(config);
167 
168         return std::get<0>(config) == Size2D(input->dimension(idx_w), input->dimension(idx_h)) && std::get<1>(config) == Size2D(weights->dimension(idx_w), weights->dimension(idx_h))
169                && std::get<2>(config) == Size2D(weights->dimension(idx_c), weights->dimension(3)) && info.pad_top() == conv_info.pad_top() && info.pad_right() == conv_info.pad_right()
170                && info.pad_bottom() == conv_info.pad_bottom() && info.pad_left() == conv_info.pad_left() && info.stride() == conv_info.stride();
171     };
172 
173     std::vector<ConfigurationMethod>::const_iterator found;
174     if((found = std::find_if(known_configs.begin(), known_configs.end(), find_config)) != known_configs.end())
175     {
176         return (*found).second;
177     }
178 
179     if(dilation != Size2D(1U, 1U))
180     {
181         return ConvolutionMethod::GEMM;
182     }
183     else
184     {
185         // SRGAN
186         // Output might not be initialized when it is an internal tensor of the layer using the convolution
187         if(input->total_size() > 1e7 && (weights->dimension(idx_h) > 7)
188            && (NEDirectConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info)))
189         {
190             return ConvolutionMethod::DIRECT;
191         }
192         if((weights->dimension(idx_h) > 7) && (input->dimension(idx_c) > output->dimension(idx_c)) && (NEFFTConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info)))
193         {
194             return ConvolutionMethod::FFT;
195         }
196         if(input->dimension(idx_c) < 16)
197         {
198             return ConvolutionMethod::GEMM;
199         }
200 
201 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
202         // This heuristics only applies to F16 data type on A55r1
203         if(NEScheduler::get().cpu_info().get_cpu_model() == CPUModel::A55r1 && enable_fast_math && input->data_type() == DataType::F16)
204         {
205             // Exclude known bad winograd configs (and defaults to GEMM)
206             const std::vector<ConvolutionConfiguration> known_bad_winograd_f16_with_fastmath_configs =
207             {
208                 // Squeezenet_V1_1 fire2 and fire3
209                 ConvolutionConfiguration(Size2D(56U, 56U), Size2D(3U, 3U), Size2D(16U, 64U), PadStrideInfo(1U, 1U, 1U, 1U)),
210                 // Squeezenet_V1_1 fire6 and fire7
211                 ConvolutionConfiguration(Size2D(14U, 14U), Size2D(3U, 3U), Size2D(48U, 192U), PadStrideInfo(1U, 1U, 1U, 1U)),
212                 // Squeezenet_V1_1 fire8 and fire9
213                 ConvolutionConfiguration(Size2D(14U, 14U), Size2D(3U, 3U), Size2D(64U, 256U), PadStrideInfo(1U, 1U, 1U, 1U)),
214             };
215             const auto find_conv_config = [&](ConvolutionConfiguration c)
216             {
217                 const PadStrideInfo info = std::get<3>(c);
218 
219                 return std::get<0>(c) == Size2D(input->dimension(idx_w), input->dimension(idx_h)) && std::get<1>(c) == Size2D(weights->dimension(idx_w), weights->dimension(idx_h))
220                        && std::get<2>(c) == Size2D(weights->dimension(idx_c), weights->dimension(3)) && info.pad_top() == conv_info.pad_top() && info.pad_right() == conv_info.pad_right()
221                        && info.pad_bottom() == conv_info.pad_bottom() && info.pad_left() == conv_info.pad_left() && info.stride() == conv_info.stride();
222             };
223 
224             bool found_bad = std::find_if(known_bad_winograd_f16_with_fastmath_configs.begin(), known_bad_winograd_f16_with_fastmath_configs.end(),
225                                           find_conv_config)
226                              != known_bad_winograd_f16_with_fastmath_configs.end();
227             if(found_bad)
228             {
229                 return ConvolutionMethod::GEMM;
230             }
231         }
232 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
233         // For 1x1 convolutions run the default GEMM
234         if(weights->dimension(idx_w) == 1 && weights->dimension(idx_h) == 1)
235         {
236             return ConvolutionMethod::GEMM;
237         }
238 
239         if(bool(NEWinogradConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info, enable_fast_math)))
240         {
241             return ConvolutionMethod::WINOGRAD;
242         }
243         if(bool(NEGEMMConv2d::validate(input, weights, nullptr, output, info)))
244         {
245             return ConvolutionMethod::GEMM_CONV2D;
246         }
247         return ConvolutionMethod::GEMM;
248     }
249 }
250 
run()251 void NEConvolutionLayer::run()
252 {
253     prepare();
254     _function->run();
255 }
256 
prepare()257 void NEConvolutionLayer::prepare()
258 {
259     _function->prepare();
260 }
261 } // namespace arm_compute
262