• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019-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 "src/core/CL/kernels/CLInstanceNormalizationLayerKernel.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"
29 #include "arm_compute/core/Helpers.h"
30 #include "arm_compute/core/TensorInfo.h"
31 #include "arm_compute/core/Utils.h"
32 #include "src/core/CL/CLValidate.h"
33 #include "src/core/helpers/AutoConfiguration.h"
34 #include "src/core/helpers/WindowHelpers.h"
35 
36 #include "support/StringSupport.h"
37 
38 namespace arm_compute
39 {
40 namespace
41 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * output,const InstanceNormalizationLayerKernelInfo & info)42 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const InstanceNormalizationLayerKernelInfo &info)
43 {
44     ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.epsilon == 0.f, "Epsilon must be different than 0");
45     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(input, DataType::F16, DataType::F32);
46 
47     if(output != nullptr && output->total_size() != 0)
48     {
49         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
50         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
51         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
52         ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_channels() != output->num_channels(), "Input and output have different number of channels");
53     }
54 
55     return Status{};
56 }
57 
validate_and_configure_window(ITensorInfo * input,ITensorInfo * output)58 std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
59 {
60     // We handle the planes manually
61     Window win = calculate_max_window(*input, Steps(1));
62 
63     // Output auto initialization if not yet initialized
64     auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type());
65 
66     // CLInstanceNormalizationLayerKernel doesn't need padding so update_window_and_padding() can be skipped
67     Coordinates coord;
68     coord.set_num_dimensions(output->num_dimensions());
69     output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
70     return std::make_pair(Status{}, win);
71 }
72 } // namespace
73 
CLInstanceNormalizationLayerKernel()74 CLInstanceNormalizationLayerKernel::CLInstanceNormalizationLayerKernel()
75     : _input(nullptr), _output(nullptr), _run_in_place(false)
76 {
77 }
78 
configure(ICLTensor * input,ICLTensor * output,const InstanceNormalizationLayerKernelInfo & info)79 void CLInstanceNormalizationLayerKernel::configure(ICLTensor *input, ICLTensor *output, const InstanceNormalizationLayerKernelInfo &info)
80 {
81     configure(CLKernelLibrary::get().get_compile_context(), input, output, info);
82 }
83 
configure(const CLCompileContext & compile_context,ICLTensor * input,ICLTensor * output,const InstanceNormalizationLayerKernelInfo & info)84 void CLInstanceNormalizationLayerKernel::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const InstanceNormalizationLayerKernelInfo &info)
85 {
86     ARM_COMPUTE_ERROR_ON_NULLPTR(input);
87 
88     _input  = input;
89     _output = output == nullptr ? input : output;
90 
91     _run_in_place = (output == nullptr) || (output == input);
92     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(_input->info(), _output->info(), info));
93     const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
94 
95     CLBuildOptions build_opts;
96     build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
97     build_opts.add_option("-DINTERNAL_DATA_TYPE=" + (info.use_mixed_precision ? "float" : get_cl_type_from_data_type(input->info()->data_type())));
98     build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
99     build_opts.add_option("-DDIM_X=" + support::cpp11::to_string(input->info()->dimension(0)));
100     build_opts.add_option("-DDIM_Y=" + support::cpp11::to_string(input->info()->dimension(1)));
101     build_opts.add_option("-DDIM_Z=" + support::cpp11::to_string(input->info()->dimension(2)));
102     build_opts.add_option("-DGAMMA=" + float_to_string_with_full_precision(info.gamma));
103     build_opts.add_option("-DBETA=" + float_to_string_with_full_precision(info.beta));
104     build_opts.add_option("-DEPSILON=" + float_to_string_with_full_precision(info.epsilon));
105     build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
106     build_opts.add_option_if(_input->info()->data_layout() == DataLayout::NHWC, "-DNHWC");
107 
108     // Create kernel
109     _kernel = create_kernel(compile_context, "instance_normalization", build_opts.options());
110 
111     // Configure kernel window
112     auto win_config = validate_and_configure_window(_input->info(), _output->info());
113     ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
114     ICLKernel::configure_internal(std::get<1>(win_config));
115 }
116 
validate(const ITensorInfo * input,const ITensorInfo * output,const InstanceNormalizationLayerKernelInfo & info)117 Status CLInstanceNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const InstanceNormalizationLayerKernelInfo &info)
118 {
119     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, info));
120     ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), (output == nullptr ? input->clone().get() : output->clone().get()))));
121     return Status{};
122 }
123 
run(const Window & window,cl::CommandQueue & queue)124 void CLInstanceNormalizationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
125 {
126     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
127     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
128 
129     Window collapsed_window = window.collapse(window, Window::DimZ);
130 
131     // We will process the planes together
132     if(_input->info()->data_layout() == DataLayout::NCHW)
133     {
134         collapsed_window.set(Window::DimX, Window::Dimension(0, 1, 1));
135         collapsed_window.set(Window::DimY, Window::Dimension(0, 1, 1));
136     }
137     else
138     {
139         collapsed_window.set(Window::DimY, Window::Dimension(0, 1, 1));
140         collapsed_window.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(3), 1));
141     }
142 
143     unsigned int idx = 0;
144     add_4D_tensor_argument(idx, _input, collapsed_window);
145     if(!_run_in_place)
146     {
147         add_4D_tensor_argument(idx, _output, collapsed_window);
148     }
149 
150     enqueue(queue, *this, collapsed_window, lws_hint());
151 }
152 } // namespace arm_compute
153