Sang-Hoon Park | d89e2fa | 2021-05-17 17:04:50 +0100 | [diff] [blame] | 1 | /* |
| 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" |
| 29 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 30 | #include "src/runtime/cpu/operators/CpuActivation.h" |
| 31 | #include "src/runtime/cpu/operators/CpuPermute.h" |
| 32 | #include "src/runtime/cpu/operators/internal/CpuGemmAssemblyDispatch.h" |
| 33 | |
| 34 | #include <set> |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace cpu |
| 39 | { |
| 40 | namespace |
| 41 | { |
| 42 | GEMMLowpOutputStageInfo calculate_output_stage_metadata(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const ActivationLayerInfo &act) |
| 43 | { |
| 44 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 45 | // Extract and negate input and weights offset |
| 46 | const QuantizationInfo iqinfo = src->quantization_info(); |
| 47 | const QuantizationInfo wqinfo = weights->quantization_info(); |
| 48 | const QuantizationInfo oqinfo = (dst->total_size() == 0) ? iqinfo : dst->quantization_info(); |
| 49 | const UniformQuantizationInfo uoqinfo = oqinfo.uniform(); |
| 50 | const DataType data_type = src->data_type(); |
| 51 | // Merge activation with output stage |
| 52 | const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU, |
| 53 | ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, |
| 54 | ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU |
| 55 | }; |
Sang-Hoon Park | b3be457 | 2021-05-18 10:46:00 +0100 | [diff] [blame] | 56 | PixelValue type_min{}; |
| 57 | PixelValue type_max{}; |
Sang-Hoon Park | d89e2fa | 2021-05-17 17:04:50 +0100 | [diff] [blame] | 58 | std::tie(type_min, type_max) = get_min_max(data_type); |
Michele Di Giorgio | 8ae3cda | 2021-06-07 15:30:26 +0100 | [diff] [blame] | 59 | int32_t min_activation = type_min.get<int32_t>(); |
| 60 | int32_t max_activation = type_max.get<int32_t>(); |
Sang-Hoon Park | d89e2fa | 2021-05-17 17:04:50 +0100 | [diff] [blame] | 61 | if(supported_acts.count(act.activation()) != 0) |
| 62 | { |
| 63 | std::tie(min_activation, max_activation) = get_quantized_activation_min_max(act, data_type, uoqinfo); |
| 64 | } |
| 65 | GEMMLowpOutputStageInfo os_info; |
| 66 | os_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT; |
| 67 | os_info.gemmlowp_offset = uoqinfo.offset; |
| 68 | os_info.gemmlowp_min_bound = min_activation; |
| 69 | os_info.gemmlowp_max_bound = max_activation; |
| 70 | os_info.is_quantized_per_channel = (weights->data_type() == DataType::QSYMM8_PER_CHANNEL); |
| 71 | quantization::calculate_quantized_multipliers(iqinfo, wqinfo, oqinfo, os_info); |
| 72 | return os_info; |
| 73 | } |
| 74 | cpu::AsmGemmInfo init_assembly_metadata(const Conv2dInfo &info, bool is_indirect) |
| 75 | { |
| 76 | cpu::AsmGemmInfo asm_info; |
| 77 | asm_info.method = is_indirect ? cpu::AsmConvMethod::Indirect : cpu::AsmConvMethod::Conv; |
| 78 | asm_info.ps_info = info.conv_info; |
| 79 | asm_info.activation_info = info.act_info; |
| 80 | asm_info.depth_output_gemm3d = true; |
| 81 | asm_info.reinterpret_input_as_3d = true; |
| 82 | asm_info.padding_top = info.conv_info.pad_top(); |
| 83 | asm_info.padding_left = info.conv_info.pad_left(); |
| 84 | asm_info.padding_value = 0.f; |
| 85 | asm_info.negated_offsets = false; |
| 86 | return asm_info; |
| 87 | } |
| 88 | } // namespace |
| 89 | |
Michele Di Giorgio | 8ae3cda | 2021-06-07 15:30:26 +0100 | [diff] [blame] | 90 | CpuGemmDirectConv2d::CpuGemmDirectConv2d(const std::shared_ptr<IMemoryManager> &memory_manager) |
| 91 | : _gemm_asm_func(std::make_unique<CpuGemmAssemblyDispatch>(memory_manager)), |
Sang-Hoon Park | d89e2fa | 2021-05-17 17:04:50 +0100 | [diff] [blame] | 92 | _activation_func(std::make_unique<CpuActivation>()), |
| 93 | _weights_permute_func(std::make_unique<CpuPermute>()), |
| 94 | _permuted_weights_info(), |
| 95 | _permuted_weights(std::make_unique<Tensor>()) |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | CpuGemmDirectConv2d::~CpuGemmDirectConv2d() = default; |
| 100 | |
| 101 | void CpuGemmDirectConv2d::configure(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const Conv2dInfo &info) |
| 102 | { |
| 103 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst); |
| 104 | ARM_COMPUTE_ERROR_THROW_ON(CpuGemmDirectConv2d::validate(src, |
| 105 | weights, |
| 106 | biases != nullptr ? biases : nullptr, |
| 107 | dst, |
| 108 | info)); |
| 109 | _original_weights_info = weights; |
| 110 | _weights_permute_func->configure(weights, &_permuted_weights_info, PermutationVector{ 3, 0, 1, 2 }); |
| 111 | |
| 112 | // Configure assembly dispatch |
| 113 | cpu::AsmGemmInfo asm_info = init_assembly_metadata(info, false); |
| 114 | if(is_data_type_quantized(src->data_type())) |
| 115 | { |
| 116 | asm_info.output_stage = calculate_output_stage_metadata(src, weights, dst, info.act_info); |
| 117 | } |
| 118 | _gemm_asm_func->configure(src, &_permuted_weights_info, biases, dst, asm_info); |
| 119 | |
| 120 | // Configure activation |
| 121 | if(info.act_info.enabled() && !_gemm_asm_func->is_activation_supported(info.act_info)) |
| 122 | { |
| 123 | _activation_func->configure(dst, nullptr, info.act_info); |
| 124 | _run_activation = true; |
| 125 | } |
| 126 | } |
| 127 | Status CpuGemmDirectConv2d::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const Conv2dInfo &info) |
| 128 | { |
| 129 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, weights, dst); |
| 130 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::BFLOAT16, DataType::F16, DataType::F32); |
| 131 | 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); |
| 132 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, weights); |
| 133 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.num_groups > 1, "Grouping (num_groups != 1) is not supported on Neon"); |
| 134 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->data_layout() != DataLayout::NHWC, "Data layout supported is NHWC"); |
| 135 | const DataType data_type = src->data_type(); |
| 136 | const TensorShape i_shape = src->tensor_shape(); |
| 137 | const TensorShape w_shape = weights->tensor_shape(); |
| 138 | ARM_COMPUTE_RETURN_ERROR_ON(w_shape[0] != i_shape[0]); |
| 139 | ARM_COMPUTE_RETURN_ERROR_ON(info.dilation != Size2D(1U, 1U)); |
| 140 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
| 141 | // Validate biases |
| 142 | if(biases != nullptr) |
| 143 | { |
| 144 | if(is_data_type_quantized_asymmetric(data_type)) |
| 145 | { |
| 146 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 147 | } |
| 148 | else if(data_type == DataType::BFLOAT16) |
| 149 | { |
| 150 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::F32); |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, biases); |
| 155 | } |
| 156 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3)); |
| 157 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 158 | } |
| 159 | |
| 160 | cpu::AsmGemmInfo asm_info = init_assembly_metadata(info, false); |
| 161 | ARM_COMPUTE_RETURN_ON_ERROR(cpu::CpuGemmAssemblyDispatch::validate(src, weights, biases, dst, asm_info)); |
| 162 | return Status{}; |
| 163 | } |
| 164 | void CpuGemmDirectConv2d::run(ITensorPack &tensors) |
| 165 | { |
| 166 | prepare(tensors); |
| 167 | |
| 168 | _gemm_asm_func->run(tensors); |
| 169 | if(_run_activation) |
| 170 | { |
| 171 | _activation_func->run(tensors); |
| 172 | } |
Michele Di Giorgio | 8ae3cda | 2021-06-07 15:30:26 +0100 | [diff] [blame] | 173 | } |
Sang-Hoon Park | d89e2fa | 2021-05-17 17:04:50 +0100 | [diff] [blame] | 174 | |
Michele Di Giorgio | 8ae3cda | 2021-06-07 15:30:26 +0100 | [diff] [blame] | 175 | void CpuGemmDirectConv2d::allocate_permuted_weights() |
| 176 | { |
| 177 | // TODO: This function will be removed when memory injection is implemeted. |
| 178 | ARM_COMPUTE_ERROR_ON(_permuted_weights == nullptr); |
| 179 | _permuted_weights->allocator()->free(); |
| 180 | _permuted_weights->allocator()->init(_permuted_weights_info); |
| 181 | _permuted_weights->allocator()->allocate(); |
Sang-Hoon Park | d89e2fa | 2021-05-17 17:04:50 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void CpuGemmDirectConv2d::prepare(ITensorPack &tensors) |
| 185 | { |
| 186 | if(!_is_prepared) |
| 187 | { |
Michele Di Giorgio | 8ae3cda | 2021-06-07 15:30:26 +0100 | [diff] [blame] | 188 | allocate_permuted_weights(); |
Sang-Hoon Park | d89e2fa | 2021-05-17 17:04:50 +0100 | [diff] [blame] | 189 | ITensorPack permute_tensors |
| 190 | { |
| 191 | { TensorType::ACL_SRC, tensors.get_const_tensor(TensorType::ACL_SRC_1) }, |
| 192 | { TensorType::ACL_DST, _permuted_weights.get() }, |
| 193 | }; |
| 194 | |
| 195 | _weights_permute_func->run(permute_tensors); |
| 196 | |
| 197 | tensors.get_const_tensor(TensorType::ACL_SRC_1)->mark_as_unused(); |
| 198 | |
| 199 | // switch the original tensor with permuted tensor |
| 200 | tensors.add_const_tensor(TensorType::ACL_SRC_1, _permuted_weights.get()); |
| 201 | _is_prepared = true; |
| 202 | } |
| 203 | } |
| 204 | |
Sang-Hoon Park | d89e2fa | 2021-05-17 17:04:50 +0100 | [diff] [blame] | 205 | } // namespace cpu |
| 206 | } // namespace arm_compute |