Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 Arm Limited. |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_MISC_INFO_HELPERS_H |
| 25 | #define ARM_COMPUTE_MISC_INFO_HELPERS_H |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Types.h" |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 29 | #include "arm_compute/runtime/common/LSTMParams.h" |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 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 | { |
Georgios Pinitas | cadb368 | 2019-03-29 10:54:36 +0000 | [diff] [blame] | 56 | 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 Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 61 | } |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 62 | |
| 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 | */ |
| 70 | template <typename T> |
| 71 | inline 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 Spyrou | 1009e87 | 2020-07-27 12:48:34 +0100 | [diff] [blame] | 89 | ITensorInfo *cell_to_input_weights_info = (lstm_params.has_peephole_opt()) ? lstm_params.cell_to_input_weights()->info() : nullptr; |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 90 | 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 Park | d5c020a | 2020-05-06 21:01:19 +0100 | [diff] [blame] | 93 | 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 Spyrou | 1009e87 | 2020-07-27 12:48:34 +0100 | [diff] [blame] | 103 | 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 Park | d5c020a | 2020-05-06 21:01:19 +0100 | [diff] [blame] | 107 | |
| 108 | lstm_params_info->set_layer_normalization_params(input_info, forget_info, cell_info, output_info); |
| 109 | } |
Sang-Hoon Park | dc4a636 | 2020-05-19 17:30:35 +0100 | [diff] [blame] | 110 | |
| 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 Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 117 | } |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 118 | } // namespace info_helpers |
| 119 | } // namespace utils |
| 120 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 121 | #endif /* ARM_COMPUTE_MISC_INFO_HELPERS_H */ |