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