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