• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018-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  */
24 #ifndef ARM_COMPUTE_CLRNN_LAYER_H
25 #define ARM_COMPUTE_CLRNN_LAYER_H
26 
27 #include "arm_compute/runtime/CL/ICLSimpleFunction.h"
28 #include "arm_compute/runtime/CL/functions/CLActivationLayer.h"
29 #include "arm_compute/runtime/CL/functions/CLElementwiseOperations.h"
30 #include "arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h"
31 #include "arm_compute/runtime/CL/functions/CLGEMM.h"
32 
33 #include <memory>
34 
35 namespace arm_compute
36 {
37 class CLCopyKernel;
38 class ICLTensor;
39 
40 /** Basic function to run @ref CLRNNLayer */
41 class CLRNNLayer : public IFunction
42 {
43 public:
44     /** Default constructor */
45     CLRNNLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
46     /** Prevent instances of this class from being copied */
47     CLRNNLayer(const CLRNNLayer &) = delete;
48     /** Prevent instances of this class from being copied */
49     CLRNNLayer &operator=(const CLRNNLayer &) = delete;
50     /** Default destructor */
51     ~CLRNNLayer();
52     /** Initialize the function
53      *
54      * @param[in]     input             Input is a 2-D tensor of shape [input_size, batch_size]. Data types supported: F16/F32
55      * @param[in]     weights           Weights tensor of shape [input_size, num_units] that multiplies the input. Data types supported: Same as @p input
56      * @param[in]     recurrent_weights Weights tensor of shape [num_units, num_units] that multiplies the current 'state'. Data types supported: Same as @p input
57      * @param[in]     bias              Bias vector of shape [num_units]. Data types supported: Same as @p input
58      * @param[out]    output            Output tensor of shape [num_units, batch_size]. Data types supported: Same as @p input
59      * @param[in,out] hidden_state      Output tensor of shape [num_units, batch_size]. Data types supported: Same as @p input
60      * @param[in]     info              Activation layer parameter.
61      */
62     void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *recurrent_weights, const ICLTensor *bias, ICLTensor *hidden_state, ICLTensor *output, ActivationLayerInfo &info);
63     /** Initialize the function
64      *
65      * @param[in]     compile_context   The compile context to be used.
66      * @param[in]     input             Input is a 2-D tensor of shape [input_size, batch_size]. Data types supported: F16/F32
67      * @param[in]     weights           Weights tensor of shape [input_size, num_units] that multiplies the input. Data types supported: Same as @p input
68      * @param[in]     recurrent_weights Weights tensor of shape [num_units, num_units] that multiplies the current 'state'. Data types supported: Same as @p input
69      * @param[in]     bias              Bias vector of shape [num_units]. Data types supported: Same as @p input
70      * @param[out]    output            Output tensor of shape [num_units, batch_size]. Data types supported: Same as @p input
71      * @param[in,out] hidden_state      Output tensor of shape [num_units, batch_size]. Data types supported: Same as @p input
72      * @param[in]     info              Activation layer parameter.
73      */
74     void configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *recurrent_weights, const ICLTensor *bias, ICLTensor *hidden_state,
75                    ICLTensor *output, ActivationLayerInfo &info);
76     /** Initialize the function
77      *
78      * @param[in] input             Input is a 2-D tensor of shape [input_size, batch_size]. Data types supported: F16/F32
79      * @param[in] weights           Weights tensor of shape [input_size, num_units] that multiplies the input. Data types supported: Same as @p input
80      * @param[in] recurrent_weights Weights tensor of shape [num_units, num_units] that multiplies the current 'state'. Data types supported: Same as @p input
81      * @param[in] bias              Bias vector of shape [num_units]. Data types supported: Same as @p input
82      * @param[in] output            Output tensor of shape [num_units, batch_size]. Data types supported: Same as @p input
83      * @param[in] hidden_state      Output tensor of shape [num_units, batch_size]. Data types supported: Same as @p input
84      * @param[in] info              Activation layer parameter.
85      *
86      * @return a status
87      */
88     static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *recurrent_weights, const ITensorInfo *bias, const ITensorInfo *hidden_state, const ITensorInfo *output,
89                            const ActivationLayerInfo &info);
90 
91     // Inherited methods overridden:
92     void run() override;
93     void prepare() override;
94 
95 private:
96     MemoryGroup                   _memory_group;
97     CLGEMM                        _gemm_state_f;
98     CLArithmeticAddition          _add_kernel;
99     CLActivationLayer             _activation;
100     CLFullyConnectedLayer         _fully_connected_kernel;
101     std::unique_ptr<CLCopyKernel> _copy_kernel;
102     CLTensor                      _fully_connected_out;
103     CLTensor                      _gemm_output;
104     CLTensor                      _add_output;
105     bool                          _is_prepared;
106 };
107 }
108 #endif /* ARM_COMPUTE_CLRNN_LAYER_H */
109