summaryrefslogtreecommitdiff
path: root/compiler/circledump/src/Dump.cpp
blob: 3d99189f9a40f7d8e9db9dcde2f755a9cfdcf2d7 (plain)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * 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.
 */

#include <circledump/Dump.h>

#include "Read.h"
#include "OpPrinter.h"

#include <ostream>

#include <algorithm> // min
#include <iomanip>   // setfill

namespace circledump
{

void dump_buffer(std::ostream &os, const uint8_t *buffer, size_t size, size_t amount)
{
  std::ios_base::fmtflags saveflags(os.flags());

  bool second = false;
  bool ellipsis = amount > 0 && size > 4;
  size_t count = ellipsis ? std::min(size, amount) : size;

  for (size_t i = 0; i < count; i++)
  {
    if (second)
    {
      os << " ";
    }

    os << std::showbase << std::setfill('0') << std::setw(2);
    os << std::hex << (uint32_t)buffer[i];

    second = true;
  }
  if (ellipsis)
  {
    os << " ...";
  }

  os.flags(saveflags);
}

void dump_vector(std::ostream &os, const std::vector<int32_t> &vs)
{
  uint32_t seq = 0;
  for (auto &v : vs)
  {
    if (seq)
      os << ", ";
    os << v;
    seq++;
  }
}

std::ostream &operator<<(std::ostream &os, const std::vector<int32_t> &vect)
{
  circledump::dump_vector(os, vect);
  return os;
}

template <typename T> void dump_fbvect(std::ostream &os, const flatbuffers::Vector<T> *fbvect)
{
  if (fbvect == nullptr)
    return;

  bool ellipsis = (fbvect->size() > 4);
  auto limit_size = ellipsis ? 4 : fbvect->size();

  if (ellipsis)
  {
    os << "(" << fbvect->size() << ") ";
  }
  for (uint32_t q = 0; q < limit_size; q++)
  {
    if (q)
      os << ", ";
    os << fbvect->Get(q);
  }
  if (ellipsis)
  {
    os << " ... ";
  }
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const flatbuffers::Vector<T> *fbvect)
{
  dump_fbvect(os, fbvect);
  return os;
}

void dump_sub_graph(std::ostream &os, circleread::Reader &reader)
{
  auto tensors = reader.tensors();
  auto operators = reader.operators();
  auto data_format = reader.data_format();

  // dump data_format
  os << "Data Format:" << std::endl;
  if (data_format == circle::DataFormat::DataFormat_CHANNELS_LAST)
  {
    os << "CHANNEL_LAST (NHWC for 2d, NDHWC for 3d data)" << std::endl;
  }
  else if (data_format == circle::DataFormat::DataFormat_CHANNELS_FIRST)
  {
    os << "CHANNEL_FIRST (NCHW for 2d, NCDHW for 3d data)" << std::endl;
  }
  os << std::endl;

  // dump operands(tensors)
  os << "Operands: T(subgraph index : tensor index) TYPE (shape) B(buffer index) OperandName"
     << std::endl;
  for (uint32_t i = 0; i < tensors->Length(); ++i)
  {
    // TODO refactor to some better structure
    auto tensor = tensors->Get(i);
    std::vector<int32_t> dims = {-1};

    if (tensor->shape())
      dims = circleread::as_index_vector(tensor->shape());

    os << "T(" << reader.subgraph_index() << ":" << i << ") " << circleread::tensor_type(tensor)
       << " ";
    os << "(" << dims << ") ";
    os << "B(" << tensor->buffer() << ") ";
    os << circleread::tensor_name(tensor) << std::endl;

    if (auto q_params = tensor->quantization())
    {
      if ((q_params->min() && q_params->max()) || (q_params->scale() && q_params->zero_point()))
      {
        std::string strquantiz = "    Quantization: ";
        std::string strqindent(strquantiz.size(), ' ');
        os << strquantiz;

        if (q_params->min())
        {
          os << "min(" << q_params->min() << ") ";
          if (q_params->min()->size() > 1)
            os << std::endl << strqindent;
        }
        if (q_params->max())
        {
          os << "max(" << q_params->max() << ") ";
          if (q_params->max()->size() > 1)
            os << std::endl << strqindent;
        }
        if (q_params->scale())
        {
          os << "scale(" << q_params->scale() << ") ";
          if (q_params->scale()->size() > 1)
            os << std::endl << strqindent;
        }
        if (q_params->zero_point())
          os << "zeropt(" << q_params->zero_point() << ") ";

        os << std::endl;
      }
    }
  }
  os << std::endl;

  // dump operators
  os << "Operators: O(subgraph index : operator index) OpCodeName " << std::endl;
  os << "    Option(values) ... <-- depending on OpCode" << std::endl;
  os << "    I T(tensor index) OperandName <-- as input" << std::endl;
  os << "    O T(tensor index) OperandName <-- as output" << std::endl;
  for (uint32_t i = 0; i < operators->Length(); ++i)
  {
    const auto op = operators->Get(i);
    circle::BuiltinOperator builtincode = reader.builtin_code(op);

    const std::vector<int32_t> &inputs = circleread::as_index_vector(op->inputs());
    const std::vector<int32_t> &outputs = circleread::as_index_vector(op->outputs());
    auto op_name = reader.opcode_name(op);

    os << "O(" << reader.subgraph_index() << ":" << i << ") " << op_name << " ";
    os << std::endl;

    if (auto op_prn = OpPrinterRegistry::get().lookup(builtincode))
    {
      op_prn->options(op, os);
    }

    for (auto input : inputs)
    {
      os << "    I T(" << input << ") ";
      if (input >= 0)
      {
        auto tensor = tensors->Get(input);
        os << circleread::tensor_name(tensor);
      }
      os << std::endl;
    }
    for (auto output : outputs)
    {
      os << "    O T(" << output << ") ";
      if (output >= 0)
      {
        auto tensor = tensors->Get(output);
        os << circleread::tensor_name(tensor);
      }
      os << std::endl;
    }
  }
  os << std::endl;

  // dump network inputs/outputs
  os << "Inputs/Outputs: I(input)/O(output) T(tensor index) OperandName" << std::endl;

  for (const auto input : reader.inputs())
  {
    auto tensor = tensors->Get(input);
    std::string name = circleread::tensor_name(tensor);
    os << "I T(" << input << ") " << name << std::endl;
  }

  for (const auto output : reader.outputs())
  {
    auto tensor = tensors->Get(output);
    std::string name = circleread::tensor_name(tensor);
    os << "O T(" << output << ") " << name << std::endl;
  }

  os << std::endl;
}

void dump_model(std::ostream &os, const circle::Model *model)
{
  circleread::Reader reader(model);

  uint32_t num_subgraph = reader.num_subgraph();

  // dump model version
  os << "===================================================================" << std::endl;
  os << "Model version: " << reader.version() << std::endl;
  os << " # sub graphs: " << num_subgraph << std::endl;
  os << std::endl;

  auto opcodes = reader.opcodes();
  auto buffers = reader.buffers();

  // dump operator_codes
  os << "Operator Codes: [order] OpCodeName (OpCode Enum)" << std::endl;
  int32_t opcode_index = 0;
  for (auto opcode : opcodes)
  {
    circle::BuiltinOperator op_code = opcode->builtin_code();
    auto op_name = circleread::opcode_name(opcode);
    auto op_version = opcode->version();

    os << "[" << opcode_index << "] " << op_name << " (code: " << op_code
       << ", version: " << op_version << ")" << std::endl;

    opcode_index++;
  }
  os << std::endl;

  // dump buffer
  os << "Buffers: B(index) (length) values, if any" << std::endl;
  for (uint32_t i = 0; i < buffers->Length(); ++i)
  {
    const uint8_t *buff_data;
    size_t size = reader.buffer_info(i, &buff_data);

    os << "B(" << i << ") (" << size << ") ";
    if (buff_data != nullptr)
    {
      dump_buffer(os, buff_data, size, 16);
    }
    os << std::endl;
  }
  os << std::endl;

  for (uint32_t sg = 0; sg < num_subgraph; ++sg)
  {
    reader.select_subgraph(sg);

    os << "-------------------------------------------------------------------" << std::endl;
    os << "Sub-Graph: #" << sg << " " << reader.subgraph_name() << std::endl;
    os << std::endl;

    dump_sub_graph(os, reader);
  }

  os << "===================================================================" << std::endl;
}

} // namespace circledump

std::ostream &operator<<(std::ostream &os, const circle::Model *model)
{
  circledump::dump_model(os, model);
  return os;
}