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