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; |
| 55 | default: |
| 56 | ALOGW("Unknown armnn::DataType for swizzling"); |
| 57 | assert(0); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void* GetMemoryFromPool(DataLocation location, const std::vector<android::nn::RunTimePoolInfo>& memPools) |
| 62 | { |
| 63 | // find the location within the pool |
| 64 | assert(location.poolIndex < memPools.size()); |
| 65 | |
surmeh01 | deb3bdb | 2018-07-05 12:06:04 +0100 | [diff] [blame] | 66 | const android::nn::RunTimePoolInfo& memPool = memPools[location.poolIndex]; |
| 67 | |
| 68 | // Type android::nn::RunTimePoolInfo has changed between Android O and Android P, where |
| 69 | // "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] | 70 | #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] | 71 | uint8_t* memPoolBuffer = memPool.getBuffer(); |
| 72 | #else // Fallback to the old Android O implementation. |
| 73 | uint8_t* memPoolBuffer = memPool.buffer; |
| 74 | #endif |
| 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: |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 91 | type = armnn::DataType::QuantisedAsymm8; |
| 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 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 108 | #ifdef ARMNN_ANDROID_NN_V1_2 // Using ::android::hardware::neuralnetworks::V1_2 |
| 109 | |
| 110 | armnn::TensorInfo GetTensorInfoForOperand(const V1_2::Operand& operand) |
| 111 | { |
| 112 | armnn::DataType type; |
| 113 | |
| 114 | switch (operand.type) |
| 115 | { |
| 116 | case V1_2::OperandType::TENSOR_FLOAT32: |
| 117 | type = armnn::DataType::Float32; |
| 118 | break; |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame^] | 119 | case V1_2::OperandType::TENSOR_FLOAT16: |
| 120 | type = armnn::DataType::Float16; |
| 121 | break; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 122 | case V1_2::OperandType::TENSOR_QUANT8_ASYMM: |
| 123 | type = armnn::DataType::QuantisedAsymm8; |
| 124 | break; |
| 125 | case V1_2::OperandType::TENSOR_QUANT16_SYMM: |
| 126 | type = armnn::DataType::QuantisedSymm16; |
| 127 | break; |
| 128 | case V1_2::OperandType::TENSOR_INT32: |
| 129 | type = armnn::DataType::Signed32; |
| 130 | break; |
| 131 | default: |
| 132 | throw UnsupportedOperand<V1_2::OperandType>(operand.type); |
| 133 | } |
| 134 | |
| 135 | armnn::TensorInfo ret(operand.dimensions.size(), operand.dimensions.data(), type); |
| 136 | |
| 137 | ret.SetQuantizationScale(operand.scale); |
| 138 | ret.SetQuantizationOffset(operand.zeroPoint); |
| 139 | |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | #endif |
| 144 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 145 | std::string GetOperandSummary(const V1_0::Operand& operand) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 146 | { |
| 147 | return android::hardware::details::arrayToString(operand.dimensions, operand.dimensions.size()) + " " + |
| 148 | toString(operand.type); |
| 149 | } |
| 150 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 151 | #ifdef ARMNN_ANDROID_NN_V1_2 // Using ::android::hardware::neuralnetworks::V1_2 |
| 152 | |
| 153 | std::string GetOperandSummary(const V1_2::Operand& operand) |
| 154 | { |
| 155 | return android::hardware::details::arrayToString(operand.dimensions, operand.dimensions.size()) + " " + |
| 156 | toString(operand.type); |
| 157 | } |
| 158 | |
| 159 | #endif |
| 160 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 161 | using DumpElementFunction = void (*)(const armnn::ConstTensor& tensor, |
| 162 | unsigned int elementIndex, |
| 163 | std::ofstream& fileStream); |
| 164 | |
| 165 | namespace |
| 166 | { |
| 167 | template <typename ElementType, typename PrintableType = ElementType> |
| 168 | void DumpTensorElement(const armnn::ConstTensor& tensor, unsigned int elementIndex, std::ofstream& fileStream) |
| 169 | { |
| 170 | const ElementType* elements = reinterpret_cast<const ElementType*>(tensor.GetMemoryArea()); |
| 171 | fileStream << static_cast<PrintableType>(elements[elementIndex]) << ","; |
| 172 | } |
| 173 | |
| 174 | constexpr const char* MemoryLayoutString(const armnn::ConstTensor& tensor) |
| 175 | { |
| 176 | const char* str = ""; |
| 177 | |
| 178 | switch (tensor.GetNumDimensions()) |
| 179 | { |
| 180 | case 4: { str = "(BHWC) "; break; } |
| 181 | case 3: { str = "(HWC) "; break; } |
| 182 | case 2: { str = "(HW) "; break; } |
| 183 | default: { str = ""; break; } |
| 184 | } |
| 185 | |
| 186 | return str; |
| 187 | } |
| 188 | } // namespace |
| 189 | |
| 190 | void DumpTensor(const std::string& dumpDir, |
| 191 | const std::string& requestName, |
| 192 | const std::string& tensorName, |
| 193 | const armnn::ConstTensor& tensor) |
| 194 | { |
| 195 | // The dump directory must exist in advance. |
| 196 | const std::string fileName = boost::str(boost::format("%1%/%2%_%3%.dump") % dumpDir % requestName % tensorName); |
| 197 | |
| 198 | std::ofstream fileStream; |
| 199 | fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc); |
| 200 | |
| 201 | if (!fileStream.good()) |
| 202 | { |
| 203 | ALOGW("Could not open file %s for writing", fileName.c_str()); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | DumpElementFunction dumpElementFunction = nullptr; |
| 208 | |
| 209 | switch (tensor.GetDataType()) |
| 210 | { |
| 211 | case armnn::DataType::Float32: |
| 212 | { |
| 213 | dumpElementFunction = &DumpTensorElement<float>; |
| 214 | break; |
| 215 | } |
| 216 | case armnn::DataType::QuantisedAsymm8: |
| 217 | { |
| 218 | dumpElementFunction = &DumpTensorElement<uint8_t, uint32_t>; |
| 219 | break; |
| 220 | } |
| 221 | case armnn::DataType::Signed32: |
| 222 | { |
| 223 | dumpElementFunction = &DumpTensorElement<int32_t>; |
| 224 | break; |
| 225 | } |
| 226 | default: |
| 227 | { |
| 228 | dumpElementFunction = nullptr; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if (dumpElementFunction != nullptr) |
| 233 | { |
| 234 | const unsigned int numDimensions = tensor.GetNumDimensions(); |
| 235 | |
| 236 | const unsigned int batch = (numDimensions == 4) ? tensor.GetShape()[numDimensions - 4] : 1; |
| 237 | |
| 238 | const unsigned int height = (numDimensions >= 3) |
| 239 | ? tensor.GetShape()[numDimensions - 3] |
| 240 | : (numDimensions >= 2) ? tensor.GetShape()[numDimensions - 2] : 1; |
| 241 | |
| 242 | const unsigned int width = (numDimensions >= 3) |
| 243 | ? tensor.GetShape()[numDimensions - 2] |
| 244 | : (numDimensions >= 1) ? tensor.GetShape()[numDimensions - 1] : 0; |
| 245 | |
| 246 | const unsigned int channels = (numDimensions >= 3) ? tensor.GetShape()[numDimensions - 1] : 1; |
| 247 | |
| 248 | fileStream << "# Number of elements " << tensor.GetNumElements() << std::endl; |
| 249 | fileStream << "# Dimensions " << MemoryLayoutString(tensor); |
| 250 | fileStream << "[" << tensor.GetShape()[0]; |
| 251 | for (unsigned int d = 1; d < numDimensions; d++) |
| 252 | { |
| 253 | fileStream << "," << tensor.GetShape()[d]; |
| 254 | } |
| 255 | fileStream << "]" << std::endl; |
| 256 | |
| 257 | for (unsigned int e = 0, b = 0; b < batch; ++b) |
| 258 | { |
| 259 | if (numDimensions >= 4) |
| 260 | { |
| 261 | fileStream << "# Batch " << b << std::endl; |
| 262 | } |
| 263 | for (unsigned int c = 0; c < channels; c++) |
| 264 | { |
| 265 | if (numDimensions >= 3) |
| 266 | { |
| 267 | fileStream << "# Channel " << c << std::endl; |
| 268 | } |
| 269 | for (unsigned int h = 0; h < height; h++) |
| 270 | { |
| 271 | for (unsigned int w = 0; w < width; w++, e += channels) |
| 272 | { |
| 273 | (*dumpElementFunction)(tensor, e, fileStream); |
| 274 | } |
| 275 | fileStream << std::endl; |
| 276 | } |
| 277 | e -= channels - 1; |
| 278 | if (c < channels) |
| 279 | { |
| 280 | e -= ((height * width) - 1) * channels; |
| 281 | } |
| 282 | } |
| 283 | fileStream << std::endl; |
| 284 | } |
| 285 | fileStream << std::endl; |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | fileStream << "Cannot dump tensor elements: Unsupported data type " |
| 290 | << static_cast<unsigned int>(tensor.GetDataType()) << std::endl; |
| 291 | } |
| 292 | |
| 293 | if (!fileStream.good()) |
| 294 | { |
| 295 | ALOGW("An error occurred when writing to file %s", fileName.c_str()); |
| 296 | } |
| 297 | } |
| 298 | |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 299 | void DumpJsonProfilingIfRequired(bool gpuProfilingEnabled, |
| 300 | const std::string& dumpDir, |
| 301 | armnn::NetworkId networkId, |
| 302 | const armnn::IProfiler* profiler) |
| 303 | { |
| 304 | // Check if profiling is required. |
| 305 | if (!gpuProfilingEnabled) |
| 306 | { |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | // The dump directory must exist in advance. |
| 311 | if (dumpDir.empty()) |
| 312 | { |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | BOOST_ASSERT(profiler); |
| 317 | |
| 318 | // Set the name of the output profiling file. |
| 319 | const std::string fileName = boost::str(boost::format("%1%/%2%_%3%.json") |
| 320 | % dumpDir |
| 321 | % std::to_string(networkId) |
| 322 | % "profiling"); |
| 323 | |
| 324 | // Open the ouput file for writing. |
| 325 | std::ofstream fileStream; |
| 326 | fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc); |
| 327 | |
| 328 | if (!fileStream.good()) |
| 329 | { |
| 330 | ALOGW("Could not open file %s for writing", fileName.c_str()); |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | // Write the profiling info to a JSON file. |
| 335 | profiler->Print(fileStream); |
| 336 | } |
| 337 | |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 338 | bool IsDynamicTensor(const armnn::TensorInfo& outputInfo) |
| 339 | { |
| 340 | // Dynamic tensors have at least one 0-sized dimension |
| 341 | return outputInfo.GetNumElements() == 0u; |
| 342 | } |
| 343 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 344 | } // namespace armnn_driver |