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" |
| 9 | |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 10 | #include <Half.hpp> |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 11 | #include <Permute.hpp> |
| 12 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 13 | #include <cassert> |
| 14 | #include <cinttypes> |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace android; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 17 | using namespace android::hardware; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 18 | using namespace android::hidl::memory::V1_0; |
| 19 | |
| 20 | namespace armnn_driver |
| 21 | { |
| 22 | const armnn::PermutationVector g_DontPermute{}; |
| 23 | |
| 24 | namespace |
| 25 | { |
| 26 | |
| 27 | template <typename T> |
| 28 | void SwizzleAndroidNn4dTensorToArmNn(const armnn::TensorShape& inTensorShape, const void* input, |
| 29 | void* output, const armnn::PermutationVector& mappings) |
| 30 | { |
| 31 | const auto inputData = static_cast<const T*>(input); |
| 32 | const auto outputData = static_cast<T*>(output); |
| 33 | |
Matteo Martincigh | 2c444fc | 2019-01-07 10:18:47 +0000 | [diff] [blame] | 34 | armnnUtils::Permute(armnnUtils::Permuted(inTensorShape, mappings), mappings, inputData, outputData, sizeof(T)); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | } // anonymous namespace |
| 38 | |
| 39 | void SwizzleAndroidNn4dTensorToArmNn(const armnn::TensorInfo& tensor, const void* input, void* output, |
| 40 | const armnn::PermutationVector& mappings) |
| 41 | { |
| 42 | assert(tensor.GetNumDimensions() == 4U); |
| 43 | |
| 44 | switch(tensor.GetDataType()) |
| 45 | { |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 46 | case armnn::DataType::Float16: |
| 47 | SwizzleAndroidNn4dTensorToArmNn<armnn::Half>(tensor.GetShape(), input, output, mappings); |
| 48 | break; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 49 | case armnn::DataType::Float32: |
| 50 | SwizzleAndroidNn4dTensorToArmNn<float>(tensor.GetShape(), input, output, mappings); |
| 51 | break; |
| 52 | case armnn::DataType::QuantisedAsymm8: |
| 53 | SwizzleAndroidNn4dTensorToArmNn<uint8_t>(tensor.GetShape(), input, output, mappings); |
| 54 | break; |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 55 | case armnn::DataType::QuantizedSymm8PerAxis: |
| 56 | SwizzleAndroidNn4dTensorToArmNn<int8_t>(tensor.GetShape(), input, output, mappings); |
| 57 | break; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 58 | default: |
| 59 | ALOGW("Unknown armnn::DataType for swizzling"); |
| 60 | assert(0); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void* GetMemoryFromPool(DataLocation location, const std::vector<android::nn::RunTimePoolInfo>& memPools) |
| 65 | { |
| 66 | // find the location within the pool |
| 67 | assert(location.poolIndex < memPools.size()); |
| 68 | |
surmeh01 | deb3bdb | 2018-07-05 12:06:04 +0100 | [diff] [blame] | 69 | const android::nn::RunTimePoolInfo& memPool = memPools[location.poolIndex]; |
| 70 | |
| 71 | // Type android::nn::RunTimePoolInfo has changed between Android O and Android P, where |
| 72 | // "buffer" has been made private and must be accessed via the accessor method "getBuffer". |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 73 | #if defined(ARMNN_ANDROID_P) || defined(ARMNN_ANDROID_Q) // Use the new Android implementation. |
surmeh01 | deb3bdb | 2018-07-05 12:06:04 +0100 | [diff] [blame] | 74 | uint8_t* memPoolBuffer = memPool.getBuffer(); |
| 75 | #else // Fallback to the old Android O implementation. |
| 76 | uint8_t* memPoolBuffer = memPool.buffer; |
| 77 | #endif |
| 78 | |
| 79 | uint8_t* memory = memPoolBuffer + location.offset; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 80 | |
| 81 | return memory; |
| 82 | } |
| 83 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 84 | armnn::TensorInfo GetTensorInfoForOperand(const V1_0::Operand& operand) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 85 | { |
| 86 | armnn::DataType type; |
| 87 | |
| 88 | switch (operand.type) |
| 89 | { |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 90 | case V1_0::OperandType::TENSOR_FLOAT32: |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 91 | type = armnn::DataType::Float32; |
| 92 | break; |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 93 | case V1_0::OperandType::TENSOR_QUANT8_ASYMM: |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 94 | type = armnn::DataType::QuantisedAsymm8; |
| 95 | break; |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 96 | case V1_0::OperandType::TENSOR_INT32: |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 97 | type = armnn::DataType::Signed32; |
| 98 | break; |
| 99 | default: |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 100 | throw UnsupportedOperand<V1_0::OperandType>(operand.type); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | armnn::TensorInfo ret(operand.dimensions.size(), operand.dimensions.data(), type); |
| 104 | |
| 105 | ret.SetQuantizationScale(operand.scale); |
| 106 | ret.SetQuantizationOffset(operand.zeroPoint); |
| 107 | |
| 108 | return ret; |
| 109 | } |
| 110 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 111 | #ifdef ARMNN_ANDROID_NN_V1_2 // Using ::android::hardware::neuralnetworks::V1_2 |
| 112 | |
| 113 | armnn::TensorInfo GetTensorInfoForOperand(const V1_2::Operand& operand) |
| 114 | { |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 115 | using namespace armnn; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 116 | |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 117 | DataType type; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 118 | switch (operand.type) |
| 119 | { |
| 120 | case V1_2::OperandType::TENSOR_FLOAT32: |
| 121 | type = armnn::DataType::Float32; |
| 122 | break; |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 123 | case V1_2::OperandType::TENSOR_FLOAT16: |
| 124 | type = armnn::DataType::Float16; |
| 125 | break; |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 126 | case V1_2::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: |
| 127 | type = armnn::DataType::QuantizedSymm8PerAxis; |
| 128 | break; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 129 | case V1_2::OperandType::TENSOR_QUANT8_ASYMM: |
| 130 | type = armnn::DataType::QuantisedAsymm8; |
| 131 | break; |
Mike Kelly | 0e2e31b | 2019-11-19 09:16:00 +0000 | [diff] [blame^] | 132 | case V1_2::OperandType::TENSOR_QUANT8_SYMM: |
| 133 | type = armnn::DataType::QuantisedSymm8; |
| 134 | break; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 135 | case V1_2::OperandType::TENSOR_QUANT16_SYMM: |
| 136 | type = armnn::DataType::QuantisedSymm16; |
| 137 | break; |
| 138 | case V1_2::OperandType::TENSOR_INT32: |
| 139 | type = armnn::DataType::Signed32; |
| 140 | break; |
| 141 | default: |
| 142 | throw UnsupportedOperand<V1_2::OperandType>(operand.type); |
| 143 | } |
| 144 | |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 145 | TensorInfo ret(operand.dimensions.size(), operand.dimensions.data(), type); |
| 146 | if (type == DataType::QuantizedSymm8PerAxis) |
| 147 | { |
| 148 | // ExtraParams is expected to be of type channelQuant |
| 149 | BOOST_ASSERT(operand.extraParams.getDiscriminator() == |
| 150 | V1_2::Operand::ExtraParams::hidl_discriminator::channelQuant); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 151 | |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 152 | auto perAxisQuantParams = operand.extraParams.channelQuant(); |
| 153 | |
| 154 | ret.SetQuantizationScales(perAxisQuantParams.scales); |
| 155 | ret.SetQuantizationDim(MakeOptional<unsigned int>(perAxisQuantParams.channelDim)); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | ret.SetQuantizationScale(operand.scale); |
| 160 | ret.SetQuantizationOffset(operand.zeroPoint); |
| 161 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | #endif |
| 167 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 168 | std::string GetOperandSummary(const V1_0::Operand& operand) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 169 | { |
| 170 | return android::hardware::details::arrayToString(operand.dimensions, operand.dimensions.size()) + " " + |
| 171 | toString(operand.type); |
| 172 | } |
| 173 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 174 | #ifdef ARMNN_ANDROID_NN_V1_2 // Using ::android::hardware::neuralnetworks::V1_2 |
| 175 | |
| 176 | std::string GetOperandSummary(const V1_2::Operand& operand) |
| 177 | { |
| 178 | return android::hardware::details::arrayToString(operand.dimensions, operand.dimensions.size()) + " " + |
| 179 | toString(operand.type); |
| 180 | } |
| 181 | |
| 182 | #endif |
| 183 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 184 | using DumpElementFunction = void (*)(const armnn::ConstTensor& tensor, |
| 185 | unsigned int elementIndex, |
| 186 | std::ofstream& fileStream); |
| 187 | |
| 188 | namespace |
| 189 | { |
| 190 | template <typename ElementType, typename PrintableType = ElementType> |
| 191 | void DumpTensorElement(const armnn::ConstTensor& tensor, unsigned int elementIndex, std::ofstream& fileStream) |
| 192 | { |
| 193 | const ElementType* elements = reinterpret_cast<const ElementType*>(tensor.GetMemoryArea()); |
| 194 | fileStream << static_cast<PrintableType>(elements[elementIndex]) << ","; |
| 195 | } |
| 196 | |
| 197 | constexpr const char* MemoryLayoutString(const armnn::ConstTensor& tensor) |
| 198 | { |
| 199 | const char* str = ""; |
| 200 | |
| 201 | switch (tensor.GetNumDimensions()) |
| 202 | { |
| 203 | case 4: { str = "(BHWC) "; break; } |
| 204 | case 3: { str = "(HWC) "; break; } |
| 205 | case 2: { str = "(HW) "; break; } |
| 206 | default: { str = ""; break; } |
| 207 | } |
| 208 | |
| 209 | return str; |
| 210 | } |
| 211 | } // namespace |
| 212 | |
| 213 | void DumpTensor(const std::string& dumpDir, |
| 214 | const std::string& requestName, |
| 215 | const std::string& tensorName, |
| 216 | const armnn::ConstTensor& tensor) |
| 217 | { |
| 218 | // The dump directory must exist in advance. |
| 219 | const std::string fileName = boost::str(boost::format("%1%/%2%_%3%.dump") % dumpDir % requestName % tensorName); |
| 220 | |
| 221 | std::ofstream fileStream; |
| 222 | fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc); |
| 223 | |
| 224 | if (!fileStream.good()) |
| 225 | { |
| 226 | ALOGW("Could not open file %s for writing", fileName.c_str()); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | DumpElementFunction dumpElementFunction = nullptr; |
| 231 | |
| 232 | switch (tensor.GetDataType()) |
| 233 | { |
| 234 | case armnn::DataType::Float32: |
| 235 | { |
| 236 | dumpElementFunction = &DumpTensorElement<float>; |
| 237 | break; |
| 238 | } |
| 239 | case armnn::DataType::QuantisedAsymm8: |
| 240 | { |
| 241 | dumpElementFunction = &DumpTensorElement<uint8_t, uint32_t>; |
| 242 | break; |
| 243 | } |
| 244 | case armnn::DataType::Signed32: |
| 245 | { |
| 246 | dumpElementFunction = &DumpTensorElement<int32_t>; |
| 247 | break; |
| 248 | } |
| 249 | default: |
| 250 | { |
| 251 | dumpElementFunction = nullptr; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | if (dumpElementFunction != nullptr) |
| 256 | { |
| 257 | const unsigned int numDimensions = tensor.GetNumDimensions(); |
| 258 | |
| 259 | const unsigned int batch = (numDimensions == 4) ? tensor.GetShape()[numDimensions - 4] : 1; |
| 260 | |
| 261 | const unsigned int height = (numDimensions >= 3) |
| 262 | ? tensor.GetShape()[numDimensions - 3] |
| 263 | : (numDimensions >= 2) ? tensor.GetShape()[numDimensions - 2] : 1; |
| 264 | |
| 265 | const unsigned int width = (numDimensions >= 3) |
| 266 | ? tensor.GetShape()[numDimensions - 2] |
| 267 | : (numDimensions >= 1) ? tensor.GetShape()[numDimensions - 1] : 0; |
| 268 | |
| 269 | const unsigned int channels = (numDimensions >= 3) ? tensor.GetShape()[numDimensions - 1] : 1; |
| 270 | |
| 271 | fileStream << "# Number of elements " << tensor.GetNumElements() << std::endl; |
| 272 | fileStream << "# Dimensions " << MemoryLayoutString(tensor); |
| 273 | fileStream << "[" << tensor.GetShape()[0]; |
| 274 | for (unsigned int d = 1; d < numDimensions; d++) |
| 275 | { |
| 276 | fileStream << "," << tensor.GetShape()[d]; |
| 277 | } |
| 278 | fileStream << "]" << std::endl; |
| 279 | |
| 280 | for (unsigned int e = 0, b = 0; b < batch; ++b) |
| 281 | { |
| 282 | if (numDimensions >= 4) |
| 283 | { |
| 284 | fileStream << "# Batch " << b << std::endl; |
| 285 | } |
| 286 | for (unsigned int c = 0; c < channels; c++) |
| 287 | { |
| 288 | if (numDimensions >= 3) |
| 289 | { |
| 290 | fileStream << "# Channel " << c << std::endl; |
| 291 | } |
| 292 | for (unsigned int h = 0; h < height; h++) |
| 293 | { |
| 294 | for (unsigned int w = 0; w < width; w++, e += channels) |
| 295 | { |
| 296 | (*dumpElementFunction)(tensor, e, fileStream); |
| 297 | } |
| 298 | fileStream << std::endl; |
| 299 | } |
| 300 | e -= channels - 1; |
| 301 | if (c < channels) |
| 302 | { |
| 303 | e -= ((height * width) - 1) * channels; |
| 304 | } |
| 305 | } |
| 306 | fileStream << std::endl; |
| 307 | } |
| 308 | fileStream << std::endl; |
| 309 | } |
| 310 | else |
| 311 | { |
| 312 | fileStream << "Cannot dump tensor elements: Unsupported data type " |
| 313 | << static_cast<unsigned int>(tensor.GetDataType()) << std::endl; |
| 314 | } |
| 315 | |
| 316 | if (!fileStream.good()) |
| 317 | { |
| 318 | ALOGW("An error occurred when writing to file %s", fileName.c_str()); |
| 319 | } |
| 320 | } |
| 321 | |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 322 | void DumpJsonProfilingIfRequired(bool gpuProfilingEnabled, |
| 323 | const std::string& dumpDir, |
| 324 | armnn::NetworkId networkId, |
| 325 | const armnn::IProfiler* profiler) |
| 326 | { |
| 327 | // Check if profiling is required. |
| 328 | if (!gpuProfilingEnabled) |
| 329 | { |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | // The dump directory must exist in advance. |
| 334 | if (dumpDir.empty()) |
| 335 | { |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | BOOST_ASSERT(profiler); |
| 340 | |
| 341 | // Set the name of the output profiling file. |
| 342 | const std::string fileName = boost::str(boost::format("%1%/%2%_%3%.json") |
| 343 | % dumpDir |
| 344 | % std::to_string(networkId) |
| 345 | % "profiling"); |
| 346 | |
| 347 | // Open the ouput file for writing. |
| 348 | std::ofstream fileStream; |
| 349 | fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc); |
| 350 | |
| 351 | if (!fileStream.good()) |
| 352 | { |
| 353 | ALOGW("Could not open file %s for writing", fileName.c_str()); |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | // Write the profiling info to a JSON file. |
| 358 | profiler->Print(fileStream); |
| 359 | } |
| 360 | |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 361 | bool IsDynamicTensor(const armnn::TensorInfo& outputInfo) |
| 362 | { |
| 363 | // Dynamic tensors have at least one 0-sized dimension |
| 364 | return outputInfo.GetNumElements() == 0u; |
| 365 | } |
| 366 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 367 | } // namespace armnn_driver |