ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator > Class Template Referenceabstract

#include <BackendContext.h>

Collaboration diagram for onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >:

Public Member Functions

 BackendContext (const Backend *backend, ContextData &&data, std::shared_ptr< ITensorRegistry > tensor_registry=nullptr, std::shared_ptr< T_TensorBuilder > tensor_builder=nullptr, std::shared_ptr< T_ConstantInitializer > constant_initializer=nullptr, std::shared_ptr< T_KernelGenerator > kernel_gen=nullptr)
 
FunctionMap genKernels () override
 
- Public Member Functions inherited from onert::backend::BackendContext
 BackendContext (const Backend *backend, ContextData &&data, std::shared_ptr< ITensorRegistry > tensor_registry=nullptr)
 
virtual ~BackendContext ()=default
 
const Backendbackend () const
 
const ir::Graphgraph () const
 
const util::Set< ir::OperandIndex > & external_operands () const
 
const ContextDatadata () const
 
virtual ITensorRegistrygenTensors ()=0
 

Data Fields

std::shared_ptr< T_TensorBuilder > tensor_builder
 
std::shared_ptr< T_ConstantInitializer > constant_initializer
 
std::shared_ptr< T_KernelGenerator > kernel_gen
 
- Data Fields inherited from onert::backend::BackendContext
std::shared_ptr< ITensorRegistrytensor_registry
 

Protected Member Functions

void initConsts ()
 
virtual void registerTensorInfo (const ir::OperandIndex &ind, const ir::OperandInfo &info)=0
 
void planTensors ()
 

Additional Inherited Members

- Protected Attributes inherited from onert::backend::BackendContext
const Backend_backend {nullptr}
 
ContextData _data
 

Detailed Description

template<typename T_TensorBuilder, typename T_ConstantInitializer, typename T_KernelGenerator>
class onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >

Definition at line 31 of file BackendContext.h.

Constructor & Destructor Documentation

◆ BackendContext()

template<typename T_TensorBuilder , typename T_ConstantInitializer , typename T_KernelGenerator >
onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::BackendContext ( const Backend backend,
ContextData &&  data,
std::shared_ptr< ITensorRegistry tensor_registry = nullptr,
std::shared_ptr< T_TensorBuilder >  tensor_builder = nullptr,
std::shared_ptr< T_ConstantInitializer >  constant_initializer = nullptr,
std::shared_ptr< T_KernelGenerator >  kernel_gen = nullptr 
)
inline

Definition at line 34 of file BackendContext.h.

42 {
43 }
std::shared_ptr< ITensorRegistry > tensor_registry
const ContextData & data() const
const Backend * backend() const
std::shared_ptr< T_TensorBuilder > tensor_builder
std::shared_ptr< T_ConstantInitializer > constant_initializer
std::shared_ptr< T_KernelGenerator > kernel_gen

Member Function Documentation

◆ genKernels()

template<typename T_TensorBuilder , typename T_ConstantInitializer , typename T_KernelGenerator >
FunctionMap onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::genKernels ( )
inlineoverridevirtual

Implements onert::backend::BackendContext.

Definition at line 45 of file BackendContext.h.

46 {
47 FunctionMap ret;
48
49 // kernel_gen
50 for (auto &&op_ind : _data.op_order)
51 {
52 auto fn_seq = kernel_gen->generate(op_ind);
53 ret.emplace(op_ind, std::move(fn_seq));
54 }
55
56 tensor_builder->allocate();
57 initConsts();
58
59 // NOTE For memory optimization, we want to free some operand data
60 const_cast<ir::Graph &>(*_data.graph)
61 .operands()
62 .iterate([&](const ir::OperandIndex &, ir::Operand &obj) { obj.releaseData(); });
63
64 for (auto &&it : ret)
65 {
66 auto &fn_seq = it.second;
67 fn_seq->iterate([&](exec::IFunction &ifunc) {
68 ifunc.prepare();
69 tensor_builder->postFunctionPrepare();
70 });
71 }
72
73 return ret;
74 }
std::unordered_map< ir::OperationIndex, std::unique_ptr< exec::FunctionSequence > > FunctionMap
::onert::util::Index< uint32_t, OperandIndexTag > OperandIndex
Definition Index.h:33
std::unique_ptr< ir::Graph > graph

References onert::backend::BackendContext::_data, onert::backend::ContextData::graph, onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::initConsts(), onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::kernel_gen, onert::backend::ContextData::op_order, onert::exec::IFunction::prepare(), and onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::tensor_builder.

◆ initConsts()

template<typename T_TensorBuilder , typename T_ConstantInitializer , typename T_KernelGenerator >
void onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::initConsts ( )
inlineprotected

Definition at line 77 of file BackendContext.h.

78 {
79 _data.graph->operations().iterate([&](const ir::OperationIndex &, const ir::IOperation &op) {
80 op.accept(*constant_initializer);
81 });
82
83 _data.graph->operands().iterate([&](const ir::OperandIndex &ind, const ir::Operand &operand) {
84 if (_data.external_operands.contains(ind) || !operand.isConstant())
85 return;
86 const auto &obj = graph()->operands().at(ind);
87 if (obj.isConstant() && !constant_initializer->exist(ind))
88 {
89 constant_initializer->registerDefaultInitializer(ind, obj);
90 }
91 });
92
94 }
const ir::Graph * graph() const
const Operands & operands() const override
Definition Graph.h:110
const Object & at(const Index &index) const
Get the object that is associated with the given index.
::onert::util::Index< uint32_t, OperationIndexTag > OperationIndex
Definition Index.h:30
util::Set< ir::OperandIndex > external_operands

References onert::backend::BackendContext::_data, onert::ir::IOperation::accept(), onert::util::ObjectManager< Index, Object >::at(), onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::constant_initializer, onert::backend::ContextData::external_operands, onert::backend::ContextData::graph, onert::backend::BackendContext::graph(), onert::ir::Operand::isConstant(), and onert::ir::Graph::operands().

Referenced by onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::genKernels().

◆ planTensors()

template<typename T_TensorBuilder , typename T_ConstantInitializer , typename T_KernelGenerator >
void onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::planTensors ( )
inlineprotected

Definition at line 98 of file BackendContext.h.

99 {
100 ir::OperandIndexMap<uint32_t> uses_map;
101 ir::OperandIndexMap<uint32_t> def_map;
102 ir::OperandIndexSequence constants;
103
104 // Prepare scanning
105 _data.graph->operands().iterate([&](const ir::OperandIndex &ind, const ir::Operand &obj) {
106 if (_data.external_operands.contains(ind))
107 return;
108
109 uses_map[ind] = obj.getUses().size();
110 def_map[ind] = obj.getDef().valid() ? 1 : 0;
111
112 if (obj.isConstant())
113 constants.append(ind);
114
115 if (!tensor_builder->isRegistered(ind))
116 {
117 // These tensors do not exist in any operation (No use and def)
118 const auto &info = obj.info();
120 }
121 });
122
123 // Start scanning to do notify{First|Last}Use for each tensor
124
125 // If a tensor is a constant, increase the use of the tensor and allocate it first.
126 // Increasing use count here makes the tensor never be deallocated, i.e it they will be
127 // deallocated last.
128 VERBOSE(planTensors) << "TENSORS as CONSTANT" << std::endl;
129 for (const auto &ind : constants)
130 {
131 uses_map[ind]++;
132 tensor_builder->notifyFirstUse(ind);
133 }
134
135 // At each operation,
136 // 1. Scan DEF of outputs. If the DEF, allocate it
137 // 2. Scan DEF of inputs. If variable tensor, allocate it
138 // 3. Scan USE of inputs. Decrease the USE and deallocate if the USE is 0
139 for (const auto &op_ind : _data.op_order)
140 {
141 const auto &op = graph()->operations().at(op_ind);
142 auto op_inputs = op.getInputs() | ir::Remove::DUPLICATED | ir::Remove::UNDEFINED;
143 auto op_outputs = op.getOutputs() | ir::Remove::DUPLICATED | ir::Remove::UNDEFINED;
144
145 // Define outputs
146 for (const auto &ind : op_outputs)
147 {
148 if (!tensor_builder->isRegistered(ind))
149 continue;
150 assert(def_map.find(ind) != def_map.end());
151 if (def_map[ind])
152 {
153 def_map[ind] = 0;
154 tensor_builder->notifyFirstUse(ind);
155 }
156 }
157
158 // Scan variable tensors
159 // This tensor has features like constant. But OperandInfo and LowerInfo treat them as
160 // non-constant because of less memory usage by memory planning in here
161 for (const auto &ind : op_inputs)
162 {
163 if (!tensor_builder->isRegistered(ind))
164 continue;
165 const auto &operand = graph()->operands().at(ind);
166 if (operand.info().isVariable())
167 {
168 // The variable tensor with buffer is not supported yet
169 assert(operand.data() == nullptr);
170 assert(operand.getUses().size() == 1 && !operand.getDef().valid());
171 assert(uses_map[ind] == 1 && def_map[ind] == 0);
172 tensor_builder->notifyFirstUse(ind);
173 }
174 }
175
176 for (const auto &ind : op_inputs)
177 {
178 if (!tensor_builder->isRegistered(ind))
179 continue;
180 assert(uses_map.find(ind) != uses_map.end());
181 assert(uses_map[ind] > 0);
182 uses_map[ind]--;
183 if (uses_map[ind] == 0)
184 {
185 // plan for deallocation of static tensornode
186 tensor_builder->notifyLastUse(ind);
187 }
188 }
189 }
190
191 _data.graph->operands().iterate([&](const ir::OperandIndex &ind, const ir::Operand &) {
192 if (uses_map[ind] == 0)
193 {
194 tensor_builder->notifyLastUse(ind);
195 }
196 });
197
198 // Dispose and validate
199 for (const auto &ind : constants)
200 {
201 --uses_map[ind];
202 if (uses_map[ind] == 0) // To prevent notifyLastUse from being called twice
203 {
204 tensor_builder->notifyLastUse(ind);
205 }
206 }
207
208 assert(
209 std::all_of(uses_map.begin(), uses_map.end(),
210 [](std::pair<const ir::OperandIndex, uint32_t> it) { return it.second == 0; }));
211
212 assert(
213 std::all_of(def_map.begin(), def_map.end(),
214 [](std::pair<const ir::OperandIndex, uint32_t> it) { return it.second == 0; }));
215 }
virtual void registerTensorInfo(const ir::OperandIndex &ind, const ir::OperandInfo &info)=0
const Operations & operations() const override
Definition Graph.h:112
volatile const char info[]
#define VERBOSE(name, lv)
Definition Log.h:71

References onert::backend::BackendContext::_data, onert::ir::OperandIndexSequence::append(), onert::util::ObjectManager< Index, Object >::at(), onert::ir::DUPLICATED, onert::backend::ContextData::external_operands, onert::backend::ContextData::graph, onert::backend::BackendContext::graph(), info, onert::backend::ContextData::op_order, onert::ir::Graph::operands(), onert::ir::Graph::operations(), onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::planTensors(), onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::registerTensorInfo(), onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::tensor_builder, onert::ir::UNDEFINED, and VERBOSE.

Referenced by onert::backend::acl_common::AclBackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator, T_Optimizer >::genTensors(), and onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::planTensors().

◆ registerTensorInfo()

template<typename T_TensorBuilder , typename T_ConstantInitializer , typename T_KernelGenerator >
virtual void onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::registerTensorInfo ( const ir::OperandIndex ind,
const ir::OperandInfo info 
)
protectedpure virtual

Field Documentation

◆ constant_initializer

template<typename T_TensorBuilder , typename T_ConstantInitializer , typename T_KernelGenerator >
std::shared_ptr<T_ConstantInitializer> onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::constant_initializer

◆ kernel_gen

template<typename T_TensorBuilder , typename T_ConstantInitializer , typename T_KernelGenerator >
std::shared_ptr<T_KernelGenerator> onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::kernel_gen

◆ tensor_builder


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