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