blob: 812dfbd576118fdebd6708e6abc7855e9a39b1f5 [file] [log] [blame]
telsoa015307bc12018-03-09 13:51:08 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beck93e48982018-09-05 13:05:09 +01003// SPDX-License-Identifier: MIT
telsoa015307bc12018-03-09 13:51:08 +00004//
5
6#pragma once
7
telsoa015307bc12018-03-09 13:51:08 +00008#include <armnn/ArmNN.hpp>
arovir01b0717b52018-09-05 17:03:25 +01009
telsoa015307bc12018-03-09 13:51:08 +000010#include <CpuExecutor.h>
arovir01b0717b52018-09-05 17:03:25 +010011#include <HalInterfaces.h>
12#include <NeuralNetworks.h>
telsoa015307bc12018-03-09 13:51:08 +000013
Matteo Martincighe48bdff2018-09-03 13:50:50 +010014#include <boost/format.hpp>
15#include <log/log.h>
16
telsoa015307bc12018-03-09 13:51:08 +000017#include <vector>
18#include <string>
Matteo Martincighe48bdff2018-09-03 13:50:50 +010019#include <fstream>
20#include <iomanip>
telsoa015307bc12018-03-09 13:51:08 +000021
22namespace armnn_driver
23{
24
25extern const armnn::PermutationVector g_DontPermute;
26
27class UnsupportedOperand: public std::runtime_error
28{
29public:
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.
39void 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
43void* GetMemoryFromPool(DataLocation location,
44 const std::vector<android::nn::RunTimePoolInfo>& memPools);
45
46/// Can throw UnsupportedOperand
47armnn::TensorInfo GetTensorInfoForOperand(const Operand& operand);
48
49std::string GetOperandSummary(const Operand& operand);
kevmay01bc5f7842018-08-30 12:34:39 +010050
Matteo Martincighe48bdff2018-09-03 13:50:50 +010051template <typename HalModel>
52std::string GetModelSummary(const HalModel& model)
kevmay01bc5f7842018-08-30 12:34:39 +010053{
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}
telsoa015307bc12018-03-09 13:51:08 +000082
83void DumpTensor(const std::string& dumpDir,
telsoa01ce3e84a2018-08-31 09:31:35 +010084 const std::string& requestName,
85 const std::string& tensorName,
86 const armnn::ConstTensor& tensor);
87
88void DumpJsonProfilingIfRequired(bool gpuProfilingEnabled,
89 const std::string& dumpDir,
90 armnn::NetworkId networkId,
91 const armnn::IProfiler* profiler);
telsoa015307bc12018-03-09 13:51:08 +000092
Matteo Martincighe48bdff2018-09-03 13:50:50 +010093template <typename HalModel>
surmeh0176660052018-03-29 16:33:54 +010094void ExportNetworkGraphToDotFile(const armnn::IOptimizedNetwork& optimizedNetwork,
95 const std::string& dumpDir,
Matteo Martincighe48bdff2018-09-03 13:50:50 +010096 const HalModel& model)
97{
98 // The dump directory must exist in advance.
99 if (dumpDir.empty())
100 {
101 return;
102 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100103
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100104 // 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
arovir01b0717b52018-09-05 17:03:25 +0100133} // namespace armnn_driver