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> |
| 14 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 15 | #include <cassert> |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 16 | #include <cerrno> |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 17 | #include <cinttypes> |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 18 | #include <sstream> |
| 19 | #include <cstdio> |
| 20 | #include <time.h> |
| 21 | |
| 22 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace android; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 25 | using namespace android::hardware; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 26 | using namespace android::hidl::memory::V1_0; |
| 27 | |
| 28 | namespace armnn_driver |
| 29 | { |
| 30 | const armnn::PermutationVector g_DontPermute{}; |
| 31 | |
| 32 | namespace |
| 33 | { |
| 34 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 35 | void SwizzleAndroidNn4dTensorToArmNn(const armnn::TensorShape& inTensorShape, const void* input, |
Matteo Martincigh | bf19d2a | 2019-11-29 11:46:50 +0000 | [diff] [blame] | 36 | void* output, size_t dataTypeSize, const armnn::PermutationVector& mappings) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 37 | { |
Matteo Martincigh | bf19d2a | 2019-11-29 11:46:50 +0000 | [diff] [blame] | 38 | assert(inTensorShape.GetNumDimensions() == 4U); |
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 | armnnUtils::Permute(armnnUtils::Permuted(inTensorShape, mappings), mappings, input, output, dataTypeSize); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | } // anonymous namespace |
| 44 | |
| 45 | void SwizzleAndroidNn4dTensorToArmNn(const armnn::TensorInfo& tensor, const void* input, void* output, |
| 46 | const armnn::PermutationVector& mappings) |
| 47 | { |
| 48 | assert(tensor.GetNumDimensions() == 4U); |
| 49 | |
Matteo Martincigh | bf19d2a | 2019-11-29 11:46:50 +0000 | [diff] [blame] | 50 | armnn::DataType dataType = tensor.GetDataType(); |
| 51 | switch (dataType) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 52 | { |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 53 | case armnn::DataType::Float16: |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 54 | case armnn::DataType::Float32: |
Derek Lamberti | 1a38cda | 2020-01-10 17:28:20 +0000 | [diff] [blame] | 55 | case armnn::DataType::QAsymmU8: |
Derek Lamberti | d00ad91 | 2020-01-22 15:55:16 +0000 | [diff] [blame] | 56 | case armnn::DataType::QSymmS8: |
Matteo Martincigh | bf19d2a | 2019-11-29 11:46:50 +0000 | [diff] [blame] | 57 | SwizzleAndroidNn4dTensorToArmNn(tensor.GetShape(), input, output, armnn::GetDataTypeSize(dataType), mappings); |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 58 | break; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 59 | default: |
| 60 | ALOGW("Unknown armnn::DataType for swizzling"); |
| 61 | assert(0); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void* GetMemoryFromPool(DataLocation location, const std::vector<android::nn::RunTimePoolInfo>& memPools) |
| 66 | { |
| 67 | // find the location within the pool |
| 68 | assert(location.poolIndex < memPools.size()); |
| 69 | |
surmeh01 | deb3bdb | 2018-07-05 12:06:04 +0100 | [diff] [blame] | 70 | const android::nn::RunTimePoolInfo& memPool = memPools[location.poolIndex]; |
| 71 | |
surmeh01 | deb3bdb | 2018-07-05 12:06:04 +0100 | [diff] [blame] | 72 | uint8_t* memPoolBuffer = memPool.getBuffer(); |
surmeh01 | deb3bdb | 2018-07-05 12:06:04 +0100 | [diff] [blame] | 73 | |
| 74 | uint8_t* memory = memPoolBuffer + location.offset; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 75 | |
| 76 | return memory; |
| 77 | } |
| 78 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 79 | armnn::TensorInfo GetTensorInfoForOperand(const V1_0::Operand& operand) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 80 | { |
| 81 | armnn::DataType type; |
| 82 | |
| 83 | switch (operand.type) |
| 84 | { |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 85 | case V1_0::OperandType::TENSOR_FLOAT32: |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 86 | type = armnn::DataType::Float32; |
| 87 | break; |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 88 | case V1_0::OperandType::TENSOR_QUANT8_ASYMM: |
Derek Lamberti | 1a38cda | 2020-01-10 17:28:20 +0000 | [diff] [blame] | 89 | type = armnn::DataType::QAsymmU8; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 90 | break; |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 91 | case V1_0::OperandType::TENSOR_INT32: |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 92 | type = armnn::DataType::Signed32; |
| 93 | break; |
| 94 | default: |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 95 | throw UnsupportedOperand<V1_0::OperandType>(operand.type); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | armnn::TensorInfo ret(operand.dimensions.size(), operand.dimensions.data(), type); |
| 99 | |
| 100 | ret.SetQuantizationScale(operand.scale); |
| 101 | ret.SetQuantizationOffset(operand.zeroPoint); |
| 102 | |
| 103 | return ret; |
| 104 | } |
| 105 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 106 | #ifdef ARMNN_ANDROID_NN_V1_2 // Using ::android::hardware::neuralnetworks::V1_2 |
| 107 | |
| 108 | armnn::TensorInfo GetTensorInfoForOperand(const V1_2::Operand& operand) |
| 109 | { |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 110 | using namespace armnn; |
Derek Lamberti | d00ad91 | 2020-01-22 15:55:16 +0000 | [diff] [blame] | 111 | bool perChannel = false; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 112 | |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 113 | DataType type; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 114 | switch (operand.type) |
| 115 | { |
Sadik Armagan | 793a70c | 2020-03-19 13:54:04 +0000 | [diff] [blame^] | 116 | case V1_2::OperandType::TENSOR_BOOL8: |
| 117 | type = armnn::DataType::Boolean; |
| 118 | break; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 119 | case V1_2::OperandType::TENSOR_FLOAT32: |
| 120 | type = armnn::DataType::Float32; |
| 121 | break; |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 122 | case V1_2::OperandType::TENSOR_FLOAT16: |
| 123 | type = armnn::DataType::Float16; |
| 124 | break; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 125 | case V1_2::OperandType::TENSOR_QUANT8_ASYMM: |
Derek Lamberti | 1a38cda | 2020-01-10 17:28:20 +0000 | [diff] [blame] | 126 | type = armnn::DataType::QAsymmU8; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 127 | break; |
Derek Lamberti | d00ad91 | 2020-01-22 15:55:16 +0000 | [diff] [blame] | 128 | case V1_2::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: |
| 129 | perChannel=true; |
| 130 | ARMNN_FALLTHROUGH; |
Mike Kelly | 0e2e31b | 2019-11-19 09:16:00 +0000 | [diff] [blame] | 131 | case V1_2::OperandType::TENSOR_QUANT8_SYMM: |
FinnWilliamsArm | 624fe9f | 2019-12-06 17:12:42 +0000 | [diff] [blame] | 132 | type = armnn::DataType::QSymmS8; |
Mike Kelly | 0e2e31b | 2019-11-19 09:16:00 +0000 | [diff] [blame] | 133 | break; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 134 | case V1_2::OperandType::TENSOR_QUANT16_SYMM: |
Derek Lamberti | 1a38cda | 2020-01-10 17:28:20 +0000 | [diff] [blame] | 135 | type = armnn::DataType::QSymmS16; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 136 | break; |
| 137 | case V1_2::OperandType::TENSOR_INT32: |
| 138 | type = armnn::DataType::Signed32; |
| 139 | break; |
| 140 | default: |
| 141 | throw UnsupportedOperand<V1_2::OperandType>(operand.type); |
| 142 | } |
| 143 | |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 144 | TensorInfo ret(operand.dimensions.size(), operand.dimensions.data(), type); |
Derek Lamberti | d00ad91 | 2020-01-22 15:55:16 +0000 | [diff] [blame] | 145 | if (perChannel) |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 146 | { |
| 147 | // ExtraParams is expected to be of type channelQuant |
| 148 | BOOST_ASSERT(operand.extraParams.getDiscriminator() == |
| 149 | V1_2::Operand::ExtraParams::hidl_discriminator::channelQuant); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 150 | |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 151 | auto perAxisQuantParams = operand.extraParams.channelQuant(); |
| 152 | |
| 153 | ret.SetQuantizationScales(perAxisQuantParams.scales); |
| 154 | ret.SetQuantizationDim(MakeOptional<unsigned int>(perAxisQuantParams.channelDim)); |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | ret.SetQuantizationScale(operand.scale); |
| 159 | ret.SetQuantizationOffset(operand.zeroPoint); |
| 160 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 161 | |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | #endif |
| 166 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 167 | std::string GetOperandSummary(const V1_0::Operand& operand) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 168 | { |
| 169 | return android::hardware::details::arrayToString(operand.dimensions, operand.dimensions.size()) + " " + |
| 170 | toString(operand.type); |
| 171 | } |
| 172 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 173 | #ifdef ARMNN_ANDROID_NN_V1_2 // Using ::android::hardware::neuralnetworks::V1_2 |
| 174 | |
| 175 | std::string GetOperandSummary(const V1_2::Operand& operand) |
| 176 | { |
| 177 | return android::hardware::details::arrayToString(operand.dimensions, operand.dimensions.size()) + " " + |
| 178 | toString(operand.type); |
| 179 | } |
| 180 | |
| 181 | #endif |
| 182 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 183 | using DumpElementFunction = void (*)(const armnn::ConstTensor& tensor, |
| 184 | unsigned int elementIndex, |
| 185 | std::ofstream& fileStream); |
| 186 | |
| 187 | namespace |
| 188 | { |
| 189 | template <typename ElementType, typename PrintableType = ElementType> |
| 190 | void DumpTensorElement(const armnn::ConstTensor& tensor, unsigned int elementIndex, std::ofstream& fileStream) |
| 191 | { |
| 192 | const ElementType* elements = reinterpret_cast<const ElementType*>(tensor.GetMemoryArea()); |
| 193 | fileStream << static_cast<PrintableType>(elements[elementIndex]) << ","; |
| 194 | } |
| 195 | |
| 196 | constexpr const char* MemoryLayoutString(const armnn::ConstTensor& tensor) |
| 197 | { |
| 198 | const char* str = ""; |
| 199 | |
| 200 | switch (tensor.GetNumDimensions()) |
| 201 | { |
| 202 | case 4: { str = "(BHWC) "; break; } |
| 203 | case 3: { str = "(HWC) "; break; } |
| 204 | case 2: { str = "(HW) "; break; } |
| 205 | default: { str = ""; break; } |
| 206 | } |
| 207 | |
| 208 | return str; |
| 209 | } |
| 210 | } // namespace |
| 211 | |
| 212 | void DumpTensor(const std::string& dumpDir, |
| 213 | const std::string& requestName, |
| 214 | const std::string& tensorName, |
| 215 | const armnn::ConstTensor& tensor) |
| 216 | { |
| 217 | // The dump directory must exist in advance. |
| 218 | const std::string fileName = boost::str(boost::format("%1%/%2%_%3%.dump") % dumpDir % requestName % tensorName); |
| 219 | |
| 220 | std::ofstream fileStream; |
| 221 | fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc); |
| 222 | |
| 223 | if (!fileStream.good()) |
| 224 | { |
| 225 | ALOGW("Could not open file %s for writing", fileName.c_str()); |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | DumpElementFunction dumpElementFunction = nullptr; |
| 230 | |
| 231 | switch (tensor.GetDataType()) |
| 232 | { |
| 233 | case armnn::DataType::Float32: |
| 234 | { |
| 235 | dumpElementFunction = &DumpTensorElement<float>; |
| 236 | break; |
| 237 | } |
Derek Lamberti | 1a38cda | 2020-01-10 17:28:20 +0000 | [diff] [blame] | 238 | case armnn::DataType::QAsymmU8: |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 239 | { |
| 240 | dumpElementFunction = &DumpTensorElement<uint8_t, uint32_t>; |
| 241 | break; |
| 242 | } |
| 243 | case armnn::DataType::Signed32: |
| 244 | { |
| 245 | dumpElementFunction = &DumpTensorElement<int32_t>; |
| 246 | break; |
| 247 | } |
Jim Flynn | f2e175c | 2019-12-12 15:11:30 +0000 | [diff] [blame] | 248 | case armnn::DataType::Float16: |
| 249 | { |
| 250 | dumpElementFunction = &DumpTensorElement<armnn::Half>; |
| 251 | break; |
| 252 | } |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 253 | default: |
| 254 | { |
| 255 | dumpElementFunction = nullptr; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | if (dumpElementFunction != nullptr) |
| 260 | { |
| 261 | const unsigned int numDimensions = tensor.GetNumDimensions(); |
| 262 | |
| 263 | const unsigned int batch = (numDimensions == 4) ? tensor.GetShape()[numDimensions - 4] : 1; |
| 264 | |
| 265 | const unsigned int height = (numDimensions >= 3) |
| 266 | ? tensor.GetShape()[numDimensions - 3] |
| 267 | : (numDimensions >= 2) ? tensor.GetShape()[numDimensions - 2] : 1; |
| 268 | |
| 269 | const unsigned int width = (numDimensions >= 3) |
| 270 | ? tensor.GetShape()[numDimensions - 2] |
| 271 | : (numDimensions >= 1) ? tensor.GetShape()[numDimensions - 1] : 0; |
| 272 | |
| 273 | const unsigned int channels = (numDimensions >= 3) ? tensor.GetShape()[numDimensions - 1] : 1; |
| 274 | |
| 275 | fileStream << "# Number of elements " << tensor.GetNumElements() << std::endl; |
| 276 | fileStream << "# Dimensions " << MemoryLayoutString(tensor); |
| 277 | fileStream << "[" << tensor.GetShape()[0]; |
| 278 | for (unsigned int d = 1; d < numDimensions; d++) |
| 279 | { |
| 280 | fileStream << "," << tensor.GetShape()[d]; |
| 281 | } |
| 282 | fileStream << "]" << std::endl; |
| 283 | |
| 284 | for (unsigned int e = 0, b = 0; b < batch; ++b) |
| 285 | { |
| 286 | if (numDimensions >= 4) |
| 287 | { |
| 288 | fileStream << "# Batch " << b << std::endl; |
| 289 | } |
| 290 | for (unsigned int c = 0; c < channels; c++) |
| 291 | { |
| 292 | if (numDimensions >= 3) |
| 293 | { |
| 294 | fileStream << "# Channel " << c << std::endl; |
| 295 | } |
| 296 | for (unsigned int h = 0; h < height; h++) |
| 297 | { |
| 298 | for (unsigned int w = 0; w < width; w++, e += channels) |
| 299 | { |
| 300 | (*dumpElementFunction)(tensor, e, fileStream); |
| 301 | } |
| 302 | fileStream << std::endl; |
| 303 | } |
| 304 | e -= channels - 1; |
| 305 | if (c < channels) |
| 306 | { |
| 307 | e -= ((height * width) - 1) * channels; |
| 308 | } |
| 309 | } |
| 310 | fileStream << std::endl; |
| 311 | } |
| 312 | fileStream << std::endl; |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | fileStream << "Cannot dump tensor elements: Unsupported data type " |
| 317 | << static_cast<unsigned int>(tensor.GetDataType()) << std::endl; |
| 318 | } |
| 319 | |
| 320 | if (!fileStream.good()) |
| 321 | { |
| 322 | ALOGW("An error occurred when writing to file %s", fileName.c_str()); |
| 323 | } |
| 324 | } |
| 325 | |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 326 | void DumpJsonProfilingIfRequired(bool gpuProfilingEnabled, |
| 327 | const std::string& dumpDir, |
| 328 | armnn::NetworkId networkId, |
| 329 | const armnn::IProfiler* profiler) |
| 330 | { |
| 331 | // Check if profiling is required. |
| 332 | if (!gpuProfilingEnabled) |
| 333 | { |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | // The dump directory must exist in advance. |
| 338 | if (dumpDir.empty()) |
| 339 | { |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | BOOST_ASSERT(profiler); |
| 344 | |
| 345 | // Set the name of the output profiling file. |
| 346 | const std::string fileName = boost::str(boost::format("%1%/%2%_%3%.json") |
| 347 | % dumpDir |
| 348 | % std::to_string(networkId) |
| 349 | % "profiling"); |
| 350 | |
| 351 | // Open the ouput file for writing. |
| 352 | std::ofstream fileStream; |
| 353 | fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc); |
| 354 | |
| 355 | if (!fileStream.good()) |
| 356 | { |
| 357 | ALOGW("Could not open file %s for writing", fileName.c_str()); |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | // Write the profiling info to a JSON file. |
| 362 | profiler->Print(fileStream); |
| 363 | } |
| 364 | |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 365 | std::string ExportNetworkGraphToDotFile(const armnn::IOptimizedNetwork& optimizedNetwork, |
| 366 | const std::string& dumpDir) |
| 367 | { |
| 368 | std::string fileName; |
| 369 | // The dump directory must exist in advance. |
| 370 | if (dumpDir.empty()) |
| 371 | { |
| 372 | return fileName; |
| 373 | } |
| 374 | |
| 375 | std::string timestamp = GetFileTimestamp(); |
| 376 | if (timestamp.empty()) |
| 377 | { |
| 378 | return fileName; |
| 379 | } |
| 380 | |
| 381 | // Set the name of the output .dot file. |
| 382 | fileName = boost::str(boost::format("%1%/%2%_networkgraph.dot") |
| 383 | % dumpDir |
| 384 | % timestamp); |
| 385 | |
| 386 | ALOGV("Exporting the optimized network graph to file: %s", fileName.c_str()); |
| 387 | |
| 388 | // Write the network graph to a dot file. |
| 389 | std::ofstream fileStream; |
| 390 | fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc); |
| 391 | |
| 392 | if (!fileStream.good()) |
| 393 | { |
| 394 | ALOGW("Could not open file %s for writing", fileName.c_str()); |
| 395 | return fileName; |
| 396 | } |
| 397 | |
| 398 | if (optimizedNetwork.SerializeToDot(fileStream) != armnn::Status::Success) |
| 399 | { |
| 400 | ALOGW("An error occurred when writing to file %s", fileName.c_str()); |
| 401 | } |
| 402 | return fileName; |
| 403 | } |
| 404 | |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 405 | bool IsDynamicTensor(const armnn::TensorInfo& outputInfo) |
| 406 | { |
| 407 | // Dynamic tensors have at least one 0-sized dimension |
| 408 | return outputInfo.GetNumElements() == 0u; |
| 409 | } |
| 410 | |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 411 | std::string GetFileTimestamp() |
| 412 | { |
| 413 | // used to get a timestamp to name diagnostic files (the ArmNN serialized graph |
| 414 | // and getSupportedOperations.txt files) |
| 415 | timespec ts; |
| 416 | int iRet = clock_gettime(CLOCK_MONOTONIC_RAW, &ts); |
| 417 | std::stringstream ss; |
| 418 | if (iRet == 0) |
| 419 | { |
| 420 | ss << std::to_string(ts.tv_sec) << "_" << std::to_string(ts.tv_nsec); |
| 421 | } |
| 422 | else |
| 423 | { |
| 424 | ALOGW("clock_gettime failed with errno %s : %s", std::to_string(errno).c_str(), std::strerror(errno)); |
| 425 | } |
| 426 | return ss.str(); |
| 427 | } |
| 428 | |
| 429 | void RenameGraphDotFile(const std::string& oldName, const std::string& dumpDir, const armnn::NetworkId networkId) |
| 430 | { |
| 431 | if (dumpDir.empty()) |
| 432 | { |
| 433 | return; |
| 434 | } |
| 435 | if (oldName.empty()) |
| 436 | { |
| 437 | return; |
| 438 | } |
| 439 | const std::string newFileName = boost::str(boost::format("%1%/%2%_networkgraph.dot") |
| 440 | % dumpDir |
| 441 | % std::to_string(networkId)); |
| 442 | int iRet = rename(oldName.c_str(), newFileName.c_str()); |
| 443 | if (iRet != 0) |
| 444 | { |
| 445 | std::stringstream ss; |
| 446 | ss << "rename of [" << oldName << "] to [" << newFileName << "] failed with errno " << std::to_string(errno) |
| 447 | << " : " << std::strerror(errno); |
| 448 | ALOGW(ss.str().c_str()); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | |
| 453 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 454 | } // namespace armnn_driver |