Compute Library
 21.02
GCActivationLayerKernel Class Reference

Interface for the activation layer kernel. More...

#include <GCActivationLayerKernel.h>

Collaboration diagram for GCActivationLayerKernel:
[legend]

Public Member Functions

 GCActivationLayerKernel (GCCoreRuntimeContext *ctx=nullptr)
 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 36 of file GCActivationLayerKernel.h.

Constructor & Destructor Documentation

◆ GCActivationLayerKernel() [1/3]

GCActivationLayerKernel ( GCCoreRuntimeContext ctx = nullptr)
explicit

Default constructor.

Parameters
[in,out]ctxCore context to use

Definition at line 44 of file GCActivationLayerKernel.cpp.

45  : _input(nullptr), _output(nullptr), _ctx(ctx)
46 {
47 }

◆ 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 49 of file GCActivationLayerKernel.cpp.

References ActivationLayerInfo::a(), ActivationLayerInfo::activation(), 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(), ActivationLayerInfo::b(), arm_compute::calculate_max_window(), arm_compute::create_opengl_kernel(), ITensorInfo::data_type(), ITensorInfo::element_size(), arm_compute::F16, arm_compute::F32, arm_compute::float_to_string_with_full_precision(), ITensor::info(), arm_compute::test::validation::input, num_elems_processed_per_iteration, 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().

50 {
52 
53  // Make sure _kernel is initialized before calling the parent's configure
54  _input = input;
55  _output = input;
56 
57  if(output != nullptr)
58  {
59  // Output auto inizialitation if not yet initialized
60  auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type());
61 
64 
65  _output = output;
66  }
67 
68  unsigned int num_elems_processed_per_iteration = 4 / input->info()->element_size();
69 
70  // Set build options
71  std::set<std::string> build_opts;
72  std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
73  build_opts.emplace(("#define " + string_from_activation_func(act_info.activation())));
74  build_opts.emplace(("#define " + dt_name));
75  build_opts.emplace(("#define A_VAL " + float_to_string_with_full_precision(act_info.a())));
76  build_opts.emplace(("#define B_VAL " + float_to_string_with_full_precision(act_info.b())));
77  build_opts.emplace(("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)));
78  build_opts.emplace(("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)));
79  build_opts.emplace(("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)));
80 
81  // Create kernel
82  _kernel = create_opengl_kernel(_ctx, "activation_layer", build_opts);
83 
84  // Configure kernel window
85  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
86 
87  if(output != nullptr)
88  {
90 
93  output_access);
94 
95  output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
96  }
97  else
98  {
101  }
102 
103  IGCKernel::configure(win);
104 }
GCKernel create_opengl_kernel(GCCoreRuntimeContext *ctx, const std::string &kernel_name, const std::set< std::string > &build_opts)
Creates an GLES kernel.
Definition: GCHelpers.cpp:37
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
float a() const
Get the alpha value.
Definition: Types.h:1590
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:163
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
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: WindowHelpers.h:46
std::string float_to_string_with_full_precision(float val)
Create a string with the float in full precision.
Definition: Utils.h:1262
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:543
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.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:441
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...
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
unsigned int num_elems_processed_per_iteration
ActivationFunction activation() const
Get the type of activation function.
Definition: Types.h:1585
float b() const
Get the beta value.
Definition: Types.h:1595
Container for valid region of a window.
Definition: Types.h:188
Describe a multidimensional execution window.
Definition: Window.h:39

◆ 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 106 of file GCActivationLayerKernel.cpp.

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().

107 {
110 
111  _kernel.use();
112 
113  _output->set_needs_shifting(true);
114 
116  Window slice = collapsed.first_slice_window_3D();
117  Window slice_in = collapsed.first_slice_window_3D();
118 
119  slice.shift(Window::DimX, -(_output->info()->padding()).left);
120 
121  if(_input == _output)
122  {
123  slice_in.shift(Window::DimX, -(_input->info()->padding()).left);
124  }
125 
126  do
127  {
128  unsigned int idx = 0;
129  unsigned int binding = 1;
130  add_3D_tensor_argument(idx, _input, binding++, slice);
131  add_3D_tensor_argument(idx, _output, binding++, slice_in);
132  _kernel.update_shader_params();
133  enqueue(*this, slice);
134  }
135  while(collapsed.slide_window_slice_3D(slice) && collapsed.slide_window_slice_3D(slice_in));
136 }
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&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: IGCKernel.cpp:132
void enqueue(IGCKernel &kernel, const Window &window, const gles::NDRange &lws=gles::NDRange(1U, 1U, 1U))
Add the kernel to the command queue with the given window.
Definition: IGCKernel.cpp:41
void shift(size_t dimension, int shift_value)
Shift the values of a given dimension by the given shift_value.
Definition: Window.inl:133
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:68
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&#39;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:335
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
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:291
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)

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