Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 1 | /* |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +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 | */ |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 24 | #include "arm_compute/core/CL/kernels/CLL2NormalizeLayerKernel.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Utils.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | |
| 35 | #include "support/ToolchainSupport.h" |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 39 | CLL2NormalizeLayerKernel::CLL2NormalizeLayerKernel() |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 40 | : _input(nullptr), _sum(nullptr), _output(nullptr), _axis(0), _epsilon(1e-12) |
| 41 | { |
| 42 | } |
| 43 | |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 44 | namespace |
| 45 | { |
| 46 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, unsigned int axis, float epsilon) |
| 47 | { |
| 48 | ARM_COMPUTE_UNUSED(epsilon); |
| 49 | |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, sum, output); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis"); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 54 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions"); |
| 55 | |
| 56 | // Reduce shape on axis |
| 57 | TensorShape sum_shape = input->tensor_shape(); |
| 58 | sum_shape.set(axis, 1); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(sum->tensor_shape(), sum_shape); |
| 60 | |
| 61 | if(output->total_size() != 0) |
| 62 | { |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 64 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 65 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape()); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | return Status{}; |
| 70 | } |
| 71 | |
| 72 | std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
| 73 | { |
| 74 | const unsigned int num_elems_processed_per_iteration = 16; |
| 75 | |
| 76 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 77 | |
| 78 | // Output tensor auto initialization if not yet initialized |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 79 | auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type()); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 80 | |
| 81 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 82 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 83 | |
| 84 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 85 | output_access.set_valid_region(win, input->valid_region()); |
| 86 | |
| 87 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 88 | |
| 89 | return std::make_tuple(err, win); |
| 90 | } |
| 91 | } // namespace |
| 92 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 93 | void CLL2NormalizeLayerKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, unsigned int axis, float epsilon) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 94 | { |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 95 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output); |
| 96 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon)); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 97 | |
| 98 | _input = input; |
| 99 | _sum = sum; |
| 100 | _output = output; |
| 101 | _axis = axis; |
| 102 | _epsilon = epsilon; |
| 103 | |
| 104 | const unsigned int num_elems_processed_per_iteration = 16; |
| 105 | |
| 106 | // Set build options |
| 107 | std::set<std::string> build_opts; |
| 108 | build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()))); |
| 109 | build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration))); |
| 110 | |
| 111 | // Create kernel |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 112 | const DataLayout data_layout = input->info()->data_layout(); |
| 113 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("l2_normalize_" + lower_string(string_from_data_layout(data_layout)), build_opts)); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 114 | |
| 115 | // Set epsilon argument |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 116 | unsigned int idx = data_layout == DataLayout::NCHW ? num_arguments_per_1D_tensor() * 3 : num_arguments_per_2D_tensor() * 3; |
| 117 | if(input->info()->data_type() == DataType::F32) |
| 118 | { |
| 119 | _kernel.setArg<cl_uint>(idx, _epsilon); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | _kernel.setArg<cl_ushort>(idx, _epsilon); |
| 124 | } |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 125 | |
| 126 | // Configure kernel window |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 127 | auto win_config = validate_and_configure_window(_input->info(), _output->info()); |
| 128 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 129 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 130 | ICLKernel::configure_internal(std::get<1>(win_config)); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 131 | } |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 132 | |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 133 | Status CLL2NormalizeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, unsigned int axis, float epsilon) |
| 134 | { |
| 135 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, sum, output, axis, epsilon)); |
| 136 | ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get()))); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 137 | |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 138 | return Status{}; |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 141 | void CLL2NormalizeLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 142 | { |
| 143 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 144 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 145 | |
| 146 | Window window_sum(window); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 147 | |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 148 | switch(_input->info()->data_layout()) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 149 | { |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 150 | case DataLayout::NCHW: |
| 151 | { |
| 152 | window_sum.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 153 | Window in_slice = window.first_slice_window_1D(); |
| 154 | Window sum_slice = window_sum.first_slice_window_1D(); |
| 155 | do |
| 156 | { |
| 157 | unsigned int idx = 0; |
| 158 | add_1D_tensor_argument(idx, _input, in_slice); |
| 159 | add_1D_tensor_argument(idx, _sum, sum_slice); |
| 160 | add_1D_tensor_argument(idx, _output, in_slice); |
| 161 | enqueue(queue, *this, in_slice); |
| 162 | } |
| 163 | while(window.slide_window_slice_1D(in_slice) && window.slide_window_slice_1D(sum_slice)); |
| 164 | } |
| 165 | break; |
| 166 | case DataLayout::NHWC: |
| 167 | { |
| 168 | window_sum.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 169 | Window in_slice = window.first_slice_window_2D(); |
| 170 | Window sum_slice = window_sum.first_slice_window_2D(); |
| 171 | do |
| 172 | { |
| 173 | unsigned int idx = 0; |
| 174 | add_2D_tensor_argument(idx, _input, in_slice); |
| 175 | add_2D_tensor_argument(idx, _sum, sum_slice); |
| 176 | add_2D_tensor_argument(idx, _output, in_slice); |
| 177 | enqueue(queue, *this, in_slice); |
| 178 | } |
| 179 | while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice)); |
| 180 | } |
| 181 | break; |
| 182 | default: |
| 183 | ARM_COMPUTE_ERROR("Not supported"); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 184 | } |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 185 | } |