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