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 | { |
| 83 | armnn::DataType type; |
| 84 | |
| 85 | switch (operand.type) |
| 86 | { |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 87 | case V1_0::OperandType::TENSOR_FLOAT32: |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 88 | type = armnn::DataType::Float32; |
| 89 | break; |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 90 | case V1_0::OperandType::TENSOR_QUANT8_ASYMM: |
Derek Lamberti | 1a38cda | 2020-01-10 17:28:20 +0000 | [diff] [blame] | 91 | type = armnn::DataType::QAsymmU8; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 92 | break; |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 93 | case V1_0::OperandType::TENSOR_INT32: |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 94 | type = armnn::DataType::Signed32; |
| 95 | break; |
| 96 | default: |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 97 | throw UnsupportedOperand<V1_0::OperandType>(operand.type); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | armnn::TensorInfo ret(operand.dimensions.size(), operand.dimensions.data(), type); |
| 101 | |
| 102 | ret.SetQuantizationScale(operand.scale); |
| 103 | ret.SetQuantizationOffset(operand.zeroPoint); |
| 104 | |
| 105 | return ret; |
| 106 | } |
| 107 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 108 | #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] | 109 | |
| 110 | armnn::TensorInfo GetTensorInfoForOperand(const V1_2::Operand& operand) |
| 111 | { |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 112 | using namespace armnn; |
Derek Lamberti | d00ad91 | 2020-01-22 15:55:16 +0000 | [diff] [blame] | 113 | bool perChannel = false; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 114 | |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 115 | DataType type; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 116 | switch (operand.type) |
| 117 | { |
Sadik Armagan | 793a70c | 2020-03-19 13:54:04 +0000 | [diff] [blame] | 118 | case V1_2::OperandType::TENSOR_BOOL8: |
| 119 | type = armnn::DataType::Boolean; |
| 120 | break; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 121 | case V1_2::OperandType::TENSOR_FLOAT32: |
| 122 | type = armnn::DataType::Float32; |
| 123 | break; |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 124 | case V1_2::OperandType::TENSOR_FLOAT16: |
| 125 | type = armnn::DataType::Float16; |
| 126 | break; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 127 | case V1_2::OperandType::TENSOR_QUANT8_ASYMM: |
Derek Lamberti | 1a38cda | 2020-01-10 17:28:20 +0000 | [diff] [blame] | 128 | type = armnn::DataType::QAsymmU8; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 129 | break; |
Derek Lamberti | d00ad91 | 2020-01-22 15:55:16 +0000 | [diff] [blame] | 130 | case V1_2::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: |
| 131 | perChannel=true; |
| 132 | ARMNN_FALLTHROUGH; |
Mike Kelly | 0e2e31b | 2019-11-19 09:16:00 +0000 | [diff] [blame] | 133 | case V1_2::OperandType::TENSOR_QUANT8_SYMM: |
FinnWilliamsArm | 624fe9f | 2019-12-06 17:12:42 +0000 | [diff] [blame] | 134 | type = armnn::DataType::QSymmS8; |
Mike Kelly | 0e2e31b | 2019-11-19 09:16:00 +0000 | [diff] [blame] | 135 | break; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 136 | case V1_2::OperandType::TENSOR_QUANT16_SYMM: |
Derek Lamberti | 1a38cda | 2020-01-10 17:28:20 +0000 | [diff] [blame] | 137 | type = armnn::DataType::QSymmS16; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 138 | break; |
| 139 | case V1_2::OperandType::TENSOR_INT32: |
| 140 | type = armnn::DataType::Signed32; |
| 141 | break; |
| 142 | default: |
| 143 | throw UnsupportedOperand<V1_2::OperandType>(operand.type); |
| 144 | } |
| 145 | |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 146 | TensorInfo ret(operand.dimensions.size(), operand.dimensions.data(), type); |
Derek Lamberti | d00ad91 | 2020-01-22 15:55:16 +0000 | [diff] [blame] | 147 | if (perChannel) |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 148 | { |
| 149 | // ExtraParams is expected to be of type channelQuant |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 150 | ARMNN_ASSERT(operand.extraParams.getDiscriminator() == |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 151 | V1_2::Operand::ExtraParams::hidl_discriminator::channelQuant); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 152 | |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 153 | auto perAxisQuantParams = operand.extraParams.channelQuant(); |
| 154 | |
| 155 | ret.SetQuantizationScales(perAxisQuantParams.scales); |
| 156 | ret.SetQuantizationDim(MakeOptional<unsigned int>(perAxisQuantParams.channelDim)); |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | ret.SetQuantizationScale(operand.scale); |
| 161 | ret.SetQuantizationOffset(operand.zeroPoint); |
| 162 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 163 | |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | #endif |
| 168 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 169 | #ifdef ARMNN_ANDROID_NN_V1_3 // Using ::android::hardware::neuralnetworks::V1_3 |
| 170 | |
| 171 | armnn::TensorInfo GetTensorInfoForOperand(const V1_3::Operand& operand) |
| 172 | { |
| 173 | using namespace armnn; |
| 174 | bool perChannel = false; |
| 175 | |
| 176 | DataType type; |
| 177 | switch (operand.type) |
| 178 | { |
Sadik Armagan | 51ba2c6 | 2020-03-31 15:36:25 +0100 | [diff] [blame] | 179 | case V1_3::OperandType::TENSOR_BOOL8: |
| 180 | type = armnn::DataType::Boolean; |
| 181 | break; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 182 | case V1_3::OperandType::TENSOR_FLOAT32: |
| 183 | type = armnn::DataType::Float32; |
| 184 | break; |
| 185 | case V1_3::OperandType::TENSOR_FLOAT16: |
| 186 | type = armnn::DataType::Float16; |
| 187 | break; |
| 188 | case V1_3::OperandType::TENSOR_QUANT8_ASYMM: |
| 189 | type = armnn::DataType::QAsymmU8; |
| 190 | break; |
| 191 | case V1_3::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: |
| 192 | perChannel=true; |
| 193 | ARMNN_FALLTHROUGH; |
| 194 | case V1_3::OperandType::TENSOR_QUANT8_SYMM: |
| 195 | type = armnn::DataType::QSymmS8; |
| 196 | break; |
| 197 | case V1_3::OperandType::TENSOR_QUANT16_SYMM: |
| 198 | type = armnn::DataType::QSymmS16; |
| 199 | break; |
| 200 | case V1_3::OperandType::TENSOR_INT32: |
| 201 | type = armnn::DataType::Signed32; |
| 202 | break; |
| 203 | case V1_3::OperandType::TENSOR_QUANT8_ASYMM_SIGNED: |
| 204 | type = armnn::DataType::QAsymmS8; |
| 205 | break; |
| 206 | default: |
| 207 | throw UnsupportedOperand<V1_3::OperandType>(operand.type); |
| 208 | } |
| 209 | |
| 210 | TensorInfo ret(operand.dimensions.size(), operand.dimensions.data(), type); |
| 211 | if (perChannel) |
| 212 | { |
| 213 | // ExtraParams is expected to be of type channelQuant |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 214 | ARMNN_ASSERT(operand.extraParams.getDiscriminator() == |
Kevin May | 352d838 | 2020-03-31 15:03:42 +0100 | [diff] [blame] | 215 | V1_2::Operand::ExtraParams::hidl_discriminator::channelQuant); |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 216 | |
| 217 | auto perAxisQuantParams = operand.extraParams.channelQuant(); |
| 218 | |
| 219 | ret.SetQuantizationScales(perAxisQuantParams.scales); |
| 220 | ret.SetQuantizationDim(MakeOptional<unsigned int>(perAxisQuantParams.channelDim)); |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | ret.SetQuantizationScale(operand.scale); |
| 225 | ret.SetQuantizationOffset(operand.zeroPoint); |
| 226 | } |
| 227 | |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | #endif |
| 232 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 233 | std::string GetOperandSummary(const V1_0::Operand& operand) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 234 | { |
| 235 | return android::hardware::details::arrayToString(operand.dimensions, operand.dimensions.size()) + " " + |
| 236 | toString(operand.type); |
| 237 | } |
| 238 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 239 | #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] | 240 | |
| 241 | std::string GetOperandSummary(const V1_2::Operand& operand) |
| 242 | { |
| 243 | return android::hardware::details::arrayToString(operand.dimensions, operand.dimensions.size()) + " " + |
| 244 | toString(operand.type); |
| 245 | } |
| 246 | |
| 247 | #endif |
| 248 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 249 | #ifdef ARMNN_ANDROID_NN_V1_3 // Using ::android::hardware::neuralnetworks::V1_3 |
| 250 | |
| 251 | std::string GetOperandSummary(const V1_3::Operand& operand) |
| 252 | { |
| 253 | return android::hardware::details::arrayToString(operand.dimensions, operand.dimensions.size()) + " " + |
| 254 | toString(operand.type); |
| 255 | } |
| 256 | |
| 257 | #endif |
| 258 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 259 | using DumpElementFunction = void (*)(const armnn::ConstTensor& tensor, |
| 260 | unsigned int elementIndex, |
| 261 | std::ofstream& fileStream); |
| 262 | |
| 263 | namespace |
| 264 | { |
| 265 | template <typename ElementType, typename PrintableType = ElementType> |
| 266 | void DumpTensorElement(const armnn::ConstTensor& tensor, unsigned int elementIndex, std::ofstream& fileStream) |
| 267 | { |
| 268 | const ElementType* elements = reinterpret_cast<const ElementType*>(tensor.GetMemoryArea()); |
| 269 | fileStream << static_cast<PrintableType>(elements[elementIndex]) << ","; |
| 270 | } |
| 271 | |
| 272 | constexpr const char* MemoryLayoutString(const armnn::ConstTensor& tensor) |
| 273 | { |
| 274 | const char* str = ""; |
| 275 | |
| 276 | switch (tensor.GetNumDimensions()) |
| 277 | { |
| 278 | case 4: { str = "(BHWC) "; break; } |
| 279 | case 3: { str = "(HWC) "; break; } |
| 280 | case 2: { str = "(HW) "; break; } |
| 281 | default: { str = ""; break; } |
| 282 | } |
| 283 | |
| 284 | return str; |
| 285 | } |
| 286 | } // namespace |
| 287 | |
| 288 | void DumpTensor(const std::string& dumpDir, |
| 289 | const std::string& requestName, |
| 290 | const std::string& tensorName, |
| 291 | const armnn::ConstTensor& tensor) |
| 292 | { |
| 293 | // The dump directory must exist in advance. |
| 294 | const std::string fileName = boost::str(boost::format("%1%/%2%_%3%.dump") % dumpDir % requestName % tensorName); |
| 295 | |
| 296 | std::ofstream fileStream; |
| 297 | fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc); |
| 298 | |
| 299 | if (!fileStream.good()) |
| 300 | { |
| 301 | ALOGW("Could not open file %s for writing", fileName.c_str()); |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | DumpElementFunction dumpElementFunction = nullptr; |
| 306 | |
| 307 | switch (tensor.GetDataType()) |
| 308 | { |
| 309 | case armnn::DataType::Float32: |
| 310 | { |
| 311 | dumpElementFunction = &DumpTensorElement<float>; |
| 312 | break; |
| 313 | } |
Derek Lamberti | 1a38cda | 2020-01-10 17:28:20 +0000 | [diff] [blame] | 314 | case armnn::DataType::QAsymmU8: |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 315 | { |
| 316 | dumpElementFunction = &DumpTensorElement<uint8_t, uint32_t>; |
| 317 | break; |
| 318 | } |
| 319 | case armnn::DataType::Signed32: |
| 320 | { |
| 321 | dumpElementFunction = &DumpTensorElement<int32_t>; |
| 322 | break; |
| 323 | } |
Jim Flynn | f2e175c | 2019-12-12 15:11:30 +0000 | [diff] [blame] | 324 | case armnn::DataType::Float16: |
| 325 | { |
| 326 | dumpElementFunction = &DumpTensorElement<armnn::Half>; |
| 327 | break; |
| 328 | } |
Teresa Charlin | b248ec1 | 2020-04-30 11:06:34 +0100 | [diff] [blame^] | 329 | case armnn::DataType::QAsymmS8: |
| 330 | { |
| 331 | dumpElementFunction = &DumpTensorElement<int8_t, int32_t>; |
| 332 | break; |
| 333 | } |
| 334 | case armnn::DataType::Boolean: |
| 335 | { |
| 336 | dumpElementFunction = &DumpTensorElement<bool>; |
| 337 | break; |
| 338 | } |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 339 | default: |
| 340 | { |
| 341 | dumpElementFunction = nullptr; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | if (dumpElementFunction != nullptr) |
| 346 | { |
| 347 | const unsigned int numDimensions = tensor.GetNumDimensions(); |
| 348 | |
| 349 | const unsigned int batch = (numDimensions == 4) ? tensor.GetShape()[numDimensions - 4] : 1; |
| 350 | |
| 351 | const unsigned int height = (numDimensions >= 3) |
| 352 | ? tensor.GetShape()[numDimensions - 3] |
| 353 | : (numDimensions >= 2) ? tensor.GetShape()[numDimensions - 2] : 1; |
| 354 | |
| 355 | const unsigned int width = (numDimensions >= 3) |
| 356 | ? tensor.GetShape()[numDimensions - 2] |
| 357 | : (numDimensions >= 1) ? tensor.GetShape()[numDimensions - 1] : 0; |
| 358 | |
| 359 | const unsigned int channels = (numDimensions >= 3) ? tensor.GetShape()[numDimensions - 1] : 1; |
| 360 | |
| 361 | fileStream << "# Number of elements " << tensor.GetNumElements() << std::endl; |
| 362 | fileStream << "# Dimensions " << MemoryLayoutString(tensor); |
| 363 | fileStream << "[" << tensor.GetShape()[0]; |
| 364 | for (unsigned int d = 1; d < numDimensions; d++) |
| 365 | { |
| 366 | fileStream << "," << tensor.GetShape()[d]; |
| 367 | } |
| 368 | fileStream << "]" << std::endl; |
| 369 | |
| 370 | for (unsigned int e = 0, b = 0; b < batch; ++b) |
| 371 | { |
| 372 | if (numDimensions >= 4) |
| 373 | { |
| 374 | fileStream << "# Batch " << b << std::endl; |
| 375 | } |
| 376 | for (unsigned int c = 0; c < channels; c++) |
| 377 | { |
| 378 | if (numDimensions >= 3) |
| 379 | { |
| 380 | fileStream << "# Channel " << c << std::endl; |
| 381 | } |
| 382 | for (unsigned int h = 0; h < height; h++) |
| 383 | { |
| 384 | for (unsigned int w = 0; w < width; w++, e += channels) |
| 385 | { |
| 386 | (*dumpElementFunction)(tensor, e, fileStream); |
| 387 | } |
| 388 | fileStream << std::endl; |
| 389 | } |
| 390 | e -= channels - 1; |
| 391 | if (c < channels) |
| 392 | { |
| 393 | e -= ((height * width) - 1) * channels; |
| 394 | } |
| 395 | } |
| 396 | fileStream << std::endl; |
| 397 | } |
| 398 | fileStream << std::endl; |
| 399 | } |
| 400 | else |
| 401 | { |
| 402 | fileStream << "Cannot dump tensor elements: Unsupported data type " |
| 403 | << static_cast<unsigned int>(tensor.GetDataType()) << std::endl; |
| 404 | } |
| 405 | |
| 406 | if (!fileStream.good()) |
| 407 | { |
| 408 | ALOGW("An error occurred when writing to file %s", fileName.c_str()); |
| 409 | } |
| 410 | } |
| 411 | |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 412 | void DumpJsonProfilingIfRequired(bool gpuProfilingEnabled, |
| 413 | const std::string& dumpDir, |
| 414 | armnn::NetworkId networkId, |
| 415 | const armnn::IProfiler* profiler) |
| 416 | { |
| 417 | // Check if profiling is required. |
| 418 | if (!gpuProfilingEnabled) |
| 419 | { |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | // The dump directory must exist in advance. |
| 424 | if (dumpDir.empty()) |
| 425 | { |
| 426 | return; |
| 427 | } |
| 428 | |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 429 | ARMNN_ASSERT(profiler); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 430 | |
| 431 | // Set the name of the output profiling file. |
| 432 | const std::string fileName = boost::str(boost::format("%1%/%2%_%3%.json") |
| 433 | % dumpDir |
| 434 | % std::to_string(networkId) |
| 435 | % "profiling"); |
| 436 | |
| 437 | // Open the ouput file for writing. |
| 438 | std::ofstream fileStream; |
| 439 | fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc); |
| 440 | |
| 441 | if (!fileStream.good()) |
| 442 | { |
| 443 | ALOGW("Could not open file %s for writing", fileName.c_str()); |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | // Write the profiling info to a JSON file. |
| 448 | profiler->Print(fileStream); |
| 449 | } |
| 450 | |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 451 | std::string ExportNetworkGraphToDotFile(const armnn::IOptimizedNetwork& optimizedNetwork, |
| 452 | const std::string& dumpDir) |
| 453 | { |
| 454 | std::string fileName; |
| 455 | // The dump directory must exist in advance. |
| 456 | if (dumpDir.empty()) |
| 457 | { |
| 458 | return fileName; |
| 459 | } |
| 460 | |
| 461 | std::string timestamp = GetFileTimestamp(); |
| 462 | if (timestamp.empty()) |
| 463 | { |
| 464 | return fileName; |
| 465 | } |
| 466 | |
| 467 | // Set the name of the output .dot file. |
| 468 | fileName = boost::str(boost::format("%1%/%2%_networkgraph.dot") |
| 469 | % dumpDir |
| 470 | % timestamp); |
| 471 | |
| 472 | ALOGV("Exporting the optimized network graph to file: %s", fileName.c_str()); |
| 473 | |
| 474 | // Write the network graph to a dot file. |
| 475 | std::ofstream fileStream; |
| 476 | fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc); |
| 477 | |
| 478 | if (!fileStream.good()) |
| 479 | { |
| 480 | ALOGW("Could not open file %s for writing", fileName.c_str()); |
| 481 | return fileName; |
| 482 | } |
| 483 | |
| 484 | if (optimizedNetwork.SerializeToDot(fileStream) != armnn::Status::Success) |
| 485 | { |
| 486 | ALOGW("An error occurred when writing to file %s", fileName.c_str()); |
| 487 | } |
| 488 | return fileName; |
| 489 | } |
| 490 | |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 491 | bool IsDynamicTensor(const armnn::TensorInfo& outputInfo) |
| 492 | { |
| 493 | // Dynamic tensors have at least one 0-sized dimension |
| 494 | return outputInfo.GetNumElements() == 0u; |
| 495 | } |
| 496 | |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 497 | std::string GetFileTimestamp() |
| 498 | { |
| 499 | // used to get a timestamp to name diagnostic files (the ArmNN serialized graph |
| 500 | // and getSupportedOperations.txt files) |
| 501 | timespec ts; |
| 502 | int iRet = clock_gettime(CLOCK_MONOTONIC_RAW, &ts); |
| 503 | std::stringstream ss; |
| 504 | if (iRet == 0) |
| 505 | { |
| 506 | ss << std::to_string(ts.tv_sec) << "_" << std::to_string(ts.tv_nsec); |
| 507 | } |
| 508 | else |
| 509 | { |
| 510 | ALOGW("clock_gettime failed with errno %s : %s", std::to_string(errno).c_str(), std::strerror(errno)); |
| 511 | } |
| 512 | return ss.str(); |
| 513 | } |
| 514 | |
| 515 | void RenameGraphDotFile(const std::string& oldName, const std::string& dumpDir, const armnn::NetworkId networkId) |
| 516 | { |
| 517 | if (dumpDir.empty()) |
| 518 | { |
| 519 | return; |
| 520 | } |
| 521 | if (oldName.empty()) |
| 522 | { |
| 523 | return; |
| 524 | } |
| 525 | const std::string newFileName = boost::str(boost::format("%1%/%2%_networkgraph.dot") |
| 526 | % dumpDir |
| 527 | % std::to_string(networkId)); |
| 528 | int iRet = rename(oldName.c_str(), newFileName.c_str()); |
| 529 | if (iRet != 0) |
| 530 | { |
| 531 | std::stringstream ss; |
| 532 | ss << "rename of [" << oldName << "] to [" << newFileName << "] failed with errno " << std::to_string(errno) |
| 533 | << " : " << std::strerror(errno); |
| 534 | ALOGW(ss.str().c_str()); |
| 535 | } |
| 536 | } |
| 537 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 538 | void CommitPools(std::vector<::android::nn::RunTimePoolInfo>& memPools) |
| 539 | { |
| 540 | if (memPools.empty()) |
| 541 | { |
| 542 | return; |
| 543 | } |
| 544 | // Commit output buffers. |
| 545 | // Note that we update *all* pools, even if they aren't actually used as outputs - |
| 546 | // this is simpler and is what the CpuExecutor does. |
| 547 | for (auto& pool : memPools) |
| 548 | { |
| 549 | // Type android::nn::RunTimePoolInfo has changed between Android P & Q and Android R, where |
| 550 | // update() has been removed and flush() added. |
| 551 | #if defined(ARMNN_ANDROID_R) // Use the new Android implementation. |
| 552 | pool.flush(); |
| 553 | #else |
| 554 | pool.update(); |
| 555 | #endif |
| 556 | } |
| 557 | } |
| 558 | |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 559 | |
| 560 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 561 | } // namespace armnn_driver |