arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "HalPolicy.hpp" |
| 7 | |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 8 | #include "Utils.hpp" |
Aron Virginas-Tar | 366e0a6 | 2019-07-10 13:01:41 +0100 | [diff] [blame] | 9 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 10 | #include "../1.0/HalPolicy.hpp" |
| 11 | |
Éanna Ó Catháin | 2fc21f7 | 2019-05-13 11:01:33 +0100 | [diff] [blame] | 12 | namespace |
| 13 | { |
| 14 | static std::vector<V1_0::OperationType> opsEquivalentInV10({ |
| 15 | V1_0::OperationType::ADD, |
| 16 | V1_0::OperationType::AVERAGE_POOL_2D, |
| 17 | V1_0::OperationType::CONCATENATION, |
| 18 | V1_0::OperationType::CONV_2D, |
| 19 | V1_0::OperationType::DEPTHWISE_CONV_2D, |
David Monahan | d5bfae1 | 2019-05-30 12:07:44 +0100 | [diff] [blame] | 20 | V1_0::OperationType::DEQUANTIZE, |
Éanna Ó Catháin | 2fc21f7 | 2019-05-13 11:01:33 +0100 | [diff] [blame] | 21 | V1_0::OperationType::FLOOR, |
| 22 | V1_0::OperationType::FULLY_CONNECTED, |
| 23 | V1_0::OperationType::LOCAL_RESPONSE_NORMALIZATION, |
| 24 | V1_0::OperationType::LOGISTIC, |
| 25 | V1_0::OperationType::LSTM, |
| 26 | V1_0::OperationType::L2_NORMALIZATION, |
| 27 | V1_0::OperationType::L2_POOL_2D, |
| 28 | V1_0::OperationType::MAX_POOL_2D, |
| 29 | V1_0::OperationType::MUL, |
| 30 | V1_0::OperationType::RELU, |
| 31 | V1_0::OperationType::RELU1, |
| 32 | V1_0::OperationType::RELU6, |
| 33 | V1_0::OperationType::SOFTMAX, |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 34 | V1_0::OperationType::SPACE_TO_DEPTH, |
Éanna Ó Catháin | 2fc21f7 | 2019-05-13 11:01:33 +0100 | [diff] [blame] | 35 | V1_0::OperationType::TANH, |
| 36 | V1_0::OperationType::RESHAPE, |
| 37 | V1_0::OperationType::RESIZE_BILINEAR, |
| 38 | }); |
| 39 | |
| 40 | bool CompliantWithVersion10(const V1_1::Operation & operation) |
| 41 | { |
| 42 | std::vector<V1_0::OperationType>::iterator it; |
| 43 | it = std::find(opsEquivalentInV10.begin(), opsEquivalentInV10.end(), |
| 44 | static_cast<V1_0::OperationType>(operation.type)); |
| 45 | |
| 46 | if(it != opsEquivalentInV10.end()) |
| 47 | { |
| 48 | return true; |
| 49 | } |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | V1_0::Operation ConvertOperationToVersion10(const V1_1::Operation & operation) |
| 54 | { |
| 55 | V1_0::Operation v10Operation; |
| 56 | v10Operation.type = static_cast<V1_0::OperationType>(operation.type); |
| 57 | v10Operation.inputs = operation.inputs; |
| 58 | v10Operation.outputs = operation.outputs; |
| 59 | return v10Operation; |
| 60 | } |
| 61 | } |
| 62 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 63 | namespace armnn_driver |
| 64 | { |
| 65 | namespace hal_1_1 |
| 66 | { |
| 67 | |
| 68 | bool HalPolicy::ConvertOperation(const Operation& operation, const Model& model, ConversionData& data) |
| 69 | { |
Éanna Ó Catháin | 2fc21f7 | 2019-05-13 11:01:33 +0100 | [diff] [blame] | 70 | if (CompliantWithVersion10(operation)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 71 | { |
Éanna Ó Catháin | 2fc21f7 | 2019-05-13 11:01:33 +0100 | [diff] [blame] | 72 | hal_1_0::HalPolicy::Operation v10Operation = ConvertOperationToVersion10(operation); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 73 | hal_1_0::HalPolicy::Model v10Model = convertToV1_0(model); |
| 74 | |
| 75 | return hal_1_0::HalPolicy::ConvertOperation(v10Operation, v10Model, data); |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | switch (operation.type) |
| 80 | { |
| 81 | case V1_1::OperationType::DIV: |
| 82 | return ConvertDiv(operation, model, data); |
David Beck | 38e1294 | 2018-09-12 16:02:24 +0100 | [diff] [blame] | 83 | case V1_1::OperationType::SUB: |
| 84 | return ConvertSub(operation, model, data); |
narpra01 | 3c05256 | 2018-09-17 14:25:04 +0100 | [diff] [blame] | 85 | case V1_1::OperationType::MEAN: |
| 86 | return ConvertMean(operation, model, data); |
Nina Drozd | 62a4a9f | 2018-10-01 14:20:25 +0100 | [diff] [blame] | 87 | case V1_1::OperationType::PAD: |
Aron Virginas-Tar | c921f6b | 2019-07-25 10:14:33 +0100 | [diff] [blame] | 88 | return ConvertPad(operation, model, data); |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 89 | case V1_1::OperationType::SPACE_TO_BATCH_ND: |
| 90 | return ConvertSpaceToBatchNd(operation, model, data); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 91 | case V1_1::OperationType::SQUEEZE: |
| 92 | return ConvertSqueeze(operation, model, data); |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 93 | case V1_1::OperationType::STRIDED_SLICE: |
| 94 | return ConvertStridedSlice(operation, model, data); |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 95 | case V1_1::OperationType::TRANSPOSE: |
| 96 | return ConvertTranspose(operation, model, data); |
Éanna Ó Catháin | 2cd99b9 | 2018-11-14 14:33:52 +0000 | [diff] [blame] | 97 | case V1_1::OperationType::BATCH_TO_SPACE_ND: |
| 98 | return ConvertBatchToSpaceNd(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 99 | default: |
| 100 | return Fail("%s: Operation type %s not supported in ArmnnDriver", |
| 101 | __func__, toString(operation.type).c_str()); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | bool HalPolicy::ConvertDiv(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_1::HalPolicy::ConvertDiv()"); |
| 109 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 110 | LayerInputHandle input0 = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data); |
| 111 | LayerInputHandle input1 = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 1, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 112 | |
| 113 | if (!input0.IsValid() || !input1.IsValid()) |
| 114 | { |
| 115 | return Fail("%s: Operation has invalid inputs", __func__); |
| 116 | } |
| 117 | |
| 118 | // The FuseActivation parameter is always the input index 2 |
| 119 | // and it should be optional |
| 120 | ActivationFn activationFunction; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 121 | if (!GetOptionalInputActivation<hal_1_1::HalPolicy>(operation, 2, activationFunction, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 122 | { |
| 123 | return Fail("%s: Operation has invalid inputs", __func__); |
| 124 | } |
| 125 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 126 | const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model); |
| 127 | if (!output) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 128 | { |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 129 | return Fail("%s: Could not read output 0", __func__); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 132 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 133 | if (IsDynamicTensor(outputInfo)) |
| 134 | { |
| 135 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 136 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 137 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 138 | bool isSupported = false; |
| 139 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 140 | IsDivisionSupported, |
| 141 | data.m_Backends, |
| 142 | isSupported, |
| 143 | input0.GetTensorInfo(), |
| 144 | input1.GetTensorInfo(), |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 145 | outputInfo); |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 146 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 147 | { |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | armnn::IConnectableLayer* const startLayer = data.m_Network->AddDivisionLayer(); |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 152 | armnn::IConnectableLayer* const endLayer = ProcessActivation(outputInfo, activationFunction, startLayer, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 153 | |
| 154 | const armnn::TensorInfo& inputTensorInfo0 = input0.GetTensorInfo(); |
| 155 | const armnn::TensorInfo& inputTensorInfo1 = input1.GetTensorInfo(); |
| 156 | |
| 157 | if (endLayer) |
| 158 | { |
| 159 | BroadcastTensor(input0, input1, startLayer, *data.m_Network); |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 160 | return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *endLayer, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | return Fail("%s: ProcessActivation failed", __func__); |
| 164 | } |
| 165 | |
David Beck | 38e1294 | 2018-09-12 16:02:24 +0100 | [diff] [blame] | 166 | bool HalPolicy::ConvertSub(const Operation& operation, const Model& model, ConversionData& data) |
| 167 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 168 | ALOGV("hal_1_1::HalPolicy::ConvertSub()"); |
Mike Kelly | 0a87936 | 2019-07-29 16:56:31 +0100 | [diff] [blame] | 169 | return ::ConvertSub<hal_1_1::HalPolicy>(operation, model, data); |
David Beck | 38e1294 | 2018-09-12 16:02:24 +0100 | [diff] [blame] | 170 | } |
| 171 | |
narpra01 | 3c05256 | 2018-09-17 14:25:04 +0100 | [diff] [blame] | 172 | bool HalPolicy::ConvertMean(const Operation& operation, const Model& model, ConversionData& data) |
| 173 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 174 | ALOGV("hal_1_1::HalPolicy::ConvertMean()"); |
| 175 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 176 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data); |
narpra01 | 3c05256 | 2018-09-17 14:25:04 +0100 | [diff] [blame] | 177 | if (!input.IsValid()) |
| 178 | { |
| 179 | return Fail("%s: Operation has invalid inputs", __func__); |
| 180 | } |
| 181 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 182 | const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model); |
| 183 | if (!output) |
| 184 | { |
| 185 | return Fail("%s: Could not read output 0", __func__); |
| 186 | } |
| 187 | |
| 188 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 189 | if (IsDynamicTensor(outputInfo)) |
| 190 | { |
| 191 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 192 | } |
| 193 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 194 | const Operand* axisOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 1, model); |
Matteo Martincigh | ae622b7 | 2018-10-23 18:25:38 +0100 | [diff] [blame] | 195 | if (!axisOperand) |
| 196 | { |
| 197 | return Fail("%s: Could not read input 1", __func__); |
| 198 | } |
| 199 | |
| 200 | std::vector<int32_t> axis; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 201 | if (!GetTensorInt32Values<hal_1_1::HalPolicy>(*axisOperand, axis, model, data)) |
Matteo Martincigh | ae622b7 | 2018-10-23 18:25:38 +0100 | [diff] [blame] | 202 | { |
| 203 | return Fail("%s: Input 1 has invalid values", __func__); |
| 204 | } |
| 205 | |
| 206 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 207 | |
| 208 | // Convert the axis to unsigned int and remove duplicates. |
| 209 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 210 | std::set<unsigned int> uniqueAxis; |
| 211 | std::transform(axis.begin(), axis.end(), |
| 212 | std::inserter(uniqueAxis, uniqueAxis.begin()), |
| 213 | [rank](int i) -> unsigned int { return (i + rank) % rank; }); |
| 214 | |
| 215 | // Get the "keep dims" flag. |
| 216 | int32_t keepDims = 0; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 217 | if (!GetInputInt32<hal_1_1::HalPolicy>(operation, 2, keepDims, model, data)) |
Matteo Martincigh | ae622b7 | 2018-10-23 18:25:38 +0100 | [diff] [blame] | 218 | { |
| 219 | return Fail("%s: Could not read input 2", __func__); |
| 220 | } |
narpra01 | 3c05256 | 2018-09-17 14:25:04 +0100 | [diff] [blame] | 221 | |
| 222 | armnn::MeanDescriptor descriptor; |
Matteo Martincigh | ae622b7 | 2018-10-23 18:25:38 +0100 | [diff] [blame] | 223 | descriptor.m_Axis.assign(uniqueAxis.begin(), uniqueAxis.end()); |
| 224 | descriptor.m_KeepDims = keepDims > 0; |
narpra01 | 3c05256 | 2018-09-17 14:25:04 +0100 | [diff] [blame] | 225 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 226 | bool isSupported = false; |
| 227 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 228 | IsMeanSupported, |
| 229 | data.m_Backends, |
| 230 | isSupported, |
| 231 | inputInfo, |
| 232 | outputInfo, |
| 233 | descriptor); |
| 234 | if (!isSupported) |
narpra01 | 3c05256 | 2018-09-17 14:25:04 +0100 | [diff] [blame] | 235 | { |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | armnn::IConnectableLayer* const layer = data.m_Network->AddMeanLayer(descriptor); |
narpra01 | 96bedf0 | 2018-09-26 16:57:28 +0100 | [diff] [blame] | 240 | assert(layer != nullptr); |
| 241 | input.Connect(layer->GetInputSlot(0)); |
narpra01 | 3c05256 | 2018-09-17 14:25:04 +0100 | [diff] [blame] | 242 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 243 | return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *layer, model, data); |
narpra01 | 3c05256 | 2018-09-17 14:25:04 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Aron Virginas-Tar | c921f6b | 2019-07-25 10:14:33 +0100 | [diff] [blame] | 246 | bool HalPolicy::ConvertPad(const Operation& operation, const Model& model, ConversionData& data) |
| 247 | { |
| 248 | ALOGV("hal_1_1::HalPolicy::ConvertPad()"); |
| 249 | return ::ConvertPad<hal_1_1::HalPolicy>(operation, model, data); |
| 250 | } |
| 251 | |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 252 | bool HalPolicy::ConvertSpaceToBatchNd(const Operation& operation, const Model& model, ConversionData& data) |
| 253 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 254 | ALOGV("hal_1_1::HalPolicy::ConvertSpaceToBatchNd()"); |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 255 | |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 256 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data); |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 257 | if (!input.IsValid()) |
| 258 | { |
| 259 | return Fail("%s: Operation has invalid inputs", __func__); |
| 260 | } |
| 261 | |
| 262 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 263 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 264 | unsigned int spatialDim = rank - 2; |
| 265 | |
| 266 | if (rank != 4) |
| 267 | { |
| 268 | Fail("%s: Only inputs with rank 4 are supported", __func__); |
| 269 | } |
| 270 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 271 | const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model); |
| 272 | if (!output) |
| 273 | { |
| 274 | return Fail("%s: Could not read output 0", __func__); |
| 275 | } |
| 276 | |
| 277 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 278 | if (IsDynamicTensor(outputInfo)) |
| 279 | { |
| 280 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 281 | } |
| 282 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 283 | const Operand* blockShapeOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 1, model); |
| 284 | const Operand* paddingsOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 2, model); |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 285 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 286 | armnn::TensorShape blockShapeOperandShape = GetTensorShapeForOperand(*blockShapeOperand); |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 287 | if (blockShapeOperandShape.GetNumDimensions() != 1 || blockShapeOperandShape.GetNumElements() != spatialDim) |
| 288 | { |
| 289 | return Fail("%s: Operation has invalid block shape operand: expected shape [%d]", __func__, spatialDim); |
| 290 | } |
| 291 | |
| 292 | std::vector<int32_t> blockShape; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 293 | GetTensorInt32Values<hal_1_1::HalPolicy>(*blockShapeOperand, blockShape, model, data); |
Sadik Armagan | 8bef7b3 | 2018-12-20 14:14:12 +0000 | [diff] [blame] | 294 | if (std::any_of(blockShape.cbegin(), blockShape.cend(), [](int32_t i){ return i < 1; })) |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 295 | { |
Sadik Armagan | 8bef7b3 | 2018-12-20 14:14:12 +0000 | [diff] [blame] | 296 | return Fail("%s: Block shape must be at least 1 in all dimensions.", __func__); |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | armnn::TensorShape paddingsOperandShape = GetTensorShapeForOperand(*paddingsOperand); |
| 300 | if (paddingsOperandShape.GetNumDimensions() != 2 || paddingsOperandShape.GetNumElements() != 2 * spatialDim) |
| 301 | { |
| 302 | return Fail("%s: Operation has invalid paddings operand: expected shape [%d, 2]", __func__, spatialDim); |
| 303 | } |
| 304 | |
Sadik Armagan | 8bef7b3 | 2018-12-20 14:14:12 +0000 | [diff] [blame] | 305 | std::vector<std::pair<unsigned int, unsigned int>> paddingList; |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 306 | std::vector<int32_t> paddings; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 307 | GetTensorInt32Values<hal_1_1::HalPolicy>(*paddingsOperand, paddings, model, data); |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 308 | for (unsigned int i = 0; i < paddings.size() - 1; i += 2) |
| 309 | { |
| 310 | int paddingBeforeInput = paddings[i]; |
| 311 | int paddingAfterInput = paddings[i + 1]; |
| 312 | if (paddingBeforeInput < 0 || paddingAfterInput < 0) |
| 313 | { |
| 314 | return Fail("%s: Operation has invalid paddings operand, invalid padding values.", __func__); |
| 315 | } |
| 316 | |
Sadik Armagan | 8bef7b3 | 2018-12-20 14:14:12 +0000 | [diff] [blame] | 317 | paddingList.emplace_back((unsigned int) paddingBeforeInput, (unsigned int) paddingAfterInput); |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Sadik Armagan | 8bef7b3 | 2018-12-20 14:14:12 +0000 | [diff] [blame] | 320 | armnn::SpaceToBatchNdDescriptor descriptor; |
| 321 | descriptor.m_DataLayout = armnn::DataLayout::NHWC; |
| 322 | descriptor.m_BlockShape.assign(blockShape.cbegin(), blockShape.cend()); |
| 323 | descriptor.m_PadList.assign(paddingList.cbegin(), paddingList.cend()); |
| 324 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 325 | bool isSupported = false; |
| 326 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 327 | IsSpaceToBatchNdSupported, |
| 328 | data.m_Backends, |
| 329 | isSupported, |
| 330 | inputInfo, |
| 331 | outputInfo, |
| 332 | descriptor); |
| 333 | if (!isSupported) |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 334 | { |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | armnn::IConnectableLayer* const layer = data.m_Network->AddSpaceToBatchNdLayer(descriptor); |
| 339 | assert(layer != nullptr); |
| 340 | input.Connect(layer->GetInputSlot(0)); |
| 341 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 342 | return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *layer, model, data); |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 343 | } |
| 344 | |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 345 | bool HalPolicy::ConvertSqueeze(const Operation& operation, const Model& model, ConversionData& data) |
| 346 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 347 | ALOGV("hal_1_1::HalPolicy::ConvertSqueeze()"); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 348 | |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 349 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 350 | if (!input.IsValid()) |
| 351 | { |
| 352 | return Fail("%s: Operation has invalid inputs", __func__); |
| 353 | } |
| 354 | |
| 355 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 356 | unsigned int rank = inputInfo.GetNumDimensions(); |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 357 | if (rank > 4) |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 358 | { |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 359 | Fail("%s: Inputs with rank greater than 4 are not supported", __func__); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 360 | } |
| 361 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 362 | const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model); |
| 363 | if (!output) |
| 364 | { |
| 365 | return Fail("%s: Could not read output 0", __func__); |
| 366 | } |
| 367 | |
| 368 | if (IsDynamicTensor(GetTensorInfoForOperand(*output))) |
| 369 | { |
| 370 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 371 | } |
| 372 | |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 373 | // NOTE: Axis is an optional parameter to SQUEEZE, therefore we do not want to generate a failure |
| 374 | // if the operand index is out of bounds. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 375 | const Operand* axisOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 1, model, false); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 376 | |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 377 | const uint32_t dimensionSequence[] = { 0, 1, 2, 3 }; |
| 378 | |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 379 | std::vector<int32_t> axis; |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 380 | if (!axisOperand) |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 381 | { |
| 382 | axis.assign(dimensionSequence, |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 383 | dimensionSequence + rank); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 384 | } |
| 385 | else |
| 386 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 387 | GetTensorInt32Values<hal_1_1::HalPolicy>(*axisOperand, axis, model, data); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 388 | } |
| 389 | |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 390 | |
saoste01 | a893efa | 2018-10-13 11:56:12 +0100 | [diff] [blame] | 391 | std::vector<uint32_t> outputDims; |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 392 | for (unsigned int i = 0; i < rank; i++) |
saoste01 | a893efa | 2018-10-13 11:56:12 +0100 | [diff] [blame] | 393 | { |
| 394 | bool skipSqueeze = (std::find(axis.begin(), axis.end(), i) == axis.end()); |
| 395 | auto currentDimension = inputInfo.GetShape()[i]; |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 396 | if (skipSqueeze || currentDimension != 1) |
| 397 | { |
| 398 | outputDims.push_back(currentDimension); |
| 399 | } |
| 400 | } |
| 401 | |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 402 | armnn::TensorShape outShape = armnn::TensorShape(outputDims.size(), outputDims.data()); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 403 | |
| 404 | armnn::TensorInfo outputInfo = inputInfo; |
| 405 | outputInfo.SetShape(outShape); |
| 406 | |
| 407 | armnn::ReshapeDescriptor reshapeDesc; |
| 408 | reshapeDesc.m_TargetShape = outputInfo.GetShape(); |
| 409 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 410 | bool isSupported = false; |
| 411 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 412 | IsReshapeSupported, |
| 413 | data.m_Backends, |
| 414 | isSupported, |
| 415 | inputInfo, |
| 416 | reshapeDesc); |
| 417 | if (!isSupported) |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 418 | { |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | armnn::IConnectableLayer* const layer = data.m_Network->AddReshapeLayer(reshapeDesc); |
| 423 | assert(layer != nullptr); |
| 424 | input.Connect(layer->GetInputSlot(0)); |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 425 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 426 | return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *layer, model, data); |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 427 | } |
| 428 | |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 429 | bool HalPolicy::ConvertStridedSlice(const Operation& operation, const Model& model, ConversionData& data) |
| 430 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 431 | ALOGV("hal_1_1::HalPolicy::ConvertStridedSlice()"); |
| 432 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 433 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data); |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 434 | if (!input.IsValid()) |
| 435 | { |
| 436 | return Fail("%s: Operation has invalid inputs", __func__); |
| 437 | } |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 438 | |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 439 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 440 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 441 | if (rank > 4) |
| 442 | { |
| 443 | Fail("%s: Inputs with rank greater than 4 are not supported", __func__); |
| 444 | } |
| 445 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 446 | const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model); |
| 447 | if (!output) |
| 448 | { |
| 449 | return Fail("%s: Could not read output 0", __func__); |
| 450 | } |
| 451 | |
| 452 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 453 | if (IsDynamicTensor(outputInfo)) |
| 454 | { |
| 455 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 456 | } |
| 457 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 458 | const Operand* beginOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 1, model); |
| 459 | const Operand* endOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 2, model); |
| 460 | const Operand* stridesOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 3, model); |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 461 | |
| 462 | std::vector<int32_t> beginValues; |
| 463 | std::vector<int32_t> endValues; |
| 464 | std::vector<int32_t> stridesValues; |
| 465 | |
| 466 | // The length of the beginOperand, endOperand and stridesOperand must be of a rank(input) |
| 467 | auto ValidateInputOperands = [&] (const Operand& operand, std::vector<int32_t>& operandValues) |
| 468 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 469 | if (!GetTensorInt32Values<hal_1_1::HalPolicy>(operand, operandValues, model, data)) |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 470 | { |
| 471 | return false; |
| 472 | } |
| 473 | |
| 474 | if (operandValues.size() != rank) |
| 475 | { |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | return true; |
| 480 | }; |
| 481 | |
| 482 | if (!ValidateInputOperands(*beginOperand, beginValues) |
| 483 | || !ValidateInputOperands(*endOperand, endValues) |
| 484 | || !ValidateInputOperands(*stridesOperand, stridesValues)) |
| 485 | { |
| 486 | return Fail("%s: Operation has invalid input operand", __func__); |
| 487 | } |
| 488 | |
| 489 | // Stride cannot have value '0' |
| 490 | if (std::any_of(stridesValues.cbegin(), stridesValues.cend(), [](int32_t i){ return i == 0; })) |
| 491 | { |
| 492 | return Fail("%s: Stride must be non-zero value.", __func__); |
| 493 | } |
| 494 | |
| 495 | armnn::StridedSliceDescriptor descriptor; |
| 496 | descriptor.m_Begin.assign(beginValues.cbegin(), beginValues.cend()); |
| 497 | descriptor.m_End.assign(endValues.cbegin(), endValues.cend()); |
| 498 | descriptor.m_Stride.assign(stridesValues.cbegin(), stridesValues.cend()); |
| 499 | descriptor.m_DataLayout = armnn::DataLayout::NHWC; |
| 500 | |
| 501 | // Get the "begin_mask", "end_mask", and "shrink_axis_mask" flags |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 502 | if (!GetInputInt32<hal_1_1::HalPolicy>(operation, 4, descriptor.m_BeginMask, model, data) || |
| 503 | !GetInputInt32<hal_1_1::HalPolicy>(operation, 5, descriptor.m_EndMask, model, data) || |
| 504 | !GetInputInt32<hal_1_1::HalPolicy>(operation, 6, descriptor.m_ShrinkAxisMask, model, data)) |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 505 | { |
| 506 | return Fail("%s: Operation has invalid inputs", __func__); |
| 507 | } |
| 508 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 509 | bool isSupported = false; |
| 510 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 511 | IsStridedSliceSupported, |
| 512 | data.m_Backends, |
| 513 | isSupported, |
| 514 | inputInfo, |
| 515 | outputInfo, |
| 516 | descriptor); |
| 517 | if (!isSupported) |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 518 | { |
| 519 | return false; |
| 520 | } |
| 521 | |
| 522 | armnn::IConnectableLayer* const layer = data.m_Network->AddStridedSliceLayer(descriptor); |
| 523 | assert(layer != nullptr); |
| 524 | input.Connect(layer->GetInputSlot(0)); |
| 525 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 526 | return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *layer, model, data); |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 527 | } |
| 528 | |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 529 | bool HalPolicy::ConvertTranspose(const Operation& operation, const Model& model, ConversionData& data) |
| 530 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 531 | ALOGV("hal_1_1::HalPolicy::ConvertTranspose()"); |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 532 | |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 533 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data); |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 534 | if (!input.IsValid()) |
| 535 | { |
| 536 | return Fail("%s: Operation has invalid inputs", __func__); |
| 537 | } |
| 538 | |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 539 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 540 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 541 | if (rank > 4) |
| 542 | { |
| 543 | Fail("%s: Inputs with rank greater than 4 are not supported", __func__); |
| 544 | } |
| 545 | |
| 546 | // NOTE: Axis is an optional parameter to TRANSPOSE, therefore we do not want to generate a failure |
| 547 | // if the operand index is out of bounds. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 548 | const Operand* permOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 1, model, false); |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 549 | |
| 550 | std::vector<int32_t> perm(rank); |
| 551 | if (!permOperand) |
| 552 | { |
| 553 | // NOTE: If perm is not given, it is set to (n-1...0), where n is the rank of the tensor |
| 554 | for (unsigned int i = rank; i > 0; i--) |
| 555 | { |
| 556 | perm[rank - i] = boost::numeric_cast<int> (i - 1); |
| 557 | } |
| 558 | } |
| 559 | else |
| 560 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 561 | GetTensorInt32Values<hal_1_1::HalPolicy>(*permOperand, perm, model, data); |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | std::vector<uint32_t> outputDims(perm.begin(), perm.begin() + rank); |
| 565 | |
| 566 | auto permutationVector = armnn::PermutationVector(outputDims.data(), outputDims.size()); |
| 567 | if (!permutationVector.IsEqual(NHWCToArmNN) |
| 568 | && !permutationVector.IsEqual(ArmNNToNHWC) |
| 569 | && !permutationVector.IsEqual({ 3, 2, 0, 1 })) |
| 570 | { |
| 571 | return Fail("%s: Only [0, 3, 1, 2], [0, 2, 3, 1] and [3, 2, 0, 1] permutations are supported.", __func__); |
| 572 | } |
| 573 | |
| 574 | armnn::PermuteDescriptor permuteDesc; |
| 575 | permuteDesc.m_DimMappings = permutationVector; |
| 576 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 577 | const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model); |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 578 | if (!output) |
| 579 | { |
| 580 | return Fail("%s: Could not read output 0", __func__); |
| 581 | } |
| 582 | |
| 583 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 584 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 585 | bool isSupported = false; |
| 586 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 587 | IsPermuteSupported, |
| 588 | data.m_Backends, |
| 589 | isSupported, |
| 590 | inputInfo, |
| 591 | outputInfo, |
| 592 | permuteDesc); |
| 593 | if (!isSupported) |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 594 | { |
| 595 | return false; |
| 596 | } |
| 597 | |
| 598 | armnn::IConnectableLayer* const layer = data.m_Network->AddPermuteLayer(permuteDesc); |
| 599 | assert(layer != nullptr); |
| 600 | input.Connect(layer->GetInputSlot(0)); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 601 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 602 | return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *layer, model, data); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 603 | } |
| 604 | |
Éanna Ó Catháin | 2cd99b9 | 2018-11-14 14:33:52 +0000 | [diff] [blame] | 605 | bool HalPolicy::ConvertBatchToSpaceNd(const Operation& operation, const Model& model, ConversionData& data) |
| 606 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 607 | ALOGV("hal_1_1::HalPolicy::ConvertBatchToSpaceNd()"); |
| 608 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 609 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data); |
Éanna Ó Catháin | 2cd99b9 | 2018-11-14 14:33:52 +0000 | [diff] [blame] | 610 | if (!input.IsValid()) |
| 611 | { |
| 612 | return Fail("%s: Operation has invalid inputs", __func__); |
| 613 | } |
| 614 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 615 | const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model); |
| 616 | if (!output) |
| 617 | { |
| 618 | return Fail("%s: Could not read output 0", __func__); |
| 619 | } |
| 620 | |
| 621 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 622 | if (IsDynamicTensor(outputInfo)) |
| 623 | { |
| 624 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 625 | } |
| 626 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 627 | const Operand* blockOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 1, model); |
Éanna Ó Catháin | 2cd99b9 | 2018-11-14 14:33:52 +0000 | [diff] [blame] | 628 | if (!blockOperand) |
| 629 | { |
| 630 | return Fail("%s: Could not read input 1", __func__); |
| 631 | } |
| 632 | |
| 633 | // Convert the block operand to int32 |
| 634 | std::vector<int32_t> block; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 635 | if (!GetTensorInt32Values<hal_1_1::HalPolicy>(*blockOperand, block, model, data)) |
Éanna Ó Catháin | 2cd99b9 | 2018-11-14 14:33:52 +0000 | [diff] [blame] | 636 | { |
| 637 | return Fail("%s: Input 1 has invalid values", __func__); |
| 638 | } |
| 639 | |
| 640 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 641 | |
| 642 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 643 | if (rank != 4) |
| 644 | { |
| 645 | Fail("%s: Only inputs with rank equal to 4 are supported", __func__); |
| 646 | } |
| 647 | |
| 648 | if (std::any_of(block.cbegin(), block.cend(), [](int32_t i){ return i < 1; })) |
| 649 | { |
| 650 | return Fail("%s: Block sizes for each spatial dimension of the input tensor must be" |
| 651 | " greater than or equal to 1", __func__); |
| 652 | } |
| 653 | |
| 654 | armnn::BatchToSpaceNdDescriptor batchToSpaceNdDesc; |
| 655 | batchToSpaceNdDesc.m_BlockShape.assign(block.cbegin(), block.cend()); |
| 656 | batchToSpaceNdDesc.m_DataLayout = armnn::DataLayout::NHWC; |
| 657 | |
| 658 | // Setting crops to 0,0 0,0 as it is not supported in Android NN API |
| 659 | batchToSpaceNdDesc.m_Crops = {{0, 0}, {0, 0}}; |
| 660 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 661 | bool isSupported = false; |
| 662 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 663 | IsBatchToSpaceNdSupported, |
| 664 | data.m_Backends, |
| 665 | isSupported, |
| 666 | inputInfo, |
| 667 | outputInfo, |
| 668 | batchToSpaceNdDesc); |
| 669 | if (!isSupported) |
Éanna Ó Catháin | 2cd99b9 | 2018-11-14 14:33:52 +0000 | [diff] [blame] | 670 | { |
| 671 | return false; |
| 672 | } |
| 673 | |
| 674 | armnn::IConnectableLayer* const layer = data.m_Network->AddBatchToSpaceNdLayer(batchToSpaceNdDesc); |
| 675 | assert(layer != nullptr); |
| 676 | input.Connect(layer->GetInputSlot(0)); |
| 677 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 678 | return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *layer, model, data); |
Éanna Ó Catháin | 2cd99b9 | 2018-11-14 14:33:52 +0000 | [diff] [blame] | 679 | } |
| 680 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 681 | } // namespace hal_1_1 |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 682 | } // namespace armnn_driver |