ONE - On-device Neural Engine
Loading...
Searching...
No Matches
mir_tflite::TFLiteOpCreator Class Reference

#include <tflite_op_creator.h>

Public Member Functions

 TFLiteOpCreator (mir::Graph *g)
 
std::vector< mir::Operation::Output * > convertConv2D (const tflite::Conv2DOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertDepthwiseConv2D (const tflite::DepthwiseConv2DOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertConcatenation (const tflite::ConcatenationOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertMaxPool2D (const tflite::Pool2DOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertAveragePool2D (const tflite::Pool2DOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertMean (const tflite::ReducerOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertSoftmax (const tflite::SoftmaxOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertSlice (const tflite::SliceOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertReshape (const tflite::ReshapeOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertFullyConnected (const tflite::FullyConnectedOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertResizeNearestNeighbor (const tflite::ResizeNearestNeighborOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertLogistic (const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertRsqrt (const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertSqrt (const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertSqueeze (const tflite::SqueezeOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertAdd (const tflite::AddOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertSub (const tflite::SubOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertMul (const tflite::MulOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertDiv (const tflite::DivOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertMax (const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertSquaredDifference (const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertTanh (const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertReLU (const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertReLU6 (const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertTransposeConv (const tflite::TransposeConvOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertPad (const tflite::PadOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertTranspose (const tflite::TransposeOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertStridedSlice (const tflite::StridedSliceOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertLeakyReLU (const tflite::LeakyReluOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertShape (const tflite::ShapeOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 
std::vector< mir::Operation::Output * > convertHardSwish (const tflite::HardSwishOptionsT *opts, const std::vector< mir::Operation::Output * > &inputs)
 

Detailed Description

Definition at line 30 of file tflite_op_creator.h.

Constructor & Destructor Documentation

◆ TFLiteOpCreator()

mir_tflite::TFLiteOpCreator::TFLiteOpCreator ( mir::Graph g)
inlineexplicit

Definition at line 33 of file tflite_op_creator.h.

33: _graph(g) {}

Member Function Documentation

◆ convertAdd()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertAdd ( const tflite::AddOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 340 of file tflite_op_creator.cpp.

342{
343 assert(inputs.size() == 2);
344 auto result = createOp<ops::AddOp>(inputs[0], inputs[1])->getOutput(0);
345 return {addFusedActivation(result, opts->fused_activation_function)};
346}
result
Definition infer.py:103

◆ convertAveragePool2D()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertAveragePool2D ( const tflite::Pool2DOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 233 of file tflite_op_creator.cpp.

235{
236 auto input = inputs.at(0);
237
238 const auto &input_shape = input->getShape();
239
241 attributes.window = {opts->filter_height, opts->filter_width};
242 attributes.strides = {opts->stride_h, opts->stride_w};
243 attributes.include_pad = false;
244
245 const auto padding_type = convertPadding(opts->padding);
246 const auto &window_size = attributes.window;
247 const auto &strides = attributes.strides;
248 auto &pad_before = attributes.padding_before;
249 auto &pad_after = attributes.padding_after;
250 calculatePadding(padding_type, input_shape, window_size, strides, pad_before, pad_after);
251
252 auto result = createOp<ops::AvgPool2DOp>(input, attributes);
253 return {addFusedActivation(result->getOutput(0), opts->fused_activation_function)};
254}
std::vector< std::int32_t > window
Definition Attributes.h:42
std::vector< std::int32_t > padding_before
Definition Attributes.h:44
std::vector< std::int32_t > padding_after
Definition Attributes.h:45
std::vector< std::int32_t > strides
Definition Attributes.h:43

References mir::AvgPool2DOpAttributes::include_pad, mir::AvgPool2DOpAttributes::padding_after, mir::AvgPool2DOpAttributes::padding_before, mir::AvgPool2DOpAttributes::strides, and mir::AvgPool2DOpAttributes::window.

◆ convertConcatenation()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertConcatenation ( const tflite::ConcatenationOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 202 of file tflite_op_creator.cpp.

204{
205 auto result = createOp<ops::ConcatOp>(inputs, opts->axis);
206 return {addFusedActivation(result->getOutput(0), opts->fused_activation_function)};
207}

◆ convertConv2D()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertConv2D ( const tflite::Conv2DOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 132 of file tflite_op_creator.cpp.

134{
135 auto input = inputs.at(0);
136 auto kernel = inputs.at(1);
137 auto bias = inputs.at(2);
138
139 mir::Conv2DOpAttributes attributes;
140 attributes.strides = {opts->stride_h, opts->stride_w};
141
142 const auto padding_type = convertPadding(opts->padding);
143 const auto &input_shape = input->getShape();
144 const auto &kernel_shape = kernel->getShape();
145 const auto &strides = attributes.strides;
146 auto &pad_before = attributes.padding_before;
147 auto &pad_after = attributes.padding_after;
148 std::vector<std::int32_t> kernel_size{kernel_shape.dim(1), kernel_shape.dim(2)};
149 calculatePadding(padding_type, input_shape, kernel_size, strides, pad_before, pad_after);
150
152 if (input->getType().isQuantized())
153 {
154 result = createOp<ops::Conv2DOp>(input, kernel, bias, attributes)->getOutput(0);
155 }
156 else // TODO Fuse bias to other backends
157 {
158 result = createOp<ops::Conv2DOp>(input, kernel, attributes)->getOutput(0);
159 result = createOp<ops::AddOp>(result, bias)->getOutput(0);
160 }
161 return {addFusedActivation(result, opts->fused_activation_function)};
162}
Represents an output of a node.
Definition Operation.h:60
std::vector< std::int32_t > padding_after
Definition Attributes.h:33
std::vector< std::int32_t > strides
Definition Attributes.h:31
std::vector< std::int32_t > padding_before
Definition Attributes.h:32

References mir::Conv2DOpAttributes::padding_after, mir::Conv2DOpAttributes::padding_before, and mir::Conv2DOpAttributes::strides.

◆ convertDepthwiseConv2D()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertDepthwiseConv2D ( const tflite::DepthwiseConv2DOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 165 of file tflite_op_creator.cpp.

167{
168 auto input = inputs.at(0);
169 auto kernel = inputs.at(1);
170 auto bias = inputs.at(2);
171
172 // OHWI -> HWIO
173 const std::vector<std::size_t> axis_order{1, 2, 3, 0};
174 kernel = createOp<ops::TransposeOp>(kernel, axis_order)->getOutput(0);
175
176 mir::Conv2DOpAttributes attributes;
177 attributes.strides = {opts->stride_h, opts->stride_w};
178
179 const auto padding_type = convertPadding(opts->padding);
180 const auto &input_shape = input->getShape();
181 const auto &kernel_shape = kernel->getShape();
182 std::vector<std::int32_t> kernel_size{kernel_shape.dim(0), kernel_shape.dim(1)};
183 const auto &strides = attributes.strides;
184 auto &pad_before = attributes.padding_before;
185 auto &pad_after = attributes.padding_after;
186 calculatePadding(padding_type, input_shape, kernel_size, strides, pad_before, pad_after);
187
189 if (input->getType().isQuantized())
190 {
191 result = createOp<ops::DepthwiseConv2DOp>(input, kernel, bias, attributes)->getOutput(0);
192 }
193 else // TODO Fuse bias to other backends
194 {
195 result = createOp<ops::DepthwiseConv2DOp>(input, kernel, attributes)->getOutput(0);
196 result = createOp<ops::AddOp>(result, bias)->getOutput(0);
197 }
198 return {addFusedActivation(result, opts->fused_activation_function)};
199}

References mir::Conv2DOpAttributes::padding_after, mir::Conv2DOpAttributes::padding_before, and mir::Conv2DOpAttributes::strides.

◆ convertDiv()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertDiv ( const tflite::DivOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 367 of file tflite_op_creator.cpp.

369{
370 assert(inputs.size() == 2);
371 auto result = createOp<ops::DivOp>(inputs[0], inputs[1])->getOutput(0);
372 return {addFusedActivation(result, opts->fused_activation_function)};
373}

◆ convertFullyConnected()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertFullyConnected ( const tflite::FullyConnectedOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 405 of file tflite_op_creator.cpp.

407{
408 auto input = inputs.at(0);
409 auto weights = inputs.at(1);
410 auto bias = inputs.at(2);
411
412 // Flatten input to 2-D shape.
413 const auto &input_shape = input->getShape();
414 int32_t outer_size = input_shape.dim(0);
415 int32_t inner_size = input_shape.numElements() / outer_size;
416 auto flatten = createOp<ops::ReshapeOp>(input, Shape{outer_size, inner_size})->getOutput(0);
417
418 // Transpose the weights.
419 const std::vector<std::size_t> axis_order{1, 0};
420 weights = createOp<ops::TransposeOp>(weights, axis_order)->getOutput(0);
421
423 if (input->getType().isQuantized())
424 {
425 result = createOp<ops::FullyConnectedOp>(flatten, weights, bias)->getOutput(0);
426 }
427 else // TODO Fuse bias to other backends
428 {
429 result = createOp<ops::FullyConnectedOp>(flatten, weights)->getOutput(0);
430 result = createOp<ops::AddOp>(result, bias)->getOutput(0);
431 }
432 return {addFusedActivation(result, opts->fused_activation_function)};
433}
Definition Shape.h:28

References mir::Shape::dim(), and mir::Shape::numElements().

◆ convertHardSwish()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertHardSwish ( const tflite::HardSwishOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 645 of file tflite_op_creator.cpp.

647{
648 auto result = createOp<ops::HardSwishOp>(inputs[0])->getOutput(0);
649 return {result};
650}

◆ convertLeakyReLU()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertLeakyReLU ( const tflite::LeakyReluOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 614 of file tflite_op_creator.cpp.

616{
617 auto input = inputs.at(0);
618
619 auto result = createOp<ops::LeakyReluOp>(input, opts->alpha);
620 return {result->getOutput(0)};
621}

◆ convertLogistic()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertLogistic ( const std::vector< mir::Operation::Output * > &  inputs)

Definition at line 537 of file tflite_op_creator.cpp.

538{
539 auto input = inputs.at(0);
540
541 auto result = createOp<ops::SigmoidOp>(input);
542 return {result->getOutput(0)};
543}

◆ convertMax()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertMax ( const std::vector< mir::Operation::Output * > &  inputs)

Definition at line 376 of file tflite_op_creator.cpp.

377{
378 assert(inputs.size() == 2);
379 auto result = createOp<ops::MaxOp>(inputs[0], inputs[1])->getOutput(0);
380 return {result};
381}

◆ convertMaxPool2D()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertMaxPool2D ( const tflite::Pool2DOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 210 of file tflite_op_creator.cpp.

212{
213 auto input = inputs.at(0);
214
215 const auto &input_shape = input->getShape();
216
218 attributes.window = {opts->filter_height, opts->filter_width};
219 attributes.strides = {opts->stride_h, opts->stride_w};
220
221 const auto padding_type = convertPadding(opts->padding);
222 const auto &window_size = attributes.window;
223 const auto &strides = attributes.strides;
224 auto &pad_before = attributes.padding_before;
225 auto &pad_after = attributes.padding_after;
226 calculatePadding(padding_type, input_shape, window_size, strides, pad_before, pad_after);
227
228 auto result = createOp<ops::MaxPool2DOp>(input, attributes);
229 return {addFusedActivation(result->getOutput(0), opts->fused_activation_function)};
230}
std::vector< std::int32_t > window
Definition Attributes.h:54
std::vector< std::int32_t > padding_after
Definition Attributes.h:57
std::vector< std::int32_t > strides
Definition Attributes.h:55
std::vector< std::int32_t > padding_before
Definition Attributes.h:56

References mir::MaxPool2DOpAttributes::padding_after, mir::MaxPool2DOpAttributes::padding_before, mir::MaxPool2DOpAttributes::strides, and mir::MaxPool2DOpAttributes::window.

◆ convertMean()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertMean ( const tflite::ReducerOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 393 of file tflite_op_creator.cpp.

395{
396 auto input = inputs.at(0);
397 mir::Tensor<int32_t> axes_tensor(extractTensor(inputs.at(1)));
398
399 std::vector<int32_t> axes = convertIntTensorToVector<int32_t>(axes_tensor);
400 auto result = createOp<ops::ReduceMeanOp>(input, axes, opts->keep_dims);
401 return {result->getOutput(0)};
402}

◆ convertMul()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertMul ( const tflite::MulOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 358 of file tflite_op_creator.cpp.

360{
361 assert(inputs.size() == 2);
362 auto result = createOp<ops::MulOp>(inputs[0], inputs[1])->getOutput(0);
363 return {addFusedActivation(result, opts->fused_activation_function)};
364}

◆ convertPad()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertPad ( const tflite::PadOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 467 of file tflite_op_creator.cpp.

469{
470 auto input = inputs.at(0);
471 mir::Tensor<int32_t> paddings_tensor(extractTensor(inputs.at(1)));
472
473 const auto &input_shape = input->getShape();
474 const int num_dims = input_shape.rank();
475
476 mir::PadOpAttributes attributes(num_dims);
477 for (int i = 0; i < num_dims; i++)
478 {
479 attributes.padding_before[i] = paddings_tensor.at(mir::Index({i, 0}));
480 attributes.padding_after[i] = paddings_tensor.at(mir::Index({i, 1}));
481 }
482
483 auto result = createOp<ops::PadOp>(input, attributes)->getOutput(0);
484 return {result};
485}

References mir::Tensor< T >::at(), mir::PadOpAttributes::padding_after, mir::PadOpAttributes::padding_before, and mir::Shape::rank().

◆ convertReLU()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertReLU ( const std::vector< mir::Operation::Output * > &  inputs)

Definition at line 497 of file tflite_op_creator.cpp.

498{
499 auto input = inputs.at(0);
500
501 auto result = createOp<ops::ReluOp>(input);
502 return {result->getOutput(0)};
503}

◆ convertReLU6()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertReLU6 ( const std::vector< mir::Operation::Output * > &  inputs)

Definition at line 506 of file tflite_op_creator.cpp.

507{
508 auto input = inputs.at(0);
509
510 auto result = createOp<ops::CappedReluOp>(input, 6);
511 return {result->getOutput(0)};
512}

◆ convertReshape()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertReshape ( const tflite::ReshapeOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 284 of file tflite_op_creator.cpp.

286{
287 auto input = inputs.at(0);
288
289 // TODO: we should also support "-1" values in new_shape, which means that correct
290 // shape values must be calculated. Better do it in the shape inference module.
291 Shape new_shape(opts->new_shape.size());
292 for (int i = 0; i < static_cast<int>(opts->new_shape.size()); ++i)
293 {
294 new_shape.dim(i) = opts->new_shape[i];
295 }
296 auto result = createOp<ops::ReshapeOp>(input, new_shape);
297 return {result->getOutput(0)};
298}

References mir::Shape::dim().

◆ convertResizeNearestNeighbor()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertResizeNearestNeighbor ( const tflite::ResizeNearestNeighborOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 322 of file tflite_op_creator.cpp.

324{
325 if (opts->align_corners)
326 throw std::runtime_error("'align_corners' is not currently supported");
327
328 auto input = inputs.at(0);
329 mir::Tensor<int32_t> size_tensor(extractTensor(inputs.at(1)));
330
331 const auto &input_shape = input->getShape();
332 Shape res_shape{input_shape.dim(0), size_tensor.at(mir::Index{0}), size_tensor.at(mir::Index{1}),
333 input_shape.dim(3)};
334 auto result =
335 createOp<ops::ResizeOp>(input, ops::ResizeOp::ResizeMethod::nearestNeighbor, res_shape);
336 return {result->getOutput(0)};
337}

References mir::Tensor< T >::at(), and mir::Shape::dim().

◆ convertRsqrt()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertRsqrt ( const std::vector< mir::Operation::Output * > &  inputs)

Definition at line 515 of file tflite_op_creator.cpp.

516{
517 auto input = inputs.at(0);
518
519 const float one_value = 1.0f;
520 mir::TensorVariant one_tensor({mir::DataType::FLOAT32, {}}, &one_value);
521 auto one = createOp<ops::ConstantOp>(one_tensor)->getOutput(0);
522 auto sqrt = createOp<ops::SqrtOp>(input)->getOutput(0);
523 auto result = createOp<ops::DivOp>(one, sqrt)->getOutput(0);
524 return {result};
525}

◆ convertShape()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertShape ( const tflite::ShapeOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 624 of file tflite_op_creator.cpp.

626{
627 if (opts->out_type != tflite::TensorType_INT32)
628 {
629 throw std::runtime_error(std::string("SHAPE: Unsupported tensor type: ") +
630 EnumNameTensorType(opts->out_type));
631 }
632
633 const auto &input_shape = inputs[0]->getShape();
634 int32_t rank = input_shape.rank();
635 std::vector<int32_t> data;
636 data.reserve(static_cast<uint64_t>(rank));
637 for (int32_t i = 0; i < rank; i++)
638 data.emplace_back(input_shape.dim(i));
639 mir::TensorVariant tensor({mir::DataType::INT32, {rank}}, data.data());
640 auto result = createOp<ops::ConstantOp>(tensor);
641 return {result->getOutput(0)};
642}

References mir::Shape::dim(), and mir::Shape::rank().

◆ convertSlice()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertSlice ( const tflite::SliceOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 270 of file tflite_op_creator.cpp.

272{
273 auto input = inputs.at(0);
274 mir::Tensor<int32_t> begin_tensor(extractTensor(inputs.at(1)));
275 mir::Tensor<int32_t> size_tensor(extractTensor(inputs.at(2)));
276
277 Shape starts(convertIntTensorToVector<int32_t>(begin_tensor));
278 Shape sizes(convertIntTensorToVector<int32_t>(size_tensor));
279 auto result = createOp<ops::SliceOp>(input, starts, sizes);
280 return {result->getOutput(0)};
281}

◆ convertSoftmax()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertSoftmax ( const tflite::SoftmaxOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 257 of file tflite_op_creator.cpp.

259{
260 auto input = inputs.at(0);
261
262 // Softmax in TFLite is always 2-D.
263 assert(input->getShape().rank() == 2);
264 const int32_t axis = 1;
265 auto result = createOp<ops::SoftmaxOp>(input, axis);
266 return {result->getOutput(0)};
267}

◆ convertSqrt()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertSqrt ( const std::vector< mir::Operation::Output * > &  inputs)

Definition at line 528 of file tflite_op_creator.cpp.

529{
530 auto input = inputs.at(0);
531
532 auto result = createOp<ops::SqrtOp>(input)->getOutput(0);
533 return {result};
534}

◆ convertSquaredDifference()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertSquaredDifference ( const std::vector< mir::Operation::Output * > &  inputs)

Definition at line 384 of file tflite_op_creator.cpp.

385{
386 assert(inputs.size() == 2);
387 auto result = createOp<ops::SubOp>(inputs[0], inputs[1])->getOutput(0);
388 result = createOp<ops::MulOp>(result, result)->getOutput(0);
389 return {result};
390}

◆ convertSqueeze()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertSqueeze ( const tflite::SqueezeOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 456 of file tflite_op_creator.cpp.

458{
459 auto input = inputs.at(0);
460
461 std::vector<int32_t> squeeze_dims(opts->squeeze_dims.begin(), opts->squeeze_dims.end());
462 auto result = createOp<ops::SqueezeOp>(input, squeeze_dims);
463 return {result->getOutput(0)};
464}

◆ convertStridedSlice()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertStridedSlice ( const tflite::StridedSliceOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 558 of file tflite_op_creator.cpp.

560{
561 if (opts->ellipsis_mask != 0)
562 throw std::runtime_error("StridedSlice: parameter 'ellipsis_mask' is not supported.");
563
564 if (opts->new_axis_mask != 0)
565 throw std::runtime_error("StridedSlice: parameter 'new_axis_mask' is not supported.");
566
567 auto input = inputs.at(0);
568 mir::Tensor<int32_t> begin_tensor(extractTensor(inputs.at(1)));
569 mir::Tensor<int32_t> end_tensor(extractTensor(inputs.at(2)));
570 mir::Tensor<int32_t> strides_tensor(extractTensor(inputs.at(3)));
571
572 std::vector<int32_t> begin = convertIntTensorToVector<int32_t>(begin_tensor);
573 std::vector<int32_t> end = convertIntTensorToVector<int32_t>(end_tensor);
574 std::vector<int32_t> strides = convertIntTensorToVector<int32_t>(strides_tensor);
575
576 int32_t begin_mask = opts->begin_mask;
577 int32_t end_mask = opts->end_mask;
578 int32_t shrink_axis_mask = opts->shrink_axis_mask;
579
580 const auto &input_shape = input->getShape();
581 int32_t num_dims = input_shape.rank();
582
583 for (int32_t stride : strides)
584 {
585 if (stride != 1)
586 throw std::runtime_error("StridedSlice: parameter 'strides' is not supported");
587 }
588
589 Shape start(num_dims);
590 Shape size(num_dims);
591 std::vector<int32_t> squeeze_dims;
592 for (int axis = 0; axis < num_dims; axis++)
593 {
594 if (static_cast<uint32_t>(begin_mask) & (1u << static_cast<uint32_t>(axis)))
595 start.dim(axis) = 0;
596 else
597 start.dim(axis) = begin.at(static_cast<uint64_t>(axis));
598
599 if (static_cast<uint32_t>(end_mask) & (1u << static_cast<uint32_t>(axis)))
600 size.dim(axis) = input_shape.dim(axis) - start.dim(axis);
601 else
602 size.dim(axis) = end.at(static_cast<uint64_t>(axis)) - start.dim(axis);
603
604 if (static_cast<uint32_t>(shrink_axis_mask) & (1u << static_cast<uint32_t>(axis)))
605 squeeze_dims.push_back(axis);
606 }
607
608 auto result = createOp<ops::SliceOp>(input, start, size);
609 result = createOp<ops::SqueezeOp>(result->getOutput(0), squeeze_dims);
610 return {result->getOutput(0)};
611}
ShapeIterator end(const Shape &s)
int32_t size[5]
Definition Slice.cpp:35
int32_t begin[5]
Definition Slice.cpp:33

References begin, mir::Shape::dim(), mir::Shape::rank(), and size.

◆ convertSub()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertSub ( const tflite::SubOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 349 of file tflite_op_creator.cpp.

351{
352 assert(inputs.size() == 2);
353 auto result = createOp<ops::SubOp>(inputs[0], inputs[1])->getOutput(0);
354 return {addFusedActivation(result, opts->fused_activation_function)};
355}

◆ convertTanh()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertTanh ( const std::vector< mir::Operation::Output * > &  inputs)

Definition at line 488 of file tflite_op_creator.cpp.

489{
490 auto input = inputs.at(0);
491
492 auto result = createOp<ops::TanhOp>(input);
493 return {result->getOutput(0)};
494}

◆ convertTranspose()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertTranspose ( const tflite::TransposeOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 546 of file tflite_op_creator.cpp.

548{
549 auto input = inputs.at(0);
550 mir::Tensor<int32_t> perm_tensor(extractTensor(inputs.at(1)));
551
552 std::vector<std::size_t> axis_order = convertIntTensorToVector<std::size_t>(perm_tensor);
553 auto result = createOp<ops::TransposeOp>(input, axis_order);
554 return {result->getOutput(0)};
555}

◆ convertTransposeConv()

std::vector< mir::Operation::Output * > mir_tflite::TFLiteOpCreator::convertTransposeConv ( const tflite::TransposeConvOptionsT *  opts,
const std::vector< mir::Operation::Output * > &  inputs 
)

Definition at line 301 of file tflite_op_creator.cpp.

303{
304 mir::Tensor<int32_t> output_shape_tensor(extractTensor(inputs.at(0)));
305 auto kernel = inputs.at(1);
306 auto input = inputs.at(2);
307
308 mir::Deconv2DOpAttributes attributes;
309 attributes.strides = {opts->stride_h, opts->stride_w};
310 Shape output_shape(convertIntTensorToVector<int32_t>(output_shape_tensor));
311
312 // OHWI -> HWOI
313 const std::vector<std::size_t> axis_order{1, 2, 0, 3};
314 kernel = createOp<ops::TransposeOp>(kernel, axis_order)->getOutput(0);
315
316 attributes.padding_type = convertPadding(opts->padding);
317 auto result = createOp<ops::DeConv2DOp>(input, kernel, attributes, output_shape)->getOutput(0);
318 return {result};
319}
const luci_interpreter::RuntimeShape output_shape
ops::PaddingType padding_type
Definition Attributes.h:69
std::vector< std::int32_t > strides
Definition Attributes.h:65

References output_shape, mir::Deconv2DOpAttributes::padding_type, and mir::Deconv2DOpAttributes::strides.


The documentation for this class was generated from the following files: