Compute Library
 20.05
GCSoftmaxLayerKernel.cpp
Go to the documentation of this file.
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  */
25 
33 #include "arm_compute/core/Utils.h"
36 #include "support/StringSupport.h"
37 
38 #include <set>
39 #include <string>
40 
41 using namespace arm_compute;
42 
44 {
47 
48  // Softmax across the x dimension
49  TensorShape output_shape{ input->info()->tensor_shape() };
50  output_shape.set(0, 1);
51 
52  // Output auto initialization if not yet initialized
53  auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
54 
57 
58  _input = input;
59  _output = output;
60 
61  // Set build options
62  std::set<std::string> build_opts;
63  std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
64  build_opts.insert("#define " + dt_name);
65  build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
66  build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
67  build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
68  build_opts.insert("#define SOFTMAX_LAYER_MAX");
69 
70  // Tell the kernel that the width is not a multiple of 8
71  if((input->info()->dimension(0) % 8) != 0)
72  {
73  build_opts.insert("#define NON_MULTIPLE_OF_8");
74  }
75 
76  // Create kernel
77  _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("softmax_layer_max", build_opts));
78 
79  // Set fixed arguments
80  unsigned int idx = 2 * num_arguments_per_3D_tensor(); //Skip the input and output parameters
81  _kernel.set_argument(idx++, input->info()->dimension(0));
82 
83  // Configure kernel window
84  // The kernel loops over all elements in steps of 8
85  const unsigned int num_elems_processed_per_iteration = ceil_to_multiple(input->info()->dimension(0), 8);
86  unsigned int num_elems_written_per_iteration = 1;
87  if(input->info()->data_type() == DataType::F16)
88  {
89  num_elems_written_per_iteration = 2;
90  }
91 
94  AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
95 
96  update_window_and_padding(win, input_access, output_access);
97 
98  output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
99 
100  IGCKernel::configure(win);
101 }
102 
104  : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr)
105 {
106 }
107 
109 {
111  ARM_COMPUTE_ERROR_ON_NULLPTR(max, sum, output);
112 
113  // Output auto initialization if not yet initialized
114  auto_init_if_empty(*sum->info(), max->info()->tensor_shape(), 1, input->info()->data_type());
115  auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type());
116 
120 
121  _input = input;
122  _max = max;
123  _output = output;
124  _sum = sum;
125 
126  // Set build options
127  std::set<std::string> build_opts;
128  std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
129  build_opts.insert("#define " + dt_name);
130  build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
131  build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
132  build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
133  build_opts.insert("#define SOFTMAX_LAYER_SHIFT_EXP_SUM");
134 
135  // Tell the kernel that the width is not a multiple of 8
136  if((input->info()->dimension(0) % 8) != 0)
137  {
138  build_opts.insert("#define NON_MULTIPLE_OF_8");
139  }
140 
141  // Create kernel
142  _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("softmax_layer_shift_exp_sum", build_opts));
143 
144  // Set fixed arguments
145  unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters
146  _kernel.set_argument(idx++, input->info()->dimension(0));
147 
148  // Configure window
149  // The kernel loops over all elements in steps of 8
150  const unsigned int num_elems_processed_per_iteration = ceil_to_multiple(input->info()->dimension(0), 8);
151  unsigned int num_elems_written_per_iteration = 1;
152  if(input->info()->data_type() == DataType::F16)
153  {
154  num_elems_written_per_iteration = 2;
155  }
156 
158 
160  AccessWindowHorizontal max_access(max->info(), 0, num_elems_written_per_iteration);
162  AccessWindowHorizontal sum_access(sum->info(), 0, num_elems_written_per_iteration);
163 
164  update_window_and_padding(win, input_access, max_access, output_access, sum_access);
165 
166  output_access.set_valid_region(win, input->info()->valid_region());
167  sum_access.set_valid_region(win, ValidRegion(Coordinates(), sum->info()->tensor_shape()));
168 
169  IGCKernel::configure(win);
170 }
171 
173 {
176 
178  Window slice = window_collapsed.first_slice_window_3D();
179 
180  _kernel.use();
181 
182  do
183  {
184  unsigned int idx = 0;
185  unsigned int binding = 1; // SSBO binding starts from 1.
186  // Set inputs
187  add_3D_tensor_argument(idx, _input, binding++, slice);
188  add_3D_tensor_argument(idx, _max, binding++, slice);
189  add_3D_tensor_argument(idx, _output, binding++, slice);
190  add_3D_tensor_argument(idx, _sum, binding++, slice);
191  _kernel.update_shader_params();
192  enqueue(*this, slice);
193  }
194  while(window_collapsed.slide_window_slice_3D(slice));
195 }
196 
198  : _input(nullptr), _sum(nullptr), _output(nullptr)
199 {
200 }
201 
203 {
206 
207  // Output auto initialization if not yet initialized
208  auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type());
209 
212 
213  _input = input;
214  _sum = sum;
215  _output = output;
216 
217  // Set build options
218  std::set<std::string> build_opts;
219  std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
220  build_opts.insert("#define " + dt_name);
221  build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
222  build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
223  build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
224  build_opts.insert("#define SOFTMAX_LAYER_NORM");
225 
226  // Create kernel
227  _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("softmax_layer_norm", build_opts));
228 
229  // Configure window
230  constexpr unsigned int num_elems_processed_per_iteration = 8;
231  unsigned int num_elems_written_per_iteration = 1;
232  if(input->info()->data_type() == DataType::F16)
233  {
234  num_elems_written_per_iteration = 2;
235  }
236 
238 
240  AccessWindowStatic sum_access(sum->info(), 0, 0, num_elems_written_per_iteration, sum->info()->dimension(1));
242 
243  update_window_and_padding(win, input_access, sum_access, output_access);
244 
245  output_access.set_valid_region(win, input->info()->valid_region());
246 
247  IGCKernel::configure(win);
248 }
249 
251 {
254 
256  Window slice = window_collapsed.first_slice_window_3D();
257 
258  _kernel.use();
259 
260  do
261  {
262  Window sum_slice = slice;
263  sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1));
264 
265  unsigned int idx = 0;
266  unsigned int binding = 1; // SSBO binding starts from 1.
267  // Set inputs
268  add_3D_tensor_argument(idx, _input, binding++, slice);
269  add_3D_tensor_argument(idx, _sum, binding++, slice);
270  add_3D_tensor_argument(idx, _output, binding++, slice);
271 
272  _kernel.update_shader_params();
273  enqueue(*this, slice);
274  }
275  while(window_collapsed.slide_window_slice_3D(slice));
276 }
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
Shape of a tensor.
Definition: TensorShape.h:39
void add_3D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: IGCKernel.cpp:132
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:543
void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint=CLKernelLibrary::get().default_ndrange(), bool use_dummy_work_items=false)
Add the kernel to the command queue with the given window.
Definition: ICLKernel.cpp:39
DATA_TYPE sum(__global const DATA_TYPE *input)
Calculate sum of a vector.
std::string to_string(T &&value)
Convert integer and float values to string.
1 channel, 1 F32 per channel
Interface for GLES Compute tensor.
Definition: IGCTensor.h:35
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:75
unsigned int num_arguments_per_3D_tensor() const
Returns the number of arguments enqueued per 3D tensor object.
Definition: IGCKernel.cpp:147
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps=Steps(), bool skip_border=false, BorderSize border_size=BorderSize())
Calculate the maximum window for a given tensor shape and border setting.
Definition: Helpers.cpp:28
Copyright (c) 2017-2020 ARM Limited.
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
Definition: Helpers.inl:202
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:441
1 channel, 1 F16 per channel
Implementation of a static rectangular access pattern.
void run(const Window &window) override
Enqueue the OpenGL ES shader to process the given window.
void run(const Window &window) override
Enqueue the OpenGL ES shader to process the given window.
cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set< std::string > &build_opts=std::set< std::string >())
Creates an opencl kernel using a compile context.
Definition: CLHelpers.cpp:387
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: Helpers.h:437
Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed=nullptr) const
Collapse the dimensions between first and last if possible.
Definition: Window.inl:68
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
auto ceil_to_multiple(S value, T divisor) -> decltype(((value+divisor - 1)/divisor) *divisor)
Computes the smallest number larger or equal to value that is a multiple of divisor.
Definition: Utils.h:67
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
Implementation of a row access pattern.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:286
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:333
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
static GCKernelLibrary & get()
Get the static instance of GCKernelLibrary.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
unsigned int num_elems_processed_per_iteration
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:78
Container for valid region of a window.
Definition: Types.h:187
void configure(const IGCTensor *input, IGCTensor *output)
Set the input and output tensors.
void configure(const IGCTensor *input, const IGCTensor *max, IGCTensor *output, IGCTensor *sum)
Set the input and output tensors.
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:289
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
void configure(const IGCTensor *input, const IGCTensor *sum, IGCTensor *output)
Set the input and output tensors.
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)