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