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