2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "OpPrinter.h"
22 #include <flatbuffers/flexbuffers.h>
24 using std::make_unique;
29 // TODO move to some header
30 std::ostream &operator<<(std::ostream &os, const std::vector<int32_t> &vect);
32 // TODO Re-arrange in alphabetical order
34 class AddPrinter : public OpPrinter
37 void options(const tflite::Operator *op, std::ostream &os) const override
39 if (auto *params = op->builtin_options_as_AddOptions())
42 os << "Activation(" << EnumNameActivationFunctionType(params->fused_activation_function())
49 class ArgMaxPrinter : public OpPrinter
52 void options(const tflite::Operator *op, std::ostream &os) const override
54 if (auto *params = op->builtin_options_as_ArgMaxOptions())
57 os << "OutputType(" << EnumNameTensorType(params->output_type()) << ") ";
63 class ArgMinPrinter : public OpPrinter
66 void options(const tflite::Operator *op, std::ostream &os) const override
68 if (auto *params = op->builtin_options_as_ArgMinOptions())
71 os << "OutputType(" << EnumNameTensorType(params->output_type()) << ") ";
77 class CastPrinter : public OpPrinter
80 void options(const tflite::Operator *op, std::ostream &os) const override
82 if (auto cast_params = op->builtin_options_as_CastOptions())
85 os << "in_data_type(" << tflite::EnumNameTensorType(cast_params->in_data_type()) << ") ";
86 os << "out_data_type(" << tflite::EnumNameTensorType(cast_params->out_data_type()) << ") ";
92 class Conv2DPrinter : public OpPrinter
95 void options(const tflite::Operator *op, std::ostream &os) const override
97 if (auto conv_params = op->builtin_options_as_Conv2DOptions())
100 os << "Padding(" << conv_params->padding() << ") ";
101 os << "Stride.W(" << conv_params->stride_w() << ") ";
102 os << "Stride.H(" << conv_params->stride_h() << ") ";
103 os << "Dilation.W(" << conv_params->dilation_w_factor() << ") ";
104 os << "Dilation.H(" << conv_params->dilation_h_factor() << ") ";
106 << EnumNameActivationFunctionType(conv_params->fused_activation_function()) << ")";
112 class DivPrinter : public OpPrinter
115 void options(const tflite::Operator *op, std::ostream &os) const override
117 if (auto *params = op->builtin_options_as_DivOptions())
120 os << "Activation(" << EnumNameActivationFunctionType(params->fused_activation_function())
127 class Pool2DPrinter : public OpPrinter
130 void options(const tflite::Operator *op, std::ostream &os) const override
132 if (auto pool_params = op->builtin_options_as_Pool2DOptions())
135 os << "Padding(" << pool_params->padding() << ") ";
136 os << "Stride.W(" << pool_params->stride_w() << ") ";
137 os << "Stride.H(" << pool_params->stride_h() << ") ";
138 os << "Filter.W(" << pool_params->filter_width() << ") ";
139 os << "Filter.H(" << pool_params->filter_height() << ") ";
141 << EnumNameActivationFunctionType(pool_params->fused_activation_function()) << ")";
147 class ConcatenationPrinter : public OpPrinter
150 void options(const tflite::Operator *op, std::ostream &os) const override
152 if (auto *concatenation_params = op->builtin_options_as_ConcatenationOptions())
156 << EnumNameActivationFunctionType(concatenation_params->fused_activation_function())
158 os << "Axis(" << concatenation_params->axis() << ")";
164 class ReducerPrinter : public OpPrinter
167 void options(const tflite::Operator *op, std::ostream &os) const override
169 if (auto reducer_params = op->builtin_options_as_ReducerOptions())
172 os << "keep_dims(" << reducer_params->keep_dims() << ") ";
178 class ReshapePrinter : public OpPrinter
181 void options(const tflite::Operator *op, std::ostream &os) const override
183 if (auto *reshape_params = op->builtin_options_as_ReshapeOptions())
185 auto new_shape = tflread::as_index_vector(reshape_params->new_shape());
187 os << "NewShape(" << new_shape << ")";
193 class ResizeBilinearPrinter : public OpPrinter
196 void options(const tflite::Operator *op, std::ostream &os) const override
198 if (auto *resize_params = op->builtin_options_as_ResizeBilinearOptions())
201 os << std::boolalpha;
202 os << "align_corners(" << resize_params->align_corners() << ")";
203 os << "half_pixel_centers(" << resize_params->half_pixel_centers() << ")";
209 class ResizeNearestNeighborPrinter : public OpPrinter
212 void options(const tflite::Operator *op, std::ostream &os) const override
214 if (auto *resize_params = op->builtin_options_as_ResizeNearestNeighborOptions())
217 os << std::boolalpha;
218 os << "align_corners(" << resize_params->align_corners() << ")";
224 class ReverseSequencePrinter : public OpPrinter
227 void options(const tflite::Operator *op, std::ostream &os) const override
229 if (auto *std_params = op->builtin_options_as_ReverseSequenceOptions())
232 os << "seq_dim(" << std_params->seq_dim() << ") ";
233 os << "batch_dim(" << std_params->batch_dim() << ") ";
239 class DepthToSpacePrinter : public OpPrinter
242 void options(const tflite::Operator *op, std::ostream &os) const override
244 if (auto *std_params = op->builtin_options_as_DepthToSpaceOptions())
247 os << "BlockSize(" << std_params->block_size() << ")";
253 class SparseToDensePrinter : public OpPrinter
256 void options(const tflite::Operator *op, std::ostream &os) const override
258 if (auto *std_params = op->builtin_options_as_SparseToDenseOptions())
261 os << "ValidateIndices(" << std_params->validate_indices() << ")";
267 class DepthwiseConv2DPrinter : public OpPrinter
270 void options(const tflite::Operator *op, std::ostream &os) const override
272 if (auto conv_params = op->builtin_options_as_DepthwiseConv2DOptions())
275 os << "Padding(" << conv_params->padding() << ") ";
276 os << "Stride.W(" << conv_params->stride_w() << ") ";
277 os << "Stride.H(" << conv_params->stride_h() << ") ";
278 os << "DepthMultiplier(" << conv_params->depth_multiplier() << ") ";
279 os << "Dilation.W(" << conv_params->dilation_w_factor() << ") ";
280 os << "Dilation.H(" << conv_params->dilation_h_factor() << ")";
282 << EnumNameActivationFunctionType(conv_params->fused_activation_function()) << ") ";
288 class FullyConnectedPrinter : public OpPrinter
291 void options(const tflite::Operator *op, std::ostream &os) const override
293 if (auto *params = op->builtin_options_as_FullyConnectedOptions())
296 os << "WeightFormat(" << EnumNameFullyConnectedOptionsWeightsFormat(params->weights_format())
298 os << "Activation(" << EnumNameActivationFunctionType(params->fused_activation_function())
306 class GatherPrinter : public OpPrinter
309 void options(const tflite::Operator *op, std::ostream &os) const override
311 if (auto *params = op->builtin_options_as_GatherOptions())
314 os << "Axis(" << params->axis() << ") ";
321 class IfPrinter : public OpPrinter
324 void options(const tflite::Operator *op, std::ostream &os) const override
326 if (auto *params = op->builtin_options_as_IfOptions())
329 os << "then_subgraph_index(" << params->then_subgraph_index() << ") ";
330 os << "else_subgraph_index(" << params->else_subgraph_index() << ") ";
336 class L2NormPrinter : public OpPrinter
339 void options(const tflite::Operator *op, std::ostream &os) const override
341 if (auto *params = op->builtin_options_as_L2NormOptions())
344 os << "Activation(" << EnumNameActivationFunctionType(params->fused_activation_function())
351 class LeakyReluPrinter : public OpPrinter
354 void options(const tflite::Operator *op, std::ostream &os) const override
356 if (auto *params = op->builtin_options_as_LeakyReluOptions())
359 os << "alpha(" << params->alpha() << ") ";
364 class LocalResponseNormalizationPrinter : public OpPrinter
367 void options(const tflite::Operator *op, std::ostream &os) const override
369 if (auto *params = op->builtin_options_as_LocalResponseNormalizationOptions())
372 os << "radius(" << params->radius() << ") ";
373 os << "bias(" << params->bias() << ") ";
374 os << "alpha(" << params->alpha() << ") ";
375 os << "beta(" << params->beta() << ") ";
381 class MirrorPadPrinter : public OpPrinter
384 void options(const tflite::Operator *op, std::ostream &os) const override
386 if (auto *params = op->builtin_options_as_MirrorPadOptions())
389 os << "mode(" << EnumNameMirrorPadMode(params->mode()) << ") ";
395 class MulPrinter : public OpPrinter
398 void options(const tflite::Operator *op, std::ostream &os) const override
400 if (auto *params = op->builtin_options_as_MulOptions())
403 os << "Activation(" << EnumNameActivationFunctionType(params->fused_activation_function())
410 class PackPrinter : public OpPrinter
413 void options(const tflite::Operator *op, std::ostream &os) const override
415 if (auto *params = op->builtin_options_as_PackOptions())
418 os << "ValuesCount(" << params->values_count() << ") ";
419 os << "Axis(" << params->axis() << ") ";
425 class OneHotPrinter : public OpPrinter
428 void options(const tflite::Operator *op, std::ostream &os) const override
430 if (auto *params = op->builtin_options_as_OneHotOptions())
433 os << "Axis(" << params->axis() << ") ";
440 class ShapePrinter : public OpPrinter
443 void options(const tflite::Operator *op, std::ostream &os) const override
445 if (auto *params = op->builtin_options_as_ShapeOptions())
448 os << "out_type(" << EnumNameTensorType(params->out_type()) << ") ";
454 class SoftmaxPrinter : public OpPrinter
457 void options(const tflite::Operator *op, std::ostream &os) const override
459 if (auto *softmax_params = op->builtin_options_as_SoftmaxOptions())
462 os << "Beta(" << softmax_params->beta() << ")";
468 class SpaceToDepthPrinter : public OpPrinter
471 void options(const tflite::Operator *op, std::ostream &os) const override
473 if (auto *std_params = op->builtin_options_as_SpaceToDepthOptions())
476 os << "BlockSize(" << std_params->block_size() << ")";
482 class SqueezePrinter : public OpPrinter
485 void options(const tflite::Operator *op, std::ostream &os) const override
487 if (auto *params = op->builtin_options_as_SqueezeOptions())
490 os << "SqueezeDims(";
491 for (int i = 0; i < params->squeeze_dims()->size(); ++i)
495 os << params->squeeze_dims()->Get(i);
503 class StridedSlicePrinter : public OpPrinter
506 void options(const tflite::Operator *op, std::ostream &os) const override
508 if (auto *strided_slice_params = op->builtin_options_as_StridedSliceOptions())
511 os << "begin_mask(" << strided_slice_params->begin_mask() << ") ";
512 os << "end_mask(" << strided_slice_params->end_mask() << ") ";
513 os << "ellipsis_mask(" << strided_slice_params->ellipsis_mask() << ") ";
514 os << "new_axis_mask(" << strided_slice_params->new_axis_mask() << ") ";
515 os << "shrink_axis_mask(" << strided_slice_params->shrink_axis_mask() << ") ";
521 class SplitPrinter : public OpPrinter
524 void options(const tflite::Operator *op, std::ostream &os) const override
526 if (auto *params = op->builtin_options_as_SplitOptions())
529 os << "num_splits(" << params->num_splits() << ") ";
535 class SplitVPrinter : public OpPrinter
538 void options(const tflite::Operator *op, std::ostream &os) const override
540 if (auto *params = op->builtin_options_as_SplitVOptions())
543 os << "num_splits(" << params->num_splits() << ") ";
549 class SubPrinter : public OpPrinter
552 void options(const tflite::Operator *op, std::ostream &os) const override
554 if (auto *params = op->builtin_options_as_SubOptions())
557 os << "Activation(" << EnumNameActivationFunctionType(params->fused_activation_function())
564 class TransposeConvPrinter : public OpPrinter
567 void options(const tflite::Operator *op, std::ostream &os) const override
569 if (auto *params = op->builtin_options_as_TransposeConvOptions())
572 os << "Padding(" << params->padding() << ") ";
573 os << "Stride.W(" << params->stride_w() << ") ";
574 os << "Stride.H(" << params->stride_h() << ") ";
580 class WhilePrinter : public OpPrinter
583 void options(const tflite::Operator *op, std::ostream &os) const override
585 if (auto *params = op->builtin_options_as_WhileOptions())
588 os << "cond_subgraph_index(" << params->cond_subgraph_index() << ") ";
589 os << "body_subgraph_index(" << params->body_subgraph_index() << ") ";
595 class UnidirectionalSequenceLSTMPrinter : public OpPrinter
598 void options(const tflite::Operator *op, std::ostream &os) const override
600 if (auto *params = op->builtin_options_as_UnidirectionalSequenceLSTMOptions())
603 os << "Activation(" << EnumNameActivationFunctionType(params->fused_activation_function())
605 os << "cell_clip(" << params->cell_clip() << ") ";
606 os << "proj_clip(" << params->proj_clip() << ") ";
607 os << "time_major(" << params->time_major() << ") ";
608 os << "asymmetric_quantize_inputs(" << params->asymmetric_quantize_inputs() << ") ";
614 class UniquePrinter : public OpPrinter
617 void options(const tflite::Operator *op, std::ostream &os) const override
619 if (auto *params = op->builtin_options_as_UniqueOptions())
622 os << "idx_out_type(" << EnumNameTensorType(params->idx_out_type()) << ") ";
628 class CustomOpPrinter : public OpPrinter
631 void options(const tflite::Operator *op, std::ostream &os) const override
633 if (op->custom_options_format() != tflite::CustomOptionsFormat::CustomOptionsFormat_FLEXBUFFERS)
636 os << "Unknown custom option format";
640 const flatbuffers::Vector<uint8_t> *option_buf = op->custom_options();
642 if (option_buf == nullptr || option_buf->size() == 0)
644 os << "No attrs found." << std::endl;
649 // attrs of custom ops are encoded in flexbuffer format
650 auto attr_map = flexbuffers::GetRoot(option_buf->data(), option_buf->size()).AsMap();
653 auto keys = attr_map.Keys();
654 for (int i = 0; i < keys.size(); i++)
656 auto key = keys[i].ToString();
657 os << key << "(" << attr_map[key].ToString() << ") ";
660 // Note: attr in "Shape" type does not seem to be converted by tflite_convert.
661 // When the converted tflite file (with custom op) is opened with hexa editory,
662 // attrs names can be found but attr name in "Shape" type is not found.
668 OpPrinterRegistry::OpPrinterRegistry()
670 _op_map[tflite::BuiltinOperator_ADD] = make_unique<AddPrinter>();
671 // There is no Option for ADD_N
672 _op_map[tflite::BuiltinOperator_ARG_MAX] = make_unique<ArgMaxPrinter>();
673 _op_map[tflite::BuiltinOperator_ARG_MIN] = make_unique<ArgMinPrinter>();
674 _op_map[tflite::BuiltinOperator_AVERAGE_POOL_2D] = make_unique<Pool2DPrinter>();
675 _op_map[tflite::BuiltinOperator_CAST] = make_unique<CastPrinter>();
676 // There is no Option for CEIL
677 _op_map[tflite::BuiltinOperator_CONCATENATION] = make_unique<ConcatenationPrinter>();
678 _op_map[tflite::BuiltinOperator_CONV_2D] = make_unique<Conv2DPrinter>();
679 _op_map[tflite::BuiltinOperator_DEPTH_TO_SPACE] = make_unique<DepthToSpacePrinter>();
680 _op_map[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = make_unique<DepthwiseConv2DPrinter>();
681 // There is no Option for DEQUANTIZE
682 _op_map[tflite::BuiltinOperator_DIV] = make_unique<DivPrinter>();
683 // There is no Option for FLOOR
684 // There is no Option for FLOOR_MOD
685 _op_map[tflite::BuiltinOperator_FULLY_CONNECTED] = make_unique<FullyConnectedPrinter>();
686 _op_map[tflite::BuiltinOperator_GATHER] = make_unique<GatherPrinter>();
687 _op_map[tflite::BuiltinOperator_IF] = make_unique<IfPrinter>();
688 _op_map[tflite::BuiltinOperator_L2_POOL_2D] = make_unique<Pool2DPrinter>();
689 _op_map[tflite::BuiltinOperator_L2_NORMALIZATION] = make_unique<L2NormPrinter>();
690 _op_map[tflite::BuiltinOperator_LEAKY_RELU] = make_unique<LeakyReluPrinter>();
691 _op_map[tflite::BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION] =
692 make_unique<LocalResponseNormalizationPrinter>();
693 // There is no Option for LOG
694 // There is no Option for LOGISTIC
695 // There is no Option for LOG_SOFTMAX
696 _op_map[tflite::BuiltinOperator_MAX_POOL_2D] = make_unique<Pool2DPrinter>();
697 _op_map[tflite::BuiltinOperator_MEAN] = make_unique<ReducerPrinter>();
698 _op_map[tflite::BuiltinOperator_MIRROR_PAD] = make_unique<MirrorPadPrinter>();
699 _op_map[tflite::BuiltinOperator_MUL] = make_unique<MulPrinter>();
700 // There is no Option for NON_MAX_SUPPRESSION_V4
701 // There is no Option for NON_MAX_SUPPRESSION_V5
702 _op_map[tflite::BuiltinOperator_ONE_HOT] = make_unique<OneHotPrinter>();
703 _op_map[tflite::BuiltinOperator_PACK] = make_unique<PackPrinter>();
704 // There is no Option for PAD
705 // There is no Option for PADV2
706 // There is no Option for PRELU
707 // There is no Option for RELU
708 // There is no Option for RELU6
709 // There is no Option for RELU_N1_TO_1
710 _op_map[tflite::BuiltinOperator_REDUCE_ANY] = make_unique<ReducerPrinter>();
711 _op_map[tflite::BuiltinOperator_REDUCE_MAX] = make_unique<ReducerPrinter>();
712 _op_map[tflite::BuiltinOperator_REDUCE_MIN] = make_unique<ReducerPrinter>();
713 _op_map[tflite::BuiltinOperator_REDUCE_PROD] = make_unique<ReducerPrinter>();
714 _op_map[tflite::BuiltinOperator_RESHAPE] = make_unique<ReshapePrinter>();
715 _op_map[tflite::BuiltinOperator_RESIZE_BILINEAR] = make_unique<ResizeBilinearPrinter>();
716 _op_map[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] =
717 make_unique<ResizeNearestNeighborPrinter>();
718 _op_map[tflite::BuiltinOperator_REVERSE_SEQUENCE] = make_unique<ReverseSequencePrinter>();
719 // There is no Option for ROUND
720 // There is no Option for SELECT
721 // There is no Option for SELECT_V2
722 _op_map[tflite::BuiltinOperator_SHAPE] = make_unique<ShapePrinter>();
723 // There is no Option for SIN
724 // There is no Option for SLICE
725 _op_map[tflite::BuiltinOperator_SOFTMAX] = make_unique<SoftmaxPrinter>();
726 _op_map[tflite::BuiltinOperator_SPACE_TO_DEPTH] = make_unique<SpaceToDepthPrinter>();
727 // There is no Option for SPACE_TO_BATCH_ND
728 _op_map[tflite::BuiltinOperator_SPARSE_TO_DENSE] = make_unique<SparseToDensePrinter>();
729 _op_map[tflite::BuiltinOperator_SPLIT] = make_unique<SplitPrinter>();
730 _op_map[tflite::BuiltinOperator_SPLIT_V] = make_unique<SplitVPrinter>();
731 _op_map[tflite::BuiltinOperator_SQUEEZE] = make_unique<SqueezePrinter>();
732 _op_map[tflite::BuiltinOperator_STRIDED_SLICE] = make_unique<StridedSlicePrinter>();
733 _op_map[tflite::BuiltinOperator_SUB] = make_unique<SubPrinter>();
734 _op_map[tflite::BuiltinOperator_SUM] = make_unique<ReducerPrinter>();
735 _op_map[tflite::BuiltinOperator_TRANSPOSE_CONV] = make_unique<TransposeConvPrinter>();
736 // There is no Option for TOPK_V2
737 _op_map[tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM] =
738 make_unique<UnidirectionalSequenceLSTMPrinter>();
739 _op_map[tflite::BuiltinOperator_UNIQUE] = make_unique<UniquePrinter>();
740 _op_map[tflite::BuiltinOperator_WHILE] = make_unique<WhilePrinter>();
741 _op_map[tflite::BuiltinOperator_CUSTOM] = make_unique<CustomOpPrinter>();
744 } // namespace tfldump