arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1 | // |
Renato Grottesi | 77a0fb0 | 2023-05-08 12:55:03 +0000 | [diff] [blame] | 2 | // Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved. |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "HalPolicy.hpp" |
| 7 | |
Matthew Bentham | f61c270 | 2019-04-23 16:43:27 +0100 | [diff] [blame] | 8 | #include <armnn/Optional.hpp> |
| 9 | |
| 10 | #include "FullyConnected.hpp" |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 11 | #include "Utils.hpp" |
arovir01 | 5602b19 | 2018-10-04 16:15:02 +0100 | [diff] [blame] | 12 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 13 | namespace armnn_driver |
| 14 | { |
| 15 | namespace hal_1_0 |
| 16 | { |
| 17 | |
| 18 | bool HalPolicy::ConvertOperation(const Operation& operation, const Model& model, ConversionData& data) |
| 19 | { |
| 20 | switch (operation.type) |
| 21 | { |
| 22 | case V1_0::OperationType::ADD: |
Renato Grottesi | 77a0fb0 | 2023-05-08 12:55:03 +0000 | [diff] [blame] | 23 | return ConvertElementwiseBinary(operation, model, data, armnn::BinaryOperation::Add); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 24 | case V1_0::OperationType::AVERAGE_POOL_2D: |
| 25 | return ConvertAveragePool2d(operation, model, data); |
| 26 | case V1_0::OperationType::CONCATENATION: |
| 27 | return ConvertConcatenation(operation, model, data); |
| 28 | case V1_0::OperationType::CONV_2D: |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 29 | return ConvertConv2d(operation, model, data); |
Aron Virginas-Tar | 8edb16d | 2019-10-01 13:34:59 +0100 | [diff] [blame] | 30 | case V1_0::OperationType::DEPTH_TO_SPACE: |
| 31 | return ConvertDepthToSpace(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 32 | case V1_0::OperationType::DEPTHWISE_CONV_2D: |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 33 | return ConvertDepthwiseConv2d(operation, model, data); |
David Monahan | acf479a | 2019-05-29 14:27:04 +0100 | [diff] [blame] | 34 | case V1_0::OperationType::DEQUANTIZE: |
| 35 | return ConvertDequantize(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 36 | case V1_0::OperationType::FLOOR: |
| 37 | return ConvertFloor(operation, model, data); |
| 38 | case V1_0::OperationType::FULLY_CONNECTED: |
| 39 | return ConvertFullyConnected(operation, model, data); |
| 40 | case V1_0::OperationType::LOCAL_RESPONSE_NORMALIZATION: |
| 41 | return ConvertLocalResponseNormalization(operation, model, data); |
| 42 | case V1_0::OperationType::LOGISTIC: |
| 43 | return ConvertLogistic(operation, model, data); |
| 44 | case V1_0::OperationType::LSTM: |
| 45 | return ConvertLstm(operation, model, data); |
| 46 | case V1_0::OperationType::L2_NORMALIZATION: |
| 47 | return ConvertL2Normalization(operation, model, data); |
| 48 | case V1_0::OperationType::L2_POOL_2D: |
| 49 | return ConvertL2Pool2d(operation, model, data); |
| 50 | case V1_0::OperationType::MAX_POOL_2D: |
| 51 | return ConvertMaxPool2d(operation, model, data); |
| 52 | case V1_0::OperationType::MUL: |
Renato Grottesi | 77a0fb0 | 2023-05-08 12:55:03 +0000 | [diff] [blame] | 53 | return ConvertElementwiseBinary(operation, model, data, armnn::BinaryOperation::Mul); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 54 | case V1_0::OperationType::RELU: |
| 55 | return ConvertReLu(operation, model, data); |
| 56 | case V1_0::OperationType::RELU1: |
| 57 | return ConvertReLu1(operation, model, data); |
| 58 | case V1_0::OperationType::RELU6: |
| 59 | return ConvertReLu6(operation, model, data); |
| 60 | case V1_0::OperationType::SOFTMAX: |
| 61 | return ConvertSoftmax(operation, model, data); |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 62 | case V1_0::OperationType::SPACE_TO_DEPTH: |
| 63 | return ConvertSpaceToDepth(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 64 | case V1_0::OperationType::TANH: |
| 65 | return ConvertTanH(operation, model, data); |
| 66 | case V1_0::OperationType::RESHAPE: |
| 67 | return ConvertReshape(operation, model, data); |
| 68 | case V1_0::OperationType::RESIZE_BILINEAR: |
| 69 | return ConvertResizeBilinear(operation, model, data); |
| 70 | default: |
| 71 | return Fail("%s: Operation type %s not supported in ArmnnDriver", |
| 72 | __func__, toString(operation.type).c_str()); |
| 73 | } |
| 74 | } |
| 75 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 76 | bool HalPolicy::ConvertAveragePool2d(const Operation& operation, const Model& model, ConversionData& data) |
| 77 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 78 | ALOGV("hal_1_0::HalPolicy::ConvertAveragePool2d()"); |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 79 | return ConvertPooling2d<hal_1_0::HalPolicy>(operation, __func__, armnn::PoolingAlgorithm::Average, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | bool HalPolicy::ConvertConcatenation(const Operation& operation, const Model& model, ConversionData& data) |
| 83 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 84 | ALOGV("hal_1_0::HalPolicy::ConvertConcatenation()"); |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 85 | return ::ConvertConcatenation<hal_1_0::HalPolicy>(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 88 | bool HalPolicy::ConvertConv2d(const Operation& operation, const Model& model, ConversionData& data) |
| 89 | { |
| 90 | ALOGV("hal_1_0::HalPolicy::ConvertConv2d()"); |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 91 | return ::ConvertConv2d<hal_1_0::HalPolicy>(operation, model, data); |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Aron Virginas-Tar | 8edb16d | 2019-10-01 13:34:59 +0100 | [diff] [blame] | 94 | bool HalPolicy::ConvertDepthToSpace(const Operation& operation, const Model& model, ConversionData& data) |
| 95 | { |
| 96 | ALOGV("hal_1_0::HalPolicy::ConvertDepthToSpace()"); |
| 97 | return ::ConvertDepthToSpace<hal_1_0::HalPolicy>(operation, model, data); |
| 98 | } |
| 99 | |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 100 | bool HalPolicy::ConvertDepthwiseConv2d(const Operation& operation, const Model& model, ConversionData& data) |
| 101 | { |
| 102 | ALOGV("hal_1_0::HalPolicy::ConvertDepthwiseConv2d()"); |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 103 | return ::ConvertDepthwiseConv2d<hal_1_0::HalPolicy>(operation, model, data); |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 104 | } |
| 105 | |
David Monahan | acf479a | 2019-05-29 14:27:04 +0100 | [diff] [blame] | 106 | bool HalPolicy::ConvertDequantize(const Operation& operation, const Model& model, ConversionData& data) |
| 107 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 108 | ALOGV("hal_1_0::HalPolicy::ConvertDequantize()"); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 109 | return ::ConvertDequantize<hal_1_0::HalPolicy>(operation, model, data); |
David Monahan | acf479a | 2019-05-29 14:27:04 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Renato Grottesi | 77a0fb0 | 2023-05-08 12:55:03 +0000 | [diff] [blame] | 112 | bool HalPolicy::ConvertElementwiseBinary(const Operation& operation, |
| 113 | const Model& model, |
| 114 | ConversionData& data, |
| 115 | armnn::BinaryOperation binaryOperation) |
| 116 | { |
| 117 | ALOGV("hal_1_0::HalPolicy::ConvertElementwiseBinary()"); |
| 118 | return ::ConvertElementwiseBinary<hal_1_0::HalPolicy>(operation, model, data, binaryOperation); |
| 119 | } |
| 120 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 121 | bool HalPolicy::ConvertFloor(const Operation& operation, const Model& model, ConversionData& data) |
| 122 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 123 | ALOGV("hal_1_0::HalPolicy::ConvertFloor()"); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 124 | return ::ConvertFloor<hal_1_0::HalPolicy>(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | bool HalPolicy::ConvertFullyConnected(const Operation& operation, const Model& model, ConversionData& data) |
| 128 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 129 | ALOGV("hal_1_0::HalPolicy::ConvertFullyConnected()"); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 130 | return ::ConvertFullyConnected<hal_1_0::HalPolicy>(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | bool HalPolicy::ConvertLocalResponseNormalization(const Operation& operation, |
| 134 | const Model& model, |
| 135 | ConversionData& data) |
| 136 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 137 | ALOGV("hal_1_0::HalPolicy::ConvertLocalResponseNormalization()"); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 138 | return ::ConvertLocalResponseNormalization<hal_1_0::HalPolicy>(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | bool HalPolicy::ConvertLogistic(const Operation& operation, const Model& model, ConversionData& data) |
| 142 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 143 | ALOGV("hal_1_0::HalPolicy::ConvertLogistic()"); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 144 | return ::ConvertLogistic<hal_1_0::HalPolicy>(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | bool HalPolicy::ConvertLstm(const Operation& operation, const Model& model, ConversionData& data) |
| 148 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 149 | ALOGV("hal_1_0::HalPolicy::ConvertLstm()"); |
| 150 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 151 | // Inputs: |
| 152 | // 00: The input: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, input_size], where |
| 153 | // “batch_size” corresponds to the batching dimension, and “input_size” is the size of the input. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 154 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 0, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 155 | if (!input.IsValid()) |
| 156 | { |
| 157 | return Fail("%s: Could not read input 0: input", __func__); |
| 158 | } |
| 159 | // 18: The output state: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, output_size]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 160 | LayerInputHandle outputStateIn = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 18, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 161 | if (!outputStateIn.IsValid()) |
| 162 | { |
| 163 | return Fail("%s: Could not read input 18: outputStateIn", __func__); |
| 164 | } |
| 165 | // 19: The cell state: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 166 | LayerInputHandle cellStateIn = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 19, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 167 | if (!cellStateIn.IsValid()) |
| 168 | { |
| 169 | return Fail("%s: Could not read input 19: cellStateIn", __func__); |
| 170 | } |
| 171 | |
| 172 | // Get the mandatory input tensors: |
| 173 | // 02: The input-to-forget weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 174 | // [num_units, input_size]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 175 | const ConstTensorPin inputToForgetWeightsPin = |
| 176 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 2, model, data); |
| 177 | // 03: The input-to-cell weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 178 | // [num_units, input_size]. |
| 179 | const ConstTensorPin inputToCellWeightsPin = |
| 180 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 3, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 181 | // 04: The input-to-output weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 182 | // [num_units, input_size]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 183 | const ConstTensorPin inputToOutputWeightsPin = |
| 184 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 4, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 185 | // 06: The recurrent-to-forget weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 186 | // [num_units, output_size]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 187 | const ConstTensorPin recurrentToForgetWeightsPin = |
| 188 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 6, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 189 | // 07: The recurrent-to-cell weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 190 | // [num_units, output_size]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 191 | const ConstTensorPin recurrentToCellWeightsPin = |
| 192 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 7, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 193 | // 08: The recurrent-to-output weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 194 | // [num_units, output_size]. |
| 195 | const ConstTensorPin recurrentToOutputWeightsPin = |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 196 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 8, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 197 | // 13: The forget gate bias: A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 198 | const ConstTensorPin forgetGateBiasPin = |
| 199 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 13, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 200 | // 14: The cell bias: A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 201 | const ConstTensorPin cellBiasPin = |
| 202 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 14, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 203 | // 15: The output gate bias: A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 204 | const ConstTensorPin outputGateBiasPin = |
| 205 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 15, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 206 | |
| 207 | if (!inputToForgetWeightsPin.IsValid() || |
| 208 | !inputToCellWeightsPin.IsValid() || |
| 209 | !inputToOutputWeightsPin.IsValid() || |
| 210 | !recurrentToForgetWeightsPin.IsValid() || |
| 211 | !recurrentToCellWeightsPin.IsValid() || |
| 212 | !recurrentToOutputWeightsPin.IsValid() || |
| 213 | !forgetGateBiasPin.IsValid() || |
| 214 | !cellBiasPin.IsValid() || |
| 215 | !outputGateBiasPin.IsValid()) |
| 216 | { |
| 217 | return Fail("%s: Operation has invalid tensor inputs", __func__); |
| 218 | } |
| 219 | |
| 220 | // Get the optional input tensors: |
| 221 | // 01: The input-to-input weights: Optional. A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 222 | // [num_units, input_size], where “num_units” corresponds to the number of cell units. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 223 | const ConstTensorPin inputToInputWeightsPin = |
| 224 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, |
| 225 | 1, |
| 226 | model, |
| 227 | data, |
| 228 | g_DontPermute, |
| 229 | nullptr, |
| 230 | true); |
| 231 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 232 | // 05: The recurrent-to-input weights: Optional. A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 233 | // [num_units, output_size], where “output_size” corresponds to either the number of cell units (i.e., |
| 234 | // “num_units”), or the second dimension of the “projection_weights”, if defined. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 235 | const ConstTensorPin recurrentToInputWeightsPin = |
| 236 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, |
| 237 | 5, |
| 238 | model, |
| 239 | data, |
| 240 | g_DontPermute, |
| 241 | nullptr, |
| 242 | true); |
| 243 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 244 | // 09: The cell-to-input weights: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 245 | const ConstTensorPin cellToInputWeightsPin = |
| 246 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, |
| 247 | 9, |
| 248 | model, |
| 249 | data, |
| 250 | g_DontPermute, |
| 251 | nullptr, |
| 252 | true); |
| 253 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 254 | // 10: The cell-to-forget weights: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 255 | const ConstTensorPin cellToForgetWeightsPin = |
| 256 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, |
| 257 | 10, |
| 258 | model, |
| 259 | data, |
| 260 | g_DontPermute, |
| 261 | nullptr, |
| 262 | true); |
| 263 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 264 | // 11: The cell-to-output weights: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 265 | const ConstTensorPin cellToOutputWeightsPin = |
| 266 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, |
| 267 | 11, |
| 268 | model, |
| 269 | data, |
| 270 | g_DontPermute, |
| 271 | nullptr, |
| 272 | true); |
| 273 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 274 | // 12: The input gate bias: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 275 | const ConstTensorPin inputGateBiasPin = |
| 276 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, |
| 277 | 12, |
| 278 | model, |
| 279 | data, |
| 280 | g_DontPermute, |
| 281 | nullptr, |
| 282 | true); |
| 283 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 284 | // 16: The projection weights: Optional. A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 285 | // [output_size, num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 286 | const ConstTensorPin projectionWeightsPin = |
| 287 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, |
| 288 | 16, |
| 289 | model, |
| 290 | data, |
| 291 | g_DontPermute, |
| 292 | nullptr, |
| 293 | true); |
| 294 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 295 | // 17: The projection bias: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [output_size]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 296 | const ConstTensorPin projectionBiasPin = |
| 297 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, |
| 298 | 17, |
| 299 | model, |
| 300 | data, |
| 301 | g_DontPermute, |
| 302 | nullptr, |
| 303 | true); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 304 | |
| 305 | if ((!inputToInputWeightsPin.IsValid() && !inputToInputWeightsPin.IsOptional()) || |
| 306 | (!recurrentToInputWeightsPin.IsValid() && !recurrentToInputWeightsPin.IsOptional()) || |
| 307 | (!cellToInputWeightsPin.IsValid() && !cellToInputWeightsPin.IsOptional()) || |
| 308 | (!cellToForgetWeightsPin.IsValid() && !cellToForgetWeightsPin.IsOptional()) || |
| 309 | (!cellToOutputWeightsPin.IsValid() && !cellToOutputWeightsPin.IsOptional()) || |
| 310 | (!inputGateBiasPin.IsValid() && !inputGateBiasPin.IsOptional()) || |
| 311 | (!projectionWeightsPin.IsValid() && !projectionWeightsPin.IsOptional()) || |
| 312 | (!projectionBiasPin.IsValid() && !projectionBiasPin.IsOptional())) |
| 313 | { |
| 314 | return Fail("%s: Operation has invalid tensor inputs", __func__); |
| 315 | } |
| 316 | |
| 317 | // Get the mandatory input scalars (actually 1-D tensors of size 1): |
| 318 | // 20: The activation function: A value indicating the activation function: |
| 319 | // 0: None; 1: Relu; 3: Relu6; 4: Tanh; 6: Sigmoid. |
| 320 | // 21: The clipping threshold: for the cell state, such that values are bound within [-cell_clip, cell_clip]. |
| 321 | // If set to 0.0 then clipping is disabled. |
| 322 | // 22: The clipping threshold: for the output from the projection layer, such that values are bound within |
| 323 | // [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled. |
| 324 | ActivationFn activation; |
| 325 | float cellClip; |
| 326 | float projClip; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 327 | if (!GetInputActivationFunctionFromTensor<hal_1_0::HalPolicy>(operation, 20, activation, model, data) || |
| 328 | !GetInputScalar<hal_1_0::HalPolicy>(operation, 21, OperandType::FLOAT32, cellClip, model, data) || |
| 329 | !GetInputScalar<hal_1_0::HalPolicy>(operation, 22, OperandType::FLOAT32, projClip, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 330 | { |
| 331 | return Fail("%s: Operation has invalid scalar inputs", __func__); |
| 332 | } |
| 333 | |
| 334 | // Outputs: |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 335 | // 00: The scratch buffer: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, num_units * 4] |
| 336 | // with CIFG, or [batch_size, num_units * 3] without CIFG. |
| 337 | const Operand* scratchBuffer = GetOutputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 338 | if (!scratchBuffer) |
| 339 | { |
| 340 | return Fail("%s: Could not read output 0: scratchBuffer", __func__); |
| 341 | } |
| 342 | // 01: The output state (out): A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, output_size]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 343 | const Operand* outputStateOut = GetOutputOperand<hal_1_0::HalPolicy>(operation, 1, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 344 | if (!outputStateOut) |
| 345 | { |
| 346 | return Fail("%s: Could not read output 1: outputStateOut", __func__); |
| 347 | } |
| 348 | // 02: The cell state (out): A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 349 | const Operand* cellStateOut = GetOutputOperand<hal_1_0::HalPolicy>(operation, 2, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 350 | if (!cellStateOut) |
| 351 | { |
| 352 | return Fail("%s: Could not read output 2: cellStateOut", __func__); |
| 353 | } |
| 354 | // 03: The output: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, output_size]. This is |
| 355 | // effectively the same as the current “output state (out)” value. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 356 | const Operand* output = GetOutputOperand<hal_1_0::HalPolicy>(operation, 3, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 357 | if (!output) |
| 358 | { |
| 359 | return Fail("%s: Could not read output 3: output", __func__); |
| 360 | } |
| 361 | |
| 362 | // set the params structure for the AddLstmLayer call |
| 363 | armnn::LstmInputParams params; |
| 364 | params.m_InputToInputWeights = inputToInputWeightsPin.GetConstTensorPtr(); |
| 365 | params.m_InputToForgetWeights = inputToForgetWeightsPin.GetConstTensorPtr(); |
| 366 | params.m_InputToCellWeights = inputToCellWeightsPin.GetConstTensorPtr(); |
| 367 | params.m_InputToOutputWeights = inputToOutputWeightsPin.GetConstTensorPtr(); |
| 368 | params.m_RecurrentToInputWeights = recurrentToInputWeightsPin.GetConstTensorPtr(); |
| 369 | params.m_RecurrentToForgetWeights = recurrentToForgetWeightsPin.GetConstTensorPtr(); |
| 370 | params.m_RecurrentToCellWeights = recurrentToCellWeightsPin.GetConstTensorPtr(); |
| 371 | params.m_RecurrentToOutputWeights = recurrentToOutputWeightsPin.GetConstTensorPtr(); |
| 372 | params.m_CellToInputWeights = cellToInputWeightsPin.GetConstTensorPtr(); |
| 373 | params.m_CellToForgetWeights = cellToForgetWeightsPin.GetConstTensorPtr(); |
| 374 | params.m_CellToOutputWeights = cellToOutputWeightsPin.GetConstTensorPtr(); |
| 375 | params.m_InputGateBias = inputGateBiasPin.GetConstTensorPtr(); |
| 376 | params.m_ForgetGateBias = forgetGateBiasPin.GetConstTensorPtr(); |
| 377 | params.m_CellBias = cellBiasPin.GetConstTensorPtr(); |
| 378 | params.m_OutputGateBias = outputGateBiasPin.GetConstTensorPtr(); |
| 379 | params.m_ProjectionWeights = projectionWeightsPin.GetConstTensorPtr(); |
| 380 | params.m_ProjectionBias = projectionBiasPin.GetConstTensorPtr(); |
| 381 | |
| 382 | // set the layer descriptor |
| 383 | armnn::LstmDescriptor desc; |
| 384 | desc.m_ActivationFunc = activation; |
| 385 | desc.m_ClippingThresCell = cellClip; |
| 386 | desc.m_ClippingThresProj = projClip; |
| 387 | desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr || |
| 388 | params.m_RecurrentToInputWeights == nullptr || |
| 389 | params.m_InputGateBias == nullptr); |
| 390 | desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr || |
| 391 | params.m_CellToOutputWeights != nullptr); |
| 392 | desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr); |
| 393 | |
| 394 | // validate the optional input groups |
| 395 | if (desc.m_CifgEnabled && |
| 396 | (params.m_InputToInputWeights != nullptr || |
| 397 | params.m_RecurrentToInputWeights != nullptr || |
| 398 | params.m_InputGateBias != nullptr)) |
| 399 | { |
| 400 | return Fail("%s: All, or none, of input-to-input weights, recurrent-to-input weights," |
| 401 | " and input gate bias must be provided", __func__); |
| 402 | } |
| 403 | |
| 404 | if (!desc.m_ProjectionEnabled && params.m_ProjectionBias != nullptr) |
| 405 | { |
| 406 | return Fail("%s: projection bias should not be provided without projection weights", __func__); |
| 407 | } |
| 408 | |
| 409 | if (desc.m_PeepholeEnabled && |
| 410 | (params.m_CellToForgetWeights == nullptr || |
| 411 | params.m_CellToOutputWeights == nullptr || |
| 412 | (!desc.m_CifgEnabled && params.m_CellToInputWeights == nullptr))) |
| 413 | { |
| 414 | return Fail("%s: All, or none, of cell-to-forget weights and cell-to-output weights must be provided" |
| 415 | " and, if CIFG is not enabled, cell-to-input weights must also be provided", __func__); |
| 416 | } |
| 417 | |
| 418 | // Check if the layer is supported |
| 419 | // Inputs |
| 420 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 421 | const armnn::TensorInfo& outputStateInInfo = outputStateIn.GetTensorInfo(); |
| 422 | const armnn::TensorInfo& cellStateInInfo = cellStateIn.GetTensorInfo(); |
| 423 | |
| 424 | // Outputs |
| 425 | const armnn::TensorInfo& scratchBufferInfo = GetTensorInfoForOperand(*scratchBuffer); |
| 426 | const armnn::TensorInfo& outputStateOutInfo = GetTensorInfoForOperand(*outputStateOut); |
| 427 | const armnn::TensorInfo& cellStateOutInfo = GetTensorInfoForOperand(*cellStateOut); |
| 428 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 429 | |
| 430 | // Basic parameters |
Ferran Balaguer | 177fa0b | 2019-07-02 17:34:46 +0100 | [diff] [blame] | 431 | armnn::LstmInputParamsInfo paramsInfo; |
| 432 | paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo()); |
| 433 | paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo()); |
| 434 | paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo()); |
| 435 | paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo()); |
| 436 | paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo()); |
| 437 | paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo()); |
| 438 | paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo()); |
| 439 | paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo()); |
| 440 | paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 441 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 442 | // Optional parameters |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 443 | if(!desc.m_CifgEnabled) |
| 444 | { |
Ferran Balaguer | 177fa0b | 2019-07-02 17:34:46 +0100 | [diff] [blame] | 445 | paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo()); |
| 446 | paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 447 | if (params.m_CellToInputWeights != nullptr) |
| 448 | { |
Ferran Balaguer | 177fa0b | 2019-07-02 17:34:46 +0100 | [diff] [blame] | 449 | paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 450 | } |
Ferran Balaguer | 177fa0b | 2019-07-02 17:34:46 +0100 | [diff] [blame] | 451 | paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | if(desc.m_ProjectionEnabled) |
| 455 | { |
Ferran Balaguer | 177fa0b | 2019-07-02 17:34:46 +0100 | [diff] [blame] | 456 | paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 457 | if (params.m_ProjectionBias != nullptr) |
| 458 | { |
Ferran Balaguer | 177fa0b | 2019-07-02 17:34:46 +0100 | [diff] [blame] | 459 | paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | if(desc.m_PeepholeEnabled) |
| 464 | { |
Ferran Balaguer | 177fa0b | 2019-07-02 17:34:46 +0100 | [diff] [blame] | 465 | paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo()); |
| 466 | paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 467 | } |
| 468 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 469 | bool isSupported = false; |
Renato Grottesi | 77a0fb0 | 2023-05-08 12:55:03 +0000 | [diff] [blame] | 470 | armnn::BackendId setBackend; |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 471 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 472 | IsLstmSupported, |
| 473 | data.m_Backends, |
| 474 | isSupported, |
Renato Grottesi | 77a0fb0 | 2023-05-08 12:55:03 +0000 | [diff] [blame] | 475 | setBackend, |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 476 | inputInfo, |
| 477 | outputStateInInfo, |
| 478 | cellStateInInfo, |
| 479 | scratchBufferInfo, |
| 480 | outputStateOutInfo, |
| 481 | cellStateOutInfo, |
| 482 | outputInfo, |
| 483 | desc, |
| 484 | paramsInfo); |
| 485 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 486 | { |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | // Add the layer |
| 491 | armnn::IConnectableLayer* layer = data.m_Network->AddLstmLayer(desc, params, "Lstm"); |
Renato Grottesi | 77a0fb0 | 2023-05-08 12:55:03 +0000 | [diff] [blame] | 492 | layer->SetBackendId(setBackend); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 493 | |
| 494 | input.Connect(layer->GetInputSlot(0)); |
| 495 | outputStateIn.Connect(layer->GetInputSlot(1)); |
| 496 | cellStateIn.Connect(layer->GetInputSlot(2)); |
| 497 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 498 | return (SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 0, *layer, 0, model, data) && |
| 499 | SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 1, *layer, 1, model, data) && |
| 500 | SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 2, *layer, 2, model, data) && |
| 501 | SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 3, *layer, 3, model, data)); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | bool HalPolicy::ConvertL2Normalization(const Operation& operation, const Model& model, ConversionData& data) |
| 505 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 506 | ALOGV("hal_1_0::HalPolicy::ConvertL2Normalization()"); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 507 | return ::ConvertL2Normalization<hal_1_0::HalPolicy>(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | bool HalPolicy::ConvertL2Pool2d(const Operation& operation, const Model& model, ConversionData& data) |
| 511 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 512 | ALOGV("hal_1_0::HalPolicy::ConvertL2Pool2d()"); |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 513 | return ConvertPooling2d<hal_1_0::HalPolicy>(operation, __func__, armnn::PoolingAlgorithm::L2, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | bool HalPolicy::ConvertMaxPool2d(const Operation& operation, const Model& model, ConversionData& data) |
| 517 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 518 | ALOGV("hal_1_0::HalPolicy::ConvertMaxPool2d()"); |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 519 | return ConvertPooling2d<hal_1_0::HalPolicy>(operation, __func__, armnn::PoolingAlgorithm::Max, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 520 | } |
| 521 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 522 | bool HalPolicy::ConvertReLu(const Operation& operation, const Model& model, ConversionData& data) |
| 523 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 524 | ALOGV("hal_1_0::HalPolicy::ConvertReLu()"); |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 525 | return ::ConvertReLu<hal_1_0::HalPolicy>(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | bool HalPolicy::ConvertReLu1(const Operation& operation, const Model& model, ConversionData& data) |
| 529 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 530 | ALOGV("hal_1_0::HalPolicy::ConvertReLu1()"); |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 531 | return ::ConvertReLu1<hal_1_0::HalPolicy>(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | bool HalPolicy::ConvertReLu6(const Operation& operation, const Model& model, ConversionData& data) |
| 535 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 536 | ALOGV("hal_1_0::HalPolicy::ConvertReLu6()"); |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 537 | return ::ConvertReLu6<hal_1_0::HalPolicy>(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | bool HalPolicy::ConvertSoftmax(const Operation& operation, const Model& model, ConversionData& data) |
| 541 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 542 | ALOGV("hal_1_0::HalPolicy::ConvertSoftmax()"); |
| 543 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 544 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 0, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 545 | if (!input.IsValid()) |
| 546 | { |
| 547 | return Fail("%s: Operation has invalid inputs", __func__); |
| 548 | } |
| 549 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 550 | const Operand* outputOperand = GetOutputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 551 | if (!outputOperand) |
| 552 | { |
| 553 | return Fail("%s: Operation has no outputs", __func__); |
| 554 | } |
| 555 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 556 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand); |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 557 | if (IsDynamicTensor(outputInfo)) |
Aron Virginas-Tar | 366e0a6 | 2019-07-10 13:01:41 +0100 | [diff] [blame] | 558 | { |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 559 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
Aron Virginas-Tar | 366e0a6 | 2019-07-10 13:01:41 +0100 | [diff] [blame] | 560 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 561 | |
| 562 | armnn::SoftmaxDescriptor desc; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 563 | if (!GetInputFloat32<hal_1_0::HalPolicy>(operation, 1, desc.m_Beta, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 564 | { |
| 565 | return Fail("%s: Operation has invalid inputs", __func__); |
| 566 | } |
| 567 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 568 | bool isSupported = false; |
Renato Grottesi | 77a0fb0 | 2023-05-08 12:55:03 +0000 | [diff] [blame] | 569 | armnn::BackendId setBackend; |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 570 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 571 | IsSoftmaxSupported, |
| 572 | data.m_Backends, |
| 573 | isSupported, |
Renato Grottesi | 77a0fb0 | 2023-05-08 12:55:03 +0000 | [diff] [blame] | 574 | setBackend, |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 575 | input.GetTensorInfo(), |
| 576 | outputInfo, |
| 577 | desc); |
| 578 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 579 | { |
| 580 | return false; |
| 581 | } |
| 582 | |
| 583 | armnn::IConnectableLayer* layer = data.m_Network->AddSoftmaxLayer(desc); |
Renato Grottesi | 77a0fb0 | 2023-05-08 12:55:03 +0000 | [diff] [blame] | 584 | layer->SetBackendId(setBackend); |
| 585 | if (!layer) |
| 586 | { |
| 587 | return Fail("%s: Could not add the SoftmaxLayer", __func__); |
| 588 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 589 | input.Connect(layer->GetInputSlot(0)); |
| 590 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 591 | return SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 0, *layer, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 592 | } |
| 593 | |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 594 | bool HalPolicy::ConvertSpaceToDepth(const Operation& operation, const Model& model, ConversionData& data) |
| 595 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 596 | ALOGV("hal_1_0::HalPolicy::ConvertSpaceToDepth()"); |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 597 | |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 598 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 0, model, data); |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 599 | if (!input.IsValid() ) |
| 600 | { |
| 601 | return Fail("%s: Operation has invalid inputs", __func__); |
| 602 | } |
| 603 | |
| 604 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 605 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 606 | |
| 607 | if (rank != 4) |
| 608 | { |
| 609 | return Fail("%s: Only inputs with rank 4 are supported", __func__); |
| 610 | } |
| 611 | |
| 612 | armnn::SpaceToDepthDescriptor desc; |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 613 | |
| 614 | GetInputScalar<hal_1_0::HalPolicy>(operation, 1, OperandType::INT32, desc.m_BlockSize, model, data); |
| 615 | |
| 616 | if (desc.m_BlockSize <= 1) |
| 617 | { |
| 618 | return Fail("%s: Block size must be at least 1 in all dimensions"); |
| 619 | } |
| 620 | |
| 621 | const Operand* output = GetOutputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
| 622 | if (!output) |
| 623 | { |
| 624 | return Fail("%s: Could not read output 0", __func__); |
| 625 | } |
| 626 | |
| 627 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Aron Virginas-Tar | a3609cc | 2019-07-29 10:50:25 +0100 | [diff] [blame] | 628 | if (IsDynamicTensor(outputInfo)) |
| 629 | { |
| 630 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 631 | } |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 632 | |
| 633 | bool isSupported = false; |
Renato Grottesi | 77a0fb0 | 2023-05-08 12:55:03 +0000 | [diff] [blame] | 634 | armnn::BackendId setBackend; |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 635 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 636 | IsSpaceToDepthSupported, |
| 637 | data.m_Backends, |
| 638 | isSupported, |
Renato Grottesi | 77a0fb0 | 2023-05-08 12:55:03 +0000 | [diff] [blame] | 639 | setBackend, |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 640 | inputInfo, |
| 641 | outputInfo, |
| 642 | desc); |
| 643 | if (!isSupported) |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 644 | { |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | armnn::IConnectableLayer* const layer = data.m_Network->AddSpaceToDepthLayer(desc); |
Renato Grottesi | 77a0fb0 | 2023-05-08 12:55:03 +0000 | [diff] [blame] | 649 | layer->SetBackendId(setBackend); |
| 650 | if (!layer) |
| 651 | { |
| 652 | return Fail("%s: Could not add the SpaceToDepthLayer", __func__); |
| 653 | } |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 654 | input.Connect(layer->GetInputSlot(0)); |
| 655 | |
| 656 | return SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 0, *layer, model, data); |
| 657 | } |
| 658 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 659 | bool HalPolicy::ConvertTanH(const Operation& operation, const Model& model, ConversionData& data) |
| 660 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 661 | ALOGV("hal_1_0::HalPolicy::ConvertTanH()"); |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 662 | return ::ConvertTanH<hal_1_0::HalPolicy>(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | bool HalPolicy::ConvertReshape(const Operation& operation, const Model& model, ConversionData& data) |
| 666 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 667 | ALOGV("hal_1_0::HalPolicy::ConvertReshape()"); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 668 | return ::ConvertReshape<hal_1_0::HalPolicy>(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | bool HalPolicy::ConvertResizeBilinear(const Operation& operation, const Model& model, ConversionData& data) |
| 672 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 673 | ALOGV("hal_1_0::HalPolicy::ConvertResizeBilinear()"); |
| 674 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 675 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 0, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 676 | if (!input.IsValid()) |
| 677 | { |
| 678 | return Fail("%s: Could not read input 0", __func__); |
| 679 | } |
| 680 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 681 | const Operand* output = GetOutputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 682 | if (!output) |
| 683 | { |
| 684 | return Fail("%s: Could not read output 0", __func__); |
| 685 | } |
| 686 | |
Aron Virginas-Tar | a3609cc | 2019-07-29 10:50:25 +0100 | [diff] [blame] | 687 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 688 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 689 | |
Aron Virginas-Tar | a3609cc | 2019-07-29 10:50:25 +0100 | [diff] [blame] | 690 | if (IsDynamicTensor(outputInfo)) |
| 691 | { |
| 692 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 693 | } |
| 694 | |
Aron Virginas-Tar | a5daf86 | 2019-07-01 19:07:20 +0100 | [diff] [blame] | 695 | armnn::ResizeDescriptor desc; |
| 696 | desc.m_Method = armnn::ResizeMethod::Bilinear; |
Mohamed Nour Abouelseoud | 81afa30 | 2018-10-29 14:32:55 +0000 | [diff] [blame] | 697 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 698 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 699 | bool isSupported = false; |
Renato Grottesi | 77a0fb0 | 2023-05-08 12:55:03 +0000 | [diff] [blame] | 700 | armnn::BackendId setBackend; |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 701 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 702 | IsResizeSupported, |
| 703 | data.m_Backends, |
| 704 | isSupported, |
Renato Grottesi | 77a0fb0 | 2023-05-08 12:55:03 +0000 | [diff] [blame] | 705 | setBackend, |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 706 | inputInfo, |
| 707 | outputInfo, |
| 708 | desc); |
| 709 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 710 | { |
| 711 | return false; |
| 712 | } |
| 713 | |
Aron Virginas-Tar | 535607d | 2019-07-03 15:46:15 +0100 | [diff] [blame] | 714 | if (!GetInputScalar<hal_1_0::HalPolicy>(operation, 1, OperandType::INT32, desc.m_TargetWidth, model, data) || |
| 715 | !GetInputScalar<hal_1_0::HalPolicy>(operation, 2, OperandType::INT32, desc.m_TargetHeight, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 716 | { |
| 717 | return Fail("%s: Operation has invalid inputs", __func__); |
| 718 | } |
| 719 | |
Aron Virginas-Tar | a5daf86 | 2019-07-01 19:07:20 +0100 | [diff] [blame] | 720 | armnn::IConnectableLayer* layer = data.m_Network->AddResizeLayer(desc); |
Renato Grottesi | 77a0fb0 | 2023-05-08 12:55:03 +0000 | [diff] [blame] | 721 | layer->SetBackendId(setBackend); |
| 722 | if (!layer) |
| 723 | { |
| 724 | return Fail("%s: Could not add the ResizeLayer", __func__); |
| 725 | } |
Mohamed Nour Abouelseoud | 81afa30 | 2018-10-29 14:32:55 +0000 | [diff] [blame] | 726 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 727 | input.Connect(layer->GetInputSlot(0)); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 728 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 729 | return SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 0, *layer, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 730 | |
| 731 | } |
| 732 | |
| 733 | } // namespace hal_1_0 |
Matteo Martincigh | 58f7109 | 2018-09-25 15:58:52 +0100 | [diff] [blame] | 734 | } // namespace armnn_driver |