ONE - On-device Neural Engine
Loading...
Searching...
No Matches
ANNBinder Class Reference

A bridge between ann::Module and coco::Block. More...

#include <Binder.h>

Public Member Functions

 ANNBinder (coco::Block *block, std::unique_ptr< ann::Module > &&module)
 
const coco::Blockblock (void) const
 
coco::Blockblock (void)
 
const ann::Modulemodule (void) const
 
std::set< coco::Bag * > bags (void) const
 Return the set of bags that the current ANN subnet accesses.
 
template<typename T >
ann::OperandID addOperand (void)
 
template<typename T >
ann::OperandID addOperand (const nncc::core::ADT::tensor::Shape &shape)
 
template<typename T >
ann::OperandID addOperand (const coco::FeatureObject *obj)
 
template<typename T >
ann::OperandID addOperand (const coco::KernelObject *obj)
 
template<typename T >
void setOperand (const ann::OperandID &id, const T &value)
 Set scalar weight.
 
template<typename It >
void setOperand (const ann::OperandID &id, It beg, It end)
 Set non-scalar weight.
 
void addOperation (ann::Operation::Code code, std::initializer_list< ann::OperandID > inputs, std::initializer_list< ann::OperandID > outputs)
 
template<typename It >
void identifyInputs (It beg, It end)
 Identify a sequence of coco::Bag * as subnet's inputs.
 
template<typename T >
void identifyInputs (T &&values)
 
template<typename It >
void identifyOutputs (It beg, It end)
 Identify a sequence of coco::Bag * as subnet's outputs.
 
template<typename T >
void identifyOutputs (T &&values)
 
coco::Baginput (uint32_t n) const
 
coco::Bagoutput (uint32_t n) const
 
bool associated (coco::Bag *b) const
 Return true if a given bag has an associated operand in ANN IR.
 
ann::OperandID operand (coco::Bag *b) const
 Return operand ID associated with a given bag.
 

Detailed Description

A bridge between ann::Module and coco::Block.

Definition at line 31 of file Binder.h.

Constructor & Destructor Documentation

◆ ANNBinder()

ANNBinder::ANNBinder ( coco::Block block,
std::unique_ptr< ann::Module > &&  module 
)
inline

Definition at line 34 of file Binder.h.

35 : _block{block}, _module{std::move(module)}
36 {
37 // DO NOTHING
38 }
const ann::Module * module(void) const
Definition Binder.h:45
const coco::Block * block(void) const
Definition Binder.h:41

Member Function Documentation

◆ addOperand() [1/4]

template<typename T >
ann::OperandID ANNBinder::addOperand ( const coco::FeatureObject obj)
inline

Definition at line 75 of file Binder.h.

76 {
77 auto bag = obj->bag();
78 assert(bag != nullptr);
79
80 auto it = _operands.find(bag);
81
82 if (it != _operands.end())
83 {
84 return it->second;
85 }
86
87 auto operand = addOperand<T>(morph::nnapi::as_tensor_shape(obj->shape()));
88 _operands[obj->bag()] = operand;
89 return operand;
90 };
ann::OperandID operand(coco::Bag *b) const
Return operand ID associated with a given bag.
Definition Binder.h:200
nncc::core::ADT::tensor::Shape as_tensor_shape(const nncc::core::ADT::feature::Shape &)
Definition nnapi.cpp:28

References morph::nnapi::as_tensor_shape(), and operand().

◆ addOperand() [2/4]

template<typename T >
ann::OperandID ANNBinder::addOperand ( const coco::KernelObject obj)
inline

Definition at line 92 of file Binder.h.

93 {
94 auto bag = obj->bag();
95 assert(bag != nullptr);
96
97 auto it = _operands.find(bag);
98
99 if (it != _operands.end())
100 {
101 return it->second;
102 }
103
104 auto operand = addOperand<T>(morph::nnapi::as_tensor_shape(obj->shape()));
105 _operands[obj->bag()] = operand;
106 return operand;
107 };

References morph::nnapi::as_tensor_shape(), and operand().

◆ addOperand() [3/4]

template<typename T >
ann::OperandID ANNBinder::addOperand ( const nncc::core::ADT::tensor::Shape shape)
inline

Definition at line 69 of file Binder.h.

70 {
71 return _module->operand()->create(ann::dtype<T>(), shape);
72 }

◆ addOperand() [4/4]

template<typename T >
ann::OperandID ANNBinder::addOperand ( void  )
inline

Definition at line 64 of file Binder.h.

65 {
66 return _module->operand()->create(ann::dtype<T>());
67 };

◆ addOperation()

void ANNBinder::addOperation ( ann::Operation::Code  code,
std::initializer_list< ann::OperandID inputs,
std::initializer_list< ann::OperandID outputs 
)
inline

Definition at line 128 of file Binder.h.

130 {
131 _module->operation()->create(code, inputs, outputs);
132 }

◆ associated()

bool ANNBinder::associated ( coco::Bag b) const
inline

Return true if a given bag has an associated operand in ANN IR.

Definition at line 194 of file Binder.h.

194{ return _operands.find(b) != _operands.end(); }

Referenced by operand().

◆ bags()

std::set< coco::Bag * > ANNBinder::bags ( void  ) const
inline

Return the set of bags that the current ANN subnet accesses.

Definition at line 51 of file Binder.h.

52 {
53 std::set<coco::Bag *> res;
54
55 for (auto it = _operands.begin(); it != _operands.end(); ++it)
56 {
57 res.insert(it->first);
58 }
59
60 return res;
61 }

◆ block() [1/2]

coco::Block * ANNBinder::block ( void  )
inline

Definition at line 42 of file Binder.h.

42{ return _block; }

◆ block() [2/2]

const coco::Block * ANNBinder::block ( void  ) const
inline

Definition at line 41 of file Binder.h.

41{ return _block; }

◆ identifyInputs() [1/2]

template<typename It >
void ANNBinder::identifyInputs ( It  beg,
It  end 
)
inline

Identify a sequence of coco::Bag * as subnet's inputs.

NOTE 1. This method takes input iterator over coco::Bag * values NOTE 2. All the identifyInputs class except the last one will be ignored if there are multiple identifyInputs calls

Definition at line 142 of file Binder.h.

143 {
144 _inputs.clear();
145 _module->input()->clear();
146
147 for (auto it = beg; it != end; ++it)
148 {
149 auto const bag = *it;
150 _inputs.emplace_back(*it);
151 _module->input()->emplace_back(_operands.at(bag));
152 }
153 }
ShapeIterator end(const Shape &s)

Referenced by identifyInputs().

◆ identifyInputs() [2/2]

template<typename T >
void ANNBinder::identifyInputs ( T &&  values)
inline

Definition at line 155 of file Binder.h.

156 {
157 identifyInputs(std::begin(values), std::end(values));
158 }
void identifyInputs(It beg, It end)
Identify a sequence of coco::Bag * as subnet's inputs.
Definition Binder.h:142

References identifyInputs().

◆ identifyOutputs() [1/2]

template<typename It >
void ANNBinder::identifyOutputs ( It  beg,
It  end 
)
inline

Identify a sequence of coco::Bag * as subnet's outputs.

NOTE 1. This method takes input iterator over coco::Bag * values NOTE 2. All the identifyOutputs class except the last one will be ignored if there are multiple identifyOutputs calls

Definition at line 168 of file Binder.h.

169 {
170 _outputs.clear();
171 _module->output()->clear();
172
173 for (auto it = beg; it != end; ++it)
174 {
175 auto const bag = *it;
176 _outputs.emplace_back(bag);
177 _module->output()->emplace_back(_operands.at(bag));
178 }
179 }

Referenced by identifyOutputs().

◆ identifyOutputs() [2/2]

template<typename T >
void ANNBinder::identifyOutputs ( T &&  values)
inline

Definition at line 181 of file Binder.h.

182 {
183 identifyOutputs(std::begin(values), std::end(values));
184 }
void identifyOutputs(It beg, It end)
Identify a sequence of coco::Bag * as subnet's outputs.
Definition Binder.h:168

References identifyOutputs().

◆ input()

coco::Bag * ANNBinder::input ( uint32_t  n) const
inline

Definition at line 187 of file Binder.h.

187{ return _inputs.at(n); }

Referenced by enco::SubnetBlockCompiler::compile().

◆ module()

const ann::Module * ANNBinder::module ( void  ) const
inline

Definition at line 45 of file Binder.h.

45{ return _module.get(); }

Referenced by enco::SubnetStructBuilder::build(), and enco::SubnetBlockCompiler::compile().

◆ operand()

ann::OperandID ANNBinder::operand ( coco::Bag b) const
inline

Return operand ID associated with a given bag.

Note
The behavior of operand(b) is defined only when associated(b) holds.

Definition at line 200 of file Binder.h.

201 {
202 assert(associated(b));
203 return _operands.at(b);
204 }
bool associated(coco::Bag *b) const
Return true if a given bag has an associated operand in ANN IR.
Definition Binder.h:194

References associated().

Referenced by addOperand(), and addOperand().

◆ output()

coco::Bag * ANNBinder::output ( uint32_t  n) const
inline

Definition at line 188 of file Binder.h.

188{ return _outputs.at(n); }

Referenced by enco::SubnetBlockCompiler::compile().

◆ setOperand() [1/2]

template<typename T >
void ANNBinder::setOperand ( const ann::OperandID id,
const T &  value 
)
inline

Set scalar weight.

Definition at line 111 of file Binder.h.

112 {
113 static_assert(std::is_arithmetic<T>::value, "T should be arithmetic");
114 auto weight = _module->weight()->create();
115 weight->fill(value);
116 _module->operand()->at(id)->weight(weight);
117 }

◆ setOperand() [2/2]

template<typename It >
void ANNBinder::setOperand ( const ann::OperandID id,
It  beg,
It  end 
)
inline

Set non-scalar weight.

Definition at line 120 of file Binder.h.

121 {
122 auto weight = _module->weight()->create();
123 weight->fill(beg, end);
124 _module->operand()->at(id)->weight(weight);
125 }

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