blob: 10eece99eb325eee3fe791137437236b3b0233c1 [file] [log] [blame]
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001/*
2 * Copyright (c) 2021 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 "src/runtime/cpu/operators/CpuGemmDirectConv2d.h"
25
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"
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010029#include "src/core/helpers/MemoryHelpers.h"
30#include "src/runtime/cpu/utils/CpuAuxTensorHandler.h"
31
32#include "support/Cast.h"
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010033
34#include <set>
35
36namespace arm_compute
37{
38namespace cpu
39{
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010040using namespace arm_compute::experimental;
41using namespace arm_compute::utils::cast;
42
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010043namespace
44{
45GEMMLowpOutputStageInfo calculate_output_stage_metadata(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const ActivationLayerInfo &act)
46{
47 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
48 // Extract and negate input and weights offset
49 const QuantizationInfo iqinfo = src->quantization_info();
50 const QuantizationInfo wqinfo = weights->quantization_info();
51 const QuantizationInfo oqinfo = (dst->total_size() == 0) ? iqinfo : dst->quantization_info();
52 const UniformQuantizationInfo uoqinfo = oqinfo.uniform();
53 const DataType data_type = src->data_type();
54 // Merge activation with output stage
55 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
56 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
57 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
58 };
Sang-Hoon Parkb3be4572021-05-18 10:46:00 +010059 PixelValue type_min{};
60 PixelValue type_max{};
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010061 std::tie(type_min, type_max) = get_min_max(data_type);
Michele Di Giorgio8ae3cda2021-06-07 15:30:26 +010062 int32_t min_activation = type_min.get<int32_t>();
63 int32_t max_activation = type_max.get<int32_t>();
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010064 if(supported_acts.count(act.activation()) != 0)
65 {
66 std::tie(min_activation, max_activation) = get_quantized_activation_min_max(act, data_type, uoqinfo);
67 }
68 GEMMLowpOutputStageInfo os_info;
69 os_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
70 os_info.gemmlowp_offset = uoqinfo.offset;
71 os_info.gemmlowp_min_bound = min_activation;
72 os_info.gemmlowp_max_bound = max_activation;
73 os_info.is_quantized_per_channel = (weights->data_type() == DataType::QSYMM8_PER_CHANNEL);
74 quantization::calculate_quantized_multipliers(iqinfo, wqinfo, oqinfo, os_info);
75 return os_info;
76}
77cpu::AsmGemmInfo init_assembly_metadata(const Conv2dInfo &info, bool is_indirect)
78{
79 cpu::AsmGemmInfo asm_info;
80 asm_info.method = is_indirect ? cpu::AsmConvMethod::Indirect : cpu::AsmConvMethod::Conv;
81 asm_info.ps_info = info.conv_info;
82 asm_info.activation_info = info.act_info;
83 asm_info.depth_output_gemm3d = true;
84 asm_info.reinterpret_input_as_3d = true;
85 asm_info.padding_top = info.conv_info.pad_top();
86 asm_info.padding_left = info.conv_info.pad_left();
87 asm_info.padding_value = 0.f;
88 asm_info.negated_offsets = false;
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010089 asm_info.fast_mode = info.enable_fast_math;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010090 return asm_info;
91}
92} // namespace
93
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010094CpuGemmDirectConv2d::CpuGemmDirectConv2d()
95 : _gemm_asm_func(std::make_unique<CpuGemmAssemblyDispatch>()),
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010096 _activation_func(std::make_unique<CpuActivation>()),
97 _weights_permute_func(std::make_unique<CpuPermute>()),
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010098 _aux_mem(AuxTensorIdx::Count),
99 _perm_weights(),
100 _run_activation(false),
101 _is_prepared(false)
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100102{
103}
104
105CpuGemmDirectConv2d::~CpuGemmDirectConv2d() = default;
106
107void CpuGemmDirectConv2d::configure(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const Conv2dInfo &info)
108{
109 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst);
110 ARM_COMPUTE_ERROR_THROW_ON(CpuGemmDirectConv2d::validate(src,
111 weights,
112 biases != nullptr ? biases : nullptr,
113 dst,
114 info));
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100115 _run_activation = info.act_info.enabled() && !_gemm_asm_func->is_activation_supported(info.act_info);
116 _is_prepared = false;
117
118 _weights_permute_func->configure(weights, &_perm_weights, PermutationVector{ 3, 0, 1, 2 });
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100119
120 // Configure assembly dispatch
121 cpu::AsmGemmInfo asm_info = init_assembly_metadata(info, false);
122 if(is_data_type_quantized(src->data_type()))
123 {
124 asm_info.output_stage = calculate_output_stage_metadata(src, weights, dst, info.act_info);
125 }
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100126 _gemm_asm_func->configure(src, &_perm_weights, biases, dst, asm_info);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100127
128 // Configure activation
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100129 if(_run_activation)
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100130 {
131 _activation_func->configure(dst, nullptr, info.act_info);
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100132 }
133
134 // Add auxiliary memory requirements of the assembly dispatch
135 auto asm_mem_req = _gemm_asm_func->workspace();
136 _aux_mem[AsmGemmWorkspace] = asm_mem_req[AsmGemmWorkspace];
137 _aux_mem[Pretranspose] = asm_mem_req[Pretranspose];
138
139 if(_aux_mem[Pretranspose].size > 0)
140 {
141 // Release permuted weights at the of prepare as they are further transposed by the assembly dispatch
142 _aux_mem[PermutedWeights] = MemoryInfo(offset_int_vec(PermutedWeights), MemoryLifetime::Prepare, weights->total_size());
143 }
144 else
145 {
146 _aux_mem[PermutedWeights] = MemoryInfo(offset_int_vec(PermutedWeights), MemoryLifetime::Persistent, weights->total_size());
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100147 }
148}
149Status CpuGemmDirectConv2d::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const Conv2dInfo &info)
150{
151 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, weights, dst);
152 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::BFLOAT16, DataType::F16, DataType::F32);
153 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);
154 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, weights);
155 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.num_groups > 1, "Grouping (num_groups != 1) is not supported on Neon");
156 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->data_layout() != DataLayout::NHWC, "Data layout supported is NHWC");
157 const DataType data_type = src->data_type();
158 const TensorShape i_shape = src->tensor_shape();
159 const TensorShape w_shape = weights->tensor_shape();
160 ARM_COMPUTE_RETURN_ERROR_ON(w_shape[0] != i_shape[0]);
161 ARM_COMPUTE_RETURN_ERROR_ON(info.dilation != Size2D(1U, 1U));
162 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
163 // Validate biases
164 if(biases != nullptr)
165 {
166 if(is_data_type_quantized_asymmetric(data_type))
167 {
168 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
169 }
170 else if(data_type == DataType::BFLOAT16)
171 {
172 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::F32);
173 }
174 else
175 {
176 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, biases);
177 }
178 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
179 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
180 }
181
182 cpu::AsmGemmInfo asm_info = init_assembly_metadata(info, false);
183 ARM_COMPUTE_RETURN_ON_ERROR(cpu::CpuGemmAssemblyDispatch::validate(src, weights, biases, dst, asm_info));
184 return Status{};
185}
186void CpuGemmDirectConv2d::run(ITensorPack &tensors)
187{
188 prepare(tensors);
189
190 _gemm_asm_func->run(tensors);
191 if(_run_activation)
192 {
193 _activation_func->run(tensors);
194 }
Michele Di Giorgio8ae3cda2021-06-07 15:30:26 +0100195}
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100196
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100197void CpuGemmDirectConv2d::prepare(ITensorPack &tensors)
198{
199 if(!_is_prepared)
200 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100201 const ITensor *weights = tensors.get_const_tensor(ACL_SRC_1);
202 ITensor *weights_aux = utils::cast::polymorphic_cast<ITensor *>(tensors.get_tensor(offset_int_vec(PermutedWeights)));
203 ARM_COMPUTE_ERROR_ON_NULLPTR(weights, weights_aux);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100204
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100205 CpuAuxTensorHandler permuted_weights(_perm_weights, *weights_aux);
206 ITensorPack permute_tensors{ { ACL_SRC, weights }, { ACL_DST, permuted_weights.get() } };
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100207 _weights_permute_func->run(permute_tensors);
208
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100209 tensors.add_const_tensor(ACL_SRC_1, permuted_weights.get());
210 // Call prepare of assembly dispatch
211 _gemm_asm_func->prepare(tensors);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100212
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100213 _is_prepared = true;
214 }
215}
216
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100217experimental::MemoryRequirements CpuGemmDirectConv2d::workspace() const
218{
219 return _aux_mem;
220}
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100221} // namespace cpu
222} // namespace arm_compute