telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // See LICENSE file in the project root for full license information. |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 8 | #include "ArmnnDriver.hpp" |
| 9 | |
| 10 | #include <NeuralNetworks.h> |
| 11 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 12 | #include <armnn/ArmNN.hpp> |
| 13 | #include <CpuExecutor.h> |
| 14 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 15 | #include <boost/format.hpp> |
| 16 | #include <log/log.h> |
| 17 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 18 | #include <vector> |
| 19 | #include <string> |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 20 | #include <fstream> |
| 21 | #include <iomanip> |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 22 | |
| 23 | namespace armnn_driver |
| 24 | { |
| 25 | |
| 26 | extern const armnn::PermutationVector g_DontPermute; |
| 27 | |
| 28 | class UnsupportedOperand: public std::runtime_error |
| 29 | { |
| 30 | public: |
| 31 | UnsupportedOperand(const OperandType type) |
| 32 | : std::runtime_error("Operand type is unsupported") |
| 33 | , m_type(type) |
| 34 | {} |
| 35 | |
| 36 | OperandType m_type; |
| 37 | }; |
| 38 | |
| 39 | /// Swizzles tensor data in @a input according to the dimension mappings. |
| 40 | void SwizzleAndroidNn4dTensorToArmNn(const armnn::TensorInfo& tensor, const void* input, void* output, |
| 41 | const armnn::PermutationVector& mappings); |
| 42 | |
| 43 | /// Returns a pointer to a specific location in a pool |
| 44 | void* GetMemoryFromPool(DataLocation location, |
| 45 | const std::vector<android::nn::RunTimePoolInfo>& memPools); |
| 46 | |
| 47 | /// Can throw UnsupportedOperand |
| 48 | armnn::TensorInfo GetTensorInfoForOperand(const Operand& operand); |
| 49 | |
| 50 | std::string GetOperandSummary(const Operand& operand); |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 51 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 52 | template <typename HalModel> |
| 53 | std::string GetModelSummary(const HalModel& model) |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 54 | { |
| 55 | std::stringstream result; |
| 56 | |
| 57 | result << model.inputIndexes.size() << " input(s), " << model.operations.size() << " operation(s), " << |
| 58 | model.outputIndexes.size() << " output(s), " << model.operands.size() << " operand(s)" << std::endl; |
| 59 | |
| 60 | result << "Inputs: "; |
| 61 | for (uint32_t i = 0; i < model.inputIndexes.size(); i++) |
| 62 | { |
| 63 | result << GetOperandSummary(model.operands[model.inputIndexes[i]]) << ", "; |
| 64 | } |
| 65 | result << std::endl; |
| 66 | |
| 67 | result << "Operations: "; |
| 68 | for (uint32_t i = 0; i < model.operations.size(); i++) |
| 69 | { |
| 70 | result << toString(model.operations[i].type).c_str() << ", "; |
| 71 | } |
| 72 | result << std::endl; |
| 73 | |
| 74 | result << "Outputs: "; |
| 75 | for (uint32_t i = 0; i < model.outputIndexes.size(); i++) |
| 76 | { |
| 77 | result << GetOperandSummary(model.operands[model.outputIndexes[i]]) << ", "; |
| 78 | } |
| 79 | result << std::endl; |
| 80 | |
| 81 | return result.str(); |
| 82 | } |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 83 | |
| 84 | void DumpTensor(const std::string& dumpDir, |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 85 | const std::string& requestName, |
| 86 | const std::string& tensorName, |
| 87 | const armnn::ConstTensor& tensor); |
| 88 | |
| 89 | void DumpJsonProfilingIfRequired(bool gpuProfilingEnabled, |
| 90 | const std::string& dumpDir, |
| 91 | armnn::NetworkId networkId, |
| 92 | const armnn::IProfiler* profiler); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 93 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 94 | template <typename HalModel> |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 95 | void ExportNetworkGraphToDotFile(const armnn::IOptimizedNetwork& optimizedNetwork, |
| 96 | const std::string& dumpDir, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 97 | const HalModel& model) |
| 98 | { |
| 99 | // The dump directory must exist in advance. |
| 100 | if (dumpDir.empty()) |
| 101 | { |
| 102 | return; |
| 103 | } |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 104 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 105 | // Get the memory address of the model and convert it to a hex string (of at least a '0' character). |
| 106 | size_t modelAddress = uintptr_t(&model); |
| 107 | std::stringstream ss; |
| 108 | ss << std::uppercase << std::hex << std::setfill('0') << std::setw(1) << modelAddress; |
| 109 | std::string modelAddressHexString = ss.str(); |
| 110 | |
| 111 | // Set the name of the output .dot file. |
| 112 | const std::string fileName = boost::str(boost::format("%1%/networkgraph_%2%.dot") |
| 113 | % dumpDir |
| 114 | % modelAddressHexString); |
| 115 | |
| 116 | ALOGV("Exporting the optimized network graph to file: %s", fileName.c_str()); |
| 117 | |
| 118 | // Write the network graph to a dot file. |
| 119 | std::ofstream fileStream; |
| 120 | fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc); |
| 121 | |
| 122 | if (!fileStream.good()) |
| 123 | { |
| 124 | ALOGW("Could not open file %s for writing", fileName.c_str()); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | if (optimizedNetwork.SerializeToDot(fileStream) != armnn::Status::Success) |
| 129 | { |
| 130 | ALOGW("An error occurred when writing to file %s", fileName.c_str()); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } |