blob: 8fa81b19077a2e2c1b51d6ab297bbd82ea7865c6 [file] [log] [blame]
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001/*
Jonathan Deakin0ffc88b2023-03-02 15:15:15 +00002 * Copyright (c) 2021-2023 Arm Limited.
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01003 *
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 Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/operators/CpuGemmDirectConv2d.h"
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010025
26#include "arm_compute/core/utils/misc/ShapeCalculator.h"
27#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
28#include "arm_compute/runtime/FunctionDescriptors.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029
ramelg013ae3d882021-09-12 23:07:47 +010030#include "src/common/utils/Log.h"
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010031#include "src/core/helpers/MemoryHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010032#include "src/cpu/utils/CpuAuxTensorHandler.h"
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010033#include "support/Cast.h"
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010034
35#include <set>
36
37namespace arm_compute
38{
39namespace cpu
40{
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010041using namespace arm_compute::experimental;
42using namespace arm_compute::utils::cast;
43
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010044namespace
45{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010046GEMMLowpOutputStageInfo calculate_output_stage_metadata(const ITensorInfo *src,
47 const ITensorInfo *weights,
48 const ITensorInfo *dst,
49 const ActivationLayerInfo &act)
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010050{
51 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
52 // Extract and negate input and weights offset
53 const QuantizationInfo iqinfo = src->quantization_info();
54 const QuantizationInfo wqinfo = weights->quantization_info();
55 const QuantizationInfo oqinfo = (dst->total_size() == 0) ? iqinfo : dst->quantization_info();
56 const UniformQuantizationInfo uoqinfo = oqinfo.uniform();
57 const DataType data_type = src->data_type();
58 // Merge activation with output stage
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010059 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = {
60 ActivationLayerInfo::ActivationFunction::RELU, ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
61 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU};
62 PixelValue type_min{};
63 PixelValue type_max{};
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010064 std::tie(type_min, type_max) = get_min_max(data_type);
Milos Puzovic13b623e2022-07-27 17:53:21 +000065 int32_t min_activation = type_min.get<int32_t>();
66 int32_t max_activation = type_max.get<int32_t>();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 if (supported_acts.count(act.activation()) != 0)
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010068 {
69 std::tie(min_activation, max_activation) = get_quantized_activation_min_max(act, data_type, uoqinfo);
70 }
71 GEMMLowpOutputStageInfo os_info;
72 os_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
73 os_info.gemmlowp_offset = uoqinfo.offset;
74 os_info.gemmlowp_min_bound = min_activation;
75 os_info.gemmlowp_max_bound = max_activation;
76 os_info.is_quantized_per_channel = (weights->data_type() == DataType::QSYMM8_PER_CHANNEL);
77 quantization::calculate_quantized_multipliers(iqinfo, wqinfo, oqinfo, os_info);
78 return os_info;
79}
80cpu::AsmGemmInfo init_assembly_metadata(const Conv2dInfo &info, bool is_indirect)
81{
82 cpu::AsmGemmInfo asm_info;
83 asm_info.method = is_indirect ? cpu::AsmConvMethod::Indirect : cpu::AsmConvMethod::Conv;
84 asm_info.ps_info = info.conv_info;
85 asm_info.activation_info = info.act_info;
86 asm_info.depth_output_gemm3d = true;
87 asm_info.reinterpret_input_as_3d = true;
88 asm_info.padding_top = info.conv_info.pad_top();
89 asm_info.padding_left = info.conv_info.pad_left();
90 asm_info.padding_value = 0.f;
91 asm_info.negated_offsets = false;
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010092 asm_info.fast_mode = info.enable_fast_math;
Milos Puzovic13b623e2022-07-27 17:53:21 +000093 asm_info.fixed_format = info.weights_info.weight_format() != WeightFormat::UNSPECIFIED;
94 asm_info.weight_format = info.weights_info.weight_format();
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010095 return asm_info;
96}
97} // namespace
98
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010099CpuGemmDirectConv2d::CpuGemmDirectConv2d()
100 : _gemm_asm_func(std::make_unique<CpuGemmAssemblyDispatch>()),
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100101 _activation_func(std::make_unique<CpuActivation>()),
102 _weights_permute_func(std::make_unique<CpuPermute>()),
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100103 _aux_mem(AuxTensorIdx::Count),
104 _perm_weights(),
105 _run_activation(false),
106 _is_prepared(false)
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100107{
108}
109
110CpuGemmDirectConv2d::~CpuGemmDirectConv2d() = default;
111
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100112void CpuGemmDirectConv2d::configure(const ITensorInfo *src,
113 const ITensorInfo *weights,
114 const ITensorInfo *biases,
115 ITensorInfo *dst,
116 const Conv2dInfo &info)
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100117{
118 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119 ARM_COMPUTE_ERROR_THROW_ON(
120 CpuGemmDirectConv2d::validate(src, weights, biases != nullptr ? biases : nullptr, dst, info));
ramelg013ae3d882021-09-12 23:07:47 +0100121 ARM_COMPUTE_LOG_PARAMS(src, weights, biases, dst, info);
122
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100123 _run_activation = info.act_info.enabled() && !_gemm_asm_func->is_activation_supported(info.act_info);
124 _is_prepared = false;
125
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 _weights_permute_func->configure(weights, &_perm_weights, PermutationVector{3, 0, 1, 2});
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100127
128 // Configure assembly dispatch
129 cpu::AsmGemmInfo asm_info = init_assembly_metadata(info, false);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100130 if (is_data_type_quantized(src->data_type()))
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100131 {
132 asm_info.output_stage = calculate_output_stage_metadata(src, weights, dst, info.act_info);
133 }
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100134 _gemm_asm_func->configure(src, &_perm_weights, biases, dst, asm_info);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100135
136 // Configure activation
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100137 if (_run_activation)
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100138 {
139 _activation_func->configure(dst, nullptr, info.act_info);
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100140 }
141
142 // Add auxiliary memory requirements of the assembly dispatch
143 auto asm_mem_req = _gemm_asm_func->workspace();
144 _aux_mem[AsmGemmWorkspace] = asm_mem_req[AsmGemmWorkspace];
145 _aux_mem[Pretranspose] = asm_mem_req[Pretranspose];
146
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100147 if (_aux_mem[Pretranspose].size > 0)
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100148 {
149 // Release permuted weights at the of prepare as they are further transposed by the assembly dispatch
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100150 _aux_mem[PermutedWeights] =
151 MemoryInfo(offset_int_vec(PermutedWeights), MemoryLifetime::Prepare, weights->total_size());
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100152 }
153 else
154 {
Milos Puzovic13b623e2022-07-27 17:53:21 +0000155 // We must permute weights if they are WeightFormat::UNSPECIFIED
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100156 if (info.weights_info.weight_format() == WeightFormat::UNSPECIFIED)
157 _aux_mem[PermutedWeights] =
158 MemoryInfo(offset_int_vec(PermutedWeights), MemoryLifetime::Persistent, weights->total_size());
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100159 }
160}
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100161Status CpuGemmDirectConv2d::validate(const ITensorInfo *src,
162 const ITensorInfo *weights,
163 const ITensorInfo *biases,
164 const ITensorInfo *dst,
165 const Conv2dInfo &info)
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100166{
167 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, weights, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100168 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
169 DataType::BFLOAT16, DataType::F16, DataType::F32);
170 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
171 DataType::QSYMM8_PER_CHANNEL, DataType::BFLOAT16,
172 DataType::F16, DataType::F32);
173 if (!is_fixed_format(info.weights_info.weight_format()))
Jonathan Deakin0ffc88b2023-03-02 15:15:15 +0000174 {
175 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, weights);
176 }
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100177 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.num_groups > 1, "Grouping (num_groups != 1) is not supported on Neon");
178 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->data_layout() != DataLayout::NHWC, "Data layout supported is NHWC");
179 const DataType data_type = src->data_type();
180 const TensorShape i_shape = src->tensor_shape();
181 const TensorShape w_shape = weights->tensor_shape();
182 ARM_COMPUTE_RETURN_ERROR_ON(w_shape[0] != i_shape[0]);
183 ARM_COMPUTE_RETURN_ERROR_ON(info.dilation != Size2D(1U, 1U));
184 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
185 // Validate biases
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100186 if (biases != nullptr)
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100187 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100188 if (is_data_type_quantized_asymmetric(data_type))
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100189 {
190 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
191 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100192 else if (data_type == DataType::BFLOAT16)
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100193 {
194 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::F32);
195 }
196 else
197 {
198 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, biases);
199 }
200 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
201 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
202 }
203
204 cpu::AsmGemmInfo asm_info = init_assembly_metadata(info, false);
205 ARM_COMPUTE_RETURN_ON_ERROR(cpu::CpuGemmAssemblyDispatch::validate(src, weights, biases, dst, asm_info));
206 return Status{};
207}
208void CpuGemmDirectConv2d::run(ITensorPack &tensors)
209{
210 prepare(tensors);
211
212 _gemm_asm_func->run(tensors);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100213 if (_run_activation)
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100214 {
Michalis Spyrou16aa4742022-06-29 15:13:45 +0100215 ITensor *io = tensors.get_tensor(ACL_DST);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100216 ITensorPack pack{{ACL_SRC, io}, {ACL_DST, io}};
Michalis Spyrou16aa4742022-06-29 15:13:45 +0100217 _activation_func->run(pack);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100218 }
Michele Di Giorgio8ae3cda2021-06-07 15:30:26 +0100219}
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100220
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100221void CpuGemmDirectConv2d::prepare(ITensorPack &tensors)
222{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100223 if (!_is_prepared)
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100224 {
Milos Puzovic13b623e2022-07-27 17:53:21 +0000225 // If we are using fixed-format kernel the weights are already reshaped
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100226 if (_gemm_asm_func && _gemm_asm_func->isVarWeightsKernel())
Milos Puzovic13b623e2022-07-27 17:53:21 +0000227 {
228 _gemm_asm_func->prepare(tensors);
229 _is_prepared = true;
230 return;
231 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100232 const ITensor *weights = tensors.get_const_tensor(ACL_SRC_1);
233 ITensor *weights_aux =
234 utils::cast::polymorphic_cast<ITensor *>(tensors.get_tensor(offset_int_vec(PermutedWeights)));
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100235 ARM_COMPUTE_ERROR_ON_NULLPTR(weights, weights_aux);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100236
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100237 CpuAuxTensorHandler permuted_weights(_perm_weights, *weights_aux);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100238 ITensorPack permute_tensors{{ACL_SRC, weights}, {ACL_DST, permuted_weights.get()}};
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100239 _weights_permute_func->run(permute_tensors);
240
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100241 tensors.add_const_tensor(ACL_SRC_1, permuted_weights.get());
242 // Call prepare of assembly dispatch
243 _gemm_asm_func->prepare(tensors);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100244
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100245 _is_prepared = true;
246 }
247}
248
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100249experimental::MemoryRequirements CpuGemmDirectConv2d::workspace() const
250{
251 return _aux_mem;
252}
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100253} // namespace cpu
Milos Puzovic13b623e2022-07-27 17:53:21 +0000254} // namespace arm_compute