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

GraphBuilder for Div operator. More...

#include <Div.h>

Collaboration diagram for tflimport::DivGraphBuilder:

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 Div operator.

Definition at line 30 of file Div.h.

Member Function Documentation

◆ build()

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

Implements tflimport::GraphBuilder.

Definition at line 40 of file Div.cpp.

41{
42 assert(context != nullptr);
43
44 coco::Module *m = context->m();
45 coco::Block *blk = context->block();
46 TensorContext &tensor_context = context->tensor();
47 TensorBags &bags = context->bags();
48
49 IndexVector opinputs = as_index_vector(op->inputs());
50 IndexVector opoutputs = as_index_vector(op->outputs());
51
52 // these are fixed in tflite
53 // input index 0 : numerator
54 // input index 1 : denominator
55 // output index 0 : result
56 assert(opinputs.size() == 2);
57 assert(opoutputs.size() == 1);
58
59 tflite::ActivationFunctionType activation;
60 if (auto *options = op->builtin_options_as_DivOptions())
61 {
62 activation = options->fused_activation_function();
63 }
64 else
65 {
66 activation = tflite::ActivationFunctionType_NONE;
67 }
68
69 // TODO activation, e.g. ReLU
70 assert(activation == tflite::ActivationFunctionType_NONE);
71
72 auto num_idx = opinputs.at(0);
73 auto denom_idx = opinputs.at(1);
74 auto out_idx = opoutputs.at(0);
75
76 const tensor::Shape &num_shape = tensor_context.shape(num_idx);
77 const tensor::Shape &denom_shape = tensor_context.shape(denom_idx);
78 const tensor::Shape &out_shape = tensor_context.shape(out_idx);
79
80 // TODO Now input/output assumes Feature map, but Div should support generic object type
81 // Create an object for an input
82 auto *num_obj = m->entity()->object()->create<coco::FeatureObject>();
83 auto *num_bag = bags.bag(num_idx);
84 num_obj->bag(num_bag);
85 num_obj->layout(coco::FeatureLayouts::BHWC::create(as_feature_shape(num_shape)));
86
87 auto *denom_obj = m->entity()->object()->create<coco::FeatureObject>();
88 auto *denom_bag = bags.bag(denom_idx);
89 denom_obj->bag(denom_bag);
90 denom_obj->layout(coco::FeatureLayouts::BHWC::create(as_feature_shape(denom_shape)));
91
92 // Create an object for an output
93 auto *out_obj = m->entity()->object()->create<coco::FeatureObject>();
94 auto *out_bag = bags.bag(out_idx);
95 out_obj->bag(out_bag);
96 out_obj->layout(coco::FeatureLayouts::BHWC::create(as_feature_shape(out_shape)));
97
98 // Create a Load ops for each input
99 auto coco_load_num = op_builder(m).load(num_obj).pop();
100 auto coco_load_denom = op_builder(m).load(denom_obj).pop();
101
102 // Create a Div op
103 auto coco_div = m->entity()->op()->create<coco::Div>();
104
105 // Link ops
106 coco_div->left(coco_load_num);
107 coco_div->right(coco_load_denom);
108
109 // Create an Eval instruction
110 auto eval_ins = instr_builder(m).eval(out_obj, coco_div);
111
112 // Append the instruction to the block
113 blk->instr()->append(eval_ins);
114}
OpBuilder op_builder(coco::Module *m)
Definition IRBuilder.h:144
InstrBuilder instr_builder(coco::Module *m)
Definition IRBuilder.h:174
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
Op * left(void) const
Definition Op.h:232
A unit of (grouped) instructions.
Definition Block.h:40
InstrList * instr(void)
Definition Block.h:65
void append(Child *child)
Element-wise division.
Definition Ops.h:344
static std::unique_ptr< BHWC > create(const nncc::core::ADT::feature::Shape &shape)
FeatureMap values (used in CNN)
Top-level element of coco IR which represents a neural network.
Definition Module.h:34
coco::Bag * bag(void) const
Definition Object.h:74
nncc::core::ADT::feature::Shape as_feature_shape(const nncc::core::ADT::tensor::Shape &)
Definition tflite.cpp:54
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(), morph::tflite::as_feature_shape(), tflimport::as_index_vector(), tflimport::TensorBags::bag(), tflimport::GraphBuilderContext::bags(), tflimport::GraphBuilderContext::block(), coco::FeatureLayouts::BHWC::create(), InstrBuilder::eval(), coco::Block::instr(), instr_builder(), coco::BinaryOp::left(), OpBuilder::load(), tflimport::GraphBuilderContext::m(), m, op_builder(), OpBuilder::pop(), tflimport::TensorContext::shape(), and tflimport::GraphBuilderContext::tensor().


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