blob: 971b540a83db61bb9c37a3249c3b9a2d67d1d992 [file] [log] [blame]
zhenglin0fb6cf52017-12-12 15:56:09 +08001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
zhenglin0fb6cf52017-12-12 15:56:09 +08003 *
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/GLES_COMPUTE/kernels/GCNormalizePlanarYUVLayerKernel.h"
25
zhenglin0fb6cf52017-12-12 15:56:09 +080026#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h"
27#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h"
28#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/Utils.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/Window.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/AccessWindowStatic.h"
34#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
zhenglin0fb6cf52017-12-12 15:56:09 +080036
Matthew Bentham758b5ba2020-03-05 23:37:48 +000037#include "support/StringSupport.h"
zhenglin0fb6cf52017-12-12 15:56:09 +080038
39using namespace arm_compute;
40
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010041namespace
42{
43Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *mean, const ITensorInfo *std)
44{
45 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16);
46 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
47 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() != DataLayout::NCHW);
48
49 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, mean, std);
50 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, std);
51 ARM_COMPUTE_RETURN_ERROR_ON_MSG(mean->num_dimensions() > 1, "mean and std must be vectors");
52
53 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
54 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(channel_idx) != mean->dimension(0));
55
56 // Checks performed when output is configured
57 if(output->total_size() != 0)
58 {
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
61 }
62
63 return Status{};
64}
65
66std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, ITensorInfo *mean, ITensorInfo *std)
67{
68 // Output tensor auto initialization if not yet initialized
69 auto_init_if_empty(*output, *input->clone());
70
71 const unsigned int num_elems_processed_per_iteration = 4;
72
73 // Configure kernel window
74 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
75
76 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
77 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
78 const int mean_padding = ceil_to_multiple(mean->dimension(0), num_elems_processed_per_iteration) - mean->dimension(0);
79 const int std_padding = ceil_to_multiple(std->dimension(0), num_elems_processed_per_iteration) - std->dimension(0);
80 AccessWindowStatic mean_access(mean, 0, 0, mean->dimension(0) + mean_padding, mean->dimension(1));
81 AccessWindowStatic std_access(std, 0, 0, std->dimension(0) + std_padding, std->dimension(1));
82
83 const bool window_changed = update_window_and_padding(win, input_access, output_access, mean_access, std_access);
84 output_access.set_valid_region(win, input->valid_region());
85
86 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
87 return std::make_pair(err, win);
88}
89} // namespace
90
zhenglin0fb6cf52017-12-12 15:56:09 +080091GCNormalizePlanarYUVLayerKernel::GCNormalizePlanarYUVLayerKernel()
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010092 : _input(nullptr), _output(nullptr), _mean(nullptr), _std(nullptr)
zhenglin0fb6cf52017-12-12 15:56:09 +080093{
94}
95
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010096void GCNormalizePlanarYUVLayerKernel::configure(const IGCTensor *input, IGCTensor *output, const IGCTensor *mean, const IGCTensor *std)
zhenglin0fb6cf52017-12-12 15:56:09 +080097{
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010098 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, mean, std);
99
100 // Output tensor auto initialization if not yet initialized
101 auto_init_if_empty(*output->info(), *input->info()->clone());
102
103 // Perform validation step
104 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), mean->info(), std->info()));
zhenglin0fb6cf52017-12-12 15:56:09 +0800105
106 _input = input;
107 _output = output;
108 _mean = mean;
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100109 _std = std;
zhenglin0fb6cf52017-12-12 15:56:09 +0800110
111 // Set build options
112 std::set<std::string> build_opts;
113 build_opts.emplace(("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)));
114 build_opts.emplace(("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)));
115 build_opts.emplace(("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)));
116
117 // Create kernel
118 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("normalize_planar_yuv_layer", build_opts));
119
120 // Configure kernel window
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100121 auto win_config = validate_and_configure_window(input->info(), output->info(), mean->info(), std->info());
122 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
zhenglin0fb6cf52017-12-12 15:56:09 +0800123
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100124 IGCKernel::configure(std::get<1>(win_config));
125}
zhenglin0fb6cf52017-12-12 15:56:09 +0800126
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100127Status GCNormalizePlanarYUVLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *mean, const ITensorInfo *std)
128{
129 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, std));
130 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), mean->clone().get(), std->clone().get())));
131 return Status{};
zhenglin0fb6cf52017-12-12 15:56:09 +0800132}
133
134void GCNormalizePlanarYUVLayerKernel::run(const Window &window)
135{
136 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
137 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
138
139 _kernel.use();
140
Frank Lei4406fd62018-02-01 14:47:14 +0800141 _output->set_needs_shifting(true);
142
zhenglin0fb6cf52017-12-12 15:56:09 +0800143 Window slice = window.first_slice_window_3D();
144
145 Window slice_in;
146 //slice_in.use_tensor_dimensions(_mean->info()->tensor_shape());
147 slice_in = window.first_slice_window_1D();
148 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
149
150 unsigned int idx = 2 * num_arguments_per_3D_tensor();
151 add_1D_tensor_argument(idx, _mean, 3, slice_in);
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100152 add_1D_tensor_argument(idx, _std, 4, slice_in);
zhenglin0fb6cf52017-12-12 15:56:09 +0800153
Frank Lei4406fd62018-02-01 14:47:14 +0800154 slice_in = window.first_slice_window_3D();
155
156 slice.shift(Window::DimX, -(_output->info()->padding()).left);
157
zhenglin0fb6cf52017-12-12 15:56:09 +0800158 do
159 {
160 idx = 0;
Frank Lei4406fd62018-02-01 14:47:14 +0800161 add_3D_tensor_argument(idx, _input, 1, slice_in);
zhenglin0fb6cf52017-12-12 15:56:09 +0800162 add_3D_tensor_argument(idx, _output, 2, slice);
163
164 _kernel.update_shader_params();
165
166 enqueue(*this, slice);
167 }
Frank Lei4406fd62018-02-01 14:47:14 +0800168 while(window.slide_window_slice_3D(slice) && window.slide_window_slice_3D(slice_in));
zhenglin0fb6cf52017-12-12 15:56:09 +0800169}