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 | |
| 274 | if (!input.IsValid()) |
| 275 | { |
| 276 | return Fail("%s: Operation has invalid inputs", __func__); |
| 277 | } |
| 278 | |
| 279 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 280 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 281 | const Operand* paddingsOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 1, model); |
Nina Drozd | 62a4a9f | 2018-10-01 14:20:25 +0100 | [diff] [blame] | 282 | |
| 283 | if (!paddingsOperand) |
| 284 | { |
| 285 | return Fail("%s: Could not read paddings operand", __func__); |
| 286 | } |
| 287 | |
| 288 | unsigned int rank = inputInfo.GetNumDimensions(); |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 289 | armnn::TensorShape paddingsOperandShape = GetTensorShapeForOperand(*paddingsOperand); |
Éanna Ó Catháin | 074f1ec | 2019-01-14 16:18:49 +0000 | [diff] [blame] | 290 | if (paddingsOperandShape.GetNumDimensions() != 2 || paddingsOperandShape.GetNumElements() != rank * 2) |
Nina Drozd | 62a4a9f | 2018-10-01 14:20:25 +0100 | [diff] [blame] | 291 | { |
| 292 | return Fail("%s: Operation has invalid paddings operand: expected shape [%d, 2]", __func__, rank); |
| 293 | } |
| 294 | |
| 295 | std::vector<int32_t> paddings; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 296 | GetTensorInt32Values<hal_1_1::HalPolicy>(*paddingsOperand, paddings, model, data); |
Nina Drozd | 62a4a9f | 2018-10-01 14:20:25 +0100 | [diff] [blame] | 297 | |
| 298 | // add padding for each dimension of input tensor. |
| 299 | armnn::PadDescriptor descriptor; |
| 300 | for (unsigned int i = 0; i < paddings.size() - 1; i += 2) |
| 301 | { |
| 302 | int paddingBeforeInput = paddings[i]; |
| 303 | int paddingAfterInput = paddings[i + 1]; |
| 304 | if (paddingBeforeInput < 0 || paddingAfterInput < 0) |
| 305 | { |
| 306 | return Fail("%s: Operation has invalid paddings operand, invalid padding values.", __func__); |
| 307 | } |
| 308 | descriptor.m_PadList.emplace_back((unsigned int) paddingBeforeInput, (unsigned int) paddingAfterInput); |
| 309 | } |
| 310 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 311 | const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model); |
Nina Drozd | 62a4a9f | 2018-10-01 14:20:25 +0100 | [diff] [blame] | 312 | if (!output) |
| 313 | { |
| 314 | return Fail("%s: Could not read output 0", __func__); |
| 315 | } |
| 316 | |
| 317 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 318 | |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 319 | if (!IsLayerSupportedForAnyBackend(__func__, |
| 320 | armnn::IsPadSupported, |
| 321 | data.m_Backends, |
| 322 | inputInfo, |
| 323 | outputInfo, |
| 324 | descriptor)) |
Nina Drozd | 62a4a9f | 2018-10-01 14:20:25 +0100 | [diff] [blame] | 325 | { |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | armnn::IConnectableLayer* const layer = data.m_Network->AddPadLayer(descriptor); |
| 330 | assert(layer != nullptr); |
| 331 | input.Connect(layer->GetInputSlot(0)); |
| 332 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 333 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 334 | return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *layer, model, data); |
Nina Drozd | 62a4a9f | 2018-10-01 14:20:25 +0100 | [diff] [blame] | 335 | } |
| 336 | |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 337 | bool HalPolicy::ConvertSpaceToBatchNd(const Operation& operation, const Model& model, ConversionData& data) |
| 338 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 339 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data); |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 340 | |
| 341 | if (!input.IsValid()) |
| 342 | { |
| 343 | return Fail("%s: Operation has invalid inputs", __func__); |
| 344 | } |
| 345 | |
| 346 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 347 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 348 | unsigned int spatialDim = rank - 2; |
| 349 | |
| 350 | if (rank != 4) |
| 351 | { |
| 352 | Fail("%s: Only inputs with rank 4 are supported", __func__); |
| 353 | } |
| 354 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 355 | const Operand* blockShapeOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 1, model); |
| 356 | const Operand* paddingsOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 2, model); |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 357 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 358 | armnn::TensorShape blockShapeOperandShape = GetTensorShapeForOperand(*blockShapeOperand); |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 359 | if (blockShapeOperandShape.GetNumDimensions() != 1 || blockShapeOperandShape.GetNumElements() != spatialDim) |
| 360 | { |
| 361 | return Fail("%s: Operation has invalid block shape operand: expected shape [%d]", __func__, spatialDim); |
| 362 | } |
| 363 | |
| 364 | std::vector<int32_t> blockShape; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 365 | GetTensorInt32Values<hal_1_1::HalPolicy>(*blockShapeOperand, blockShape, model, data); |
Sadik Armagan | 8bef7b3 | 2018-12-20 14:14:12 +0000 | [diff] [blame] | 366 | 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] | 367 | { |
Sadik Armagan | 8bef7b3 | 2018-12-20 14:14:12 +0000 | [diff] [blame] | 368 | 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] | 369 | } |
| 370 | |
| 371 | armnn::TensorShape paddingsOperandShape = GetTensorShapeForOperand(*paddingsOperand); |
| 372 | if (paddingsOperandShape.GetNumDimensions() != 2 || paddingsOperandShape.GetNumElements() != 2 * spatialDim) |
| 373 | { |
| 374 | return Fail("%s: Operation has invalid paddings operand: expected shape [%d, 2]", __func__, spatialDim); |
| 375 | } |
| 376 | |
Sadik Armagan | 8bef7b3 | 2018-12-20 14:14:12 +0000 | [diff] [blame] | 377 | std::vector<std::pair<unsigned int, unsigned int>> paddingList; |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 378 | std::vector<int32_t> paddings; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 379 | GetTensorInt32Values<hal_1_1::HalPolicy>(*paddingsOperand, paddings, model, data); |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 380 | for (unsigned int i = 0; i < paddings.size() - 1; i += 2) |
| 381 | { |
| 382 | int paddingBeforeInput = paddings[i]; |
| 383 | int paddingAfterInput = paddings[i + 1]; |
| 384 | if (paddingBeforeInput < 0 || paddingAfterInput < 0) |
| 385 | { |
| 386 | return Fail("%s: Operation has invalid paddings operand, invalid padding values.", __func__); |
| 387 | } |
| 388 | |
Sadik Armagan | 8bef7b3 | 2018-12-20 14:14:12 +0000 | [diff] [blame] | 389 | paddingList.emplace_back((unsigned int) paddingBeforeInput, (unsigned int) paddingAfterInput); |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Sadik Armagan | 8bef7b3 | 2018-12-20 14:14:12 +0000 | [diff] [blame] | 392 | armnn::SpaceToBatchNdDescriptor descriptor; |
| 393 | descriptor.m_DataLayout = armnn::DataLayout::NHWC; |
| 394 | descriptor.m_BlockShape.assign(blockShape.cbegin(), blockShape.cend()); |
| 395 | descriptor.m_PadList.assign(paddingList.cbegin(), paddingList.cend()); |
| 396 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 397 | const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model); |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 398 | if (!output) |
| 399 | { |
| 400 | return Fail("%s: Could not read output 0", __func__); |
| 401 | } |
| 402 | |
| 403 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 404 | if (!IsLayerSupportedForAnyBackend(__func__, |
| 405 | armnn::IsSpaceToBatchNdSupported, |
| 406 | data.m_Backends, |
| 407 | inputInfo, |
| 408 | outputInfo, |
| 409 | descriptor)) |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 410 | { |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | armnn::IConnectableLayer* const layer = data.m_Network->AddSpaceToBatchNdLayer(descriptor); |
| 415 | assert(layer != nullptr); |
| 416 | input.Connect(layer->GetInputSlot(0)); |
| 417 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 418 | return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *layer, model, data); |
Nattapat Chaimanowong | 81a6834 | 2018-11-05 14:04:47 +0000 | [diff] [blame] | 419 | } |
| 420 | |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 421 | bool HalPolicy::ConvertSqueeze(const Operation& operation, const Model& model, ConversionData& data) |
| 422 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 423 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 424 | |
| 425 | if (!input.IsValid()) |
| 426 | { |
| 427 | return Fail("%s: Operation has invalid inputs", __func__); |
| 428 | } |
| 429 | |
| 430 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 431 | |
| 432 | unsigned int rank = inputInfo.GetNumDimensions(); |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 433 | if (rank > 4) |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 434 | { |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 435 | Fail("%s: Inputs with rank greater than 4 are not supported", __func__); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | // NOTE: Axis is an optional parameter to SQUEEZE, therefore we do not want to generate a failure |
| 439 | // if the operand index is out of bounds. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 440 | const Operand* axisOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 1, model, false); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 441 | |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 442 | const uint32_t dimensionSequence[] = { 0, 1, 2, 3 }; |
| 443 | |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 444 | std::vector<int32_t> axis; |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 445 | if (!axisOperand) |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 446 | { |
| 447 | axis.assign(dimensionSequence, |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 448 | dimensionSequence + rank); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 449 | } |
| 450 | else |
| 451 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 452 | GetTensorInt32Values<hal_1_1::HalPolicy>(*axisOperand, axis, model, data); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 453 | } |
| 454 | |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 455 | |
saoste01 | a893efa | 2018-10-13 11:56:12 +0100 | [diff] [blame] | 456 | std::vector<uint32_t> outputDims; |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 457 | for (unsigned int i = 0; i < rank; i++) |
saoste01 | a893efa | 2018-10-13 11:56:12 +0100 | [diff] [blame] | 458 | { |
| 459 | bool skipSqueeze = (std::find(axis.begin(), axis.end(), i) == axis.end()); |
| 460 | auto currentDimension = inputInfo.GetShape()[i]; |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 461 | if (skipSqueeze || currentDimension != 1) |
| 462 | { |
| 463 | outputDims.push_back(currentDimension); |
| 464 | } |
| 465 | } |
| 466 | |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 467 | armnn::TensorShape outShape = armnn::TensorShape(outputDims.size(), outputDims.data()); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 468 | |
| 469 | armnn::TensorInfo outputInfo = inputInfo; |
| 470 | outputInfo.SetShape(outShape); |
| 471 | |
| 472 | armnn::ReshapeDescriptor reshapeDesc; |
| 473 | reshapeDesc.m_TargetShape = outputInfo.GetShape(); |
| 474 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 475 | const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 476 | if (!output) |
| 477 | { |
| 478 | return Fail("%s: Could not read output 0", __func__); |
| 479 | } |
| 480 | |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 481 | if (!IsLayerSupportedForAnyBackend(__func__, |
| 482 | armnn::IsReshapeSupported, |
| 483 | data.m_Backends, |
| 484 | inputInfo, |
| 485 | reshapeDesc)) |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 486 | { |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | armnn::IConnectableLayer* const layer = data.m_Network->AddReshapeLayer(reshapeDesc); |
| 491 | assert(layer != nullptr); |
| 492 | input.Connect(layer->GetInputSlot(0)); |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 493 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 494 | return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *layer, model, data); |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 495 | } |
| 496 | |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 497 | bool HalPolicy::ConvertStridedSlice(const Operation& operation, const Model& model, ConversionData& data) |
| 498 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 499 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data); |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 500 | if (!input.IsValid()) |
| 501 | { |
| 502 | return Fail("%s: Operation has invalid inputs", __func__); |
| 503 | } |
| 504 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 505 | |
| 506 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 507 | if (rank > 4) |
| 508 | { |
| 509 | Fail("%s: Inputs with rank greater than 4 are not supported", __func__); |
| 510 | } |
| 511 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 512 | const Operand* beginOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 1, model); |
| 513 | const Operand* endOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 2, model); |
| 514 | const Operand* stridesOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 3, model); |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 515 | |
| 516 | std::vector<int32_t> beginValues; |
| 517 | std::vector<int32_t> endValues; |
| 518 | std::vector<int32_t> stridesValues; |
| 519 | |
| 520 | // The length of the beginOperand, endOperand and stridesOperand must be of a rank(input) |
| 521 | auto ValidateInputOperands = [&] (const Operand& operand, std::vector<int32_t>& operandValues) |
| 522 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 523 | if (!GetTensorInt32Values<hal_1_1::HalPolicy>(operand, operandValues, model, data)) |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 524 | { |
| 525 | return false; |
| 526 | } |
| 527 | |
| 528 | if (operandValues.size() != rank) |
| 529 | { |
| 530 | return false; |
| 531 | } |
| 532 | |
| 533 | return true; |
| 534 | }; |
| 535 | |
| 536 | if (!ValidateInputOperands(*beginOperand, beginValues) |
| 537 | || !ValidateInputOperands(*endOperand, endValues) |
| 538 | || !ValidateInputOperands(*stridesOperand, stridesValues)) |
| 539 | { |
| 540 | return Fail("%s: Operation has invalid input operand", __func__); |
| 541 | } |
| 542 | |
| 543 | // Stride cannot have value '0' |
| 544 | if (std::any_of(stridesValues.cbegin(), stridesValues.cend(), [](int32_t i){ return i == 0; })) |
| 545 | { |
| 546 | return Fail("%s: Stride must be non-zero value.", __func__); |
| 547 | } |
| 548 | |
| 549 | armnn::StridedSliceDescriptor descriptor; |
| 550 | descriptor.m_Begin.assign(beginValues.cbegin(), beginValues.cend()); |
| 551 | descriptor.m_End.assign(endValues.cbegin(), endValues.cend()); |
| 552 | descriptor.m_Stride.assign(stridesValues.cbegin(), stridesValues.cend()); |
| 553 | descriptor.m_DataLayout = armnn::DataLayout::NHWC; |
| 554 | |
| 555 | // Get the "begin_mask", "end_mask", and "shrink_axis_mask" flags |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 556 | if (!GetInputInt32<hal_1_1::HalPolicy>(operation, 4, descriptor.m_BeginMask, model, data) || |
| 557 | !GetInputInt32<hal_1_1::HalPolicy>(operation, 5, descriptor.m_EndMask, model, data) || |
| 558 | !GetInputInt32<hal_1_1::HalPolicy>(operation, 6, descriptor.m_ShrinkAxisMask, model, data)) |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 559 | { |
| 560 | return Fail("%s: Operation has invalid inputs", __func__); |
| 561 | } |
| 562 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 563 | const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model); |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 564 | if (!output) |
| 565 | { |
| 566 | return Fail("%s: Could not read output 0", __func__); |
| 567 | } |
| 568 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 569 | |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 570 | if (!IsLayerSupportedForAnyBackend(__func__, |
| 571 | armnn::IsStridedSliceSupported, |
| 572 | data.m_Backends, |
| 573 | inputInfo, |
| 574 | outputInfo, |
| 575 | descriptor)) |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 576 | { |
| 577 | return false; |
| 578 | } |
| 579 | |
| 580 | armnn::IConnectableLayer* const layer = data.m_Network->AddStridedSliceLayer(descriptor); |
| 581 | assert(layer != nullptr); |
| 582 | input.Connect(layer->GetInputSlot(0)); |
| 583 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 584 | return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *layer, model, data); |
Sadik Armagan | 758eee8 | 2018-11-15 15:34:49 +0000 | [diff] [blame] | 585 | } |
| 586 | |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 587 | bool HalPolicy::ConvertTranspose(const Operation& operation, const Model& model, ConversionData& data) |
| 588 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 589 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data); |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 590 | |
| 591 | if (!input.IsValid()) |
| 592 | { |
| 593 | return Fail("%s: Operation has invalid inputs", __func__); |
| 594 | } |
| 595 | |
| 596 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 597 | |
| 598 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 599 | if (rank > 4) |
| 600 | { |
| 601 | Fail("%s: Inputs with rank greater than 4 are not supported", __func__); |
| 602 | } |
| 603 | |
| 604 | // NOTE: Axis is an optional parameter to TRANSPOSE, therefore we do not want to generate a failure |
| 605 | // if the operand index is out of bounds. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 606 | const Operand* permOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 1, model, false); |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 607 | |
| 608 | std::vector<int32_t> perm(rank); |
| 609 | if (!permOperand) |
| 610 | { |
| 611 | // NOTE: If perm is not given, it is set to (n-1...0), where n is the rank of the tensor |
| 612 | for (unsigned int i = rank; i > 0; i--) |
| 613 | { |
| 614 | perm[rank - i] = boost::numeric_cast<int> (i - 1); |
| 615 | } |
| 616 | } |
| 617 | else |
| 618 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 619 | GetTensorInt32Values<hal_1_1::HalPolicy>(*permOperand, perm, model, data); |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | std::vector<uint32_t> outputDims(perm.begin(), perm.begin() + rank); |
| 623 | |
| 624 | auto permutationVector = armnn::PermutationVector(outputDims.data(), outputDims.size()); |
| 625 | if (!permutationVector.IsEqual(NHWCToArmNN) |
| 626 | && !permutationVector.IsEqual(ArmNNToNHWC) |
| 627 | && !permutationVector.IsEqual({ 3, 2, 0, 1 })) |
| 628 | { |
| 629 | return Fail("%s: Only [0, 3, 1, 2], [0, 2, 3, 1] and [3, 2, 0, 1] permutations are supported.", __func__); |
| 630 | } |
| 631 | |
| 632 | armnn::PermuteDescriptor permuteDesc; |
| 633 | permuteDesc.m_DimMappings = permutationVector; |
| 634 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 635 | const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model); |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 636 | if (!output) |
| 637 | { |
| 638 | return Fail("%s: Could not read output 0", __func__); |
| 639 | } |
| 640 | |
| 641 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 642 | |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 643 | if (!IsLayerSupportedForAnyBackend(__func__, |
| 644 | armnn::IsPermuteSupported, |
| 645 | data.m_Backends, |
| 646 | inputInfo, |
| 647 | outputInfo, |
| 648 | permuteDesc)) |
saoste01 | fe46315 | 2018-10-18 17:49:56 +0100 | [diff] [blame] | 649 | { |
| 650 | return false; |
| 651 | } |
| 652 | |
| 653 | armnn::IConnectableLayer* const layer = data.m_Network->AddPermuteLayer(permuteDesc); |
| 654 | assert(layer != nullptr); |
| 655 | input.Connect(layer->GetInputSlot(0)); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 656 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 657 | return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *layer, model, data); |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 658 | } |
| 659 | |
Éanna Ó Catháin | 2cd99b9 | 2018-11-14 14:33:52 +0000 | [diff] [blame] | 660 | bool HalPolicy::ConvertBatchToSpaceNd(const Operation& operation, const Model& model, ConversionData& data) |
| 661 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 662 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data); |
Éanna Ó Catháin | 2cd99b9 | 2018-11-14 14:33:52 +0000 | [diff] [blame] | 663 | if (!input.IsValid()) |
| 664 | { |
| 665 | return Fail("%s: Operation has invalid inputs", __func__); |
| 666 | } |
| 667 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 668 | const Operand* blockOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 1, model); |
Éanna Ó Catháin | 2cd99b9 | 2018-11-14 14:33:52 +0000 | [diff] [blame] | 669 | if (!blockOperand) |
| 670 | { |
| 671 | return Fail("%s: Could not read input 1", __func__); |
| 672 | } |
| 673 | |
| 674 | // Convert the block operand to int32 |
| 675 | std::vector<int32_t> block; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 676 | if (!GetTensorInt32Values<hal_1_1::HalPolicy>(*blockOperand, block, model, data)) |
Éanna Ó Catháin | 2cd99b9 | 2018-11-14 14:33:52 +0000 | [diff] [blame] | 677 | { |
| 678 | return Fail("%s: Input 1 has invalid values", __func__); |
| 679 | } |
| 680 | |
| 681 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 682 | |
| 683 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 684 | if (rank != 4) |
| 685 | { |
| 686 | Fail("%s: Only inputs with rank equal to 4 are supported", __func__); |
| 687 | } |
| 688 | |
| 689 | if (std::any_of(block.cbegin(), block.cend(), [](int32_t i){ return i < 1; })) |
| 690 | { |
| 691 | return Fail("%s: Block sizes for each spatial dimension of the input tensor must be" |
| 692 | " greater than or equal to 1", __func__); |
| 693 | } |
| 694 | |
| 695 | armnn::BatchToSpaceNdDescriptor batchToSpaceNdDesc; |
| 696 | batchToSpaceNdDesc.m_BlockShape.assign(block.cbegin(), block.cend()); |
| 697 | batchToSpaceNdDesc.m_DataLayout = armnn::DataLayout::NHWC; |
| 698 | |
| 699 | // Setting crops to 0,0 0,0 as it is not supported in Android NN API |
| 700 | batchToSpaceNdDesc.m_Crops = {{0, 0}, {0, 0}}; |
| 701 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 702 | const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model); |
Éanna Ó Catháin | 2cd99b9 | 2018-11-14 14:33:52 +0000 | [diff] [blame] | 703 | if (!output) |
| 704 | { |
| 705 | return Fail("%s: Could not read output 0", __func__); |
| 706 | } |
| 707 | |
| 708 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 709 | |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 710 | if (!IsLayerSupportedForAnyBackend(__func__, |
| 711 | armnn::IsBatchToSpaceNdSupported, |
| 712 | data.m_Backends, |
| 713 | inputInfo, |
| 714 | outputInfo, |
| 715 | batchToSpaceNdDesc)) |
Éanna Ó Catháin | 2cd99b9 | 2018-11-14 14:33:52 +0000 | [diff] [blame] | 716 | { |
| 717 | return false; |
| 718 | } |
| 719 | |
| 720 | armnn::IConnectableLayer* const layer = data.m_Network->AddBatchToSpaceNdLayer(batchToSpaceNdDesc); |
| 721 | assert(layer != nullptr); |
| 722 | input.Connect(layer->GetInputSlot(0)); |
| 723 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame^] | 724 | return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *layer, model, data); |
Éanna Ó Catháin | 2cd99b9 | 2018-11-14 14:33:52 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 728 | } // namespace hal_1_1 |
Matteo Martincigh | 265d1ad | 2019-01-08 18:14:53 +0000 | [diff] [blame] | 729 | } // namespace armnn_driver |