blob: 03463b25522e31166f091717aa45231a51a7765a [file] [log] [blame]
zhenglin0fb6cf52017-12-12 15:56:09 +08001/*
Frank Lei4406fd62018-02-01 14:47:14 +08002 * Copyright (c) 2017-2018 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
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h"
28#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h"
29#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h"
30#include "arm_compute/core/Helpers.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
37using namespace arm_compute;
38
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010039namespace
40{
41Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *mean, const ITensorInfo *std)
42{
43 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16);
44 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
45 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() != DataLayout::NCHW);
46
47 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, mean, std);
48 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, std);
49 ARM_COMPUTE_RETURN_ERROR_ON_MSG(mean->num_dimensions() > 1, "mean and std must be vectors");
50
51 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
52 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(channel_idx) != mean->dimension(0));
53
54 // Checks performed when output is configured
55 if(output->total_size() != 0)
56 {
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
58 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
59 }
60
61 return Status{};
62}
63
64std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, ITensorInfo *mean, ITensorInfo *std)
65{
66 // Output tensor auto initialization if not yet initialized
67 auto_init_if_empty(*output, *input->clone());
68
69 const unsigned int num_elems_processed_per_iteration = 4;
70
71 // Configure kernel window
72 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
73
74 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
75 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
76 const int mean_padding = ceil_to_multiple(mean->dimension(0), num_elems_processed_per_iteration) - mean->dimension(0);
77 const int std_padding = ceil_to_multiple(std->dimension(0), num_elems_processed_per_iteration) - std->dimension(0);
78 AccessWindowStatic mean_access(mean, 0, 0, mean->dimension(0) + mean_padding, mean->dimension(1));
79 AccessWindowStatic std_access(std, 0, 0, std->dimension(0) + std_padding, std->dimension(1));
80
81 const bool window_changed = update_window_and_padding(win, input_access, output_access, mean_access, std_access);
82 output_access.set_valid_region(win, input->valid_region());
83
84 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
85 return std::make_pair(err, win);
86}
87} // namespace
88
zhenglin0fb6cf52017-12-12 15:56:09 +080089GCNormalizePlanarYUVLayerKernel::GCNormalizePlanarYUVLayerKernel()
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010090 : _input(nullptr), _output(nullptr), _mean(nullptr), _std(nullptr)
zhenglin0fb6cf52017-12-12 15:56:09 +080091{
92}
93
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010094void GCNormalizePlanarYUVLayerKernel::configure(const IGCTensor *input, IGCTensor *output, const IGCTensor *mean, const IGCTensor *std)
zhenglin0fb6cf52017-12-12 15:56:09 +080095{
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010096 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, mean, std);
97
98 // Output tensor auto initialization if not yet initialized
99 auto_init_if_empty(*output->info(), *input->info()->clone());
100
101 // Perform validation step
102 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), mean->info(), std->info()));
zhenglin0fb6cf52017-12-12 15:56:09 +0800103
104 _input = input;
105 _output = output;
106 _mean = mean;
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100107 _std = std;
zhenglin0fb6cf52017-12-12 15:56:09 +0800108
109 // Set build options
110 std::set<std::string> build_opts;
111 build_opts.emplace(("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)));
112 build_opts.emplace(("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)));
113 build_opts.emplace(("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)));
114
115 // Create kernel
116 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("normalize_planar_yuv_layer", build_opts));
117
118 // Configure kernel window
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100119 auto win_config = validate_and_configure_window(input->info(), output->info(), mean->info(), std->info());
120 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
zhenglin0fb6cf52017-12-12 15:56:09 +0800121
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100122 IGCKernel::configure(std::get<1>(win_config));
123}
zhenglin0fb6cf52017-12-12 15:56:09 +0800124
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100125Status GCNormalizePlanarYUVLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *mean, const ITensorInfo *std)
126{
127 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, std));
128 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), mean->clone().get(), std->clone().get())));
129 return Status{};
zhenglin0fb6cf52017-12-12 15:56:09 +0800130}
131
132void GCNormalizePlanarYUVLayerKernel::run(const Window &window)
133{
134 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
135 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
136
137 _kernel.use();
138
Frank Lei4406fd62018-02-01 14:47:14 +0800139 _output->set_needs_shifting(true);
140
zhenglin0fb6cf52017-12-12 15:56:09 +0800141 Window slice = window.first_slice_window_3D();
142
143 Window slice_in;
144 //slice_in.use_tensor_dimensions(_mean->info()->tensor_shape());
145 slice_in = window.first_slice_window_1D();
146 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
147
148 unsigned int idx = 2 * num_arguments_per_3D_tensor();
149 add_1D_tensor_argument(idx, _mean, 3, slice_in);
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100150 add_1D_tensor_argument(idx, _std, 4, slice_in);
zhenglin0fb6cf52017-12-12 15:56:09 +0800151
Frank Lei4406fd62018-02-01 14:47:14 +0800152 slice_in = window.first_slice_window_3D();
153
154 slice.shift(Window::DimX, -(_output->info()->padding()).left);
155
zhenglin0fb6cf52017-12-12 15:56:09 +0800156 do
157 {
158 idx = 0;
Frank Lei4406fd62018-02-01 14:47:14 +0800159 add_3D_tensor_argument(idx, _input, 1, slice_in);
zhenglin0fb6cf52017-12-12 15:56:09 +0800160 add_3D_tensor_argument(idx, _output, 2, slice);
161
162 _kernel.update_shader_params();
163
164 enqueue(*this, slice);
165 }
Frank Lei4406fd62018-02-01 14:47:14 +0800166 while(window.slide_window_slice_3D(slice) && window.slide_window_slice_3D(slice_in));
zhenglin0fb6cf52017-12-12 15:56:09 +0800167}