Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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/core/CL/kernels/CLDirectConvolutionLayerOutputStageKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame^] | 27 | #include "arm_compute/core/CL/CLValidate.h" |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/Error.h" |
| 30 | #include "arm_compute/core/Helpers.h" |
| 31 | #include "arm_compute/core/Types.h" |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/Window.h" |
| 33 | |
| 34 | #include <cstddef> |
| 35 | #include <cstdint> |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
| 39 | namespace |
| 40 | { |
| 41 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output) |
| 42 | { |
| 43 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame^] | 44 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 45 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::S32, DataType::F16, |
| 46 | DataType::F32); |
| 47 | |
| 48 | if(bias != nullptr) |
| 49 | { |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame^] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(bias); |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32, DataType::F16, DataType::F32); |
| 52 | |
| 53 | if(is_data_type_quantized_asymmetric(input->data_type())) |
| 54 | { |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32); |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias); |
| 60 | } |
| 61 | |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1); |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_float(input->data_type()), |
| 67 | "Calling output stage kernel with floating point arguments"); |
| 68 | } |
| 69 | |
| 70 | // Checks performed on output |
| 71 | if(input->data_type() == DataType::S32) |
| 72 | { |
| 73 | // Quantized configuration checks |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8); |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | // In case of out-of-place computation (supported for non-quantized configurations) |
| 79 | if((output != nullptr) && (output->total_size() != 0)) |
| 80 | { |
| 81 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return Status{}; |
| 86 | } |
| 87 | |
| 88 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *bias, ITensorInfo *output) |
| 89 | { |
| 90 | bool window_changed = false; |
| 91 | unsigned int num_elems_processed_per_iteration = 16 / element_size_from_data_type(input->data_type()); |
| 92 | |
| 93 | // Update processed elements when input is S32 (comes from quantization input) |
| 94 | if(input->data_type() == DataType::S32) |
| 95 | { |
| 96 | num_elems_processed_per_iteration = 16; |
| 97 | } |
| 98 | |
| 99 | // Configure kernel window |
| 100 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 101 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 102 | |
| 103 | if(output != nullptr && (output->total_size() != 0)) |
| 104 | { |
| 105 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 106 | |
| 107 | if(bias == nullptr) |
| 108 | { |
| 109 | window_changed = update_window_and_padding(win, input_access, output_access); |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | AccessWindowStatic bias_access(bias, 0, 0, bias->dimension(0), bias->dimension(1)); |
| 114 | window_changed = update_window_and_padding(win, input_access, output_access, bias_access); |
| 115 | } |
| 116 | |
| 117 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | if(bias == nullptr) |
| 122 | { |
| 123 | window_changed = update_window_and_padding(win, input_access); |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | AccessWindowStatic bias_access(bias, 0, 0, bias->dimension(0), bias->dimension(1)); |
| 128 | window_changed = update_window_and_padding(win, input_access, bias_access); |
| 129 | } |
| 130 | |
| 131 | input_access.set_valid_region(win, ValidRegion(Coordinates(), input->tensor_shape())); |
| 132 | } |
| 133 | |
| 134 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 135 | return std::make_pair(err, win); |
| 136 | } |
| 137 | } // namespace |
| 138 | |
| 139 | CLDirectConvolutionLayerOutputStageKernel::CLDirectConvolutionLayerOutputStageKernel() |
| 140 | : _input(nullptr), _bias(nullptr), _output(nullptr), _result_fixedpoint_multiplier(0), _result_shift(0), _result_offset_after_shift(0) |
| 141 | { |
| 142 | } |
| 143 | |
| 144 | void CLDirectConvolutionLayerOutputStageKernel::configure(ICLTensor *input, const ICLTensor *bias, ICLTensor *output, |
| 145 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift) |
| 146 | { |
| 147 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
| 148 | |
Georgios Pinitas | 9be0c5a | 2018-02-19 12:46:29 +0000 | [diff] [blame] | 149 | // Auto-initialize output if required |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 150 | if(output != nullptr) |
| 151 | { |
| 152 | // Work out expected output data type |
| 153 | const DataType output_dt = (input->info()->data_type() == DataType::S32) ? DataType::QASYMM8 : input->info()->data_type(); |
| 154 | // Output tensor auto initialization if not yet initialized |
| 155 | auto_init_if_empty(*output->info(), input->info()->clone()->set_data_type(output_dt)); |
| 156 | } |
| 157 | |
| 158 | // Perform validation step |
| 159 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias == nullptr) ? nullptr : bias->info(), (output == nullptr) ? nullptr : output->info())); |
| 160 | |
| 161 | _bias = bias; |
| 162 | _input = input; |
| 163 | _output = output; |
| 164 | _result_fixedpoint_multiplier = result_fixedpoint_multiplier; |
| 165 | _result_shift = result_shift; |
| 166 | _result_offset_after_shift = result_offset_after_shift; |
| 167 | |
| 168 | // Create kernel |
| 169 | CLBuildOptions build_opts; |
| 170 | build_opts.add_option_if(bias != nullptr, "-DHAS_BIAS"); |
| 171 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("output_stage_quantized", build_opts.options())); |
| 172 | |
| 173 | // Set static kernel arguments |
| 174 | int idx = 2 * num_arguments_per_3D_tensor() + ((bias != nullptr) ? num_arguments_per_1D_tensor() : 0); |
| 175 | _kernel.setArg<int>(idx++, _result_offset_after_shift); |
| 176 | _kernel.setArg<int>(idx++, _result_fixedpoint_multiplier); |
| 177 | _kernel.setArg<int>(idx++, _result_shift); |
| 178 | |
| 179 | // Configure kernel window |
| 180 | auto win_config = validate_and_configure_window(input->info(), (bias == nullptr) ? nullptr : bias->info(), (output == nullptr) ? nullptr : output->info()); |
| 181 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 182 | ICLKernel::configure(win_config.second); |
| 183 | } |
| 184 | |
| 185 | Status CLDirectConvolutionLayerOutputStageKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output) |
| 186 | { |
| 187 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output)); |
| 188 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), bias->clone().get(), output == nullptr ? nullptr : output->clone().get()).first); |
| 189 | |
| 190 | return Status{}; |
| 191 | } |
| 192 | |
| 193 | void CLDirectConvolutionLayerOutputStageKernel::run(const Window &window, cl::CommandQueue &queue) |
| 194 | { |
| 195 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 196 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 197 | |
| 198 | Window slice = window.first_slice_window_3D(); |
| 199 | |
| 200 | // Set bias vector |
| 201 | if(_bias != nullptr) |
| 202 | { |
| 203 | unsigned int idx1 = 2 * num_arguments_per_3D_tensor(); |
| 204 | Window slice_biases; |
| 205 | slice_biases.use_tensor_dimensions(_bias->info()->tensor_shape()); |
| 206 | add_1D_tensor_argument(idx1, _bias, slice_biases); |
| 207 | } |
| 208 | |
| 209 | // Run kernel |
| 210 | do |
| 211 | { |
| 212 | // Set arguments |
| 213 | unsigned int idx = 0; |
| 214 | add_3D_tensor_argument(idx, _input, slice); |
| 215 | add_3D_tensor_argument(idx, _output, slice); |
| 216 | enqueue(queue, *this, slice, _lws_hint); |
| 217 | } |
| 218 | while(window.slide_window_slice_3D(slice)); |
| 219 | } |