• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017-2020 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/GLES_COMPUTE/kernels/GCNormalizePlanarYUVLayerKernel.h"
25 
26 #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"
33 #include "src/core/AccessWindowStatic.h"
34 #include "src/core/helpers/AutoConfiguration.h"
35 #include "src/core/helpers/WindowHelpers.h"
36 
37 #include "support/StringSupport.h"
38 
39 using namespace arm_compute;
40 
41 namespace
42 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * output,const ITensorInfo * mean,const ITensorInfo * std)43 Status 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 
validate_and_configure_window(ITensorInfo * input,ITensorInfo * output,ITensorInfo * mean,ITensorInfo * std)66 std::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 
GCNormalizePlanarYUVLayerKernel()91 GCNormalizePlanarYUVLayerKernel::GCNormalizePlanarYUVLayerKernel()
92     : _input(nullptr), _output(nullptr), _mean(nullptr), _std(nullptr)
93 {
94 }
95 
configure(const IGCTensor * input,IGCTensor * output,const IGCTensor * mean,const IGCTensor * std)96 void GCNormalizePlanarYUVLayerKernel::configure(const IGCTensor *input, IGCTensor *output, const IGCTensor *mean, const IGCTensor *std)
97 {
98     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()));
105 
106     _input  = input;
107     _output = output;
108     _mean   = mean;
109     _std    = std;
110 
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
121     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));
123 
124     IGCKernel::configure(std::get<1>(win_config));
125 }
126 
validate(const ITensorInfo * input,const ITensorInfo * output,const ITensorInfo * mean,const ITensorInfo * std)127 Status 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{};
132 }
133 
run(const Window & window)134 void 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 
141     _output->set_needs_shifting(true);
142 
143     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);
152     add_1D_tensor_argument(idx, _std, 4, slice_in);
153 
154     slice_in = window.first_slice_window_3D();
155 
156     slice.shift(Window::DimX, -(_output->info()->padding()).left);
157 
158     do
159     {
160         idx = 0;
161         add_3D_tensor_argument(idx, _input, 1, slice_in);
162         add_3D_tensor_argument(idx, _output, 2, slice);
163 
164         _kernel.update_shader_params();
165 
166         enqueue(*this, slice);
167     }
168     while(window.slide_window_slice_3D(slice) && window.slide_window_slice_3D(slice_in));
169 }
170