• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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/NEGEMMConv2d.h"
25 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
26 #include "arm_compute/core/utils/quantization/AsymmHelpers.h"
27 #include "arm_compute/runtime/NEON/NEScheduler.h"
28 #include <set>
29 namespace arm_compute
30 {
31 namespace
32 {
calculate_output_stage_metadata(const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * output,const ActivationLayerInfo & act)33 GEMMLowpOutputStageInfo calculate_output_stage_metadata(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, const ActivationLayerInfo &act)
34 {
35     // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
36     // Extract and negate input and weights offset
37     const QuantizationInfo        iqinfo    = input->quantization_info();
38     const QuantizationInfo        wqinfo    = weights->quantization_info();
39     const QuantizationInfo        oqinfo    = (output->total_size() == 0) ? iqinfo : output->quantization_info();
40     const UniformQuantizationInfo uoqinfo   = oqinfo.uniform();
41     const DataType                data_type = input->data_type();
42     // Merge activation with output stage
43     const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
44                                                                                ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
45                                                                                ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
46                                                                              };
47     PixelValue type_min{};
48     PixelValue type_max{};
49     std::tie(type_min, type_max) = get_min_max(data_type);
50     int32_t min_activation = type_min.get<int32_t>();
51     int32_t max_activation = type_max.get<int32_t>();
52     if(supported_acts.count(act.activation()) != 0)
53     {
54         std::tie(min_activation, max_activation) = get_quantized_activation_min_max(act, data_type, uoqinfo);
55     }
56     GEMMLowpOutputStageInfo os_info;
57     os_info.type                     = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
58     os_info.gemmlowp_offset          = uoqinfo.offset;
59     os_info.gemmlowp_min_bound       = min_activation;
60     os_info.gemmlowp_max_bound       = max_activation;
61     os_info.is_quantized_per_channel = (weights->data_type() == DataType::QSYMM8_PER_CHANNEL);
62     quantization::calculate_quantized_multipliers(iqinfo, wqinfo, oqinfo, os_info);
63     return os_info;
64 }
init_assembly_metadata(const Conv2dInfo & info,bool is_indirect)65 AsmGemmInfo init_assembly_metadata(const Conv2dInfo &info, bool is_indirect)
66 {
67     AsmGemmInfo asm_info;
68     asm_info.method                  = is_indirect ? AsmConvMethod::Indirect : AsmConvMethod::Conv;
69     asm_info.ps_info                 = info.conv_info;
70     asm_info.activation_info         = info.act_info;
71     asm_info.depth_output_gemm3d     = true;
72     asm_info.reinterpret_input_as_3d = true;
73     asm_info.padding_top             = info.conv_info.pad_top();
74     asm_info.padding_left            = info.conv_info.pad_left();
75     asm_info.padding_value           = 0.f;
76     asm_info.negated_offsets         = false;
77     return asm_info;
78 }
79 } // namespace
80 
NEGEMMConv2d(const std::shared_ptr<IMemoryManager> & memory_manager)81 NEGEMMConv2d::NEGEMMConv2d(const std::shared_ptr<IMemoryManager> &memory_manager)
82     : _gemm_asm_func(memory_manager), _activation_func(), _weights_permute_func(), _original_weights(nullptr), _permuted_weights(), _is_prepared(false), _run_activation(false)
83 {
84 }
configure(ITensor * input,const ITensor * weights,const ITensor * biases,ITensor * output,const Conv2dInfo & info)85 void NEGEMMConv2d::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const Conv2dInfo &info)
86 {
87     ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
88     ARM_COMPUTE_ERROR_THROW_ON(NEGEMMConv2d::validate(input->info(),
89                                                       weights->info(),
90                                                       biases != nullptr ? biases->info() : nullptr,
91                                                       output->info(),
92                                                       info));
93     _original_weights = weights;
94     _weights_permute_func.configure(weights, &_permuted_weights, PermutationVector{ 3, 0, 1, 2 });
95 
96     // Configure assembly dispatch
97     AsmGemmInfo asm_info = init_assembly_metadata(info, false);
98     if(is_data_type_quantized(input->info()->data_type()))
99     {
100         asm_info.output_stage = calculate_output_stage_metadata(input->info(), weights->info(), output->info(), info.act_info);
101     }
102     _gemm_asm_func.configure(input, &_permuted_weights, biases, output, asm_info);
103 
104     // Configure activation
105     if(info.act_info.enabled() && !_gemm_asm_func.is_activation_supported(info.act_info))
106     {
107         _activation_func.configure(output, nullptr, info.act_info);
108         _run_activation = true;
109     }
110 }
validate(const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * biases,const ITensorInfo * output,const Conv2dInfo & info)111 Status NEGEMMConv2d::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const Conv2dInfo &info)
112 {
113     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
114     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::BFLOAT16, DataType::F16, DataType::F32);
115     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL, DataType::BFLOAT16, DataType::F16, DataType::F32);
116     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
117     ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.num_groups > 1, "Grouping (num_groups != 1) is not supported on NEON");
118     ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_layout() != DataLayout::NHWC, "Data layout supported is NHWC");
119     const DataType    data_type = input->data_type();
120     const TensorShape i_shape   = input->tensor_shape();
121     const TensorShape w_shape   = weights->tensor_shape();
122     ARM_COMPUTE_RETURN_ERROR_ON(w_shape[0] != i_shape[0]);
123     ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
124     // Validate biases
125     if(biases != nullptr)
126     {
127         if(is_data_type_quantized_asymmetric(data_type))
128         {
129             ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
130         }
131         else if(data_type == DataType::BFLOAT16)
132         {
133             ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::F32);
134         }
135         else
136         {
137             ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
138         }
139         ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
140         ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
141     }
142 
143     AsmGemmInfo asm_info = init_assembly_metadata(info, false);
144     ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMAssemblyDispatch::validate(input, weights, biases, output, asm_info));
145     return Status{};
146 }
run()147 void NEGEMMConv2d::run()
148 {
149     prepare();
150 
151     _gemm_asm_func.run();
152     if(_run_activation)
153     {
154         _activation_func.run();
155     }
156 }
prepare()157 void NEGEMMConv2d::prepare()
158 {
159     if(!_is_prepared)
160     {
161         _permuted_weights.allocator()->allocate();
162         _weights_permute_func.run();
163         _original_weights->mark_as_unused();
164         _is_prepared = true;
165     }
166 }
167 } // namespace arm_compute
168