Compute Library
 19.08
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...
 
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 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 39 of file CLRNNLayer.h.

Constructor & Destructor Documentation

◆ CLRNNLayer()

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

Default constructor.

Definition at line 38 of file CLRNNLayer.cpp.

39  : _memory_group(std::move(memory_manager)), _gemm_state_f(), _add_kernel(), _activation_kernel(), _fully_connected_kernel(), _copy_kernel(), _fully_connected_out(), _gemm_output(), _add_output(),
40  _is_prepared(false)
41 {
42 }

Member Function Documentation

◆ configure()

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 69 of file CLRNNLayer.cpp.

71 {
72  ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, recurrent_weights, bias, hidden_state, output);
73  ARM_COMPUTE_ERROR_THROW_ON(CLRNNLayer::validate(input->info(), weights->info(), recurrent_weights->info(), bias->info(), hidden_state->info(), output->info(), info));
74 
76  TensorShape shape = compute_rnn_shape(recurrent_weights->info(), hidden_state->info()->dimension(idx_height));
77 
78  _is_prepared = false;
79 
80  _fully_connected_out.allocator()->init(TensorInfo(shape, 1, input->info()->data_type()));
81  _gemm_output.allocator()->init(TensorInfo(shape, 1, input->info()->data_type()));
82 
83  // Manage intermediate buffers and configure
84  _memory_group.manage(&_fully_connected_out);
85  _fully_connected_kernel.configure(input, weights, bias, &_fully_connected_out);
86 
87  _memory_group.manage(&_gemm_output);
88  _gemm_state_f.configure(hidden_state, recurrent_weights, nullptr, &_gemm_output, 1.f, 0.f);
89 
90  _add_output.allocator()->init(TensorInfo(shape, 1, input->info()->data_type()));
91  _memory_group.manage(&_add_output);
92 
93  _add_kernel.configure(ArithmeticOperation::ADD, &_fully_connected_out, &_gemm_output, &_add_output, ConvertPolicy::SATURATE);
94 
95  _fully_connected_out.allocator()->allocate();
96  _gemm_output.allocator()->allocate();
97 
98  _activation_kernel.configure(&_add_output, hidden_state, info);
99  _add_output.allocator()->allocate();
100 
101  _copy_kernel.configure(hidden_state, output);
102 }
Shape of a tensor.
Definition: TensorShape.h:39
TensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: CLTensor.cpp:35
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
TensorShape compute_rnn_shape(const ITensorInfo *input, const unsigned int batch_size)
Calculate the RNN shape of a tensor.
void configure(const ICLTensor *input, ICLTensor *output, const PaddingList &padding=PaddingList(), Window *output_window=nullptr)
Initialize the kernel's input, output.
CLTensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: CLTensor.cpp:55
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:327
void init(const TensorInfo &input, size_t alignment=0)
Initialize a tensor based on the passed TensorInfo.
void configure(ICLTensor *input, ICLTensor *output, ActivationLayerInfo act_info)
Set the input and output tensor.
void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo())
Set the input and output tensors.
void manage(TensorType *obj)
Sets a object to be managed by the given memory group.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
void allocate() override
Allocate size specified by TensorInfo of OpenCL memory.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
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's inputs and output.
Definition: CLGEMM.cpp:470
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:44
void configure(ArithmeticOperation op, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const ConvertPolicy &policy)
Static function to check if given info will lead to a valid configuration of CLSaturatedArithmeticOpe...
Store the tensor's metadata.
Definition: TensorInfo.h:45
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:326
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.

References arm_compute::ADD, CLTensorAllocator::allocate(), CLTensor::allocator(), ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::test::validation::bias, arm_compute::misc::shape_calculator::compute_rnn_shape(), CLCopyKernel::configure(), CLActivationLayerKernel::configure(), CLGEMM::configure(), CLFullyConnectedLayer::configure(), CLSaturatedArithmeticOperationKernel::configure(), ITensorInfo::data_layout(), ITensorInfo::data_type(), ITensorInfo::dimension(), arm_compute::get_data_layout_dimension_index(), arm_compute::HEIGHT, ITensor::info(), CLTensor::info(), arm_compute::test::validation::info, ITensorAllocator::init(), MemoryGroupBase< TensorType >::manage(), arm_compute::SATURATE, arm_compute::test::validation::shape, CLRNNLayer::validate(), and arm_compute::test::validation::weights.

◆ 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 119 of file CLRNNLayer.cpp.

120 {
121  if(!_is_prepared)
122  {
123  _fully_connected_kernel.prepare();
124  _gemm_state_f.prepare();
125 
126  _is_prepared = true;
127  }
128 }
void prepare() override
Prepare the function for executing.
Definition: CLGEMM.cpp:632
void prepare() override
Prepare the function for executing.

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

Referenced by CLRNNLayer::run().

◆ 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 104 of file CLRNNLayer.cpp.

105 {
106  prepare();
107 
108  MemoryGroupResourceScope scope_mg(_memory_group);
109 
110  _fully_connected_kernel.run();
111  _gemm_state_f.run();
112  CLScheduler::get().enqueue(_add_kernel);
113  CLScheduler::get().enqueue(_activation_kernel);
114 
115  // copy hidden out to output
116  CLScheduler::get().enqueue(_copy_kernel);
117 }
void run() override
Run the kernels contained in the function.
Definition: CLGEMM.cpp:572
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:41
void prepare() override
Prepare the function for executing.
Definition: CLRNNLayer.cpp:119
void run() override
Run the kernels contained in the function.
void enqueue(ICLKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
Definition: CLScheduler.cpp:95
Memory group resources scope handling class.
Definition: IMemoryGroup.h:46

References CLScheduler::enqueue(), CLScheduler::get(), CLRNNLayer::prepare(), CLGEMM::run(), and CLFullyConnectedLayer::run().

◆ 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 44 of file CLRNNLayer.cpp.

46 {
49  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, recurrent_weights, bias, hidden_state, output);
50  ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(idx_width) != weights->dimension(idx_width));
51  ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_height) != recurrent_weights->dimension(idx_width));
52  ARM_COMPUTE_RETURN_ERROR_ON(recurrent_weights->dimension(idx_width) != recurrent_weights->dimension(1));
53  ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() != 1);
54  ARM_COMPUTE_RETURN_ERROR_ON(bias->dimension(idx_width) != weights->dimension(idx_height));
55  ARM_COMPUTE_RETURN_ERROR_ON(hidden_state->dimension(idx_width) != weights->dimension(idx_height));
56  ARM_COMPUTE_RETURN_ERROR_ON(hidden_state->dimension(idx_height) != input->dimension(idx_height));
58 
59  auto shape_info = TensorInfo(compute_rnn_shape(recurrent_weights, hidden_state->dimension(idx_height)), 1, input->data_type());
60 
62  ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(hidden_state, recurrent_weights, nullptr, &shape_info, 1.f, 0.f));
65 
66  return Status{};
67 }
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:193
virtual DataType data_type() const =0
Data type used for each element of the tensor.
TensorShape compute_rnn_shape(const ITensorInfo *input, const unsigned int batch_size)
Calculate the RNN shape of a tensor.
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:244
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 CLActivationLayerKernel.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
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.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy)
Static function to check if given info will lead to a valid configuration of CLSaturatedArithmeticOpe...
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
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:525
Store the tensor's metadata.
Definition: TensorInfo.h:45
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:326
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.

References arm_compute::ADD, ARM_COMPUTE_RETURN_ERROR_ON, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS, ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR, ARM_COMPUTE_RETURN_ON_ERROR, arm_compute::test::validation::bias, arm_compute::misc::shape_calculator::compute_rnn_shape(), ITensorInfo::data_layout(), ITensorInfo::data_type(), ITensorInfo::dimension(), arm_compute::get_data_layout_dimension_index(), arm_compute::HEIGHT, arm_compute::test::validation::info, arm_compute::SATURATE, ITensorInfo::tensor_shape(), CLActivationLayerKernel::validate(), CLGEMM::validate(), CLFullyConnectedLayer::validate(), CLSaturatedArithmeticOperationKernel::validate(), arm_compute::test::validation::weights, and arm_compute::WIDTH.

Referenced by CLRNNLayer::configure().


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