Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | b66aa3b | 2020-01-10 14:44:13 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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/CL/functions/CLFullyConnectedLayer.h" |
| 25 | |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Size2D.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Validate.h" |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/utils/misc/Cast.h" |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [diff] [blame] | 32 | #include "support/MemorySupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | |
| 34 | #include <algorithm> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 36 | namespace arm_compute |
| 37 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 38 | using namespace arm_compute::misc::shape_calculator; |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 39 | using namespace arm_compute::utils::cast; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 40 | |
| 41 | namespace |
| 42 | { |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 43 | Status construct_gemmlowp_output_stage(const ITensorInfo &input, const ITensorInfo &weights, const ITensorInfo &output, |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 44 | GEMMLowpOutputStageInfo &gemmlowp_output_stage, ActivationLayerInfo activation_info) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 45 | { |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 46 | gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT; |
| 47 | gemmlowp_output_stage.gemmlowp_offset = 0; |
| 48 | gemmlowp_output_stage.gemmlowp_multiplier = 0; |
| 49 | gemmlowp_output_stage.gemmlowp_shift = 0; |
| 50 | |
Sang-Hoon Park | b66aa3b | 2020-01-10 14:44:13 +0000 | [diff] [blame] | 51 | const auto data_type = input.data_type(); |
| 52 | |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 53 | // Configure output stage for quantized case |
Sang-Hoon Park | b66aa3b | 2020-01-10 14:44:13 +0000 | [diff] [blame] | 54 | if(is_data_type_quantized_asymmetric(data_type)) |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 55 | { |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 56 | const QuantizationInfo oq_info = output.quantization_info(); |
| 57 | const UniformQuantizationInfo iq_unif = input.quantization_info().uniform(); |
| 58 | const UniformQuantizationInfo wq_unif = weights.quantization_info().uniform(); |
| 59 | const UniformQuantizationInfo oq_unif = oq_info.uniform(); |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 60 | |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 61 | const auto output_quant_info = (output.total_size() == 0) ? iq_unif : oq_unif; |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 62 | |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 63 | const float multiplier = (iq_unif.scale * wq_unif.scale) / output_quant_info.scale; |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 64 | int output_multiplier = 0; |
| 65 | int output_shift = 0; |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 66 | ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift)); |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 67 | |
Sang-Hoon Park | b66aa3b | 2020-01-10 14:44:13 +0000 | [diff] [blame] | 68 | PixelValue type_min{}; |
| 69 | PixelValue type_max{}; |
| 70 | std::tie(type_min, type_max) = get_min_max(data_type); |
| 71 | |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 72 | if(activation_info.enabled()) |
| 73 | { |
| 74 | switch(activation_info.activation()) |
| 75 | { |
| 76 | case ActivationLayerInfo::ActivationFunction::RELU: |
| 77 | type_min = PixelValue(oq_unif.offset); |
| 78 | break; |
| 79 | case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU: |
| 80 | type_min = PixelValue(oq_unif.offset); |
| 81 | type_max = PixelValue(activation_info.a(), data_type, oq_info); |
| 82 | break; |
| 83 | case ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU: |
| 84 | type_min = PixelValue(activation_info.b(), data_type, oq_info); |
| 85 | type_max = PixelValue(activation_info.a(), data_type, oq_info); |
| 86 | break; |
| 87 | default: |
| 88 | ARM_COMPUTE_ERROR("Activation function not supported."); |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 93 | // Set the GEMMLowp output stage info |
| 94 | gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset; |
| 95 | gemmlowp_output_stage.gemmlowp_multiplier = output_multiplier; |
| 96 | gemmlowp_output_stage.gemmlowp_shift = output_shift; |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 97 | gemmlowp_output_stage.gemmlowp_multipliers.push_back(output_multiplier); |
| 98 | gemmlowp_output_stage.gemmlowp_shifts.push_back(output_shift); |
Sang-Hoon Park | b66aa3b | 2020-01-10 14:44:13 +0000 | [diff] [blame] | 99 | type_min.get(gemmlowp_output_stage.gemmlowp_min_bound); |
| 100 | type_max.get(gemmlowp_output_stage.gemmlowp_max_bound); |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | return Status{}; |
| 104 | } |
| 105 | |
Georgios Pinitas | 44bfc3f | 2019-10-28 14:16:31 +0000 | [diff] [blame] | 106 | Status validate_mm(const ITensorInfo &input, const ITensorInfo &weights, const ITensorInfo *bias, const ITensorInfo &output, const FullyConnectedLayerInfo &fc_info) |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 107 | { |
| 108 | GEMMLowpOutputStageInfo gemmlowp_output_stage; |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 109 | ARM_COMPUTE_RETURN_ON_ERROR(construct_gemmlowp_output_stage(input, weights, output, gemmlowp_output_stage, fc_info.activation_info)); |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 110 | |
Georgios Pinitas | 44bfc3f | 2019-10-28 14:16:31 +0000 | [diff] [blame] | 111 | const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped |
| 112 | false, // is_b_reshaped |
| 113 | true, // reshape_b_only_on_first_run |
| 114 | 0, // depth_output_gemm3d |
| 115 | false, // reinterpret_input_as_3d |
| 116 | fc_info.retain_internal_weights, // retain_internal_weights |
| 117 | gemmlowp_output_stage, // gemmlowp_output_stage |
| 118 | fc_info.fp_mixed_precision, // fp_mixed_precision |
| 119 | true, // broadcast_bias |
| 120 | ActivationLayerInfo()); // activation_info |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 121 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 122 | if(is_data_type_quantized_asymmetric(input.data_type())) |
| 123 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 124 | const UniformQuantizationInfo iq_info = input.quantization_info().uniform(); |
| 125 | const UniformQuantizationInfo wq_info = weights.quantization_info().uniform(); |
| 126 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 127 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 128 | // Extract and negate input and weights offset |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 129 | const QuantizationInfo input_quantization_info(iq_info.scale, -iq_info.offset); |
| 130 | const QuantizationInfo weights_quantization_info(wq_info.scale, -wq_info.offset); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 131 | |
| 132 | // Validate gemmlowp function |
| 133 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyCore::validate(&input.clone()->set_quantization_info(input_quantization_info), |
| 134 | &weights.clone()->set_quantization_info(weights_quantization_info), |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 135 | bias, |
| 136 | &output, |
| 137 | gemm_info)); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 138 | } |
| 139 | else |
| 140 | { |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 141 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(&input, &weights, bias, &output, 1.f, 1.f, gemm_info)); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | return Status{}; |
| 145 | } |
| 146 | } // namespace |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 147 | |
| 148 | void CLFullyConnectedLayerReshapeWeights::configure(const ICLTensor *input, ICLTensor *output) |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 149 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 150 | configure(CLKernelLibrary::get().get_compile_context(), input, output); |
| 151 | } |
| 152 | |
| 153 | void CLFullyConnectedLayerReshapeWeights::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output) |
| 154 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 155 | auto k = arm_compute::support::cpp14::make_unique<CLTransposeKernel>(); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 156 | k->configure(compile_context, input, output); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 157 | _kernel = std::move(k); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 160 | Status CLFullyConnectedLayerReshapeWeights::validate(const ITensorInfo *input, const ITensorInfo *output) |
| 161 | { |
| 162 | return CLTransposeKernel::validate(input, output); |
| 163 | } |
| 164 | |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 165 | CLFullyConnectedLayer::CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager) |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 166 | : _memory_group(memory_manager), _weights_manager(weights_manager), _convert_weights(), _convert_weights_managed(), _reshape_weights_managed_function(), _flatten_layer(), _reshape_weights_function(), |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 167 | _mm_gemm(memory_manager, weights_manager), _mm_gemmlowp(memory_manager), _flatten_output(), _converted_weights_output(), _reshape_weights_output(), _are_weights_converted(true), |
| 168 | _are_weights_reshaped(true), _is_fc_after_conv(true), _is_quantized(false), _is_prepared(false), _original_weights(nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 169 | { |
| 170 | } |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 171 | void CLFullyConnectedLayer::configure_mm(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, |
| 172 | const FullyConnectedLayerInfo &fc_info) |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 173 | { |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 174 | GEMMLowpOutputStageInfo gemmlowp_output_stage; |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 175 | construct_gemmlowp_output_stage(*input->info(), *weights->info(), *output->info(), gemmlowp_output_stage, fc_info.activation_info); |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 176 | |
Georgios Pinitas | 44bfc3f | 2019-10-28 14:16:31 +0000 | [diff] [blame] | 177 | const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped |
| 178 | false, // is_b_reshaped |
| 179 | true, // reshape_b_only_on_first_run |
| 180 | 0, // depth_output_gemm3d |
| 181 | false, // reinterpret_input_as_3d |
| 182 | fc_info.retain_internal_weights, // retain_internal_weights |
| 183 | gemmlowp_output_stage, // gemmlowp_output_stage |
| 184 | fc_info.fp_mixed_precision, // fp_mixed_precision |
| 185 | true, // broadcast_bias |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 186 | fc_info.activation_info); // activation_info |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 187 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 188 | if(_is_quantized) |
| 189 | { |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 190 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 191 | // Extract and negate input and weights offset |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 192 | const QuantizationInfo input_quantization_info = input->info()->quantization_info(); |
| 193 | const QuantizationInfo weights_quantization_info = weights->info()->quantization_info(); |
| 194 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 195 | input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset)); |
| 196 | weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset)); |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 197 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 198 | // Configure gemmlowp function |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 199 | _mm_gemmlowp.configure(compile_context, input, weights, bias, output, gemm_info); |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 200 | |
| 201 | // Revert back QuantizatioInfo as input and weights could be used in other fully connected layers |
| 202 | input->info()->set_quantization_info(input_quantization_info); |
| 203 | weights->info()->set_quantization_info(weights_quantization_info); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 204 | } |
| 205 | else |
| 206 | { |
| 207 | // Configure matrix multiply kernel |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 208 | _mm_gemm.configure(compile_context, input, weights, bias, output, 1.f, 1.f, gemm_info); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 212 | void CLFullyConnectedLayer::configure_conv_fc(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, |
| 213 | const FullyConnectedLayerInfo &fc_info) |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 214 | { |
| 215 | ARM_COMPUTE_ERROR_ON((weights->info()->dimension(1) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2)))); |
| 216 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 217 | // If the fully connected layer is called after a convolution layer, the input tensor must be linearized |
| 218 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 219 | // Initialize output tensor for flatten |
| 220 | TensorShape shape_flatten = compute_flatten_shape(input->info()); |
| 221 | _flatten_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_flatten).set_data_layout(DataLayout::NCHW)); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 222 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 223 | // Configure flatten kernel |
| 224 | _memory_group.manage(&_flatten_output); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 225 | _flatten_layer.configure(compile_context, input, &_flatten_output); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 226 | |
| 227 | // Configure matrix multiply kernel |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 228 | configure_mm(compile_context, &_flatten_output, weights, bias, output, fc_info); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 229 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 230 | // Allocate the output tensor for flatten once all the configure methods have been called |
| 231 | _flatten_output.allocator()->allocate(); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 232 | } |
| 233 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 234 | void CLFullyConnectedLayer::configure_fc_fc(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, |
| 235 | const FullyConnectedLayerInfo &fc_info) |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 236 | { |
| 237 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != weights->info()->dimension(1)); |
| 238 | |
| 239 | // Configure matrix multiply kernel |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 240 | configure_mm(compile_context, input, weights, bias, output, fc_info); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 241 | } |
| 242 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 243 | void CLFullyConnectedLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, |
| 244 | FullyConnectedLayerInfo fc_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 245 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 246 | configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, fc_info); |
| 247 | } |
| 248 | |
| 249 | void CLFullyConnectedLayer::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, |
| 250 | FullyConnectedLayerInfo fc_info) |
| 251 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 252 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
| 253 | |
| 254 | // Perform validate step |
| 255 | ARM_COMPUTE_ERROR_THROW_ON(CLFullyConnectedLayer::validate(input->info(), |
| 256 | weights->info(), |
| 257 | biases != nullptr ? biases->info() : nullptr, |
| 258 | output->info(), |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 259 | fc_info)); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 260 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 261 | _are_weights_converted = true; |
| 262 | _are_weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true; |
| 263 | _is_fc_after_conv = true; |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 264 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
Michele Di Giorgio | ba1ffe9 | 2018-08-22 14:28:30 +0100 | [diff] [blame] | 265 | _is_prepared = fc_info.retain_internal_weights; |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 266 | _original_weights = weights; |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 267 | |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 268 | if(_weights_manager) |
| 269 | { |
| 270 | _weights_manager->manage(weights); |
| 271 | } |
| 272 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 273 | const ICLTensor *weights_to_use = weights; |
| 274 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 275 | // With the Fully Connected layer we can have 4 different cases: |
| 276 | // 1) Convolution layer -> Fully Connected layer without batches |
| 277 | // 2) Fully Connected layer -> Fully Connected layer without batches |
| 278 | // 3) Convolution layer -> Fully Connected layer with batches |
| 279 | // 4) Fully Connected layer -> Fully Connected layer with batches |
| 280 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 281 | // Check if we have a fully connected layer with batches |
| 282 | const bool is_batched_fc_layer = output->info()->dimension(1) > 1; |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 283 | if(is_batched_fc_layer) |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 284 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 285 | _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->info()->tensor_shape().cbegin() + 3, |
| 286 | input->info()->tensor_shape().cend(), |
| 287 | output->info()->tensor_shape().cbegin() + 1)); |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 288 | } |
| 289 | else |
| 290 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 291 | _is_fc_after_conv = input->info()->num_dimensions() > 1; |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 294 | // Reshape weights if needed |
| 295 | if(!_are_weights_reshaped) |
| 296 | { |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 297 | if(_weights_manager && _weights_manager->are_weights_managed(weights)) |
| 298 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 299 | _reshape_weights_managed_function.configure(compile_context, weights); |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 300 | weights_to_use = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->acquire(weights, &_reshape_weights_managed_function)); |
| 301 | } |
| 302 | else |
| 303 | { |
| 304 | // Reshape the weights |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 305 | _reshape_weights_function.configure(compile_context, weights, &_reshape_weights_output); |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 306 | weights_to_use = &_reshape_weights_output; |
| 307 | } |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // Convert weights if needed |
| 311 | if(_is_fc_after_conv && (input->info()->data_layout() != fc_info.weights_trained_layout)) |
| 312 | { |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 313 | if(_weights_manager && _weights_manager->are_weights_managed(weights_to_use)) |
| 314 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 315 | _convert_weights_managed.configure(compile_context, weights_to_use, |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 316 | input->info()->tensor_shape(), |
| 317 | fc_info.weights_trained_layout); |
| 318 | weights_to_use = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->acquire(weights, &_convert_weights_managed)); |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | // Convert weights |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 323 | _convert_weights.configure(compile_context, weights_to_use, |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 324 | &_converted_weights_output, |
| 325 | input->info()->tensor_shape(), |
| 326 | fc_info.weights_trained_layout); |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 327 | |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 328 | weights_to_use = &_converted_weights_output; |
| 329 | } |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 330 | _are_weights_converted = false; |
| 331 | } |
| 332 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 333 | if(_is_fc_after_conv) |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 334 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 335 | // Fully Connected layer after a Convolution Layer without batches |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 336 | configure_conv_fc(compile_context, input, weights_to_use, biases, output, fc_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 337 | } |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 338 | else |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 339 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 340 | // Fully Connected layer after a Fully Connected Layer without batches |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 341 | configure_fc_fc(compile_context, input, weights_to_use, biases, output, fc_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 342 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 343 | } |
| 344 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 345 | Status CLFullyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
| 346 | FullyConnectedLayerInfo fc_info) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 347 | { |
| 348 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
Sang-Hoon Park | b66aa3b | 2020-01-10 14:44:13 +0000 | [diff] [blame] | 349 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 350 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output); |
| 351 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 2); |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 352 | ARM_COMPUTE_RETURN_ERROR_ON(fc_info.activation_info.enabled() && is_data_type_quantized(input->data_type()) && fc_info.activation_info.activation() != ActivationLayerInfo::ActivationFunction::RELU |
| 353 | && fc_info.activation_info.activation() != ActivationLayerInfo::ActivationFunction::BOUNDED_RELU && fc_info.activation_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 354 | |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 355 | bool weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true; |
| 356 | bool is_fc_after_conv = true; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 357 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 358 | const ITensorInfo &flatten_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_flatten_shape(input)).set_data_layout(DataLayout::NCHW)); |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 359 | const ITensorInfo &reshaped_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_transposed_shape(*weights))); |
Georgios Pinitas | 195b0ba | 2018-08-02 17:18:51 +0100 | [diff] [blame] | 360 | const ITensorInfo &converted_weights = weights_reshaped ? TensorInfo(weights->clone()->set_is_resizable(true).reset_padding()) : TensorInfo(*reshaped_weights.clone()); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 361 | |
| 362 | // With the Fully Connected layer we can have 4 different cases: |
| 363 | // 1) Convolution layer -> Fully Connected layer without batches |
| 364 | // 2) Fully Connected layer -> Fully Connected layer without batches |
| 365 | // 3) Convolution layer -> Fully Connected layer with batches |
| 366 | // 4) Fully Connected layer -> Fully Connected layer with batches |
| 367 | |
| 368 | const ITensorInfo *input_to_use = input; |
| 369 | const ITensorInfo *weights_to_use = weights; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 370 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 371 | // Check if we have a fully connected layer with batches |
| 372 | const bool is_batched_fc_layer = output->dimension(1) > 1; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 373 | if(is_batched_fc_layer) |
| 374 | { |
| 375 | is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->tensor_shape().cbegin() + 3, |
| 376 | input->tensor_shape().cend(), |
| 377 | output->tensor_shape().cbegin() + 1)); |
| 378 | } |
| 379 | else |
| 380 | { |
| 381 | is_fc_after_conv = input->num_dimensions() > 1; |
| 382 | } |
| 383 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 384 | if(!weights_reshaped) |
| 385 | { |
| 386 | // Validate reshape weights kernel |
| 387 | ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayerReshapeWeights::validate(weights, &reshaped_weights)); |
| 388 | weights_to_use = &reshaped_weights; |
| 389 | } |
| 390 | |
| 391 | if(is_fc_after_conv && (input->data_layout() != fc_info.weights_trained_layout)) |
| 392 | { |
| 393 | // Validate convert weights kernel |
| 394 | ARM_COMPUTE_RETURN_ON_ERROR(CLConvertFullyConnectedWeights::validate(weights_to_use, |
| 395 | &converted_weights, |
| 396 | input->tensor_shape(), |
| 397 | fc_info.weights_trained_layout)); |
| 398 | weights_to_use = &converted_weights; |
| 399 | } |
| 400 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 401 | if(is_fc_after_conv) |
| 402 | { |
| 403 | // Fully Connected layer after a Convolution Layer without batches |
| 404 | ARM_COMPUTE_RETURN_ERROR_ON((weights_to_use->dimension(1) != (input->dimension(0) * input->dimension(1) * input->dimension(2)))); |
| 405 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 406 | // Validate flatten kernel |
| 407 | ARM_COMPUTE_RETURN_ON_ERROR(CLFlattenLayer::validate(input, &flatten_input)); |
| 408 | input_to_use = &flatten_input; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 409 | } |
| 410 | else |
| 411 | { |
| 412 | // Fully Connected layer after a Fully Connected Layer without batches |
| 413 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != weights_to_use->dimension(1)); |
| 414 | } |
Georgios Pinitas | 8b72199 | 2019-10-28 16:24:28 +0000 | [diff] [blame] | 415 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 416 | // Validate matrix multiply kernel |
Georgios Pinitas | 44bfc3f | 2019-10-28 14:16:31 +0000 | [diff] [blame] | 417 | ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(*input_to_use, *weights_to_use, biases, *output, fc_info)); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 418 | |
| 419 | return Status{}; |
| 420 | } |
| 421 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 422 | void CLFullyConnectedLayer::run() |
| 423 | { |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 424 | prepare(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 425 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 426 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 427 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 428 | // Linearize input if it comes from a convolutional layer |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 429 | if(_is_fc_after_conv) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 430 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 431 | _flatten_layer.run(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 432 | } |
| 433 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 434 | // Run matrix multiply |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 435 | if(_is_quantized) |
| 436 | { |
| 437 | _mm_gemmlowp.run(); |
| 438 | } |
| 439 | else |
| 440 | { |
Gian Marco Iodice | c9c62c2 | 2018-04-06 10:00:10 +0100 | [diff] [blame] | 441 | _mm_gemm.run(); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 442 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 443 | } |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 444 | |
| 445 | void CLFullyConnectedLayer::prepare() |
| 446 | { |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 447 | if(!_is_prepared) |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 448 | { |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 449 | if(!_weights_manager) |
| 450 | { |
| 451 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 452 | } |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 453 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 454 | auto release_unused = [](CLTensor * w) |
| 455 | { |
| 456 | if(!w->is_used()) |
| 457 | { |
| 458 | CLScheduler::get().queue().finish(); |
| 459 | w->allocator()->free(); |
| 460 | } |
| 461 | }; |
| 462 | |
| 463 | // Pointer to current weights |
| 464 | const ICLTensor *cur_weights = _original_weights; |
| 465 | |
| 466 | // Reshape of the weights if needed (happens only once) |
| 467 | if(!_are_weights_reshaped) |
| 468 | { |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 469 | if(_weights_manager && _weights_manager->are_weights_managed(_original_weights)) |
| 470 | { |
| 471 | cur_weights = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->run(cur_weights, &_reshape_weights_managed_function)); |
| 472 | } |
| 473 | else |
| 474 | { |
| 475 | // Run reshape weights kernel and mark weights as unused |
| 476 | _reshape_weights_output.allocator()->allocate(); |
| 477 | _reshape_weights_function.run(); |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 478 | |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 479 | cur_weights->mark_as_unused(); |
| 480 | cur_weights = &_reshape_weights_output; |
| 481 | } |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 482 | _are_weights_reshaped = true; |
| 483 | } |
| 484 | |
| 485 | // Convert weights if needed (happens only once) |
| 486 | if(!_are_weights_converted) |
| 487 | { |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 488 | if(_weights_manager && _weights_manager->are_weights_managed(cur_weights)) |
| 489 | { |
| 490 | _weights_manager->run(cur_weights, &_convert_weights_managed); |
| 491 | } |
| 492 | else |
| 493 | { |
| 494 | _converted_weights_output.allocator()->allocate(); |
| 495 | _convert_weights.run(); |
| 496 | cur_weights->mark_as_unused(); |
| 497 | } |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 498 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 499 | _are_weights_converted = true; |
| 500 | } |
| 501 | |
| 502 | // Release reshaped weights if unused |
| 503 | release_unused(&_reshape_weights_output); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 504 | |
| 505 | // Prepare GEMM prepare and release unused weights |
| 506 | if(!_is_quantized) |
| 507 | { |
| 508 | _mm_gemm.prepare(); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 509 | } |
| 510 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 511 | // Release converted weights if unused |
| 512 | release_unused(&_reshape_weights_output); |
| 513 | release_unused(&_converted_weights_output); |
| 514 | |
| 515 | _is_prepared = true; |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 516 | } |
| 517 | } |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 518 | } // namespace arm_compute |