blob: ac90a9ab7b98a6476d8af783eacaf8b759740260 [file] [log] [blame]
telsoa015307bc12018-03-09 13:51:08 +00001//
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
telsoa01ce3e84a2018-08-31 09:31:35 +01008#include "ArmnnDriver.hpp"
9
10#include <NeuralNetworks.h>
11
telsoa015307bc12018-03-09 13:51:08 +000012#include <armnn/ArmNN.hpp>
13#include <CpuExecutor.h>
14
Matteo Martincighe48bdff2018-09-03 13:50:50 +010015#include <boost/format.hpp>
16#include <log/log.h>
17
telsoa015307bc12018-03-09 13:51:08 +000018#include <vector>
19#include <string>
Matteo Martincighe48bdff2018-09-03 13:50:50 +010020#include <fstream>
21#include <iomanip>
telsoa015307bc12018-03-09 13:51:08 +000022
23namespace armnn_driver
24{
25
26extern const armnn::PermutationVector g_DontPermute;
27
28class UnsupportedOperand: public std::runtime_error
29{
30public:
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.
40void 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
44void* GetMemoryFromPool(DataLocation location,
45 const std::vector<android::nn::RunTimePoolInfo>& memPools);
46
47/// Can throw UnsupportedOperand
48armnn::TensorInfo GetTensorInfoForOperand(const Operand& operand);
49
50std::string GetOperandSummary(const Operand& operand);
kevmay01bc5f7842018-08-30 12:34:39 +010051
Matteo Martincighe48bdff2018-09-03 13:50:50 +010052template <typename HalModel>
53std::string GetModelSummary(const HalModel& model)
kevmay01bc5f7842018-08-30 12:34:39 +010054{
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}
telsoa015307bc12018-03-09 13:51:08 +000083
84void DumpTensor(const std::string& dumpDir,
telsoa01ce3e84a2018-08-31 09:31:35 +010085 const std::string& requestName,
86 const std::string& tensorName,
87 const armnn::ConstTensor& tensor);
88
89void DumpJsonProfilingIfRequired(bool gpuProfilingEnabled,
90 const std::string& dumpDir,
91 armnn::NetworkId networkId,
92 const armnn::IProfiler* profiler);
telsoa015307bc12018-03-09 13:51:08 +000093
Matteo Martincighe48bdff2018-09-03 13:50:50 +010094template <typename HalModel>
surmeh0176660052018-03-29 16:33:54 +010095void ExportNetworkGraphToDotFile(const armnn::IOptimizedNetwork& optimizedNetwork,
96 const std::string& dumpDir,
Matteo Martincighe48bdff2018-09-03 13:50:50 +010097 const HalModel& model)
98{
99 // The dump directory must exist in advance.
100 if (dumpDir.empty())
101 {
102 return;
103 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100104
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100105 // 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}