1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "GraphDump"
18
19 #include "GraphDump.h"
20
21 #include "HalInterfaces.h"
22
23 #include <android-base/logging.h>
24 #include <algorithm>
25 #include <iostream>
26 #include <map>
27 #include <sstream>
28 #include <string>
29 #include <utility>
30
31 namespace android {
32 namespace nn {
33
34 using namespace hal;
35
36 // class Dumper is a wrapper around an std::ostream (if instantiated
37 // with a pointer to a stream) or around LOG(INFO) (otherwise).
38 //
39 // Send fragments of output to it with operator<<(), as per usual
40 // stream conventions. Unlike with LOG(INFO), there is no implicit
41 // end-of-line. To end a line, send Dumper::endl.
42 //
43 // Example:
44 //
45 // Dumper d(nullptr); // will go to LOG(INFO)
46 // d << "These words are";
47 // d << " all" << " on";
48 // d << " the same line." << Dumper::endl;
49 //
50 namespace {
51 class Dumper {
52 public:
Dumper(std::ostream * outStream)53 Dumper(std::ostream* outStream) : mStream(outStream) {}
54
55 Dumper(const Dumper&) = delete;
56 void operator=(const Dumper&) = delete;
57
58 template <typename T>
operator <<(const T & val)59 Dumper& operator<<(const T& val) {
60 mStringStream << val;
61 return *this;
62 }
63
64 class EndlType {};
65
operator <<(EndlType)66 Dumper& operator<<(EndlType) {
67 if (mStream) {
68 *mStream << mStringStream.str() << std::endl;
69 } else {
70 // TODO: There is a limit of how long a single LOG line
71 // can be; extra characters are truncated. (See
72 // LOGGER_ENTRY_MAX_PAYLOAD and LOGGER_ENTRY_MAX_LEN.) We
73 // may want to figure out the linebreak rules for the .dot
74 // format and try to ensure that we generate correct .dot
75 // output whose lines do not exceed some maximum length.
76 // The intelligence for breaking the lines might have to
77 // live in graphDump() rather than in the Dumper class, so
78 // that it can be sensitive to the .dot format.
79 LOG(INFO) << mStringStream.str();
80 }
81 std::ostringstream empty;
82 std::swap(mStringStream, empty);
83 return *this;
84 }
85
86 static const EndlType endl;
87
88 private:
89 std::ostream* mStream;
90 std::ostringstream mStringStream;
91 };
92
93 const Dumper::EndlType Dumper::endl;
94 } // namespace
95
96 // Provide short name for OperandType value.
translate(OperandType type)97 static std::string translate(OperandType type) {
98 switch (type) {
99 case OperandType::FLOAT32:
100 return "F32";
101 case OperandType::INT32:
102 return "I32";
103 case OperandType::UINT32:
104 return "U32";
105 case OperandType::TENSOR_FLOAT32:
106 return "TF32";
107 case OperandType::TENSOR_INT32:
108 return "TI32";
109 case OperandType::TENSOR_QUANT8_ASYMM:
110 return "TQ8A";
111 case OperandType::OEM:
112 return "OEM";
113 case OperandType::TENSOR_OEM_BYTE:
114 return "TOEMB";
115 default:
116 return toString(type);
117 }
118 }
119
120 // If the specified Operand of the specified Model has OperandType
121 // nnType corresponding to C++ type cppType and is of
122 // OperandLifeTime::CONSTANT_COPY, then write the Operand's value to
123 // the Dumper.
124 namespace {
125 template <OperandType nnType, typename cppType>
tryValueDump(Dumper & dump,const Model & model,const Operand & opnd)126 void tryValueDump(Dumper& dump, const Model& model, const Operand& opnd) {
127 if (opnd.type != nnType || opnd.lifetime != OperandLifeTime::CONSTANT_COPY ||
128 opnd.location.length != sizeof(cppType)) {
129 return;
130 }
131
132 cppType val;
133 memcpy(&val, &model.operandValues[opnd.location.offset], sizeof(cppType));
134 dump << " = " << val;
135 }
136 } // namespace
137
graphDump(const char * name,const Model & model,std::ostream * outStream)138 void graphDump(const char* name, const Model& model, std::ostream* outStream) {
139 // Operand nodes are named "d" (operanD) followed by operand index.
140 // Operation nodes are named "n" (operatioN) followed by operation index.
141 // (These names are not the names that are actually displayed -- those
142 // names are given by the "label" attribute.)
143
144 Dumper dump(outStream);
145
146 dump << "// " << name << Dumper::endl;
147 dump << "digraph {" << Dumper::endl;
148
149 // model inputs and outputs (map from operand index to input or output index)
150 std::map<uint32_t, uint32_t> modelIO;
151 for (unsigned i = 0, e = model.main.inputIndexes.size(); i < e; i++) {
152 modelIO.emplace(model.main.inputIndexes[i], i);
153 }
154 for (unsigned i = 0, e = model.main.outputIndexes.size(); i < e; i++) {
155 modelIO.emplace(model.main.outputIndexes[i], i);
156 }
157
158 // TODO(b/147661714): Add subgraph support to GraphDump.
159 if (model.referenced.size() != 0) {
160 dump << "// NOTE: " << model.referenced.size() << " subgraphs omitted" << Dumper::endl;
161 dump << "// TODO(b/147661714): Add subgraph support to GraphDump" << Dumper::endl;
162 }
163
164 // model operands
165 for (unsigned i = 0, e = model.main.operands.size(); i < e; i++) {
166 dump << " d" << i << " [";
167 if (modelIO.count(i)) {
168 dump << "style=filled fillcolor=black fontcolor=white ";
169 }
170 dump << "label=\"";
171 const Operand& opnd = model.main.operands[i];
172 const char* kind = nullptr;
173 const char* io = nullptr;
174 switch (opnd.lifetime) {
175 case OperandLifeTime::CONSTANT_COPY:
176 kind = "COPY";
177 break;
178 case OperandLifeTime::CONSTANT_REFERENCE:
179 kind = "REF";
180 break;
181 case OperandLifeTime::SUBGRAPH_INPUT:
182 io = "input";
183 break;
184 case OperandLifeTime::SUBGRAPH_OUTPUT:
185 io = "output";
186 break;
187 case OperandLifeTime::NO_VALUE:
188 kind = "NO";
189 break;
190 case OperandLifeTime::SUBGRAPH:
191 kind = "SUBGRAPH";
192 break;
193 default:
194 // nothing interesting
195 break;
196 }
197 dump << i;
198 if (io) {
199 dump << " = " << io << "[" << modelIO.at(i) << "]";
200 }
201 if (kind) {
202 dump << ": " << kind;
203 }
204 dump << "\\n" << translate(opnd.type);
205 tryValueDump<OperandType::FLOAT32, float>(dump, model, opnd);
206 tryValueDump<OperandType::INT32, int>(dump, model, opnd);
207 tryValueDump<OperandType::UINT32, unsigned>(dump, model, opnd);
208 if (opnd.dimensions.size()) {
209 dump << "(";
210 for (unsigned i = 0, e = opnd.dimensions.size(); i < e; i++) {
211 if (i > 0) {
212 dump << "x";
213 }
214 dump << opnd.dimensions[i];
215 }
216 dump << ")";
217 }
218 dump << "\"]" << Dumper::endl;
219 }
220
221 // model operations
222 for (unsigned i = 0, e = model.main.operations.size(); i < e; i++) {
223 const Operation& operation = model.main.operations[i];
224 dump << " n" << i << " [shape=box";
225 const uint32_t maxArity = std::max(operation.inputs.size(), operation.outputs.size());
226 if (maxArity > 1) {
227 if (maxArity == operation.inputs.size()) {
228 dump << " ordering=in";
229 } else {
230 dump << " ordering=out";
231 }
232 }
233 dump << " label=\"" << i << ": " << toString(operation.type) << "\"]" << Dumper::endl;
234 {
235 // operation inputs
236 for (unsigned in = 0, inE = operation.inputs.size(); in < inE; in++) {
237 dump << " d" << operation.inputs[in] << " -> n" << i;
238 if (inE > 1) {
239 dump << " [label=" << in << "]";
240 }
241 dump << Dumper::endl;
242 }
243 }
244
245 {
246 // operation outputs
247 for (unsigned out = 0, outE = operation.outputs.size(); out < outE; out++) {
248 dump << " n" << i << " -> d" << operation.outputs[out];
249 if (outE > 1) {
250 dump << " [label=" << out << "]";
251 }
252 dump << Dumper::endl;
253 }
254 }
255 }
256 dump << "}" << Dumper::endl;
257 }
258
259 } // namespace nn
260 } // namespace android
261