ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onert::backend::train::TensorBuilder Class Reference

#include <TensorBuilder.h>

Public Member Functions

 TensorBuilder (const std::shared_ptr< TensorRegistry > &tensor_reg, const exec::train::optimizer::Optimizer *optimizer)
 
void registerTensorInfo (const ir::OperandIndex &ind, const ir::OperandInfo &info)
 Register tensor information to allocate on train backend.
 
void registerBackwardTensorInfo (const ir::OperandIndex &ind, const ir::OperandInfo &info)
 Register informations of tensor used only in backward to allocate on train backend.
 
void registerDisposableBackwardTensorInfo (const DisposableTensorIndex &index, const ir::OperandInfo &info)
 
void registerLayerScopeTensor (const LayerScopeTensorIndex &index, std::shared_ptr< LayerScopeTensor > &info)
 
void notifyFirstUse (const ir::OperandIndex &)
 
void notifyLastUse (const ir::OperandIndex &)
 
void notifyBackwardFirstUse (const ir::OperandIndex &)
 
void notifyBackwardLastUse (const ir::OperandIndex &)
 
void notifyDisposableBackPropFirstUse (const DisposableTensorIndex &)
 
void notifyDisposableBackPropLastUse (const DisposableTensorIndex &)
 
void notifyLayerScopeFirstUse (const LayerScopeTensorIndex &)
 
void notifyLayerScopeLastUse (const LayerScopeTensorIndex &)
 
bool isRegistered (const ir::OperandIndex &) const
 
bool isRegisteredBackward (const ir::OperandIndex &) const
 
bool isRegisteredDisposableBackwardTensor (const DisposableTensorIndex &index) const
 
bool isRegisteredLayerScopeTensor (const ir::OperationIndex &) const
 
const util::Set< LayerScopeTensorIndex > & getRegisteredLayerScopeTensorIndices (const ir::OperationIndex &) const
 
LayerScopeTensorLifeTime getLayerScopeTensorLifeTime (const LayerScopeTensorIndex &) const
 
void allocate (void)
 
void allocateBackward (void)
 
void allocateLayerScope (void)
 

Detailed Description

Definition at line 37 of file TensorBuilder.h.

Constructor & Destructor Documentation

◆ TensorBuilder()

onert::backend::train::TensorBuilder::TensorBuilder ( const std::shared_ptr< TensorRegistry > &  tensor_reg,
const exec::train::optimizer::Optimizer optimizer 
)

Definition at line 28 of file TensorBuilder.cc.

30 : _tensor_reg{tensor_reg}, _tensor_mgr{new TensorManager(tensor_reg, optimizer->getVarCount())},
31 _optimizer{optimizer}
32{
33 /* empty */
34}
acl_common::AclTensorManager< operand::ICLTensor, operand::CLTensor, operand::CLSubTensor > TensorManager

Member Function Documentation

◆ allocate()

void onert::backend::train::TensorBuilder::allocate ( void  )

Definition at line 227 of file TensorBuilder.cc.

228{
229 _tensor_mgr->allocateNonConstTensors();
230 _tensor_mgr->allocateTrainableTensors();
231}

◆ allocateBackward()

void onert::backend::train::TensorBuilder::allocateBackward ( void  )

Definition at line 233 of file TensorBuilder.cc.

234{
235 _tensor_mgr->allocateBackPropTensors();
236 _tensor_mgr->allocateGradientTensors();
237 _tensor_mgr->allocateDisposableBackPropTensors();
238}

◆ allocateLayerScope()

void onert::backend::train::TensorBuilder::allocateLayerScope ( void  )

Definition at line 240 of file TensorBuilder.cc.

240{ _tensor_mgr->allocateLayerScopeTensors(); }

◆ getLayerScopeTensorLifeTime()

LayerScopeTensorLifeTime onert::backend::train::TensorBuilder::getLayerScopeTensorLifeTime ( const LayerScopeTensorIndex index) const

Definition at line 220 of file TensorBuilder.cc.

221{
222 const auto &ls_tensors = _tensor_reg->layerscope_tensors();
223 const auto &tensor = ls_tensors.at(index);
224 return tensor->lifetime();
225}

Referenced by onert::backend::train::TensorPlanner::planLayerScopeTensors().

◆ getRegisteredLayerScopeTensorIndices()

const util::Set< LayerScopeTensorIndex > & onert::backend::train::TensorBuilder::getRegisteredLayerScopeTensorIndices ( const ir::OperationIndex index) const

Definition at line 211 of file TensorBuilder.cc.

212{
213 const auto pair = _operation_to_layerscope.find(index);
214 assert(pair != _operation_to_layerscope.end());
215
216 return pair->second;
217}

Referenced by onert::backend::train::TensorPlanner::planLayerScopeTensors().

◆ isRegistered()

bool onert::backend::train::TensorBuilder::isRegistered ( const ir::OperandIndex index) const

Definition at line 189 of file TensorBuilder.cc.

190{
191 return _tensor_info_map.find(index) != _tensor_info_map.end();
192}

Referenced by onert::backend::train::TensorPlanner::planNonConstTensors(), and onert::backend::train::TensorPlanner::planTrainableTensors().

◆ isRegisteredBackward()

bool onert::backend::train::TensorBuilder::isRegisteredBackward ( const ir::OperandIndex index) const

Definition at line 194 of file TensorBuilder.cc.

195{
196 return _backward_tensor_info_map.find(index) != _backward_tensor_info_map.end();
197}

Referenced by onert::backend::train::TensorPlanner::planBackPropTensors(), and onert::backend::train::TensorPlanner::planGradientTensors().

◆ isRegisteredDisposableBackwardTensor()

bool onert::backend::train::TensorBuilder::isRegisteredDisposableBackwardTensor ( const DisposableTensorIndex index) const

Definition at line 199 of file TensorBuilder.cc.

200{
201 return _disposable_backprops.contains(index);
202}

Referenced by onert::backend::train::TensorPlanner::planDisposableBackPropTensors().

◆ isRegisteredLayerScopeTensor()

bool onert::backend::train::TensorBuilder::isRegisteredLayerScopeTensor ( const ir::OperationIndex index) const

Definition at line 204 of file TensorBuilder.cc.

205{
206 const auto pair = _operation_to_layerscope.find(index);
207 return (pair != _operation_to_layerscope.end());
208}

Referenced by onert::backend::train::TensorPlanner::planLayerScopeTensors().

◆ notifyBackwardFirstUse()

void onert::backend::train::TensorBuilder::notifyBackwardFirstUse ( const ir::OperandIndex index)

Definition at line 144 of file TensorBuilder.cc.

145{
146 // TODO Support momory plan
147 if (_as_constants[index])
148 {
149 _tensor_mgr->claimGradientPlan(index);
150 }
151 else
152 {
153 _tensor_mgr->claimBackPropPlan(index);
154 }
155}

Referenced by onert::backend::train::TensorPlanner::planBackPropTensors(), and onert::backend::train::TensorPlanner::planGradientTensors().

◆ notifyBackwardLastUse()

void onert::backend::train::TensorBuilder::notifyBackwardLastUse ( const ir::OperandIndex index)

Definition at line 157 of file TensorBuilder.cc.

158{
159 if (_as_constants[index])
160 {
161 _tensor_mgr->releaseGradientPlan(index);
162 }
163 else
164 {
165 _tensor_mgr->releaseBackPropPlan(index);
166 }
167}

Referenced by onert::backend::train::TensorPlanner::planBackPropTensors(), and onert::backend::train::TensorPlanner::planGradientTensors().

◆ notifyDisposableBackPropFirstUse()

void onert::backend::train::TensorBuilder::notifyDisposableBackPropFirstUse ( const DisposableTensorIndex index)

Definition at line 169 of file TensorBuilder.cc.

170{
171 _tensor_mgr->claimDisposableBackPropPlan(index);
172}

Referenced by onert::backend::train::TensorPlanner::planDisposableBackPropTensors().

◆ notifyDisposableBackPropLastUse()

void onert::backend::train::TensorBuilder::notifyDisposableBackPropLastUse ( const DisposableTensorIndex index)

Definition at line 174 of file TensorBuilder.cc.

175{
176 _tensor_mgr->releaseDisposableBackPropPlan(index);
177}

Referenced by onert::backend::train::TensorPlanner::planDisposableBackPropTensors().

◆ notifyFirstUse()

void onert::backend::train::TensorBuilder::notifyFirstUse ( const ir::OperandIndex index)

Definition at line 119 of file TensorBuilder.cc.

120{
121 // TODO Support momory plan
122 if (_as_constants[index])
123 {
124 _tensor_mgr->claimTrainablePlan(index);
125 }
126 else
127 {
128 _tensor_mgr->claimNonConstPlan(index);
129 }
130}

Referenced by onert::backend::train::TensorPlanner::planNonConstTensors(), and onert::backend::train::TensorPlanner::planTrainableTensors().

◆ notifyLastUse()

void onert::backend::train::TensorBuilder::notifyLastUse ( const ir::OperandIndex index)

Definition at line 132 of file TensorBuilder.cc.

133{
134 if (_as_constants[index])
135 {
136 _tensor_mgr->releaseTrainablePlan(index);
137 }
138 else
139 {
140 _tensor_mgr->releaseNonConstPlan(index);
141 }
142}

Referenced by onert::backend::train::TensorPlanner::planNonConstTensors(), and onert::backend::train::TensorPlanner::planTrainableTensors().

◆ notifyLayerScopeFirstUse()

void onert::backend::train::TensorBuilder::notifyLayerScopeFirstUse ( const LayerScopeTensorIndex index)

Definition at line 179 of file TensorBuilder.cc.

180{
181 _tensor_mgr->claimLayerScopePlan(index);
182}

Referenced by onert::backend::train::TensorPlanner::planLayerScopeTensors().

◆ notifyLayerScopeLastUse()

void onert::backend::train::TensorBuilder::notifyLayerScopeLastUse ( const LayerScopeTensorIndex index)

Definition at line 184 of file TensorBuilder.cc.

185{
186 _tensor_mgr->releaseLayerScopePlan(index);
187}

Referenced by onert::backend::train::TensorPlanner::planLayerScopeTensors().

◆ registerBackwardTensorInfo()

void onert::backend::train::TensorBuilder::registerBackwardTensorInfo ( const ir::OperandIndex ind,
const ir::OperandInfo info 
)

Register informations of tensor used only in backward to allocate on train backend.

Parameters
[in]indOperand index
[in]infoOperand information

Definition at line 57 of file TensorBuilder.cc.

59{
60 _backward_tensor_info_map.emplace(index, info);
61
62 assert(!info.isDynamic());
63
64 // NOTE For now, whether or not to build operands to trainable tensor depends on whether
65 // the corresponding operand is constant.
66 assert(_as_constants[index] == info.isConstant());
67 if (_as_constants[index])
68 {
69 auto tensor = std::make_unique<GradientTensor>(info);
70 _tensor_reg->setGradientTensor(index, std::move(tensor));
71
72 // Initialize tensors for gradient variables
73 for (uint32_t i = 0; i < _optimizer->getVarCount(); ++i)
74 {
75 auto tensor = std::make_unique<Tensor>(info);
76 _tensor_reg->getTrainableTensor(index)->appendOptVar(std::move(tensor));
77 }
78 }
79 else
80 {
81 auto tensor = std::make_unique<BackPropTensor>(info);
82 _tensor_reg->setBackPropTensor(index, std::move(tensor));
83 }
84}
virtual uint32_t getVarCount() const =0
Get the number of optimizer variables s.
volatile const char info[]

References onert::exec::train::optimizer::Optimizer::getVarCount(), and info.

◆ registerDisposableBackwardTensorInfo()

void onert::backend::train::TensorBuilder::registerDisposableBackwardTensorInfo ( const DisposableTensorIndex index,
const ir::OperandInfo info 
)

Definition at line 86 of file TensorBuilder.cc.

88{
89 assert(!info.isDynamic());
90 assert(!_as_constants[index.operand_index()]);
91
92 auto disposable_tensor = std::make_unique<BackPropTensor>(info);
93 _tensor_reg->setDisposableBackPropTensor(index, std::move(disposable_tensor));
94
95 _disposable_backprops.add(index);
96}
loco::GraphInputIndex index(const TFPlaceholder *node)
Definition TFNode.cpp:54

References info.

◆ registerLayerScopeTensor()

void onert::backend::train::TensorBuilder::registerLayerScopeTensor ( const LayerScopeTensorIndex index,
std::shared_ptr< LayerScopeTensor > &  info 
)

Definition at line 98 of file TensorBuilder.cc.

100{
101 const auto op_idx = index.op_index();
102
103 const auto pair = _operation_to_layerscope.find(op_idx);
104 if (pair == _operation_to_layerscope.end())
105 {
106 util::Set<LayerScopeTensorIndex> tensor_indices;
107 tensor_indices.add(index);
108 _operation_to_layerscope[op_idx] = tensor_indices;
109 }
110 else
111 {
112 assert(!pair->second.contains(index));
113 pair->second.add(index);
114 }
115
116 _tensor_reg->setLayerScopeTensor(index, tensor);
117}

References onert::util::Set< Element >::add().

◆ registerTensorInfo()

void onert::backend::train::TensorBuilder::registerTensorInfo ( const ir::OperandIndex ind,
const ir::OperandInfo info 
)

Register tensor information to allocate on train backend.

Parameters
[in]indOperand index
[in]infoOperand information

Definition at line 36 of file TensorBuilder.cc.

37{
38 _tensor_info_map.emplace(index, info);
39 _as_constants[index] = info.isConstant();
40
41 assert(!info.isDynamic());
42
43 // NOTE For now, whether or not to build operands to trainable tensor depends on whether
44 // the corresponding operand is constant.
45 if (_as_constants[index])
46 {
47 auto tensor = std::make_unique<TrainableTensor>(info);
48 _tensor_reg->setTrainableTensor(index, std::move(tensor));
49 }
50 else
51 {
52 auto tensor = std::make_unique<Tensor>(info);
53 _tensor_reg->setNonConstTensor(index, std::move(tensor));
54 }
55}

References info.


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