ONE - On-device Neural Engine
Loading...
Searching...
No Matches
tflimport::ReshapeGraphBuilder Class Reference

GraphBuilder for Reshape operator. More...

#include <Reshape.h>

Collaboration diagram for tflimport::ReshapeGraphBuilder:

Public Member Functions

void build (const tflite::Operator *op, GraphBuilderContext *) const override
 
- Public Member Functions inherited from tflimport::GraphBuilder
virtual bool validate (const tflite::Operator *) const
 
virtual ~GraphBuilder ()
 

Detailed Description

GraphBuilder for Reshape operator.

Definition at line 30 of file Reshape.h.

Member Function Documentation

◆ build()

void tflimport::ReshapeGraphBuilder::build ( const tflite::Operator *  op,
GraphBuilderContext context 
) const
overridevirtual

Implements tflimport::GraphBuilder.

Definition at line 37 of file Reshape.cpp.

38{
39 assert(context != nullptr); // check if init(..) is called
40
41 coco::Module *m = context->m();
42 coco::Block *blk = context->block();
43 TensorBags &bags = context->bags();
44
45 IndexVector opinputs = as_index_vector(op->inputs());
46 IndexVector opoutputs = as_index_vector(op->outputs());
47
48 // these are fixed in tflite
49 // input index 0 : input feature
50 // input index 1 : output shape (int32_t), (optional or not, is not clear)
51 // output index 0 : output feature
52 assert(opinputs.size() == 1 || opinputs.size() == 2);
53 assert(opoutputs.size() == 1);
54
55 // Note: there are actually 3 places where we can get output shape from
56 // current TF lite implementation. From output operand shape, second input,
57 // and ReshapeOption (new_shape). Here we use output operand shape
58 int ifm_idx = opinputs.at(0);
59 int ofm_idx = opoutputs.at(0);
60
61 auto ifm_bag = bags.bag(ifm_idx);
62 auto ofm_bag = bags.bag(ofm_idx);
63
64 // TODO: move to InstrBuilder as 'shuffle_elements()'
65 // Create a 1:1 shuffle instruction from ifm into ofm
66 // Note: Reshape is change of shape information and there is no value change
67 // in the bag itself. We implement this as just make a element wise copy of
68 // the bag from input to output. So there is no need of 'reshape' operator
69 auto shuffle_ins = m->entity()->instr()->create<coco::Shuffle>();
70 auto num_elem = ifm_bag->size();
71
72 assert(num_elem == ofm_bag->size());
73
74 shuffle_ins->from(ifm_bag);
75 shuffle_ins->into(ofm_bag);
76
77 for (uint32_t n = 0; n < num_elem; ++n)
78 {
79 const auto from = coco::ElemID(n);
80 const auto into = coco::ElemID(n);
81
82 shuffle_ins->insert(from, into);
83 }
84
85 // Append the instruction
86 blk->instr()->append(shuffle_ins);
87}
A unit of (grouped) instructions.
Definition Block.h:40
InstrList * instr(void)
Definition Block.h:65
void append(Child *child)
Top-level element of coco IR which represents a neural network.
Definition Module.h:34
Generic element transfer.
Definition Instrs.h:116
uint32_t size(void) const
Return the number of Element-wise transfers.
Definition Shuffle.cpp:22
std::vector< int32_t > IndexVector
Definition Convert.h:29
IndexVector as_index_vector(const flatbuffers::Vector< int32_t > *array)
Converts flatbuffers::Vector to IndexVector.
Definition Convert.cpp:28

References coco::DLinkedList< Child, Parent >::Head::append(), tflimport::as_index_vector(), tflimport::TensorBags::bag(), tflimport::GraphBuilderContext::bags(), tflimport::GraphBuilderContext::block(), coco::Block::instr(), tflimport::GraphBuilderContext::m(), m, and coco::Shuffle::size().


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