Compute Library
 21.02
CLRNNLayer Class Reference

Basic function to run CLRNNLayer. More...

#include <CLRNNLayer.h>

Collaboration diagram for CLRNNLayer:
[legend]

Public Member Functions

 CLRNNLayer (std::shared_ptr< IMemoryManager > memory_manager=nullptr)
 Default constructor. More...
 
 CLRNNLayer (const CLRNNLayer &)=delete
 Prevent instances of this class from being copied. More...
 
CLRNNLayeroperator= (const CLRNNLayer &)=delete
 Prevent instances of this class from being copied. More...
 
 ~CLRNNLayer ()
 Default destructor. More...
 
void configure (const ICLTensor *input, const ICLTensor *weights, const ICLTensor *recurrent_weights, const ICLTensor *bias, ICLTensor *hidden_state, ICLTensor *output, ActivationLayerInfo &info)
 Initialize the function. More...
 
void configure (const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *recurrent_weights, const ICLTensor *bias, ICLTensor *hidden_state, ICLTensor *output, ActivationLayerInfo &info)
 Initialize the function. More...
 
void run () override
 Run the kernels contained in the function. More...
 
void prepare () override
 Prepare the function for executing. More...
 
- Public Member Functions inherited from IFunction
virtual ~IFunction ()=default
 Destructor. More...
 

Static Public Member Functions

static Status validate (const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *recurrent_weights, const ITensorInfo *bias, const ITensorInfo *hidden_state, const ITensorInfo *output, const ActivationLayerInfo &info)
 Initialize the function. More...
 

Detailed Description

Basic function to run CLRNNLayer.

Definition at line 41 of file CLRNNLayer.h.

Constructor & Destructor Documentation

◆ CLRNNLayer() [1/2]

CLRNNLayer ( std::shared_ptr< IMemoryManager memory_manager = nullptr)

Default constructor.

Definition at line 48 of file CLRNNLayer.cpp.

49  : _memory_group(std::move(memory_manager)), _gemm_state_f(), _add_kernel(), _activation(), _fully_connected_kernel(), _copy(), _fully_connected_out(), _gemm_output(), _add_output(),
50  _is_prepared(false)
51 {
52 }

◆ CLRNNLayer() [2/2]

CLRNNLayer ( const CLRNNLayer )
delete

Prevent instances of this class from being copied.

◆ ~CLRNNLayer()

~CLRNNLayer ( )
default

Default destructor.

Member Function Documentation

◆ configure() [1/2]

void configure ( const ICLTensor input,
const ICLTensor weights,
const ICLTensor recurrent_weights,
const ICLTensor bias,
ICLTensor hidden_state,
ICLTensor output,
ActivationLayerInfo info 
)

Initialize the function.

Parameters
[in]inputInput is a 2-D tensor of shape [input_size, batch_size]. Data types supported: F16/F32
[in]weightsWeights tensor of shape [input_size, num_units] that multiplies the input. Data types supported: Same as input
[in]recurrent_weightsWeights tensor of shape [num_units, num_units] that multiplies the current 'state'. Data types supported: Same as input
[in]biasBias vector of shape [num_units]. Data types supported: Same as input
[out]outputOutput tensor of shape [num_units, batch_size]. Data types supported: Same as input
[in,out]hidden_stateOutput tensor of shape [num_units, batch_size]. Data types supported: Same as input
[in]infoActivation layer parameter.

Definition at line 85 of file CLRNNLayer.cpp.

References CLKernelLibrary::get().

87 {
88  configure(CLKernelLibrary::get().get_compile_context(), input, weights, recurrent_weights, bias, hidden_state, output, info);
89 }
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *recurrent_weights, const ICLTensor *bias, ICLTensor *hidden_state, ICLTensor *output, ActivationLayerInfo &info)
Initialize the function.
Definition: CLRNNLayer.cpp:85

◆ configure() [2/2]

void configure ( const CLCompileContext compile_context,
const ICLTensor input,
const ICLTensor weights,
const ICLTensor recurrent_weights,
const ICLTensor bias,
ICLTensor hidden_state,
ICLTensor output,
ActivationLayerInfo info 
)

Initialize the function.

Parameters
[in]compile_contextThe compile context to be used.
[in]inputInput is a 2-D tensor of shape [input_size, batch_size]. Data types supported: F16/F32
[in]weightsWeights tensor of shape [input_size, num_units] that multiplies the input. Data types supported: Same as input
[in]recurrent_weightsWeights tensor of shape [num_units, num_units] that multiplies the current 'state'. Data types supported: Same as input
[in]biasBias vector of shape [num_units]. Data types supported: Same as input
[out]outputOutput tensor of shape [num_units, batch_size]. Data types supported: Same as input
[in,out]hidden_stateOutput tensor of shape [num_units, batch_size]. Data types supported: Same as input
[in]infoActivation layer parameter.

Definition at line 91 of file CLRNNLayer.cpp.

References CLTensorAllocator::allocate(), CLTensor::allocator(), ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::misc::shape_calculator::compute_rnn_shape(), CLCopy::configure(), CLActivationLayer::configure(), CLArithmeticAddition::configure(), CLGEMM::configure(), CLFullyConnectedLayer::configure(), ITensorInfo::data_layout(), ITensorInfo::data_type(), ITensorInfo::dimension(), arm_compute::get_data_layout_dimension_index(), arm_compute::HEIGHT, arm_compute::test::validation::idx_height, ITensor::info(), arm_compute::test::validation::info, ITensorAllocator::init(), MemoryGroup::manage(), arm_compute::SATURATE, arm_compute::test::validation::shape, and CLRNNLayer::validate().

94 {
95  ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, recurrent_weights, bias, hidden_state, output);
96  ARM_COMPUTE_ERROR_THROW_ON(CLRNNLayer::validate(input->info(), weights->info(), recurrent_weights->info(), bias->info(), hidden_state->info(), output->info(), info));
97 
99  TensorShape shape = compute_rnn_shape(recurrent_weights->info(), hidden_state->info()->dimension(idx_height));
100 
101  _is_prepared = false;
102 
103  _fully_connected_out.allocator()->init(TensorInfo(shape, 1, input->info()->data_type()));
104  _gemm_output.allocator()->init(TensorInfo(shape, 1, input->info()->data_type()));
105 
106  // Manage intermediate buffers and configure
107  _memory_group.manage(&_fully_connected_out);
108  _fully_connected_kernel.configure(compile_context, input, weights, bias, &_fully_connected_out);
109 
110  _memory_group.manage(&_gemm_output);
111  _gemm_state_f.configure(compile_context, hidden_state, recurrent_weights, nullptr, &_gemm_output, 1.f, 0.f);
112 
113  _add_output.allocator()->init(TensorInfo(shape, 1, input->info()->data_type()));
114  _memory_group.manage(&_add_output);
115 
116  _add_kernel.configure(compile_context, &_fully_connected_out, &_gemm_output, &_add_output, ConvertPolicy::SATURATE);
117 
118  _fully_connected_out.allocator()->allocate();
119  _gemm_output.allocator()->allocate();
120 
121  _activation.configure(compile_context, &_add_output, hidden_state, info);
122  _add_output.allocator()->allocate();
123 
124  _copy.configure(compile_context, hidden_state, output);
125 }
TensorShape compute_rnn_shape(const ITensorInfo *input, const unsigned int batch_size)
Calculate the RNN shape of a tensor.
CLTensorAllocator * allocator()
Return a pointer to the tensor&#39;s allocator.
Definition: CLTensor.cpp:61
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
void init(const TensorInfo &input, size_t alignment=0)
Initialize a tensor based on the passed TensorInfo.
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo())
Set the input and output tensors.
void configure(ICLTensor *input, ICLTensor *output, Window *dst_window=nullptr)
Initialise the function&#39;s source and destination.
Definition: CLCopy.cpp:52
void configure(ICLTensor *input1, ICLTensor *input2, ICLTensor *output, ConvertPolicy policy, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Initialise the kernel&#39;s inputs, output and conversion policy.
void allocate() override
Allocate size specified by TensorInfo of OpenCL memory.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
void configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info=GEMMInfo())
Initialise the kernel&#39;s inputs and output.
Definition: CLGEMM.cpp:666
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *recurrent_weights, const ITensorInfo *bias, const ITensorInfo *hidden_state, const ITensorInfo *output, const ActivationLayerInfo &info)
Initialize the function.
Definition: CLRNNLayer.cpp:56
void configure(ICLTensor *input, ICLTensor *output, ActivationLayerInfo act_info)
Set the input and output tensor.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193

◆ operator=()

CLRNNLayer& operator= ( const CLRNNLayer )
delete

Prevent instances of this class from being copied.

◆ prepare()

void prepare ( )
overridevirtual

Prepare the function for executing.

Any one off pre-processing step required by the function is handled here

Note
Prepare stage might not need all the function's buffers' backing memory to be available in order to execute

Reimplemented from IFunction.

Definition at line 142 of file CLRNNLayer.cpp.

References CLGEMM::prepare(), and CLFullyConnectedLayer::prepare().

Referenced by CLRNNLayer::run().

143 {
144  if(!_is_prepared)
145  {
146  _fully_connected_kernel.prepare();
147  _gemm_state_f.prepare();
148 
149  _is_prepared = true;
150  }
151 }
void prepare() override
Prepare the function for executing.
Definition: CLGEMM.cpp:870
void prepare() override
Prepare the function for executing.

◆ run()

void run ( )
overridevirtual

Run the kernels contained in the function.

For Neon kernels:

  • Multi-threading is used for the kernels which are parallelisable.
  • By default std::thread::hardware_concurrency() threads are used.
Note
CPPScheduler::set_num_threads() can be used to manually set the number of threads

For OpenCL kernels:

  • All the kernels are enqueued on the queue associated with CLScheduler.
  • The queue is then flushed.
Note
The function will not block until the kernels are executed. It is the user's responsibility to wait.
Will call prepare() on first run if hasn't been done

Implements IFunction.

Definition at line 127 of file CLRNNLayer.cpp.

References CLRNNLayer::prepare(), CLCopy::run(), CLActivationLayer::run(), CLArithmeticAddition::run(), CLGEMM::run(), and CLFullyConnectedLayer::run().

128 {
129  prepare();
130 
131  MemoryGroupResourceScope scope_mg(_memory_group);
132 
133  _fully_connected_kernel.run();
134  _gemm_state_f.run();
135  _add_kernel.run();
136  _activation.run();
137 
138  // copy hidden out to output
139  _copy.run();
140 }
void run() override
Run the kernels contained in the function.
void run() override
Run the kernels contained in the function.
Definition: CLGEMM.cpp:778
void run() override
Run the kernels contained in the function.
Definition: CLCopy.cpp:73
void run() override
Run the kernels contained in the function.
void prepare() override
Prepare the function for executing.
Definition: CLRNNLayer.cpp:142
void run() override
Run the kernels contained in the function.

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo weights,
const ITensorInfo recurrent_weights,
const ITensorInfo bias,
const ITensorInfo hidden_state,
const ITensorInfo output,
const ActivationLayerInfo info 
)
static

Initialize the function.

Parameters
[in]inputInput is a 2-D tensor of shape [input_size, batch_size]. Data types supported: F16/F32
[in]weightsWeights tensor of shape [input_size, num_units] that multiplies the input. Data types supported: Same as input
[in]recurrent_weightsWeights tensor of shape [num_units, num_units] that multiplies the current 'state'. Data types supported: Same as input
[in]biasBias vector of shape [num_units]. Data types supported: Same as input
[in]outputOutput tensor of shape [num_units, batch_size]. Data types supported: Same as input
[in]hidden_stateOutput tensor of shape [num_units, batch_size]. Data types supported: Same as input
[in]infoActivation layer parameter.
Returns
a status

Definition at line 56 of file CLRNNLayer.cpp.

References ARM_COMPUTE_RETURN_ERROR_ON, ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS, ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR, ARM_COMPUTE_RETURN_ON_ERROR, arm_compute::misc::shape_calculator::compute_rnn_shape(), ITensorInfo::data_layout(), ITensorInfo::data_type(), ITensorInfo::dimension(), arm_compute::F16, arm_compute::F32, arm_compute::get_data_layout_dimension_index(), arm_compute::HEIGHT, arm_compute::test::validation::idx_height, arm_compute::test::validation::idx_width, ITensorInfo::num_dimensions(), arm_compute::SATURATE, ITensorInfo::tensor_shape(), CLActivationLayer::validate(), CLArithmeticAddition::validate(), CLGEMM::validate(), CLFullyConnectedLayer::validate(), and arm_compute::WIDTH.

Referenced by CLRNNLayer::configure(), and arm_compute::test::validation::DATA_TEST_CASE().

58 {
59  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, recurrent_weights, bias, hidden_state, output);
61  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, recurrent_weights, bias, hidden_state, output);
62 
65 
66  ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(idx_width) != weights->dimension(idx_width));
67  ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_height) != recurrent_weights->dimension(idx_width));
68  ARM_COMPUTE_RETURN_ERROR_ON(recurrent_weights->dimension(idx_width) != recurrent_weights->dimension(1));
69  ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() != 1);
70  ARM_COMPUTE_RETURN_ERROR_ON(bias->dimension(idx_width) != weights->dimension(idx_height));
71  ARM_COMPUTE_RETURN_ERROR_ON(hidden_state->dimension(idx_width) != weights->dimension(idx_height));
72  ARM_COMPUTE_RETURN_ERROR_ON(hidden_state->dimension(idx_height) != input->dimension(idx_height));
73  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), hidden_state->tensor_shape());
74 
75  auto shape_info = TensorInfo(compute_rnn_shape(recurrent_weights, hidden_state->dimension(idx_height)), 1, input->data_type());
76 
78  ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(hidden_state, recurrent_weights, nullptr, &shape_info, 1.f, 0.f));
81 
82  return Status{};
83 }
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
Static function to check if given info will lead to a valid configuration of CLActivationLayer.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
1 channel, 1 F32 per channel
TensorShape compute_rnn_shape(const ITensorInfo *input, const unsigned int batch_size)
Calculate the RNN shape of a tensor.
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
1 channel, 1 F16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo())
Static function to check if given info will lead to a valid configuration of CLFullyConnectedLayer.
static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, ConvertPolicy policy, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Static function to check if given info will lead to a valid configuration of opencl::kernels::ClSatur...
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info=GEMMInfo())
Static function to check if given info will lead to a valid configuration of CLGEMM.
Definition: CLGEMM.cpp:727
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:694
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193

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