blob: ced0d24b566e6beb5f117351d4ce9d5bf2de970a [file] [log] [blame]
Georgios Pinitas47d39dc2019-03-11 14:03:23 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
Georgios Pinitas47d39dc2019-03-11 14:03:23 +00003 *
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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_MISC_INFO_HELPERS_H
25#define ARM_COMPUTE_MISC_INFO_HELPERS_H
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000026
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Types.h"
Michele Di Giorgio47a89902020-03-09 19:32:33 +000029#include "arm_compute/runtime/common/LSTMParams.h"
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000030
31namespace arm_compute
32{
33namespace utils
34{
35namespace 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 */
43inline 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 */
54inline bool is_relu6(ActivationLayerInfo activation_info)
55{
Georgios Pinitascadb3682019-03-29 10:54:36 +000056 const bool is_lu_bounded_relu = activation_info.activation() == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
57 && activation_info.a() == 6.f && activation_info.b() == 0.f;
58 const bool is_bounded_relu = activation_info.activation() == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU
59 && activation_info.a() == 6.f;
60 return activation_info.enabled() && (is_lu_bounded_relu || is_bounded_relu);
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000061}
Michele Di Giorgio47a89902020-03-09 19:32:33 +000062
63/** Build LSTMParams<ITensorInfo> object by extracting the metadata from each
64 * tensor.
65 *
66 * @param[in] lstm_params The LSTMParams<T> object containing the tensors.
67 * @param[out] lstm_params_info The LSTMParams<ITensorInfo> to be constructed.
68 *
69 */
70template <typename T>
71inline void build_lstm_params_tensor_info(const LSTMParams<T> &lstm_params,
72 LSTMParams<ITensorInfo> *lstm_params_info)
73{
74 if(lstm_params.has_peephole_opt())
75 {
76 ARM_COMPUTE_ERROR_ON_NULLPTR(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights());
77 lstm_params_info->set_peephole_params(lstm_params.cell_to_forget_weights()->info(), lstm_params.cell_to_output_weights()->info());
78 }
79 if(lstm_params.has_projection())
80 {
81 ARM_COMPUTE_ERROR_ON_NULLPTR(lstm_params.projection_weights());
82 lstm_params_info->set_projection_params(lstm_params.projection_weights()->info(),
83 lstm_params.projection_bias() != nullptr ? lstm_params.projection_bias()->info() : nullptr);
84 }
85 if(!lstm_params.has_cifg_opt())
86 {
87 ARM_COMPUTE_ERROR_ON_NULLPTR(lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights(), lstm_params.input_gate_bias());
88
Michalis Spyrou1009e872020-07-27 12:48:34 +010089 ITensorInfo *cell_to_input_weights_info = (lstm_params.has_peephole_opt()) ? lstm_params.cell_to_input_weights()->info() : nullptr;
Michele Di Giorgio47a89902020-03-09 19:32:33 +000090 lstm_params_info->set_cifg_params(lstm_params.input_to_input_weights()->info(), lstm_params.recurrent_to_input_weights()->info(),
91 cell_to_input_weights_info, lstm_params.input_gate_bias()->info());
92 }
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +010093 if(lstm_params.use_layer_norm())
94 {
95 ARM_COMPUTE_ERROR_ON_NULLPTR(lstm_params.forget_layer_norm_weights(),
96 lstm_params.output_layer_norm_weights(),
97 lstm_params.cell_layer_norm_weights());
98 if(!lstm_params.has_cifg_opt())
99 {
100 ARM_COMPUTE_ERROR_ON_NULLPTR(lstm_params.input_layer_norm_weights());
101 }
102
Michalis Spyrou1009e872020-07-27 12:48:34 +0100103 ITensorInfo *forget_info = lstm_params.forget_layer_norm_weights()->info();
104 ITensorInfo *cell_info = lstm_params.cell_layer_norm_weights()->info();
105 ITensorInfo *output_info = lstm_params.output_layer_norm_weights()->info();
106 ITensorInfo *input_info = lstm_params.has_cifg_opt() ? nullptr : lstm_params.input_layer_norm_weights()->info();
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100107
108 lstm_params_info->set_layer_normalization_params(input_info, forget_info, cell_info, output_info);
109 }
Sang-Hoon Parkdc4a6362020-05-19 17:30:35 +0100110
111 lstm_params_info->set_matmul_scale_params(lstm_params.input_intermediate_scale(),
112 lstm_params.forget_intermediate_scale(),
113 lstm_params.cell_intermediate_scale(),
114 lstm_params.output_intermediate_scale());
115
116 lstm_params_info->set_hidden_state_params(lstm_params.hidden_state_zero(), lstm_params.hidden_state_scale());
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000117}
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000118} // namespace info_helpers
119} // namespace utils
120} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000121#endif /* ARM_COMPUTE_MISC_INFO_HELPERS_H */