summaryrefslogtreecommitdiff
path: root/compiler/tfldump/src/OpPrinter.cpp
blob: 5120f42b1d5e36fca730f13ef2bfe80976696041 (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
/*
 * Copyright (c) 2018 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 "OpPrinter.h"
#include "Read.h"

#include <stdex/Memory.h>

#include <flatbuffers/flexbuffers.h>

using stdex::make_unique;

namespace tfldump
{

// TODO move to some header
std::ostream &operator<<(std::ostream &os, const std::vector<int32_t> &vect);

// TODO Re-arrange in alphabetical order

class AddPrinter : public OpPrinter
{
public:
  void options(const tflite::Operator *op, std::ostream &os) const override
  {
    if (auto *params = op->builtin_options_as_AddOptions())
    {
      os << "    ";
      os << "Activation(" << EnumNameActivationFunctionType(params->fused_activation_function())
         << ") ";
      os << std::endl;
    }
  }
};

class ArgMaxPrinter : public OpPrinter
{
public:
  void options(const tflite::Operator *op, std::ostream &os) const override
  {
    if (auto *params = op->builtin_options_as_ArgMaxOptions())
    {
      os << "    ";
      os << "OutputType(" << EnumNameTensorType(params->output_type()) << ") ";
      os << std::endl;
    }
  }
};

class Conv2DPrinter : public OpPrinter
{
public:
  void options(const tflite::Operator *op, std::ostream &os) const override
  {
    if (auto conv_params = op->builtin_options_as_Conv2DOptions())
    {
      os << "    ";
      os << "Padding(" << conv_params->padding() << ") ";
      os << "Stride.W(" << conv_params->stride_w() << ") ";
      os << "Stride.H(" << conv_params->stride_h() << ") ";
      os << "Activation("
         << EnumNameActivationFunctionType(conv_params->fused_activation_function()) << ")";
      os << std::endl;
    }
  }
};

class DivPrinter : public OpPrinter
{
public:
  void options(const tflite::Operator *op, std::ostream &os) const override
  {
    if (auto *params = op->builtin_options_as_DivOptions())
    {
      os << "    ";
      os << "Activation(" << EnumNameActivationFunctionType(params->fused_activation_function())
         << ") ";
      os << std::endl;
    }
  }
};

class Pool2DPrinter : public OpPrinter
{
public:
  void options(const tflite::Operator *op, std::ostream &os) const override
  {
    if (auto pool_params = op->builtin_options_as_Pool2DOptions())
    {
      os << "    ";
      os << "Padding(" << pool_params->padding() << ") ";
      os << "Stride.W(" << pool_params->stride_w() << ") ";
      os << "Stride.H(" << pool_params->stride_h() << ") ";
      os << "Filter.W(" << pool_params->filter_width() << ") ";
      os << "Filter.H(" << pool_params->filter_height() << ") ";
      os << "Activation("
         << EnumNameActivationFunctionType(pool_params->fused_activation_function()) << ")";
      os << std::endl;
    }
  }
};

class ConcatenationPrinter : public OpPrinter
{
public:
  void options(const tflite::Operator *op, std::ostream &os) const override
  {
    if (auto *concatenation_params = op->builtin_options_as_ConcatenationOptions())
    {
      os << "    ";
      os << "Activation("
         << EnumNameActivationFunctionType(concatenation_params->fused_activation_function())
         << ") ";
      os << "Axis(" << concatenation_params->axis() << ")";
      os << std::endl;
    }
  }
};

class ReshapePrinter : public OpPrinter
{
public:
  void options(const tflite::Operator *op, std::ostream &os) const override
  {
    if (auto *reshape_params = op->builtin_options_as_ReshapeOptions())
    {
      auto new_shape = tflread::as_index_vector(reshape_params->new_shape());
      os << "    ";
      os << "NewShape(" << new_shape << ")";
      os << std::endl;
    }
  }
};

class DepthwiseConv2DPrinter : public OpPrinter
{
public:
  void options(const tflite::Operator *op, std::ostream &os) const override
  {
    if (auto conv_params = op->builtin_options_as_DepthwiseConv2DOptions())
    {
      os << "    ";
      os << "Padding(" << conv_params->padding() << ") ";
      os << "Stride.W(" << conv_params->stride_w() << ") ";
      os << "Stride.H(" << conv_params->stride_h() << ") ";
      os << "DepthMultiplier(" << conv_params->depth_multiplier() << ") ";
      os << "Dilation.W(" << conv_params->dilation_w_factor() << ") ";
      os << "Dilation.H(" << conv_params->dilation_h_factor() << ")";
      os << "Activation("
         << EnumNameActivationFunctionType(conv_params->fused_activation_function()) << ") ";
      os << std::endl;
    }
  }
};

class FullyConnectedPrinter : public OpPrinter
{
public:
  void options(const tflite::Operator *op, std::ostream &os) const override
  {
    if (auto *params = op->builtin_options_as_FullyConnectedOptions())
    {
      os << "    ";
      os << "WeightFormat(" << EnumNameFullyConnectedOptionsWeightsFormat(params->weights_format())
         << ") ";
      os << "Activation(" << EnumNameActivationFunctionType(params->fused_activation_function())
         << ") ";

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

class MulPrinter : public OpPrinter
{
public:
  void options(const tflite::Operator *op, std::ostream &os) const override
  {
    if (auto *params = op->builtin_options_as_MulOptions())
    {
      os << "    ";
      os << "Activation(" << EnumNameActivationFunctionType(params->fused_activation_function())
         << ") ";
      os << std::endl;
    }
  }
};

class PackPrinter : public OpPrinter
{
public:
  void options(const tflite::Operator *op, std::ostream &os) const override
  {
    if (auto *params = op->builtin_options_as_PackOptions())
    {
      os << "    ";
      os << "ValuesCount(" << params->values_count() << ") ";
      os << "Axis(" << params->axis() << ") ";
      os << std::endl;
    }
  }
};

class SoftmaxPrinter : public OpPrinter
{
public:
  void options(const tflite::Operator *op, std::ostream &os) const override
  {
    if (auto *softmax_params = op->builtin_options_as_SoftmaxOptions())
    {
      os << "    ";
      os << "Beta(" << softmax_params->beta() << ")";
      os << std::endl;
    }
  }
};

class SubPrinter : public OpPrinter
{
public:
  void options(const tflite::Operator *op, std::ostream &os) const override
  {
    if (auto *params = op->builtin_options_as_SubOptions())
    {
      os << "    ";
      os << "Activation(" << EnumNameActivationFunctionType(params->fused_activation_function())
         << ") ";
      os << std::endl;
    }
  }
};

class CustomOpPrinter : public OpPrinter
{
public:
  void options(const tflite::Operator *op, std::ostream &os) const override
  {
    if (op->custom_options_format() != tflite::CustomOptionsFormat::CustomOptionsFormat_FLEXBUFFERS)
    {
      os << "    ";
      os << "Unknown custom option format";
      return;
    }

    const flatbuffers::Vector<uint8_t> *option_buf = op->custom_options();

    if (option_buf == nullptr || option_buf->size() == 0)
    {
      os << "No attrs found." << std::endl;
      return;
    }

    // printing attrs
    // attrs of custom ops are encoded in flexbuffer format
    auto attr_map = flexbuffers::GetRoot(option_buf->data(), option_buf->size()).AsMap();

    os << "    ";
    auto keys = attr_map.Keys();
    for (int i = 0; i < keys.size(); i++)
    {
      auto key = keys[i].ToString();
      os << key << "(" << attr_map[key].ToString() << ") ";
    }

    // Note: attr in "Shape" type does not seem to be converted by tflite_convert.
    // When the converted tflite file (with custom op) is opened with hexa editory,
    // attrs names can be found but attr name in "Shape" type is not found.

    os << std::endl;
  }
};

OpPrinterRegistry::OpPrinterRegistry()
{
  _op_map[tflite::BuiltinOperator_ADD] = make_unique<AddPrinter>();
  _op_map[tflite::BuiltinOperator_ARG_MAX] = make_unique<ArgMaxPrinter>();
  _op_map[tflite::BuiltinOperator_AVERAGE_POOL_2D] = make_unique<Pool2DPrinter>();
  _op_map[tflite::BuiltinOperator_CONCATENATION] = make_unique<ConcatenationPrinter>();
  _op_map[tflite::BuiltinOperator_CONV_2D] = make_unique<Conv2DPrinter>();
  _op_map[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = make_unique<DepthwiseConv2DPrinter>();
  _op_map[tflite::BuiltinOperator_DIV] = make_unique<DivPrinter>();
  _op_map[tflite::BuiltinOperator_FULLY_CONNECTED] = make_unique<FullyConnectedPrinter>();
  _op_map[tflite::BuiltinOperator_MAX_POOL_2D] = make_unique<Pool2DPrinter>();
  _op_map[tflite::BuiltinOperator_MUL] = make_unique<MulPrinter>();
  _op_map[tflite::BuiltinOperator_PACK] = make_unique<PackPrinter>();
  // There is no Option for ReLU and ReLU6
  _op_map[tflite::BuiltinOperator_RESHAPE] = make_unique<ReshapePrinter>();
  _op_map[tflite::BuiltinOperator_SOFTMAX] = make_unique<SoftmaxPrinter>();
  _op_map[tflite::BuiltinOperator_SUB] = make_unique<SubPrinter>();
  _op_map[tflite::BuiltinOperator_CUSTOM] = make_unique<CustomOpPrinter>();
}

} // namespace tfldump