telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | 93e4898 | 2018-09-05 13:05:09 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #define LOG_TAG "ArmnnDriver" |
| 7 | |
| 8 | #include "Utils.hpp" |
Jim Flynn | f2e175c | 2019-12-12 15:11:30 +0000 | [diff] [blame] | 9 | #include "Half.hpp" |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 10 | |
Matteo Martincigh | 00d6ed1 | 2019-11-28 17:13:24 +0000 | [diff] [blame] | 11 | #include <armnnUtils/Permute.hpp> |
| 12 | |
Derek Lamberti | d00ad91 | 2020-01-22 15:55:16 +0000 | [diff] [blame] | 13 | #include <armnn/Utils.hpp> |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 14 | #include <armnn/utility/Assert.hpp> |
Derek Lamberti | d00ad91 | 2020-01-22 15:55:16 +0000 | [diff] [blame] | 15 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 16 | #include <cassert> |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 17 | #include <cerrno> |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 18 | #include <cinttypes> |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 19 | #include <sstream> |
| 20 | #include <cstdio> |
| 21 | #include <time.h> |
| 22 | |
| 23 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace android; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 26 | using namespace android::hardware; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 27 | using namespace android::hidl::memory::V1_0; |
| 28 | |
| 29 | namespace armnn_driver |
| 30 | { |
| 31 | const armnn::PermutationVector g_DontPermute{}; |
| 32 | |
| 33 | namespace |
| 34 | { |
| 35 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 36 | void SwizzleAndroidNn4dTensorToArmNn(const armnn::TensorShape& inTensorShape, const void* input, |
Matteo Martincigh | bf19d2a | 2019-11-29 11:46:50 +0000 | [diff] [blame] | 37 | void* output, size_t dataTypeSize, const armnn::PermutationVector& mappings) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 38 | { |
Matteo Martincigh | bf19d2a | 2019-11-29 11:46:50 +0000 | [diff] [blame] | 39 | assert(inTensorShape.GetNumDimensions() == 4U); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 40 | |
Matteo Martincigh | bf19d2a | 2019-11-29 11:46:50 +0000 | [diff] [blame] | 41 | armnnUtils::Permute(armnnUtils::Permuted(inTensorShape, mappings), mappings, input, output, dataTypeSize); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | } // anonymous namespace |
| 45 | |
| 46 | void SwizzleAndroidNn4dTensorToArmNn(const armnn::TensorInfo& tensor, const void* input, void* output, |
| 47 | const armnn::PermutationVector& mappings) |
| 48 | { |
| 49 | assert(tensor.GetNumDimensions() == 4U); |
| 50 | |
Matteo Martincigh | bf19d2a | 2019-11-29 11:46:50 +0000 | [diff] [blame] | 51 | armnn::DataType dataType = tensor.GetDataType(); |
| 52 | switch (dataType) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 53 | { |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 54 | case armnn::DataType::Float16: |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 55 | case armnn::DataType::Float32: |
Derek Lamberti | 1a38cda | 2020-01-10 17:28:20 +0000 | [diff] [blame] | 56 | case armnn::DataType::QAsymmU8: |
Derek Lamberti | d00ad91 | 2020-01-22 15:55:16 +0000 | [diff] [blame] | 57 | case armnn::DataType::QSymmS8: |
Sadik Armagan | 1153d1e | 2020-04-01 15:09:39 +0100 | [diff] [blame] | 58 | case armnn::DataType::QAsymmS8: |
Matteo Martincigh | bf19d2a | 2019-11-29 11:46:50 +0000 | [diff] [blame] | 59 | SwizzleAndroidNn4dTensorToArmNn(tensor.GetShape(), input, output, armnn::GetDataTypeSize(dataType), mappings); |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 60 | break; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 61 | default: |
| 62 | ALOGW("Unknown armnn::DataType for swizzling"); |
| 63 | assert(0); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void* GetMemoryFromPool(DataLocation location, const std::vector<android::nn::RunTimePoolInfo>& memPools) |
| 68 | { |
| 69 | // find the location within the pool |
| 70 | assert(location.poolIndex < memPools.size()); |
| 71 | |
surmeh01 | deb3bdb | 2018-07-05 12:06:04 +0100 | [diff] [blame] | 72 | const android::nn::RunTimePoolInfo& memPool = memPools[location.poolIndex]; |
| 73 | |
surmeh01 | deb3bdb | 2018-07-05 12:06:04 +0100 | [diff] [blame] | 74 | uint8_t* memPoolBuffer = memPool.getBuffer(); |
surmeh01 | deb3bdb | 2018-07-05 12:06:04 +0100 | [diff] [blame] | 75 | |
| 76 | uint8_t* memory = memPoolBuffer + location.offset; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 77 | |
| 78 | return memory; |
| 79 | } |
| 80 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 81 | armnn::TensorInfo GetTensorInfoForOperand(const V1_0::Operand& operand) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 82 | { |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 83 | using namespace armnn; |
| 84 | DataType type; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 85 | |
| 86 | switch (operand.type) |
| 87 | { |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 88 | case V1_0::OperandType::TENSOR_FLOAT32: |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 89 | type = armnn::DataType::Float32; |
| 90 | break; |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 91 | case V1_0::OperandType::TENSOR_QUANT8_ASYMM: |
Derek Lamberti | 1a38cda | 2020-01-10 17:28:20 +0000 | [diff] [blame] | 92 | type = armnn::DataType::QAsymmU8; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 93 | break; |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 94 | case V1_0::OperandType::TENSOR_INT32: |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 95 | type = armnn::DataType::Signed32; |
| 96 | break; |
| 97 | default: |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 98 | throw UnsupportedOperand<V1_0::OperandType>(operand.type); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 101 | TensorInfo ret; |
| 102 | if (operand.dimensions.size() == 0) |
| 103 | { |
| 104 | TensorShape tensorShape(Dimensionality::NotSpecified); |
| 105 | ret = TensorInfo(tensorShape, type); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | bool dimensionsSpecificity[5] = { true, true, true, true, true }; |
| 110 | int count = 0; |
| 111 | std::for_each(operand.dimensions.data(), |
| 112 | operand.dimensions.data() + operand.dimensions.size(), |
| 113 | [&](const unsigned int val) |
| 114 | { |
| 115 | if (val == 0) |
| 116 | { |
| 117 | dimensionsSpecificity[count] = false; |
| 118 | } |
| 119 | count++; |
| 120 | }); |
| 121 | |
| 122 | TensorShape tensorShape(operand.dimensions.size(), operand.dimensions.data(), dimensionsSpecificity); |
| 123 | ret = TensorInfo(tensorShape, type); |
| 124 | } |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 125 | |
| 126 | ret.SetQuantizationScale(operand.scale); |
| 127 | ret.SetQuantizationOffset(operand.zeroPoint); |
| 128 | |
| 129 | return ret; |
| 130 | } |
| 131 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 132 | #if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3)// Using ::android::hardware::neuralnetworks::V1_2 |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 133 | |
| 134 | armnn::TensorInfo GetTensorInfoForOperand(const V1_2::Operand& operand) |
| 135 | { |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 136 | using namespace armnn; |
Derek Lamberti | d00ad91 | 2020-01-22 15:55:16 +0000 | [diff] [blame] | 137 | bool perChannel = false; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 138 | |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 139 | DataType type; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 140 | switch (operand.type) |
| 141 | { |
Sadik Armagan | 793a70c | 2020-03-19 13:54:04 +0000 | [diff] [blame] | 142 | case V1_2::OperandType::TENSOR_BOOL8: |
| 143 | type = armnn::DataType::Boolean; |
| 144 | break; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 145 | case V1_2::OperandType::TENSOR_FLOAT32: |
| 146 | type = armnn::DataType::Float32; |
| 147 | break; |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 148 | case V1_2::OperandType::TENSOR_FLOAT16: |
| 149 | type = armnn::DataType::Float16; |
| 150 | break; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 151 | case V1_2::OperandType::TENSOR_QUANT8_ASYMM: |
Derek Lamberti | 1a38cda | 2020-01-10 17:28:20 +0000 | [diff] [blame] | 152 | type = armnn::DataType::QAsymmU8; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 153 | break; |
Derek Lamberti | d00ad91 | 2020-01-22 15:55:16 +0000 | [diff] [blame] | 154 | case V1_2::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: |
| 155 | perChannel=true; |
| 156 | ARMNN_FALLTHROUGH; |
Mike Kelly | 0e2e31b | 2019-11-19 09:16:00 +0000 | [diff] [blame] | 157 | case V1_2::OperandType::TENSOR_QUANT8_SYMM: |
FinnWilliamsArm | 624fe9f | 2019-12-06 17:12:42 +0000 | [diff] [blame] | 158 | type = armnn::DataType::QSymmS8; |
Mike Kelly | 0e2e31b | 2019-11-19 09:16:00 +0000 | [diff] [blame] | 159 | break; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 160 | case V1_2::OperandType::TENSOR_QUANT16_SYMM: |
Derek Lamberti | 1a38cda | 2020-01-10 17:28:20 +0000 | [diff] [blame] | 161 | type = armnn::DataType::QSymmS16; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 162 | break; |
| 163 | case V1_2::OperandType::TENSOR_INT32: |
| 164 | type = armnn::DataType::Signed32; |
| 165 | break; |
| 166 | default: |
| 167 | throw UnsupportedOperand<V1_2::OperandType>(operand.type); |
| 168 | } |
| 169 | |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 170 | TensorInfo ret; |
| 171 | if (operand.dimensions.size() == 0) |
| 172 | { |
| 173 | TensorShape tensorShape(Dimensionality::NotSpecified); |
| 174 | ret = TensorInfo(tensorShape, type); |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | bool dimensionsSpecificity[5] = { true, true, true, true, true }; |
| 179 | int count = 0; |
| 180 | std::for_each(operand.dimensions.data(), |
| 181 | operand.dimensions.data() + operand.dimensions.size(), |
| 182 | [&](const unsigned int val) |
| 183 | { |
| 184 | if (val == 0) |
| 185 | { |
| 186 | dimensionsSpecificity[count] = false; |
| 187 | } |
| 188 | count++; |
| 189 | }); |
| 190 | |
| 191 | TensorShape tensorShape(operand.dimensions.size(), operand.dimensions.data(), dimensionsSpecificity); |
| 192 | ret = TensorInfo(tensorShape, type); |
| 193 | } |
| 194 | |
Derek Lamberti | d00ad91 | 2020-01-22 15:55:16 +0000 | [diff] [blame] | 195 | if (perChannel) |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 196 | { |
| 197 | // ExtraParams is expected to be of type channelQuant |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 198 | ARMNN_ASSERT(operand.extraParams.getDiscriminator() == |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 199 | V1_2::Operand::ExtraParams::hidl_discriminator::channelQuant); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 200 | |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 201 | auto perAxisQuantParams = operand.extraParams.channelQuant(); |
| 202 | |
| 203 | ret.SetQuantizationScales(perAxisQuantParams.scales); |
| 204 | ret.SetQuantizationDim(MakeOptional<unsigned int>(perAxisQuantParams.channelDim)); |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | ret.SetQuantizationScale(operand.scale); |
| 209 | ret.SetQuantizationOffset(operand.zeroPoint); |
| 210 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 211 | |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | #endif |
| 216 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 217 | #ifdef ARMNN_ANDROID_NN_V1_3 // Using ::android::hardware::neuralnetworks::V1_3 |
| 218 | |
| 219 | armnn::TensorInfo GetTensorInfoForOperand(const V1_3::Operand& operand) |
| 220 | { |
| 221 | using namespace armnn; |
| 222 | bool perChannel = false; |
Teresa Charlin | 896572b | 2020-07-15 12:37:51 +0100 | [diff] [blame] | 223 | bool isScalar = false; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 224 | |
| 225 | DataType type; |
| 226 | switch (operand.type) |
| 227 | { |
Sadik Armagan | 51ba2c6 | 2020-03-31 15:36:25 +0100 | [diff] [blame] | 228 | case V1_3::OperandType::TENSOR_BOOL8: |
| 229 | type = armnn::DataType::Boolean; |
| 230 | break; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 231 | case V1_3::OperandType::TENSOR_FLOAT32: |
| 232 | type = armnn::DataType::Float32; |
| 233 | break; |
| 234 | case V1_3::OperandType::TENSOR_FLOAT16: |
| 235 | type = armnn::DataType::Float16; |
| 236 | break; |
| 237 | case V1_3::OperandType::TENSOR_QUANT8_ASYMM: |
| 238 | type = armnn::DataType::QAsymmU8; |
| 239 | break; |
| 240 | case V1_3::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: |
| 241 | perChannel=true; |
| 242 | ARMNN_FALLTHROUGH; |
| 243 | case V1_3::OperandType::TENSOR_QUANT8_SYMM: |
| 244 | type = armnn::DataType::QSymmS8; |
| 245 | break; |
| 246 | case V1_3::OperandType::TENSOR_QUANT16_SYMM: |
| 247 | type = armnn::DataType::QSymmS16; |
| 248 | break; |
| 249 | case V1_3::OperandType::TENSOR_INT32: |
| 250 | type = armnn::DataType::Signed32; |
| 251 | break; |
Finn Williams | fc884b4 | 2020-06-11 17:35:44 +0100 | [diff] [blame] | 252 | case V1_3::OperandType::INT32: |
| 253 | type = armnn::DataType::Signed32; |
Teresa Charlin | 896572b | 2020-07-15 12:37:51 +0100 | [diff] [blame] | 254 | isScalar = true; |
Finn Williams | fc884b4 | 2020-06-11 17:35:44 +0100 | [diff] [blame] | 255 | break; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 256 | case V1_3::OperandType::TENSOR_QUANT8_ASYMM_SIGNED: |
| 257 | type = armnn::DataType::QAsymmS8; |
| 258 | break; |
| 259 | default: |
| 260 | throw UnsupportedOperand<V1_3::OperandType>(operand.type); |
| 261 | } |
| 262 | |
Finn Williams | fc884b4 | 2020-06-11 17:35:44 +0100 | [diff] [blame] | 263 | TensorInfo ret; |
Teresa Charlin | 896572b | 2020-07-15 12:37:51 +0100 | [diff] [blame] | 264 | if (isScalar) |
Finn Williams | fc884b4 | 2020-06-11 17:35:44 +0100 | [diff] [blame] | 265 | { |
Teresa Charlin | 896572b | 2020-07-15 12:37:51 +0100 | [diff] [blame] | 266 | ret = TensorInfo(TensorShape(armnn::Dimensionality::Scalar), type); |
Finn Williams | fc884b4 | 2020-06-11 17:35:44 +0100 | [diff] [blame] | 267 | } |
| 268 | else |
| 269 | { |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 270 | if (operand.dimensions.size() == 0) |
| 271 | { |
| 272 | TensorShape tensorShape(Dimensionality::NotSpecified); |
| 273 | ret = TensorInfo(tensorShape, type); |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | bool dimensionsSpecificity[5] = { true, true, true, true, true }; |
| 278 | int count = 0; |
| 279 | std::for_each(operand.dimensions.data(), |
| 280 | operand.dimensions.data() + operand.dimensions.size(), |
| 281 | [&](const unsigned int val) |
| 282 | { |
| 283 | if (val == 0) |
| 284 | { |
| 285 | dimensionsSpecificity[count] = false; |
| 286 | } |
| 287 | count++; |
| 288 | }); |
| 289 | |
| 290 | TensorShape tensorShape(operand.dimensions.size(), operand.dimensions.data(), dimensionsSpecificity); |
| 291 | ret = TensorInfo(tensorShape, type); |
| 292 | } |
Finn Williams | fc884b4 | 2020-06-11 17:35:44 +0100 | [diff] [blame] | 293 | } |
| 294 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 295 | if (perChannel) |
| 296 | { |
| 297 | // ExtraParams is expected to be of type channelQuant |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 298 | ARMNN_ASSERT(operand.extraParams.getDiscriminator() == |
Kevin May | 352d838 | 2020-03-31 15:03:42 +0100 | [diff] [blame] | 299 | V1_2::Operand::ExtraParams::hidl_discriminator::channelQuant); |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 300 | |
| 301 | auto perAxisQuantParams = operand.extraParams.channelQuant(); |
| 302 | |
| 303 | ret.SetQuantizationScales(perAxisQuantParams.scales); |
| 304 | ret.SetQuantizationDim(MakeOptional<unsigned int>(perAxisQuantParams.channelDim)); |
| 305 | } |
| 306 | else |
| 307 | { |
| 308 | ret.SetQuantizationScale(operand.scale); |
| 309 | ret.SetQuantizationOffset(operand.zeroPoint); |
| 310 | } |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | #endif |
| 315 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 316 | std::string GetOperandSummary(const V1_0::Operand& operand) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 317 | { |
| 318 | return android::hardware::details::arrayToString(operand.dimensions, operand.dimensions.size()) + " " + |
| 319 | toString(operand.type); |
| 320 | } |
| 321 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 322 | #if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3) // Using ::android::hardware::neuralnetworks::V1_2 |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 323 | |
| 324 | std::string GetOperandSummary(const V1_2::Operand& operand) |
| 325 | { |
| 326 | return android::hardware::details::arrayToString(operand.dimensions, operand.dimensions.size()) + " " + |
| 327 | toString(operand.type); |
| 328 | } |
| 329 | |
| 330 | #endif |
| 331 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 332 | #ifdef ARMNN_ANDROID_NN_V1_3 // Using ::android::hardware::neuralnetworks::V1_3 |
| 333 | |
| 334 | std::string GetOperandSummary(const V1_3::Operand& operand) |
| 335 | { |
| 336 | return android::hardware::details::arrayToString(operand.dimensions, operand.dimensions.size()) + " " + |
| 337 | toString(operand.type); |
| 338 | } |
| 339 | |
| 340 | #endif |
| 341 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 342 | using DumpElementFunction = void (*)(const armnn::ConstTensor& tensor, |
| 343 | unsigned int elementIndex, |
| 344 | std::ofstream& fileStream); |
| 345 | |
| 346 | namespace |
| 347 | { |
| 348 | template <typename ElementType, typename PrintableType = ElementType> |
| 349 | void DumpTensorElement(const armnn::ConstTensor& tensor, unsigned int elementIndex, std::ofstream& fileStream) |
| 350 | { |
| 351 | const ElementType* elements = reinterpret_cast<const ElementType*>(tensor.GetMemoryArea()); |
| 352 | fileStream << static_cast<PrintableType>(elements[elementIndex]) << ","; |
| 353 | } |
| 354 | |
| 355 | constexpr const char* MemoryLayoutString(const armnn::ConstTensor& tensor) |
| 356 | { |
| 357 | const char* str = ""; |
| 358 | |
| 359 | switch (tensor.GetNumDimensions()) |
| 360 | { |
| 361 | case 4: { str = "(BHWC) "; break; } |
| 362 | case 3: { str = "(HWC) "; break; } |
| 363 | case 2: { str = "(HW) "; break; } |
| 364 | default: { str = ""; break; } |
| 365 | } |
| 366 | |
| 367 | return str; |
| 368 | } |
| 369 | } // namespace |
| 370 | |
| 371 | void DumpTensor(const std::string& dumpDir, |
| 372 | const std::string& requestName, |
| 373 | const std::string& tensorName, |
| 374 | const armnn::ConstTensor& tensor) |
| 375 | { |
| 376 | // The dump directory must exist in advance. |
| 377 | const std::string fileName = boost::str(boost::format("%1%/%2%_%3%.dump") % dumpDir % requestName % tensorName); |
| 378 | |
| 379 | std::ofstream fileStream; |
| 380 | fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc); |
| 381 | |
| 382 | if (!fileStream.good()) |
| 383 | { |
| 384 | ALOGW("Could not open file %s for writing", fileName.c_str()); |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | DumpElementFunction dumpElementFunction = nullptr; |
| 389 | |
| 390 | switch (tensor.GetDataType()) |
| 391 | { |
| 392 | case armnn::DataType::Float32: |
| 393 | { |
| 394 | dumpElementFunction = &DumpTensorElement<float>; |
| 395 | break; |
| 396 | } |
Derek Lamberti | 1a38cda | 2020-01-10 17:28:20 +0000 | [diff] [blame] | 397 | case armnn::DataType::QAsymmU8: |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 398 | { |
| 399 | dumpElementFunction = &DumpTensorElement<uint8_t, uint32_t>; |
| 400 | break; |
| 401 | } |
| 402 | case armnn::DataType::Signed32: |
| 403 | { |
| 404 | dumpElementFunction = &DumpTensorElement<int32_t>; |
| 405 | break; |
| 406 | } |
Jim Flynn | f2e175c | 2019-12-12 15:11:30 +0000 | [diff] [blame] | 407 | case armnn::DataType::Float16: |
| 408 | { |
| 409 | dumpElementFunction = &DumpTensorElement<armnn::Half>; |
| 410 | break; |
| 411 | } |
Teresa Charlin | b248ec1 | 2020-04-30 11:06:34 +0100 | [diff] [blame] | 412 | case armnn::DataType::QAsymmS8: |
| 413 | { |
| 414 | dumpElementFunction = &DumpTensorElement<int8_t, int32_t>; |
| 415 | break; |
| 416 | } |
| 417 | case armnn::DataType::Boolean: |
| 418 | { |
| 419 | dumpElementFunction = &DumpTensorElement<bool>; |
| 420 | break; |
| 421 | } |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 422 | default: |
| 423 | { |
| 424 | dumpElementFunction = nullptr; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | if (dumpElementFunction != nullptr) |
| 429 | { |
| 430 | const unsigned int numDimensions = tensor.GetNumDimensions(); |
| 431 | |
| 432 | const unsigned int batch = (numDimensions == 4) ? tensor.GetShape()[numDimensions - 4] : 1; |
| 433 | |
| 434 | const unsigned int height = (numDimensions >= 3) |
| 435 | ? tensor.GetShape()[numDimensions - 3] |
| 436 | : (numDimensions >= 2) ? tensor.GetShape()[numDimensions - 2] : 1; |
| 437 | |
| 438 | const unsigned int width = (numDimensions >= 3) |
| 439 | ? tensor.GetShape()[numDimensions - 2] |
| 440 | : (numDimensions >= 1) ? tensor.GetShape()[numDimensions - 1] : 0; |
| 441 | |
| 442 | const unsigned int channels = (numDimensions >= 3) ? tensor.GetShape()[numDimensions - 1] : 1; |
| 443 | |
| 444 | fileStream << "# Number of elements " << tensor.GetNumElements() << std::endl; |
| 445 | fileStream << "# Dimensions " << MemoryLayoutString(tensor); |
| 446 | fileStream << "[" << tensor.GetShape()[0]; |
| 447 | for (unsigned int d = 1; d < numDimensions; d++) |
| 448 | { |
| 449 | fileStream << "," << tensor.GetShape()[d]; |
| 450 | } |
| 451 | fileStream << "]" << std::endl; |
| 452 | |
| 453 | for (unsigned int e = 0, b = 0; b < batch; ++b) |
| 454 | { |
| 455 | if (numDimensions >= 4) |
| 456 | { |
| 457 | fileStream << "# Batch " << b << std::endl; |
| 458 | } |
| 459 | for (unsigned int c = 0; c < channels; c++) |
| 460 | { |
| 461 | if (numDimensions >= 3) |
| 462 | { |
| 463 | fileStream << "# Channel " << c << std::endl; |
| 464 | } |
| 465 | for (unsigned int h = 0; h < height; h++) |
| 466 | { |
| 467 | for (unsigned int w = 0; w < width; w++, e += channels) |
| 468 | { |
| 469 | (*dumpElementFunction)(tensor, e, fileStream); |
| 470 | } |
| 471 | fileStream << std::endl; |
| 472 | } |
| 473 | e -= channels - 1; |
| 474 | if (c < channels) |
| 475 | { |
| 476 | e -= ((height * width) - 1) * channels; |
| 477 | } |
| 478 | } |
| 479 | fileStream << std::endl; |
| 480 | } |
| 481 | fileStream << std::endl; |
| 482 | } |
| 483 | else |
| 484 | { |
| 485 | fileStream << "Cannot dump tensor elements: Unsupported data type " |
| 486 | << static_cast<unsigned int>(tensor.GetDataType()) << std::endl; |
| 487 | } |
| 488 | |
| 489 | if (!fileStream.good()) |
| 490 | { |
| 491 | ALOGW("An error occurred when writing to file %s", fileName.c_str()); |
| 492 | } |
| 493 | } |
| 494 | |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 495 | void DumpJsonProfilingIfRequired(bool gpuProfilingEnabled, |
| 496 | const std::string& dumpDir, |
| 497 | armnn::NetworkId networkId, |
| 498 | const armnn::IProfiler* profiler) |
| 499 | { |
| 500 | // Check if profiling is required. |
| 501 | if (!gpuProfilingEnabled) |
| 502 | { |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | // The dump directory must exist in advance. |
| 507 | if (dumpDir.empty()) |
| 508 | { |
| 509 | return; |
| 510 | } |
| 511 | |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 512 | ARMNN_ASSERT(profiler); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 513 | |
| 514 | // Set the name of the output profiling file. |
| 515 | const std::string fileName = boost::str(boost::format("%1%/%2%_%3%.json") |
| 516 | % dumpDir |
| 517 | % std::to_string(networkId) |
| 518 | % "profiling"); |
| 519 | |
| 520 | // Open the ouput file for writing. |
| 521 | std::ofstream fileStream; |
| 522 | fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc); |
| 523 | |
| 524 | if (!fileStream.good()) |
| 525 | { |
| 526 | ALOGW("Could not open file %s for writing", fileName.c_str()); |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | // Write the profiling info to a JSON file. |
| 531 | profiler->Print(fileStream); |
| 532 | } |
| 533 | |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 534 | std::string ExportNetworkGraphToDotFile(const armnn::IOptimizedNetwork& optimizedNetwork, |
| 535 | const std::string& dumpDir) |
| 536 | { |
| 537 | std::string fileName; |
| 538 | // The dump directory must exist in advance. |
| 539 | if (dumpDir.empty()) |
| 540 | { |
| 541 | return fileName; |
| 542 | } |
| 543 | |
| 544 | std::string timestamp = GetFileTimestamp(); |
| 545 | if (timestamp.empty()) |
| 546 | { |
| 547 | return fileName; |
| 548 | } |
| 549 | |
| 550 | // Set the name of the output .dot file. |
| 551 | fileName = boost::str(boost::format("%1%/%2%_networkgraph.dot") |
| 552 | % dumpDir |
| 553 | % timestamp); |
| 554 | |
| 555 | ALOGV("Exporting the optimized network graph to file: %s", fileName.c_str()); |
| 556 | |
| 557 | // Write the network graph to a dot file. |
| 558 | std::ofstream fileStream; |
| 559 | fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc); |
| 560 | |
| 561 | if (!fileStream.good()) |
| 562 | { |
| 563 | ALOGW("Could not open file %s for writing", fileName.c_str()); |
| 564 | return fileName; |
| 565 | } |
| 566 | |
| 567 | if (optimizedNetwork.SerializeToDot(fileStream) != armnn::Status::Success) |
| 568 | { |
| 569 | ALOGW("An error occurred when writing to file %s", fileName.c_str()); |
| 570 | } |
| 571 | return fileName; |
| 572 | } |
| 573 | |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 574 | bool IsDynamicTensor(const armnn::TensorInfo& tensorInfo) |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 575 | { |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 576 | if (tensorInfo.GetShape().GetDimensionality() == armnn::Dimensionality::NotSpecified) |
| 577 | { |
| 578 | return true; |
| 579 | } |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 580 | // Account for the usage of the TensorShape empty constructor |
| 581 | if (tensorInfo.GetNumDimensions() == 0) |
| 582 | { |
| 583 | return true; |
| 584 | } |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 585 | return !tensorInfo.GetShape().AreAllDimensionsSpecified(); |
| 586 | } |
| 587 | |
| 588 | bool AreDynamicTensorsSupported() |
| 589 | { |
| 590 | #if defined(ARMNN_ANDROID_NN_V1_3) |
| 591 | return true; |
| 592 | #else |
| 593 | return false; |
| 594 | #endif |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 597 | std::string GetFileTimestamp() |
| 598 | { |
| 599 | // used to get a timestamp to name diagnostic files (the ArmNN serialized graph |
| 600 | // and getSupportedOperations.txt files) |
| 601 | timespec ts; |
| 602 | int iRet = clock_gettime(CLOCK_MONOTONIC_RAW, &ts); |
| 603 | std::stringstream ss; |
| 604 | if (iRet == 0) |
| 605 | { |
| 606 | ss << std::to_string(ts.tv_sec) << "_" << std::to_string(ts.tv_nsec); |
| 607 | } |
| 608 | else |
| 609 | { |
| 610 | ALOGW("clock_gettime failed with errno %s : %s", std::to_string(errno).c_str(), std::strerror(errno)); |
| 611 | } |
| 612 | return ss.str(); |
| 613 | } |
| 614 | |
| 615 | void RenameGraphDotFile(const std::string& oldName, const std::string& dumpDir, const armnn::NetworkId networkId) |
| 616 | { |
| 617 | if (dumpDir.empty()) |
| 618 | { |
| 619 | return; |
| 620 | } |
| 621 | if (oldName.empty()) |
| 622 | { |
| 623 | return; |
| 624 | } |
| 625 | const std::string newFileName = boost::str(boost::format("%1%/%2%_networkgraph.dot") |
| 626 | % dumpDir |
| 627 | % std::to_string(networkId)); |
| 628 | int iRet = rename(oldName.c_str(), newFileName.c_str()); |
| 629 | if (iRet != 0) |
| 630 | { |
| 631 | std::stringstream ss; |
| 632 | ss << "rename of [" << oldName << "] to [" << newFileName << "] failed with errno " << std::to_string(errno) |
| 633 | << " : " << std::strerror(errno); |
| 634 | ALOGW(ss.str().c_str()); |
| 635 | } |
| 636 | } |
| 637 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 638 | void CommitPools(std::vector<::android::nn::RunTimePoolInfo>& memPools) |
| 639 | { |
| 640 | if (memPools.empty()) |
| 641 | { |
| 642 | return; |
| 643 | } |
| 644 | // Commit output buffers. |
| 645 | // Note that we update *all* pools, even if they aren't actually used as outputs - |
| 646 | // this is simpler and is what the CpuExecutor does. |
| 647 | for (auto& pool : memPools) |
| 648 | { |
| 649 | // Type android::nn::RunTimePoolInfo has changed between Android P & Q and Android R, where |
| 650 | // update() has been removed and flush() added. |
| 651 | #if defined(ARMNN_ANDROID_R) // Use the new Android implementation. |
| 652 | pool.flush(); |
| 653 | #else |
| 654 | pool.update(); |
| 655 | #endif |
| 656 | } |
| 657 | } |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 658 | } // namespace armnn_driver |