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

Data Structures

class  AvgPool2DGraphBuilder
 GraphBuilder for AvgPool2D operator. More...
 
class  ConcatenationGraphBuilder
 GraphBuilder for Concatenation operator. More...
 
class  Conv2DGraphBuilder
 GraphBuilder for Conv2D operator. More...
 
class  DepthwiseConv2DGraphBuilder
 GraphBuilder for DepthwiseConv2D operator. More...
 
class  DivGraphBuilder
 GraphBuilder for Div operator. More...
 
class  GraphBuilder
 Parent class of tflite operation graph builders (e.g., Conv2DGraphBuilder) More...
 
class  GraphBuilderContext
 Class to store context to build IR from tflite. More...
 
class  GraphBuilderRegistry
 Class to return graph builder for passed tflite::builtinOperator. More...
 
class  MaxPool2DGraphBuilder
 GraphBuilder for AvgPool2D operator. More...
 
class  ReLU6GraphBuilder
 GraphBuilder for ReLU6 operator. More...
 
class  ReLUGraphBuilder
 GraphBuilder for ReLU operator. More...
 
class  ReshapeGraphBuilder
 GraphBuilder for Reshape operator. More...
 
class  SubGraphBuilder
 GraphBuilder for Sub operator. More...
 
class  TensorBags
 Pre-creates coco:Bags for each operands(tensors) More...
 
class  TensorContext
 Extracts and holds operand(tensor) information such as name, shape, and type. More...
 
class  TflBufferContext
 Class to read and provide buffer information of tflite. More...
 
class  TflOpCodeContext
 Class that holds operator codes and related methods. More...
 

Typedefs

using IndexVector = std::vector< int32_t >
 

Functions

IndexVector as_index_vector (const flatbuffers::Vector< int32_t > *array)
 Converts flatbuffers::Vector to IndexVector.
 
tensor::Shape as_tensor_shape (const flatbuffers::Vector< int32_t > *shape)
 Converts flatbuffers::Vector to nncc::core::ADT::tensor::Shape.
 
void set_module_inputs (coco::Module *m, TensorContext &ctx, TensorBags &bags, const IndexVector &inputs)
 Set module input operands and its information.
 
void set_module_outputs (coco::Module *m, TensorContext &ctx, TensorBags &bags, const IndexVector &outputs)
 Set module output operands and its information.
 
void copy_tensors (GraphBuilderContext *ctx)
 Copy values of tfl tensors into coco::Data if the data was not copied.
 
coco::FeatureObjectbuild_activation (tflite::ActivationFunctionType act, coco::Block *block, coco::FeatureObject *ifm)
 Add coco::Eval for fused activation. This method creates an ofm object, appends Eval(ofm object, RELU(...)) into block, and returns ofm object.
 
coco::Padding2D get_padding (const tensor::Shape &ifm_shape, const int kernel_w, const int kernel_h, tflite::Padding padding, int stride_w, int stride_h, int dilation_w_factor, int dilation_h_factor)
 
coco::Padding2D pool2D_padding (const tflite::Pool2DOptions *options, const tensor::Shape &ifm_shape, const int filter_w, const int filter_h)
 
coco::Padding2D conv2D_padding (const tflite::Conv2DOptions *options, const tensor::Shape &ifm_shape, const tensor::Shape &kernel_shape)
 
coco::Padding2D depthwiseConv2D_padding (const tflite::DepthwiseConv2DOptions *options, const tensor::Shape &ifm_shape, const tensor::Shape &kernel_shape)
 

Typedef Documentation

◆ IndexVector

using tflimport::IndexVector = typedef std::vector<int32_t>

Definition at line 29 of file Convert.h.

Function Documentation

◆ as_index_vector()

IndexVector tflimport::as_index_vector ( const flatbuffers::Vector< int32_t > *  array)

Converts flatbuffers::Vector to IndexVector.

Definition at line 28 of file Convert.cpp.

29{
30 const uint32_t size = array->size();
31
32 std::vector<int32_t> res(size);
33
34 for (uint32_t i = 0; i < size; i++)
35 {
36 res[i] = array->Get(i);
37 }
38
39 return res;
40}
return_type Get(uoffset_t i) const
uoffset_t size() const
int32_t size[5]
Definition Slice.cpp:35

References flatbuffers::Vector< T >::Get(), flatbuffers::Vector< T >::size(), and size.

Referenced by tflimport::AvgPool2DGraphBuilder::build(), tflimport::ConcatenationGraphBuilder::build(), tflimport::DivGraphBuilder::build(), tflimport::MaxPool2DGraphBuilder::build(), tflimport::ReLUGraphBuilder::build(), tflimport::ReLU6GraphBuilder::build(), tflimport::ReshapeGraphBuilder::build(), tflimport::SubGraphBuilder::build(), tflimport::Conv2DGraphBuilder::build(), and tflimport::DepthwiseConv2DGraphBuilder::build().

◆ as_tensor_shape()

tensor::Shape tflimport::as_tensor_shape ( const flatbuffers::Vector< int32_t > *  shape)

Converts flatbuffers::Vector to nncc::core::ADT::tensor::Shape.

Definition at line 42 of file Convert.cpp.

43{
44 const uint32_t rank = shape->size();
45
46 tensor::Shape res;
47
48 res.resize(rank);
49 for (uint32_t axis = 0; axis < rank; ++axis)
50 {
51 res.dim(axis) = shape->Get(axis);
52 }
53
54 return res;
55}
uint32_t & dim(uint32_t axis)
Definition Shape.cpp:42
Shape & resize(uint32_t size)
Definition Shape.cpp:36

References nncc::core::ADT::tensor::Shape::dim(), flatbuffers::Vector< T >::Get(), nncc::core::ADT::tensor::Shape::resize(), and flatbuffers::Vector< T >::size().

Referenced by tflimport::TensorContext::prepare(), and tflimport::TensorBags::prepare().

◆ build_activation()

coco::FeatureObject * tflimport::build_activation ( tflite::ActivationFunctionType  act,
coco::Block block,
coco::FeatureObject ifm 
)

Add coco::Eval for fused activation. This method creates an ofm object, appends Eval(ofm object, RELU(...)) into block, and returns ofm object.

Definition at line 34 of file Activation.cpp.

36{
37 assert(ifm != nullptr && ifm->asFeature() != nullptr); // support feature only in this version
38
39 coco::Module *m = block->module();
40
41 auto shape = ifm->asFeature()->shape();
42
43 // creates output object
44 auto output_obj = m->entity()->object()->create<coco::FeatureObject>();
45 auto output_bag = m->entity()->bag()->create(num_elements(shape));
46 output_obj->bag(output_bag);
47 output_obj->layout(coco::FeatureLayouts::BHWC::create(shape));
48
49 switch (act)
50 {
51 case tflite::ActivationFunctionType::ActivationFunctionType_NONE:
52 {
53 // Create Copy Instr (copying from ifm to output_obj),
54 // redundant layer but optimized by backend
55 auto copy_ins = instr_builder(m).copy(output_obj, ifm);
56
57 // Append the instruction to the block
58 block->instr()->append(copy_ins);
59 break;
60 }
61 case tflite::ActivationFunctionType::ActivationFunctionType_RELU:
62 {
63 // Create Eval(output_obj, ReLU(load(ifm)))
64 auto load_op = op_builder(m).load(ifm).pop();
65 auto relu_op = m->entity()->op()->create<coco::ReLU>();
66 relu_op->arg(load_op);
67
68 auto eval_ins = instr_builder(m).eval(output_obj, relu_op);
69
70 // Append the instruction to the block
71 block->instr()->append(eval_ins);
72 break;
73 }
74 case tflite::ActivationFunctionType::ActivationFunctionType_RELU6:
75 {
76 // Create Eval(output_obj, ReLU6(load(ifm)))
77 auto load_op = op_builder(m).load(ifm).pop();
78 auto relu6_op = m->entity()->op()->create<coco::ReLU6>();
79 relu6_op->arg(load_op);
80
81 auto eval_ins = instr_builder(m).eval(output_obj, relu6_op);
82
83 // Append the instruction to the block
84 block->instr()->append(eval_ins);
85 break;
86 }
87 default:
88 // TODO support other fused activations
89 assert(false);
90 break;
91 }
92
93 return output_obj;
94}
#define relu_op(DATA_TYPE, x, A_VAL, B_VAL)
OpBuilder op_builder(coco::Module *m)
Definition IRBuilder.h:144
InstrBuilder instr_builder(coco::Module *m)
Definition IRBuilder.h:174
coco::Copy * copy(coco::Object *into, coco::Object *from) const
Create "Copy" instruction with given two "Object".
Definition IRBuilder.h:161
coco::Eval * eval(coco::Object *out, coco::Op *op) const
Create "Eval" instruction with a given "Object" and "Op".
Definition IRBuilder.h:162
OpBuilder & load(coco::Object *obj)
Create "Load" op and push it onto the internal stack.
Definition IRBuilder.h:70
coco::Op * pop(void)
Pop op from the internal stack.
Definition IRBuilder.h:116
InstrList * instr(void)
Definition Block.h:65
void append(Child *child)
Module * module(void) const
Definition Entity.h:39
static std::unique_ptr< BHWC > create(const nncc::core::ADT::feature::Shape &shape)
FeatureMap values (used in CNN)
const FeatureShape & shape(void) const
FeatureObject * asFeature(void) override
Top-level element of coco IR which represents a neural network.
Definition Module.h:34
coco::Bag * bag(void) const
Definition Object.h:74
Apply ReLU6 over elements.
Definition Ops.h:268
Apply ReLU over elements.
Definition Ops.h:250
Op * arg(uint32_t n) const final
Return N-th argument.

References coco::DLinkedList< Child, Parent >::Head::append(), coco::UnaryOp::arg(), coco::FeatureObject::asFeature(), coco::Object::bag(), InstrBuilder::copy(), coco::FeatureLayouts::BHWC::create(), InstrBuilder::eval(), coco::Block::instr(), instr_builder(), OpBuilder::load(), m, coco::Entity::module(), op_builder(), OpBuilder::pop(), relu_op, and coco::FeatureObject::shape().

Referenced by tflimport::Conv2DGraphBuilder::build(), and tflimport::DepthwiseConv2DGraphBuilder::build().

◆ conv2D_padding()

coco::Padding2D tflimport::conv2D_padding ( const tflite::Conv2DOptions *  options,
const tensor::Shape ifm_shape,
const tensor::Shape kernel_shape 
)

Definition at line 88 of file Padding.cpp.

90{
91 return get_padding(ifm_shape, kernel_shape.dim(2), kernel_shape.dim(1), /* kernel layout: NHWC */
92 options->padding(), options->stride_w(), options->stride_h(),
93 options->dilation_w_factor(), options->dilation_h_factor());
94}
coco::Padding2D get_padding(const tensor::Shape &ifm_shape, const int kernel_w, const int kernel_h, tflite::Padding padding, int stride_w, int stride_h, int dilation_w_factor, int dilation_h_factor)
Definition Padding.cpp:38

References nncc::core::ADT::tensor::Shape::dim(), and get_padding().

Referenced by tflimport::Conv2DGraphBuilder::build().

◆ copy_tensors()

void tflimport::copy_tensors ( GraphBuilderContext ctx)

Copy values of tfl tensors into coco::Data if the data was not copied.

Definition at line 84 of file Frontend.cpp.

85{
86 auto d = ctx->d();
87
88 // for each bag, check if bag is not allocated but tflite tensor has values
89 for (auto &iter : ctx->bags())
90 {
91 auto tfl_tensor_id = iter.first;
92 auto bag = iter.second;
93
94 auto tfl_buffer = ctx->buffer().tensor_buffer<float>(ctx->graph(), tfl_tensor_id);
95
96 // TODO remove this line when support int32 is ready
97 if (ctx->tensor().type(tfl_tensor_id) == tflite::TensorType::TensorType_INT32)
98 {
99 std::cout << "*** INT32 COPYING IS NOT SUPPORTED ***" << std::endl;
100 continue;
101 }
102
103 assert(ctx->tensor().type(tfl_tensor_id) == tflite::TensorType::TensorType_FLOAT32);
104
105 auto span = d->f32()->weight(bag); // TODO support other type
106
107 if (!(span.data() == nullptr && span.size() == 0)) // already allocated
108 continue;
109
110 if (tfl_buffer.ptr == nullptr || tfl_buffer.len == 0) // no data to copy
111 continue;
112
113 d->f32()->allocate(bag);
114
115 auto ifm_span = d->f32()->weight(bag);
116 for (uint32_t idx = 0; idx < tfl_buffer.len; ++idx)
117 {
118 ifm_span[idx] = tfl_buffer.ptr[idx];
119 }
120 }
121}
TensorContext & tensor()
Definition Context.h:152
TflBufferContext & buffer()
Definition Context.h:154
const tflite::SubGraph * graph()
Definition Context.h:155
const tflite::TensorType & type(uint32_t tensor_id)
Definition Context.h:45
TflBuffer< T > tensor_buffer(const tflite::SubGraph *graph, uint32_t tensor_idx) const
Definition Context.h:101

References tflimport::GraphBuilderContext::bags(), tflimport::GraphBuilderContext::buffer(), tflimport::GraphBuilderContext::d(), tflimport::GraphBuilderContext::graph(), tflimport::GraphBuilderContext::tensor(), tflimport::TflBufferContext::tensor_buffer(), and tflimport::TensorContext::type().

◆ depthwiseConv2D_padding()

coco::Padding2D tflimport::depthwiseConv2D_padding ( const tflite::DepthwiseConv2DOptions *  options,
const tensor::Shape ifm_shape,
const tensor::Shape kernel_shape 
)

Definition at line 96 of file Padding.cpp.

99{
100 return get_padding(ifm_shape, kernel_shape.dim(2), kernel_shape.dim(1), /* kernel layout: NHWC */
101 options->padding(), options->stride_w(), options->stride_h(),
102 options->dilation_w_factor(), options->dilation_h_factor());
103}

References nncc::core::ADT::tensor::Shape::dim(), and get_padding().

Referenced by tflimport::DepthwiseConv2DGraphBuilder::build().

◆ get_padding()

coco::Padding2D tflimport::get_padding ( const tensor::Shape ifm_shape,
const int  kernel_w,
const int  kernel_h,
tflite::Padding  padding,
int  stride_w,
int  stride_h,
int  dilation_w_factor,
int  dilation_h_factor 
)

Compute [top padding + bottom padding] (or [left padding + right padding]). If this returns an even number, top = return value / 2 and bottom = return value - top If this returns an odd number, top = return value / 2 and bottom = return value - top (so, bottom = top + 1)

Code based on https://www.tensorflow.org/api_guides/python/nn#Convolution

Definition at line 38 of file Padding.cpp.

41{
42 assert(stride_w != 0);
43 assert(stride_h != 0);
44 assert(ifm_shape.rank() == 4);
45
54 auto compute_padding = [](tflite::Padding padding, int stride, int dilation_rate, int in_size,
55 int filter_size) {
56 int effective_filter_size = (filter_size - 1) * dilation_rate + 1;
57 if (padding == tflite::Padding_SAME)
58 {
59 if (in_size % stride == 0)
60 return std::max(effective_filter_size - stride, 0);
61 else
62 return std::max(effective_filter_size - (in_size % stride), 0);
63 }
64 else // padding == VALID
65 {
66 return 0;
67 }
68 };
69
70 // ifm shape is from order of NHWC. ifm W = dim(2), ifm H = dim(1)
71 int padding_w = compute_padding(padding, stride_w, dilation_w_factor, ifm_shape.dim(2), kernel_w);
72 int padding_h = compute_padding(padding, stride_h, dilation_h_factor, ifm_shape.dim(1), kernel_h);
73
74 coco::Padding2D coco_padding;
75 coco_padding.top(padding_h / 2).bottom(padding_h - padding_h / 2);
76 coco_padding.left(padding_w / 2).right(padding_w - padding_w / 2);
77
78 return coco_padding;
79}
uint32_t left(void) const
Definition Padding2D.h:49
uint32_t top(void) const
Definition Padding2D.h:41
uint32_t rank(void) const
Definition Shape.cpp:35

References nncc::core::ADT::tensor::Shape::dim(), coco::Padding2D::left(), nncc::core::ADT::tensor::Shape::rank(), and coco::Padding2D::top().

Referenced by conv2D_padding(), depthwiseConv2D_padding(), and pool2D_padding().

◆ pool2D_padding()

coco::Padding2D tflimport::pool2D_padding ( const tflite::Pool2DOptions *  options,
const tensor::Shape ifm_shape,
const int  filter_w,
const int  filter_h 
)

Definition at line 81 of file Padding.cpp.

83{
84 return get_padding(ifm_shape, filter_w, filter_h, options->padding(), options->stride_w(),
85 options->stride_h(), 1, 1);
86}

References get_padding().

Referenced by tflimport::AvgPool2DGraphBuilder::build(), and tflimport::MaxPool2DGraphBuilder::build().

◆ set_module_inputs()

void tflimport::set_module_inputs ( coco::Module m,
TensorContext ctx,
TensorBags bags,
const IndexVector inputs 
)

Set module input operands and its information.

Definition at line 36 of file Frontend.cpp.

38{
39 for (uint32_t n = 0; n < inputs.size(); ++n)
40 {
41 auto const tensor_id = inputs.at(n);
42
43 auto const tensor_name = ctx.name(tensor_id);
44 auto const tensor_shape = ctx.shape(tensor_id);
45 auto const tensor_bag = bags.bag(tensor_id);
46
47 auto input = m->entity()->input()->create(tensor_shape);
48
49 input->name(tensor_name);
50 input->bag(tensor_bag);
51 input->reorder<tensor::LexicalLayout>();
52
53 m->input()->insert(input);
54 }
55}
coco::Bag * bag(int32_t tensor_id)
Definition TensorBags.h:52
const std::string & name(uint32_t tensor_id)
Definition Context.h:43
const tensor::Shape & shape(uint32_t tensor_id)
Definition Context.h:44

References tflimport::TensorBags::bag(), m, tflimport::TensorContext::name(), and tflimport::TensorContext::shape().

◆ set_module_outputs()

void tflimport::set_module_outputs ( coco::Module m,
TensorContext ctx,
TensorBags bags,
const IndexVector outputs 
)

Set module output operands and its information.

Definition at line 60 of file Frontend.cpp.

62{
63 for (uint32_t n = 0; n < outputs.size(); ++n)
64 {
65 auto const tensor_id = outputs.at(n);
66
67 auto const tensor_name = ctx.name(tensor_id);
68 auto const tensor_shape = ctx.shape(tensor_id);
69 auto const tensor_bag = bags.bag(tensor_id);
70
71 auto output = m->entity()->output()->create(tensor_shape);
72
73 output->name(tensor_name);
74 output->bag(tensor_bag);
75 output->reorder<tensor::LexicalLayout>();
76
77 m->output()->insert(output);
78 }
79}

References tflimport::TensorBags::bag(), m, tflimport::TensorContext::name(), and tflimport::TensorContext::shape().