ONE - On-device Neural Engine
Loading...
Searching...
No Matches
tflchef Namespace Reference

Data Structures

class  GeneratedModel
 
class  Logger
 Logger Implementation. More...
 
class  LoggerConfig
 Logger Configuration. More...
 
struct  LoggingContext
 Global logging context. More...
 
struct  RecipeChefContext
 
class  TFliteImport
 Loads TF lite file and provides helpers to access attributes. More...
 
class  TFliteOpAbs
 tflchef operator builder for abs More...
 
class  TFliteOpAdd
 tflchef operator builder for add More...
 
class  TFliteOpAddN
 tflchef operator builder for AddN More...
 
class  TFliteOpArgMax
 tflchef operator builder for ArgMax More...
 
class  TFliteOpArgMin
 tflchef operator builder for ArgMin More...
 
class  TFliteOpAveragePool2D
 tflchef operator builder for AveragePool2D More...
 
class  TFliteOpBatchMatMul
 tflchef operator builder for BATCH_MATMUL More...
 
class  TFliteOpBatchToSpaceND
 tflchef operator builder for BatchToSpaceND More...
 
class  TFliteOpBidirectionalSequenceLSTM
 tflchef operator builder for BidirectionalSequenceLSTM More...
 
class  TFliteOpBroadcastTo
 tflchef operator builder for BroadcastTo More...
 
class  TFliteOpCast
 tflchef operator builder for CAST More...
 
class  TFliteOpCeil
 tflchef operator builder for CEIL More...
 
class  TFliteOpChef
 Interface for each operators to build tflchef. More...
 
class  TFliteOpConcatenation
 tflchef operator builder for Concatenation More...
 
class  TFliteOpConv2D
 tflchef operator builder for Conv2D More...
 
class  TFliteOpCos
 tflchef operator builder for Cos More...
 
class  TFliteOpCumsum
 tflchef operator builder for CumSum More...
 
class  TFliteOpDepthToSpace
 tflchef operator builder for DepthToSpace More...
 
class  TFliteOpDepthwiseConv2D
 tflchef operator builder for DepthwiseConv2D More...
 
class  TFliteOpDequantize
 tflchef operator builder for Dequantize More...
 
class  TFliteOpDiv
 tflchef operator builder for division More...
 
class  TFliteOpELU
 tflchef operator builder for ELU More...
 
class  TFliteOpEqual
 tflchef operator builder for Equal More...
 
class  TFliteOpExp
 tflchef operator builder for Exp More...
 
class  TFliteOpExpandDims
 tflchef operator builder for ExpandDims More...
 
class  TFliteOpFakeQuant
 tflchef operator builder for FakeQuant More...
 
class  TFliteOpFill
 tflchef operator builder for Fill More...
 
class  TFliteOpFloor
 tflchef operator builder for FLOOR More...
 
class  TFliteOpFloorDiv
 tflchef operator builder for floor division More...
 
class  TFliteOpFloorMod
 tflchef operator builder for FLOOR_MOD More...
 
class  TFliteOpFullyConnected
 tflchef operator builder for FullyConnected More...
 
class  TFliteOpGather
 tflchef operator builder for Gather More...
 
class  TFliteOpGatherNd
 tflchef operator builder for GatherNd More...
 
class  TFliteOpGelu
 tflchef operator builder for Gelu More...
 
class  TFliteOpGreater
 tflchef operator builder for Greater More...
 
class  TFliteOpGreaterEqual
 tflchef operator builder for Greater Equal More...
 
class  TFliteOpHardSwish
 tflchef operator builder for Hard Swish More...
 
class  TFliteOpL2Normalize
 tflchef operator builder for L2Normalize More...
 
class  TFliteOpL2Pool2D
 tflchef operator builder for L2_POOL_2D More...
 
class  TFliteOpLeakyRelu
 tflchef operator builder for LeakyReLU More...
 
class  TFliteOpLess
 tflchef operator builder for Less More...
 
class  TFliteOpLessEqual
 tflchef operator builder for LessEqual More...
 
class  TFliteOpLocalResponseNormalization
 tflchef operator builder for LocalResponseNormalization More...
 
class  TFliteOpLog
 tflchef operator builder for Log More...
 
class  TFliteOpLogicalAnd
 tflchef operator builder for LogicalAnd More...
 
class  TFliteOpLogicalNot
 tflchef operator builder for LogicalNot More...
 
class  TFliteOpLogicalOr
 tflchef operator builder for LogicalOr More...
 
class  TFliteOpLogistic
 tflchef operator builder for LOGISTIC More...
 
class  TFliteOpLogSoftmax
 tflchef operator builder for LogSoftmax More...
 
class  TFliteOpMatrixDiag
 tflchef operator builder for MatrixDiag More...
 
class  TFliteOpMatrixSetDiag
 tflchef operator builder for MatrixSetDiag More...
 
class  TFliteOpMaximum
 tflchef operator builder for maximum More...
 
class  TFliteOpMaxPool2D
 tflchef operator builder for MaxPool2D More...
 
class  TFliteOpMean
 tflchef operator builder for mean More...
 
class  TFliteOpMinimum
 tflchef operator builder for minimum More...
 
class  TFliteOpMirrorPad
 tflchef operator builder for MIRROR_PAD More...
 
class  TFliteOpMul
 tflchef operator builder for MUL More...
 
class  TFliteOpNeg
 tflchef operator builder for abs More...
 
class  TFliteOpNonMaxSuppressionV4
 tflchef operator builder for NON_MAX_SUPPRESSION_V4 More...
 
class  TFliteOpNonMaxSuppressionV5
 tflchef operator builder for NON_MAX_SUPPRESSION_V5 More...
 
class  TFliteOpNotEqual
 tflchef operator builder for Not Equal More...
 
class  TFliteOpOneHot
 tflchef operator builder for OneHot More...
 
class  TFliteOpPack
 tflchef operator builder for pack More...
 
class  TFliteOpPad
 tflchef operator builder for PAD More...
 
class  TFliteOpPadV2
 tflchef operator builder for PADV2 More...
 
class  TFliteOpPow
 tflchef operator builder for Pow More...
 
class  TFliteOpPRelu
 tflchef operator builder for PRelu More...
 
class  TFliteOpQuantize
 tflchef operator builder for Quantize More...
 
class  TFliteOpRange
 tflchef operator builder for abs More...
 
class  TFliteOpRank
 tflchef operator builder for rank More...
 
class  TFliteOpReduceAny
 tflchef operator builder for REDUCE_ANY More...
 
class  TFliteOpReduceMax
 tflchef operator builder for Reduce Max More...
 
class  TFliteOpReduceMin
 tflchef operator builder for Reduce Min More...
 
class  TFliteOpReduceProd
 tflchef operator builder for REDUCE_PROD More...
 
class  TFliteOpRegistry
 tflchef operator registry More...
 
class  TFliteOpReLU
 tflchef operator builder for ReLU More...
 
class  TFliteOpReLU0To1
 tflchef operator builder for RELU_0_TO_1 More...
 
class  TFliteOpReLU6
 tflchef operator builder for ReLU6 More...
 
class  TFliteOpReLUN1To1
 tflchef operator builder for RELU_N1_TO_1 More...
 
class  TFliteOpReshape
 tflchef operator builder for Reshape More...
 
class  TFliteOpResizeBilinear
 tflchef operator builder for ResizeBilinear More...
 
class  TFliteOpResizeNearestNeighbor
 tflchef operator builder for ResizeNearestNeighbor More...
 
class  TFliteOpReverseSequence
 tflchef operator builder for ReverseSequence More...
 
class  TFliteOpReverseV2
 tflchef operator builder for ReverseV2 More...
 
class  TFliteOpRound
 tflchef operator builder for Round More...
 
class  TFliteOpRsqrt
 tflchef operator builder for Rsqrt More...
 
class  TFliteOpScatterNd
 tflchef operator builder for ScatterNd More...
 
class  TFliteOpSegmentSum
 tflchef operator builder for SEGMENT_SUM More...
 
class  TFliteOpSelect
 tflchef operator builder for SELECT More...
 
class  TFliteOpSelectV2
 tflchef operator builder for SELECT More...
 
class  TFliteOpShape
 tflchef operator builder for SHAPE More...
 
class  TFliteOpSin
 tflchef operator builder for Sin More...
 
class  TFliteOpSlice
 tflchef operator builder for SLICE More...
 
class  TFliteOpSoftmax
 tflchef operator builder for Softmax More...
 
class  TFliteOpSpaceToBatchND
 tflchef operator builder for SpaceToBatchND More...
 
class  TFliteOpSpaceToDepth
 tflchef operator builder for SpaceToDepth More...
 
class  TFliteOpSparseToDense
 tflchef operator builder for SparseToDense More...
 
class  TFliteOpSplit
 tflchef operator builder for SPLIT More...
 
class  TFliteOpSplitV
 tflchef operator builder for SPLIT_V More...
 
class  TFliteOpSqrt
 tflchef operator builder for Sqrt More...
 
class  TFliteOpSquare
 tflchef operator builder for Square More...
 
class  TFliteOpSquaredDifference
 tflchef operator builder for SquaredDifference More...
 
class  TFliteOpSqueeze
 tflchef operator builder for Squeeze More...
 
class  TFliteOpStridedSlice
 tflchef operator builder for StridedSlice More...
 
class  TFliteOpSub
 tflchef operator builder for Sub More...
 
class  TFliteOpSum
 tflchef operator builder for sum More...
 
class  TFliteOpSVDF
 tflchef operator builder for SVDF More...
 
class  TFliteOpTanh
 tflchef operator builder for Tanh More...
 
class  TFliteOpTile
 tflchef operator builder for Tile More...
 
class  TFliteOpTopKV2
 tflchef operator builder for TOPK_V2 More...
 
class  TFliteOpTranspose
 tflchef operator builder for Transpose More...
 
class  TFliteOpTransposeConv
 tflchef operator builder for TransposeConv More...
 
class  TFliteOpUnidirectionalSequenceLSTM
 tflchef operator builder for UnidirectionalSequenceLSTM More...
 
class  TFliteOpUnique
 tflchef operator builder for Unique More...
 
class  TFliteOpUnpack
 tflchef operator builder for Unpack More...
 
class  TFliteOpWhere
 tflchef operator builder for Where More...
 
class  TFliteOpWhile
 tflchef operator builder for While More...
 
class  TFliteOpZerosLike
 tflchef operator builder for ZerosLike More...
 

Typedefs

using TFliteSubGraphs_t = flatbuffers::Vector< flatbuffers::Offset< tflite::SubGraph > >
 
using TFliteTensors_t = flatbuffers::Vector< flatbuffers::Offset< tflite::Tensor > >
 
using TFliteBuffers_t = flatbuffers::Vector< flatbuffers::Offset< tflite::Buffer > >
 
using TFliteOperators_t = flatbuffers::Vector< flatbuffers::Offset< tflite::Operator > >
 

Functions

GeneratedModel cook (const ModelRecipe &model_recipe)
 
std::unique_ptr< ModelRecipe > generate_recipe (const tflite::Model *model)
 Create ModelRecipe from tflite::Model.
 
bool write_recipe (const std::string &filename, std::unique_ptr< ModelRecipe > &recipe)
 Write ModelRecipe to file with given name.
 
tflchef::TensorType as_tflchef_type (const tflite::TensorType type)
 
tflchef::Activation as_tflchef_activation (const tflite::ActivationFunctionType type)
 
tflchef::Padding as_tflchef_padding (const tflite::Padding padding)
 
tflchef::MirrorPadMode as_tflchef_mirrorpadmode (const tflite::MirrorPadMode mode)
 
tflchef::DimensionType as_tflchef_sparse_dim_type (const tflite::DimensionType type)
 
tflchef::SparseIndexVecType as_tflchef_sparse_idx_vec_type (const tflite::SparseIndexVector type)
 
template<typename DT >
std::vector< DT > extract_buffer (const tflite::Buffer *buffer)
 extract buffer data to std::vector<DT>
 
template<typename T >
std::vector< T > as_index_vector (const flatbuffers::Vector< T > *flat_array)
 
void fill_tensor_to_import (int32_t idx, TFliteImport *import)
 
void fill_two_inputs (const tflite::Operator *op, TFliteImport *import)
 
void set_inputs (TFliteImport *import, tflchef::Operation *operation, const tflite::Operator *op)
 
void set_outputs (TFliteImport *import, tflchef::Operation *operation, const tflite::Operator *op)
 

Typedef Documentation

◆ TFliteBuffers_t

Definition at line 34 of file TFliteImport.h.

◆ TFliteOperators_t

Definition at line 35 of file TFliteImport.h.

◆ TFliteSubGraphs_t

Definition at line 32 of file TFliteImport.h.

◆ TFliteTensors_t

Definition at line 33 of file TFliteImport.h.

Function Documentation

◆ as_index_vector()

◆ as_tflchef_activation()

tflchef::Activation tflchef::as_tflchef_activation ( const tflite::ActivationFunctionType  type)

Definition at line 50 of file Convert.cpp.

51{
52 switch (type)
53 {
54 case tflite::ActivationFunctionType_NONE:
55 return tflchef::NONE;
56 case tflite::ActivationFunctionType_RELU:
57 return tflchef::RELU;
58 case tflite::ActivationFunctionType_RELU_N1_TO_1:
59 return tflchef::RELU_N1_TO_1;
60 case tflite::ActivationFunctionType_RELU6:
61 return tflchef::RELU6;
62 case tflite::ActivationFunctionType_TANH:
63 return tflchef::TANH;
64 case tflite::ActivationFunctionType_SIGN_BIT:
65 return tflchef::SIGN_BIT;
66 default:
67 throw std::runtime_error{"unsupported activation type"};
68 }
69}

Referenced by tflchef::TFliteOpAdd::build(), tflchef::TFliteOpAveragePool2D::build(), tflchef::TFliteOpBidirectionalSequenceLSTM::build(), tflchef::TFliteOpConcatenation::build(), tflchef::TFliteOpConv2D::build(), tflchef::TFliteOpDepthwiseConv2D::build(), tflchef::TFliteOpDiv::build(), tflchef::TFliteOpFullyConnected::build(), tflchef::TFliteOpL2Normalize::build(), tflchef::TFliteOpL2Pool2D::build(), tflchef::TFliteOpMaxPool2D::build(), tflchef::TFliteOpMul::build(), tflchef::TFliteOpSub::build(), tflchef::TFliteOpSVDF::build(), tflchef::TFliteOpTransposeConv::build(), and tflchef::TFliteOpUnidirectionalSequenceLSTM::build().

◆ as_tflchef_mirrorpadmode()

tflchef::MirrorPadMode tflchef::as_tflchef_mirrorpadmode ( const tflite::MirrorPadMode  mode)

Definition at line 84 of file Convert.cpp.

85{
86 switch (mode)
87 {
88 case tflite::MirrorPadMode_REFLECT:
89 return tflchef::REFLECT;
90 case tflite::MirrorPadMode_SYMMETRIC:
91 return tflchef::SYMMETRIC;
92 default:
93 throw std::runtime_error{"Unknown mirrorpad mode"};
94 }
95}

Referenced by tflchef::TFliteOpMirrorPad::build().

◆ as_tflchef_padding()

tflchef::Padding tflchef::as_tflchef_padding ( const tflite::Padding  padding)

Definition at line 71 of file Convert.cpp.

72{
73 switch (padding)
74 {
75 case tflite::Padding_SAME:
76 return tflchef::SAME;
77 case tflite::Padding_VALID:
78 return tflchef::VALID;
79 default:
80 throw std::runtime_error{"unsupported padding"};
81 }
82}

Referenced by tflchef::TFliteOpAveragePool2D::build(), tflchef::TFliteOpConv2D::build(), tflchef::TFliteOpDepthwiseConv2D::build(), tflchef::TFliteOpL2Pool2D::build(), tflchef::TFliteOpMaxPool2D::build(), and tflchef::TFliteOpTransposeConv::build().

◆ as_tflchef_sparse_dim_type()

tflchef::DimensionType tflchef::as_tflchef_sparse_dim_type ( const tflite::DimensionType  type)

Definition at line 97 of file Convert.cpp.

98{
99 switch (type)
100 {
101 case tflite::DimensionType_DENSE:
102 return tflchef::DimensionType::DENSE;
103 case tflite::DimensionType_SPARSE_CSR:
104 return tflchef::DimensionType::SPARSE_CSR;
105 default:
106 throw std::runtime_error("unsupported sparse dimension type");
107 }
108}

Referenced by generate_recipe().

◆ as_tflchef_sparse_idx_vec_type()

tflchef::SparseIndexVecType tflchef::as_tflchef_sparse_idx_vec_type ( const tflite::SparseIndexVector  type)

Definition at line 110 of file Convert.cpp.

111{
112 switch (type)
113 {
114 case tflite::SparseIndexVector_NONE:
115 return tflchef::SparseIndexVecType::SparseIdxVecType_NONE;
116 case tflite::SparseIndexVector_Int32Vector:
117 return tflchef::SparseIndexVecType::INT32VEC;
118 case tflite::SparseIndexVector_Uint16Vector:
119 return tflchef::SparseIndexVecType::UINT16VEC;
120 case tflite::SparseIndexVector_Uint8Vector:
121 return tflchef::SparseIndexVecType::UINT8VEC;
122 default:
123 throw std::runtime_error("unsupported sparse index vector type");
124 }
125}

◆ as_tflchef_type()

tflchef::TensorType tflchef::as_tflchef_type ( const tflite::TensorType  type)

Definition at line 22 of file Convert.cpp.

23{
24 switch (type)
25 {
26 case tflite::TensorType_FLOAT32:
27 return tflchef::FLOAT32;
28 case tflite::TensorType_INT32:
29 return tflchef::INT32;
30 case tflite::TensorType_INT64:
31 return tflchef::INT64;
32 case tflite::TensorType_UINT8:
33 return tflchef::UINT8;
34 case tflite::TensorType_BOOL:
35 return tflchef::BOOL;
36 case tflite::TensorType_INT8:
37 return tflchef::INT8;
38 case tflite::TensorType_INT16:
39 return tflchef::INT16;
40 case tflite::TensorType_FLOAT16:
41 return tflchef::FLOAT16;
42 // TODO handle other types
43 // TensorType_STRING
44 // TensorType_COMPLEX64
45 default:
46 throw std::runtime_error{"unsupported tensor type"};
47 }
48}

Referenced by tflchef::TFliteOpArgMax::build(), tflchef::TFliteOpArgMin::build(), tflchef::TFliteOpCast::build(), tflchef::TFliteOpShape::build(), tflchef::TFliteOpUnique::build(), and generate_recipe().

◆ cook()

GeneratedModel tflchef::cook ( const ModelRecipe &  model_recipe)

Referenced by entry(), and entry_stream().

◆ extract_buffer()

template<typename DT >
std::vector< DT > tflchef::extract_buffer ( const tflite::Buffer *  buffer)

extract buffer data to std::vector<DT>

Definition at line 37 of file Convert.h.

38{
39 assert(buffer->data() != nullptr);
40 auto buffer_length = buffer->data()->size();
41 auto num_elements = buffer_length / sizeof(DT);
42 std::vector<DT> result(num_elements);
43 std::memcpy(result.data(), buffer->data()->data(), buffer_length);
44 return result;
45}

◆ fill_tensor_to_import()

void tflchef::fill_tensor_to_import ( int32_t  idx,
TFliteImport import 
)

Definition at line 24 of file FillerHelper.cpp.

25{
26 const tflite::Tensor *tensor = import->tensors()->Get(idx);
27 if (tensor != nullptr)
28 {
29 if (tensor->type() == tflite::TensorType::TensorType_INT32)
30 {
31 const tflite::Buffer *buffer = import->buffers()->Get(tensor->buffer());
32 if (buffer && buffer->data())
33 {
34 auto vec = extract_buffer<int32_t>(buffer);
35 import->set_tensor_filler(idx, vec);
36 }
37 }
38 else if (tensor->type() == tflite::TensorType::TensorType_FLOAT32)
39 {
40 const tflite::Buffer *buffer = import->buffers()->Get(tensor->buffer());
41 if (buffer && buffer->data())
42 {
43 auto vec = extract_buffer<float>(buffer);
44 import->set_tensor_filler(idx, vec);
45 }
46 }
47 }
48}

Referenced by fill_two_inputs(), tflchef::TFliteOpBroadcastTo::filler(), tflchef::TFliteOpAddN::filler(), tflchef::TFliteOpBidirectionalSequenceLSTM::filler(), tflchef::TFliteOpFullyConnected::filler(), tflchef::TFliteOpLess::filler(), tflchef::TFliteOpNonMaxSuppressionV4::filler(), tflchef::TFliteOpNonMaxSuppressionV5::filler(), tflchef::TFliteOpPadV2::filler(), tflchef::TFliteOpReshape::filler(), tflchef::TFliteOpReverseSequence::filler(), tflchef::TFliteOpReverseV2::filler(), tflchef::TFliteOpScatterNd::filler(), tflchef::TFliteOpSegmentSum::filler(), tflchef::TFliteOpUnidirectionalSequenceLSTM::filler(), and tflchef::TFliteOpWhile::filler().

◆ fill_two_inputs()

void tflchef::fill_two_inputs ( const tflite::Operator *  op,
TFliteImport import 
)

Definition at line 56 of file FillerHelper.cpp.

57{
58 const std::vector<int32_t> &inputs = as_index_vector(op->inputs());
59 assert(inputs.size() == 2);
60
61 fill_tensor_to_import(inputs[0], import);
62 fill_tensor_to_import(inputs[1], import);
63}
void fill_tensor_to_import(int32_t idx, TFliteImport *import)

References as_index_vector(), and fill_tensor_to_import().

Referenced by tflchef::TFliteOpAdd::filler(), tflchef::TFliteOpMaximum::filler(), tflchef::TFliteOpMinimum::filler(), tflchef::TFliteOpMul::filler(), and tflchef::TFliteOpSub::filler().

◆ generate_recipe()

std::unique_ptr< ModelRecipe > tflchef::generate_recipe ( const tflite::Model *  model)

Create ModelRecipe from tflite::Model.

This will build ModelRecipe from tflite::Model First to check operand filler options by scanning all operators, then translate all operands and operators. Last will set network inputs and outputs.

Definition at line 71 of file RecipeChef.cpp.

72{
73 std::unique_ptr<ModelRecipe> model_recipe{new ModelRecipe()};
74
75 TFliteImport tflite_import(model);
76
77 auto const num_subgraph = tflite_import.num_subgraph();
78
79 RecipeChefContext ctx;
80 for (uint32_t n = 0; n < num_subgraph; ++n)
81 {
82 tflite_import.select_sub_graph(n);
83
84 auto tensors = tflite_import.tensors();
85 auto buffers = tflite_import.buffers();
86 auto operators = tflite_import.operators();
87
88 tflchef::Graph *graph = nullptr;
89 if (n != 0)
90 graph = model_recipe->add_graph();
91
92 // operand fillers for adding all operators
93 for (uint32_t i = 0; i < operators->size(); ++i)
94 {
95 const auto *op = operators->Get(i);
96 tflite::BuiltinOperator builtincode = tflite_import.builtin_code(op);
97
98 if (const auto *graph_builder = TFliteOpRegistry::get().lookup(builtincode))
99 {
100 graph_builder->filler(op, &tflite_import, model_recipe.get());
101 }
102 else
103 {
104 std::string opcodename = tflite_import.opcode_name(op);
105 throw std::runtime_error{"Not supported: " + opcodename};
106 }
107 }
108
109 // add all operands(tensors)
110 for (uint32_t i = 0; i < tensors->size(); ++i)
111 {
112 auto tensor = tensors->Get(i);
113
114 // check buffer
115 if (tensor->buffer() >= buffers->size())
116 throw std::runtime_error{"file load failed"};
117
118 ::tflchef::Operand *operand;
119 if (graph != nullptr)
120 operand = graph->add_operand();
121 else
122 operand = model_recipe->add_operand();
123 operand->set_name(mio::tflite::tensor_name(tensor));
124 operand->set_type(as_tflchef_type(tensor->type()));
125 operand->set_is_variable(tensor->is_variable());
126
127 if (tensor->shape())
128 {
129 std::vector<int32_t> dims = as_index_vector(tensor->shape());
130 ::tflchef::TensorShape *shape = operand->mutable_shape();
131 for (auto dim : dims)
132 {
133 shape->add_dim(dim);
134 }
135 }
136
137 // filler for weights, bias and so on
138 std::vector<int32_t> expvalues;
139 std::vector<float> expfvalues;
140 if (tflite_import.get_tensor_filler(i))
141 {
142 tflchef::TensorFiller *filler = operand->mutable_filler();
143 // Note: it is OK to use random weights for functionality validation
144 filler->set_tag("gaussian");
145 filler->add_arg("0.0"); // average
146 filler->add_arg("0.1"); // standard deviation
147 }
148 else if (tflite_import.get_tensor_filler(i, expvalues))
149 {
150 tflchef::TensorFiller *filler = operand->mutable_filler();
151 filler->set_tag("explicit");
152 for (auto value : expvalues)
153 {
154 std::ostringstream ss;
155 ss << value;
156 filler->add_arg(ss.str());
157 }
158 }
159 else if (tflite_import.get_tensor_filler(i, expfvalues))
160 {
161 tflchef::TensorFiller *filler = operand->mutable_filler();
162 filler->set_tag("explicit");
163 for (auto value : expfvalues)
164 {
165 std::ostringstream ss;
166 ss << value;
167 filler->add_arg(ss.str());
168 }
169 }
170
171 auto quant = tensor->quantization();
172 if (quant != nullptr)
173 {
174 // Note: Calling 'operand->mutable_quant()' will create empty 'quant' node
175 // in the recipe file. We want this only when valid parameter exist.
176 if (quant->min() != nullptr && quant->min()->size() > 0)
177 {
178 tflchef::TensorQuantization *chef_quant = operand->mutable_quant();
179 for (uint32_t idx = 0; idx < quant->min()->size(); ++idx)
180 chef_quant->add_min(quant->min()->Get(idx));
181 }
182 if (quant->max() != nullptr && quant->max()->size() > 0)
183 {
184 tflchef::TensorQuantization *chef_quant = operand->mutable_quant();
185 for (uint32_t idx = 0; idx < quant->max()->size(); idx++)
186 chef_quant->add_max(quant->max()->Get(idx));
187 }
188 if (quant->scale() != nullptr && quant->scale()->size() > 0)
189 {
190 tflchef::TensorQuantization *chef_quant = operand->mutable_quant();
191 for (uint32_t idx = 0; idx < quant->scale()->size(); ++idx)
192 chef_quant->add_scale(quant->scale()->Get(idx));
193 }
194 if (quant->zero_point() != nullptr && quant->zero_point()->size() > 0)
195 {
196 tflchef::TensorQuantization *chef_quant = operand->mutable_quant();
197 for (uint32_t idx = 0; idx < quant->zero_point()->size(); ++idx)
198 chef_quant->add_zero_point(quant->zero_point()->Get(idx));
199 }
200 tflchef::TensorQuantization *chef_quant = operand->mutable_quant();
201 chef_quant->set_quantized_dimension(quant->quantized_dimension());
202 }
203
204 auto sparsity = tensor->sparsity();
205 if (sparsity != nullptr)
206 {
207 tflchef::TensorSparsity *chef_sparsity = operand->mutable_sparsity();
208 // traversal_order
209 auto chef_traversal_order = chef_sparsity->mutable_traversal_order();
210 for (const auto &to : *(sparsity->traversal_order()))
211 {
212 chef_traversal_order->add_dim(to);
213 }
214 // block_map
215 auto chef_block_map = chef_sparsity->mutable_block_map();
216 for (const auto &bm : *(sparsity->block_map()))
217 {
218 chef_block_map->add_dim(bm);
219 }
220 // dim_metadata
221 for (const auto &dm : *(sparsity->dim_metadata()))
222 {
223 auto chef_dm = chef_sparsity->add_dim_metadata();
224 // format
225 chef_dm->set_format(as_tflchef_sparse_dim_type(dm->format()));
226 // dense_size
227 chef_dm->set_dense_size(dm->dense_size());
228 // array_segments
229 auto chef_array_segments = chef_dm->mutable_array_segments();
230 switch (dm->array_segments_type())
231 {
232 case tflite::SparseIndexVector_NONE:
233 // DO NOTHING
234 break;
235 case tflite::SparseIndexVector_Int32Vector:
236 for (const auto &as : *(dm->array_segments_as_Int32Vector()->values()))
237 {
238 chef_array_segments->add_dim(as);
239 }
240 break;
241 case tflite::SparseIndexVector_Uint16Vector:
242 for (const auto &as : *(dm->array_segments_as_Uint16Vector()->values()))
243 {
244 chef_array_segments->add_dim(as);
245 }
246 break;
247 case tflite::SparseIndexVector_Uint8Vector:
248 for (const auto &as : *(dm->array_segments_as_Uint8Vector()->values()))
249 {
250 chef_array_segments->add_dim(as);
251 }
252 break;
253 default:
254 throw std::runtime_error("unsupported sparse index vector type");
255 }
256 // array_indices
257 auto chef_array_indices = chef_dm->mutable_array_indices();
258 switch (dm->array_indices_type())
259 {
260 case tflite::SparseIndexVector_NONE:
261 // DO NOTHING
262 break;
263 case tflite::SparseIndexVector_Int32Vector:
264 for (const auto &as : *(dm->array_indices_as_Int32Vector()->values()))
265 {
266 chef_array_indices->add_dim(as);
267 }
268 break;
269 case tflite::SparseIndexVector_Uint16Vector:
270 for (const auto &as : *(dm->array_indices_as_Uint16Vector()->values()))
271 {
272 chef_array_indices->add_dim(as);
273 }
274 break;
275 case tflite::SparseIndexVector_Uint8Vector:
276 for (const auto &as : *(dm->array_indices_as_Uint8Vector()->values()))
277 {
278 chef_array_indices->add_dim(as);
279 }
280 break;
281 default:
282 throw std::runtime_error("unsupported sparse index vector type");
283 }
284 }
285 }
286
287 auto shape_signature = tensor->shape_signature();
288 if (shape_signature != nullptr)
289 {
290 tflchef::ShapeSignature *chef_shape_signature = operand->mutable_shape_signature();
291 for (uint32_t j = 0; j < shape_signature->size(); ++j)
292 {
293 chef_shape_signature->add_dim(shape_signature->Get(j));
294 }
295 }
296 }
297
298 // add all operators
299 for (uint32_t i = 0; i < operators->size(); ++i)
300 {
301 const auto *op = operators->Get(i);
302 tflite::BuiltinOperator builtincode = tflite_import.builtin_code(op);
303
304 if (const auto *graph_builder = TFliteOpRegistry::get().lookup(builtincode))
305 {
306 tflchef::Operation *operation =
307 graph ? graph->add_operation() : model_recipe->add_operation();
308 ctx.tflop = op;
309 ctx.chefop = operation;
310 graph_builder->build(&ctx);
311
312 // common for all operators: inputs, outputs
313 set_inputs(&tflite_import, operation, op);
314 set_outputs(&tflite_import, operation, op);
315 }
316 else
317 {
318 std::string opcodename = tflite_import.opcode_name(op);
319 throw std::runtime_error{"Not supported: " + opcodename};
320 }
321 }
322
323 // network inputs/outputs
324 const std::vector<int32_t> &inputs = tflite_import.inputs();
325 const std::vector<int32_t> &outputs = tflite_import.outputs();
326
327 for (const auto input : inputs)
328 {
329 auto tensor = tensors->Get(input);
330 std::string name = mio::tflite::tensor_name(tensor);
331
332 if (graph != nullptr)
333 graph->add_input(name);
334 else
335 model_recipe->add_input(name);
336 }
337 for (const auto output : outputs)
338 {
339 auto tensor = tensors->Get(output);
340 std::string name = mio::tflite::tensor_name(tensor);
341
342 if (graph != nullptr)
343 graph->add_output(name);
344 else
345 model_recipe->add_output(name);
346 }
347 }
348
349 return std::move(model_recipe);
350}
std::vector< T > as_index_vector(const flatbuffers::Vector< T > *flat_array)
Definition Convert.h:43
void set_outputs(CircleImport *import, circlechef::Operation *operation, const circle::Operator *op)
void set_inputs(CircleImport *import, circlechef::Operation *operation, const circle::Operator *op)
const char * tensor_name(const ::tflite::Tensor *tensor)
Definition Helper.cpp:92
tflchef::DimensionType as_tflchef_sparse_dim_type(const tflite::DimensionType type)
Definition Convert.cpp:97
tflchef::TensorType as_tflchef_type(const tflite::TensorType type)
Definition Convert.cpp:22
int32_t size[5]
Definition Slice.cpp:35

References as_index_vector(), as_tflchef_sparse_dim_type(), as_tflchef_type(), tflchef::TFliteImport::buffers(), tflchef::TFliteImport::builtin_code(), tflchef::RecipeChefContext::chefop, tflchef::TFliteOpRegistry::get(), souschef::TensorFiller::get_tensor_filler(), tflchef::TFliteImport::inputs(), tflchef::TFliteImport::num_subgraph(), tflchef::TFliteImport::opcode_name(), tflchef::TFliteImport::operators(), tflchef::TFliteImport::outputs(), tflchef::TFliteImport::select_sub_graph(), set_inputs(), set_outputs(), mio::tflite::tensor_name(), tflchef::TFliteImport::tensors(), and tflchef::RecipeChefContext::tflop.

Referenced by entry().

◆ set_inputs()

void tflchef::set_inputs ( TFliteImport import,
tflchef::Operation *  operation,
const tflite::Operator *  op 
)

Definition at line 32 of file RecipeChef.cpp.

33{
34 auto tensors = import->tensors();
35 const std::vector<int32_t> &inputs = as_index_vector(op->inputs());
36
37 for (auto input : inputs)
38 {
39 if (input == -1)
40 {
41 operation->add_input("");
42 }
43 else
44 {
45 auto tensor = tensors->Get(input);
46 std::string name = mio::tflite::tensor_name(tensor);
47 operation->add_input(name);
48 }
49 }
50}

References as_index_vector(), and mio::tflite::tensor_name().

Referenced by generate_recipe().

◆ set_outputs()

void tflchef::set_outputs ( TFliteImport import,
tflchef::Operation *  operation,
const tflite::Operator *  op 
)

Definition at line 52 of file RecipeChef.cpp.

53{
54 auto tensors = import->tensors();
55 const std::vector<int32_t> &outputs = as_index_vector(op->outputs());
56
57 for (auto output : outputs)
58 {
59 auto tensor = tensors->Get(output);
60 std::string name = mio::tflite::tensor_name(tensor);
61 operation->add_output(name);
62 }
63}

References as_index_vector(), and mio::tflite::tensor_name().

Referenced by generate_recipe().

◆ write_recipe()

bool tflchef::write_recipe ( const std::string &  filename,
std::unique_ptr< ModelRecipe > &  recipe 
)

Write ModelRecipe to file with given name.

Definition at line 352 of file RecipeChef.cpp.

353{
354 std::fstream fo(filename, std::ios::binary | std::ios::out);
355
356 if (!fo.is_open())
357 {
358 throw std::runtime_error{"file store failed"};
359 }
360
361 // Note: SerializeToString() or SerializeToOstream() writes in binary mode
362 // DebugString() and Utf8DebugString() will print as a human readable text
363 fo << recipe->Utf8DebugString();
364
365 fo.close();
366
367 return true;
368}

Referenced by entry().