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