Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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/CLNormalizationLayerKernel.h" |
| 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" |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/FixedPoint.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Helpers.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Utils.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
| 35 | |
| 36 | using namespace arm_compute; |
| 37 | |
| 38 | CLNormalizationLayerKernel::CLNormalizationLayerKernel() |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 39 | : _input(nullptr), _output(nullptr), _border_size(0), _is_in_map(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | { |
| 41 | } |
| 42 | |
| 43 | BorderSize CLNormalizationLayerKernel::border_size() const |
| 44 | { |
| 45 | return _border_size; |
| 46 | } |
| 47 | |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 48 | void CLNormalizationLayerKernel::configure(const ICLTensor *input, ICLTensor *output, NormalizationLayerInfo norm_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | { |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
Georgios Pinitas | 09004ca | 2017-07-03 17:30:14 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_ERROR_ON_NULLPTR(output); |
| 52 | |
| 53 | // Output tensor auto initialization if not yet initialized |
| 54 | auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 55 | |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 56 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 57 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 58 | ARM_COMPUTE_ERROR_ON_MSG(!(norm_info.norm_size() % 2), "Normalization size should be odd"); |
| 59 | ARM_COMPUTE_ERROR_ON_MSG(norm_info.type() == NormType::IN_MAP_2D, "2D In-Map Normalization not implemented"); |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 60 | if(is_data_type_fixed_point(input->info()->data_type())) |
| 61 | { |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 62 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 63 | ARM_COMPUTE_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(norm_info.beta(), input); |
| 64 | ARM_COMPUTE_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(norm_info.kappa(), input); |
| 65 | ARM_COMPUTE_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(norm_info.scale_coeff(), input); |
| 66 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 68 | _input = input; |
| 69 | _output = output; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 71 | _is_in_map = (norm_info.type() != NormType::CROSS_MAP); |
steniu01 | f70256b | 2017-07-13 14:03:35 +0100 | [diff] [blame] | 72 | const unsigned int border_width = _is_in_map ? std::min(norm_info.norm_size() / 2, 3U) : 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | _border_size = BorderSize(0, border_width); |
| 74 | |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 75 | const unsigned int num_elems_processed_per_iteration = (is_data_type_fixed_point(input->info()->data_type())) ? 16 : 4; |
| 76 | const unsigned int num_elems_read_per_iteration = num_elems_processed_per_iteration + 2 * (norm_info.norm_size() / 2); |
| 77 | |
| 78 | // Set build options |
| 79 | std::set<std::string> build_opts; |
| 80 | build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()))); |
| 81 | if(is_data_type_fixed_point(input->info()->data_type())) |
| 82 | { |
| 83 | build_opts.emplace(("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()))); |
| 84 | } |
Georgios Pinitas | 8901096 | 2017-08-04 14:58:27 +0100 | [diff] [blame] | 85 | build_opts.emplace(("-DCOEFF=" + float_to_string_with_full_precision(norm_info.scale_coeff()))); |
| 86 | build_opts.emplace(("-DBETA=" + float_to_string_with_full_precision(norm_info.beta()))); |
| 87 | build_opts.emplace(("-DKAPPA=" + float_to_string_with_full_precision(norm_info.kappa()))); |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 88 | build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration))); |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 89 | build_opts.emplace(("-DRADIUS=" + support::cpp11::to_string(norm_info.norm_size() / 2))); |
| 90 | build_opts.emplace(("-DNUM_SLICES=" + support::cpp11::to_string(input->info()->dimension(2)))); |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 91 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 92 | // Create kernel |
| 93 | std::string kernel_name = (norm_info.type() == NormType::IN_MAP_1D) ? "normalization_layer_in_map_1D" : "normalization_layer_cross_map"; |
| 94 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts)); |
| 95 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | // Configure kernel window |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 98 | |
| 99 | AccessWindowHorizontal input_access(input->info(), -_border_size.left, num_elems_read_per_iteration); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 101 | |
Gian Marco Iodice | d60a6b9 | 2017-08-10 10:43:40 +0100 | [diff] [blame] | 102 | update_window_and_padding(win, input_access, output_access); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | |
| 104 | output_access.set_valid_region(win, input->info()->valid_region()); |
| 105 | |
| 106 | ICLKernel::configure(win); |
| 107 | } |
| 108 | |
| 109 | void CLNormalizationLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 110 | { |
| 111 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 112 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 113 | |
steniu01 | f70256b | 2017-07-13 14:03:35 +0100 | [diff] [blame] | 114 | const int collapsed_dimension = _is_in_map ? Window::DimZ : 4; |
| 115 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), collapsed_dimension); |
| 116 | Window slice = window_collapsed.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 117 | |
| 118 | do |
| 119 | { |
| 120 | unsigned int idx = 0; |
| 121 | add_3D_tensor_argument(idx, _input, slice); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 122 | add_3D_tensor_argument(idx, _output, slice); |
| 123 | enqueue(queue, *this, slice); |
| 124 | } |
steniu01 | f70256b | 2017-07-13 14:03:35 +0100 | [diff] [blame] | 125 | while(window_collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 126 | } |