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