Compute Library
 23.11
InfoHelpers.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-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_MISC_INFO_HELPERS_H
25 #define ARM_COMPUTE_MISC_INFO_HELPERS_H
26 
27 #include "arm_compute/core/Error.h"
28 #include "arm_compute/core/Types.h"
30 
31 namespace arm_compute
32 {
33 namespace utils
34 {
35 namespace info_helpers
36 {
37 /** Checks if activation information correspond to a relu activation function
38  *
39  * @param[in] activation_info Activation metadata
40  *
41  * @return True if activation metadata correspond to a relu activation else false
42  */
43 inline bool is_relu(ActivationLayerInfo activation_info)
44 {
45  return activation_info.enabled() && activation_info.activation() == ActivationLayerInfo::ActivationFunction::RELU;
46 }
47 
48 /** Checks if activation information correspond to a relu6 activation function
49  *
50  * @param[in] activation_info Activation metadata
51  *
52  * @return True if activation metadata correspond to a relu6 activation else false
53  */
54 inline bool is_relu6(ActivationLayerInfo activation_info)
55 {
56  const bool is_lu_bounded_relu =
57  activation_info.activation() == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU &&
58  activation_info.a() == 6.f && activation_info.b() == 0.f;
59  const bool is_bounded_relu =
60  activation_info.activation() == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU &&
61  activation_info.a() == 6.f;
62  return activation_info.enabled() && (is_lu_bounded_relu || is_bounded_relu);
63 }
64 
65 /** Build LSTMParams<ITensorInfo> object by extracting the metadata from each
66  * tensor.
67  *
68  * @param[in] lstm_params The LSTMParams<T> object containing the tensors.
69  * @param[out] lstm_params_info The LSTMParams<ITensorInfo> to be constructed.
70  *
71  */
72 template <typename T>
73 inline void build_lstm_params_tensor_info(const LSTMParams<T> &lstm_params, LSTMParams<ITensorInfo> *lstm_params_info)
74 {
75  if (lstm_params.has_peephole_opt())
76  {
78  lstm_params_info->set_peephole_params(lstm_params.cell_to_forget_weights()->info(),
79  lstm_params.cell_to_output_weights()->info());
80  }
81  if (lstm_params.has_projection())
82  {
84  lstm_params_info->set_projection_params(
85  lstm_params.projection_weights()->info(),
86  lstm_params.projection_bias() != nullptr ? lstm_params.projection_bias()->info() : nullptr);
87  }
88  if (!lstm_params.has_cifg_opt())
89  {
91  lstm_params.input_gate_bias());
92 
93  ITensorInfo *cell_to_input_weights_info =
94  (lstm_params.has_peephole_opt()) ? lstm_params.cell_to_input_weights()->info() : nullptr;
95  lstm_params_info->set_cifg_params(lstm_params.input_to_input_weights()->info(),
96  lstm_params.recurrent_to_input_weights()->info(), cell_to_input_weights_info,
97  lstm_params.input_gate_bias()->info());
98  }
99  if (lstm_params.use_layer_norm())
100  {
102  lstm_params.cell_layer_norm_weights());
103  if (!lstm_params.has_cifg_opt())
104  {
106  }
107 
108  ITensorInfo *forget_info = lstm_params.forget_layer_norm_weights()->info();
109  ITensorInfo *cell_info = lstm_params.cell_layer_norm_weights()->info();
110  ITensorInfo *output_info = lstm_params.output_layer_norm_weights()->info();
111  ITensorInfo *input_info = lstm_params.has_cifg_opt() ? nullptr : lstm_params.input_layer_norm_weights()->info();
112 
113  lstm_params_info->set_layer_normalization_params(input_info, forget_info, cell_info, output_info);
114  }
115 
116  lstm_params_info->set_matmul_scale_params(
117  lstm_params.input_intermediate_scale(), lstm_params.forget_intermediate_scale(),
118  lstm_params.cell_intermediate_scale(), lstm_params.output_intermediate_scale());
119 
120  lstm_params_info->set_hidden_state_params(lstm_params.hidden_state_zero(), lstm_params.hidden_state_scale());
121 }
122 } // namespace info_helpers
123 } // namespace utils
124 } // namespace arm_compute
125 #endif /* ARM_COMPUTE_MISC_INFO_HELPERS_H */
arm_compute::test::validation::input_info
input_info
Definition: DirectConvolutionLayer.cpp:547
arm_compute::LSTMParams::forget_intermediate_scale
float forget_intermediate_scale() const
Definition: LSTMParams.h:278
Types.h
arm_compute::utils::info_helpers::is_relu
bool is_relu(ActivationLayerInfo activation_info)
Checks if activation information correspond to a relu activation function.
Definition: InfoHelpers.h:43
arm_compute::LSTMParams::cell_to_input_weights
T * cell_to_input_weights() const
Definition: LSTMParams.h:213
arm_compute::LSTMParams::cell_layer_norm_weights
T * cell_layer_norm_weights() const
Definition: LSTMParams.h:253
arm_compute::LSTMParams::output_layer_norm_weights
T * output_layer_norm_weights() const
Definition: LSTMParams.h:258
arm_compute::ActivationLayerInfo::activation
ActivationFunction activation() const
Get the type of activation function.
Definition: ActivationLayerInfo.h:75
Error.h
LSTMParams.h
arm_compute::ActivationLayerInfo
Activation Layer Information class.
Definition: ActivationLayerInfo.h:55
arm_compute::LSTMParams::input_intermediate_scale
float input_intermediate_scale() const
Definition: LSTMParams.h:273
ARM_COMPUTE_ERROR_ON_NULLPTR
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
arm_compute::test::validation::output_info
output_info
Definition: DirectConvolutionLayer.cpp:547
arm_compute::LSTMParams::cell_to_output_weights
T * cell_to_output_weights() const
Definition: LSTMParams.h:228
arm_compute::ActivationLayerInfo::enabled
bool enabled() const
Check if initialised.
Definition: ActivationLayerInfo.h:90
arm_compute::LSTMParams::projection_weights
const T * projection_weights() const
Definition: LSTMParams.h:233
arm_compute::utils::info_helpers::is_relu6
bool is_relu6(ActivationLayerInfo activation_info)
Checks if activation information correspond to a relu6 activation function.
Definition: InfoHelpers.h:54
arm_compute::LSTMParams::use_layer_norm
bool use_layer_norm() const
Definition: LSTMParams.h:318
arm_compute::utils::info_helpers::build_lstm_params_tensor_info
void build_lstm_params_tensor_info(const LSTMParams< T > &lstm_params, LSTMParams< ITensorInfo > *lstm_params_info)
Build LSTMParams<ITensorInfo> object by extracting the metadata from each tensor.
Definition: InfoHelpers.h:73
arm_compute::LSTMParams::set_projection_params
LSTMParams & set_projection_params(const T *projection_weights, const T *projection_bias)
Set projection tensor parameters.
Definition: LSTMParams.h:101
arm_compute::LSTMParams::has_peephole_opt
bool has_peephole_opt() const
Definition: LSTMParams.h:303
arm_compute::LSTMParams::set_cifg_params
LSTMParams & set_cifg_params(const T *input_to_input_weights, const T *recurrent_to_input_weights, T *cell_to_input_weights, const T *input_gate_bias)
Set CIFG tensor parameters.
Definition: LSTMParams.h:82
arm_compute::LSTMParams::has_cifg_opt
bool has_cifg_opt() const
Definition: LSTMParams.h:313
arm_compute::LSTMParams::recurrent_to_input_weights
const T * recurrent_to_input_weights() const
Definition: LSTMParams.h:208
arm_compute::LSTMParams::hidden_state_zero
int32_t hidden_state_zero() const
Definition: LSTMParams.h:293
arm_compute::ActivationLayerInfo::a
float a() const
Get the alpha value.
Definition: ActivationLayerInfo.h:80
arm_compute::LSTMParams::set_layer_normalization_params
LSTMParams & set_layer_normalization_params(T *input_layer_norm_weights, T *forget_layer_norm_weights, T *cell_layer_norm_weights, T *output_layer_norm_weights)
Set layer normalization tensor parameters.
Definition: LSTMParams.h:131
arm_compute::LSTMParams::input_layer_norm_weights
T * input_layer_norm_weights() const
Definition: LSTMParams.h:243
arm_compute::LSTMParams::output_intermediate_scale
float output_intermediate_scale() const
Definition: LSTMParams.h:288
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::LSTMParams::forget_layer_norm_weights
T * forget_layer_norm_weights() const
Definition: LSTMParams.h:248
arm_compute::LSTMParams::has_projection
bool has_projection() const
Definition: LSTMParams.h:308
arm_compute::LSTMParams::set_matmul_scale_params
LSTMParams & set_matmul_scale_params(float input_intermediate_scale, float forget_intermediate_scale, float cell_intermediate_scale, float output_intermediate_scale)
Set scale of the intermediate results of matmul of each layer parameters.
Definition: LSTMParams.h:177
arm_compute::LSTMParams::hidden_state_scale
float hidden_state_scale() const
Definition: LSTMParams.h:298
arm_compute::LSTMParams::cell_to_forget_weights
T * cell_to_forget_weights() const
Definition: LSTMParams.h:223
arm_compute::LSTMParams
Definition: LSTMParams.h:36
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:44
arm_compute::LSTMParams::cell_intermediate_scale
float cell_intermediate_scale() const
Definition: LSTMParams.h:283
arm_compute::LSTMParams::set_peephole_params
LSTMParams & set_peephole_params(T *cell_to_forget_weights, T *cell_to_output_weights)
Set peephole tensor parameters.
Definition: LSTMParams.h:115
arm_compute::ActivationLayerInfo::b
float b() const
Get the beta value.
Definition: ActivationLayerInfo.h:85
arm_compute::LSTMParams::projection_bias
const T * projection_bias() const
Definition: LSTMParams.h:238
arm_compute::LSTMParams::input_to_input_weights
const T * input_to_input_weights() const
Definition: LSTMParams.h:203
arm_compute::LSTMParams::set_hidden_state_params
LSTMParams & set_hidden_state_params(int32_t hidden_state_zero, float hidden_state_scale)
Set hidden state zero and scale parameters.
Definition: LSTMParams.h:196
arm_compute::LSTMParams::input_gate_bias
const T * input_gate_bias() const
Definition: LSTMParams.h:218