Compute Library
 19.08
GCActivationLayerKernel Class Reference

Interface for the activation layer kernel. More...

#include <GCActivationLayerKernel.h>

Collaboration diagram for GCActivationLayerKernel:
[legend]

Public Member Functions

 GCActivationLayerKernel ()
 Default constructor. More...
 
 GCActivationLayerKernel (const GCActivationLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
GCActivationLayerKerneloperator= (const GCActivationLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 GCActivationLayerKernel (GCActivationLayerKernel &&)=default
 Allow instances of this class to be moved. More...
 
GCActivationLayerKerneloperator= (GCActivationLayerKernel &&)=default
 Allow instances of this class to be moved. More...
 
 ~GCActivationLayerKernel ()=default
 Default destructor. More...
 
void configure (IGCTensor *input, IGCTensor *output, ActivationLayerInfo act_info)
 Set the input and output tensor. More...
 
void run (const Window &window) override
 Enqueue the OpenGL ES shader to process the given window. More...
 
- Public Member Functions inherited from IGCKernel
 IGCKernel ()
 Constructor. More...
 
GCKernelkernel ()
 Returns a reference to the GLES kernel of this object. More...
 
void add_1D_tensor_argument (unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
 Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
void add_2D_tensor_argument (unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
 Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
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. More...
 
unsigned int num_arguments_per_1D_tensor () const
 Returns the number of arguments enqueued per 1D tensor object. More...
 
unsigned int num_arguments_per_2D_tensor () const
 Returns the number of arguments enqueued per 2D tensor object. More...
 
unsigned int num_arguments_per_3D_tensor () const
 Returns the number of arguments enqueued per 3D tensor object. More...
 
void set_lws_hint (gles::NDRange &lws_hint)
 Set the Local-Workgroup-Size hint. More...
 
void set_target (GPUTarget target)
 Set the targeted GPU architecture. More...
 
GPUTarget get_target () const
 Get the targeted GPU architecture. More...
 
- Public Member Functions inherited from IKernel
 IKernel ()
 Constructor. More...
 
virtual ~IKernel ()=default
 Destructor. More...
 
virtual bool is_parallelisable () const
 Indicates whether or not the kernel is parallelisable. More...
 
virtual BorderSize border_size () const
 The size of the border for that kernel. More...
 
const Windowwindow () const
 The maximum window the kernel can be executed on. More...
 

Detailed Description

Interface for the activation layer kernel.

Definition at line 34 of file GCActivationLayerKernel.h.

Constructor & Destructor Documentation

◆ GCActivationLayerKernel() [1/3]

Default constructor.

Definition at line 42 of file GCActivationLayerKernel.cpp.

43  : _input(nullptr), _output(nullptr)
44 {
45 }

◆ GCActivationLayerKernel() [2/3]

Prevent instances of this class from being copied (As this class contains pointers)

◆ GCActivationLayerKernel() [3/3]

Allow instances of this class to be moved.

◆ ~GCActivationLayerKernel()

Default destructor.

Member Function Documentation

◆ configure()

void configure ( IGCTensor input,
IGCTensor output,
ActivationLayerInfo  act_info 
)

Set the input and output tensor.

Note
If the output tensor is a nullptr, the activation function will be performed in-place
Parameters
[in,out]inputSource tensor. In case of output tensor = nullptr, this tensor will store the result of the activation function. Data types supported: F16/F32.
[out]outputDestination tensor. Data type should match the input data type.
[in]act_infoActivation layer information.

Definition at line 47 of file GCActivationLayerKernel.cpp.

48 {
50 
51  // Make sure _kernel is initialized before calling the parent's configure
52  _input = input;
53  _output = input;
54 
55  if(output != nullptr)
56  {
57  // Output auto inizialitation if not yet initialized
58  auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type());
59 
62 
63  _output = output;
64  }
65 
66  unsigned int num_elems_processed_per_iteration = 4 / input->info()->element_size();
67 
68  // Set build options
69  std::set<std::string> build_opts;
70  std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
71  build_opts.emplace(("#define " + string_from_activation_func(act_info.activation())));
72  build_opts.emplace(("#define " + dt_name));
73  build_opts.emplace(("#define A_VAL " + float_to_string_with_full_precision(act_info.a())));
74  build_opts.emplace(("#define B_VAL " + float_to_string_with_full_precision(act_info.b())));
75  build_opts.emplace(("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)));
76  build_opts.emplace(("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)));
77  build_opts.emplace(("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)));
78 
79  // Create kernel
80  _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("activation_layer", build_opts));
81 
82  // Configure kernel window
83  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
84 
85  if(output != nullptr)
86  {
87  AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
88 
90  AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration),
91  output_access);
92 
93  output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
94  }
95  else
96  {
98  AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration));
99  }
100 
101  IGCKernel::configure(win);
102 }
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:543
std::string to_string(T &&value)
Convert integer and float values to string.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
const std::string & string_from_activation_func(ActivationLayerInfo::ActivationFunction act)
Translates a given activation function to a string.
Definition: Utils.cpp:170
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
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
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:201
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:441
1 channel, 1 F16 per channel
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: Helpers.h:402
std::string float_to_string_with_full_precision(float val)
Create a string with the float in full precision.
Definition: Utils.h:1066
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
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.
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
std::unique_ptr< Kernel > create_kernel()
Helper function to create and return a unique_ptr pointed to a CL/GLES kernel object.
Definition: Helpers.h:86
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:789
static GCKernelLibrary & get()
Get the static instance of GCKernelLibrary.
Container for valid region of a window.
Definition: Types.h:174
Describe a multidimensional execution window.
Definition: Window.h:39

References arm_compute::test::validation::act_info, ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES, ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES, arm_compute::auto_init_if_empty(), arm_compute::calculate_max_window(), arm_compute::create_kernel(), ITensorInfo::data_type(), ITensorInfo::element_size(), arm_compute::F16, arm_compute::F32, arm_compute::float_to_string_with_full_precision(), GCKernelLibrary::get(), ITensor::info(), ITensorInfo::set_valid_region(), arm_compute::string_from_activation_func(), ITensorInfo::tensor_shape(), arm_compute::support::cpp11::to_string(), and arm_compute::update_window_and_padding().

◆ operator=() [1/2]

GCActivationLayerKernel& operator= ( const GCActivationLayerKernel )
delete

Prevent instances of this class from being copied (As this class contains pointers)

◆ operator=() [2/2]

GCActivationLayerKernel& operator= ( GCActivationLayerKernel &&  )
default

Allow instances of this class to be moved.

◆ run()

void run ( const Window window)
overridevirtual

Enqueue the OpenGL ES shader to process the given window.

Parameters
[in]windowRegion on which to execute the kernel. (Must be a valid region of the window returned by window()).

Implements IGCKernel.

Definition at line 104 of file GCActivationLayerKernel.cpp.

105 {
108 
109  _kernel.use();
110 
111  _output->set_needs_shifting(true);
112 
114  Window slice = collapsed.first_slice_window_3D();
115  Window slice_in = collapsed.first_slice_window_3D();
116 
117  slice.shift(Window::DimX, -(_output->info()->padding()).left);
118 
119  if(_input == _output)
120  {
121  slice_in.shift(Window::DimX, -(_input->info()->padding()).left);
122  }
123 
124  do
125  {
126  unsigned int idx = 0;
127  unsigned int binding = 1;
128  add_3D_tensor_argument(idx, _input, binding++, slice);
129  add_3D_tensor_argument(idx, _output, binding++, slice_in);
130  _kernel.update_shader_params();
131  enqueue(*this, slice);
132  }
133  while(collapsed.slide_window_slice_3D(slice) && collapsed.slide_window_slice_3D(slice_in));
134 }
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
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
void shift(size_t dimension, int shift_value)
Shift the values of a given dimension by the given shift_value.
Definition: Window.inl:119
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
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
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:54
void set_needs_shifting(bool needs_shifting)
Set the flag indicating whether or not a tensor needs shifting.
Definition: IGCTensor.cpp:61
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
virtual PaddingSize padding() const =0
Padding of tensor.
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:319
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:275
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:940
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)

References IGCKernel::add_3D_tensor_argument(), ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, Window::collapse_if_possible(), Window::DimX, Window::DimZ, arm_compute::enqueue(), Window::first_slice_window_3D(), ITensor::info(), ITensorInfo::padding(), IGCTensor::set_needs_shifting(), Window::shift(), arm_compute::test::validation::reference::slice(), Window::slide_window_slice_3D(), and IKernel::window().


The documentation for this class was generated from the following files: