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