ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
onert::exec::Execution Class Reference

Class to define execution instance to collect input/output information for inference and prepare executor run (TODO) More...

#include <Execution.h>

Public Member Functions

 Execution (const std::shared_ptr< IExecutors > &executors)
 Construct a new Execution object.
 
const ir::Graphprimary_subgraph () const
 Returns primary graph object.
 
void changeInputShape (const ir::IOIndex &index, const ir::Shape &new_shape)
 Change input shape.
 
void setInput (const ir::IOIndex &index, const void *buffer, size_t length)
 Set input data's information.
 
void setInput (const ir::IOIndex &index, const ir::Shape &shape, const void *buffer, size_t length)
 Set input data's information, especially to specify unknown dimensions on model build time.
 
void setOutput (const ir::IOIndex &index, void *buffer, size_t length)
 Set output data's information.
 
void setOutput (const ir::IOIndex &index, const ir::Shape &shape, void *buffer, size_t length)
 Set output data's information, especially to specify unknown dimensions on model build time.
 
void setInputLayout (const ir::IOIndex &index, ir::Layout layout)
 Set input data's data format.
 
void setOutputLayout (const ir::IOIndex &index, ir::Layout layout)
 Set output data's data format.
 
void setInputType (const ir::IOIndex &index, const ir::TypeInfo &typeInfo)
 Set input type information.
 
void setOutputType (const ir::IOIndex &index, const ir::TypeInfo &typeInfo)
 Set output type information.
 
void execute ()
 Execution.
 
void startExecute (void)
 Start asynchronous execution.
 
void waitFinish (void)
 Return when execution is finished.
 
bool isFinished (void) const
 Check execution is finished.
 
void train (uint32_t training_step)
 Train.
 
float getLoss (const ir::IOIndex &ind)
 Get loss.
 
void iterateTrainableTensors (const std::function< void(const ir::OperandIndex &, const backend::train::ITrainableTensor *)> &fn) const
 Iterate trainable tensors.
 
ir::Shape getInputShape (ir::IOIndex ind) const
 
ir::Shape getOutputShape (ir::IOIndex ind) const
 
size_t getInputTotalSize (ir::IOIndex ind) const
 
size_t getOutputTotalSize (ir::IOIndex ind) const
 
const void * getInputBuffer (ir::IOIndex ind) const
 Get pointer of Input Buffer.
 
void * getOutputBuffer (ir::IOIndex ind)
 Get pointer of Output Buffer.
 
ExecutionOptionsexecutionOptions ()
 

Detailed Description

Class to define execution instance to collect input/output information for inference and prepare executor run (TODO)

Definition at line 40 of file Execution.h.

Constructor & Destructor Documentation

◆ Execution()

onert::exec::Execution::Execution ( const std::shared_ptr< IExecutors > &  executors)

Construct a new Execution object.

Parameters
[in]executorModel executor

Definition at line 26 of file Execution.cc.

26 : _executors{executors}
27{
28 assert(executors != nullptr);
29 assert(executors->entryExecutor() != nullptr);
30
31 // Initialize I/O description
32 _ctx.desc.inputs.resize(_executors->inputSize());
33 for (uint32_t i = 0; i < _executors->inputSize(); ++i)
34 _ctx.desc.inputs.at(i) = std::make_unique<InputDesc>(_executors->inputInfo(ir::IOIndex(i)));
35
36 _ctx.desc.outputs.resize(_executors->outputSize());
37 for (uint32_t i = 0; i < _executors->outputSize(); ++i)
38 _ctx.desc.outputs.at(i) = std::make_unique<OutputDesc>(_executors->outputInfo(ir::IOIndex(i)));
39 _ctx.shape_updated = false;
40
41 // Initialize options
43}
::onert::util::Index< uint32_t, IOIndexTag > IOIndex
Definition Index.h:36
static void fromGlobalConfig(ExecutionOptions &options)
std::vector< std::unique_ptr< OutputDesc > > outputs
std::vector< std::unique_ptr< InputDesc > > inputs

References onert::exec::ExecutionContext::desc, onert::exec::ExecutionOptions::fromGlobalConfig(), onert::exec::IODescription::inputs, onert::exec::ExecutionContext::options, onert::exec::IODescription::outputs, and onert::exec::ExecutionContext::shape_updated.

Member Function Documentation

◆ changeInputShape()

void onert::exec::Execution::changeInputShape ( const ir::IOIndex index,
const ir::Shape new_shape 
)

Change input shape.

Parameters
[in]indexInput index
[in]new_shapeshape to change

Definition at line 45 of file Execution.cc.

46{
47 // This will be used later to set input tensor dynamic
48 // Note that 'compiled' model will not be updated with new_shape
49 // but new_shape will change model input shape while 'running' the model
50 auto &input_desc = _ctx.desc.inputs.at(index.value());
51 if (new_shape != input_desc->info.shape())
52 {
53 input_desc->info.shape(new_shape);
54 _ctx.shape_updated = true;
55
56 VERBOSE(Execution) << "Model input shape will be changed at the start of execute()"
57 << "(index: " << index << ")" << std::endl;
58 }
59}
#define VERBOSE(name, lv)
Definition Log.h:71
loco::GraphInputIndex index(const TFPlaceholder *node)
Definition TFNode.cpp:54

References onert::exec::ExecutionContext::desc, onert::exec::IODescription::inputs, onert::exec::ExecutionContext::shape_updated, and VERBOSE.

Referenced by setInput().

◆ execute()

void onert::exec::Execution::execute ( )

Execution.

Note
It should be called after setting input and output buffer

Definition at line 118 of file Execution.cc.

119{
120 VERBOSE(Execution) << "Start execution" << std::endl;
121
122 // Input length validation check
123 for (const auto &input : _ctx.desc.inputs)
124 {
125 if (input->info.total_size() > input->size)
126 throw std::runtime_error{"Too small input buffer length"};
127 }
128
129 // Output length validation check
130 if (!_ctx.shape_updated)
131 {
132 for (const auto &output : _ctx.desc.outputs)
133 {
134 if (output->info.total_size() > output->size)
135 throw std::runtime_error{"Too small output buffer length"};
136 }
137 }
138
139 _executors->execute(_ctx);
140 finished = true;
141
142 VERBOSE(Execution) << "Execution finished" << std::endl;
143}

References onert::exec::ExecutionContext::desc, onert::exec::IODescription::inputs, onert::exec::IODescription::outputs, onert::exec::ExecutionContext::shape_updated, and VERBOSE.

Referenced by startExecute().

◆ executionOptions()

ExecutionOptions & onert::exec::Execution::executionOptions ( )
inline

Definition at line 191 of file Execution.h.

191{ return _ctx.options; }

References onert::exec::ExecutionContext::options.

◆ getInputBuffer()

const void * onert::exec::Execution::getInputBuffer ( ir::IOIndex  ind) const

Get pointer of Input Buffer.

Parameters
[in]indexInput index
Returns
Pointer of Input Buffer

Definition at line 224 of file Execution.cc.

225{
226 return _ctx.desc.inputs.at(ind.value())->buffer;
227}

References onert::exec::ExecutionContext::desc, onert::exec::IODescription::inputs, and onert::util::Index< T, DummyTag >::value().

◆ getInputShape()

ir::Shape onert::exec::Execution::getInputShape ( ir::IOIndex  ind) const

Definition at line 197 of file Execution.cc.

198{
199 return _ctx.desc.inputs.at(ind.value())->info.shape();
200}

References onert::exec::ExecutionContext::desc, onert::exec::IODescription::inputs, and onert::util::Index< T, DummyTag >::value().

◆ getInputTotalSize()

size_t onert::exec::Execution::getInputTotalSize ( ir::IOIndex  ind) const

Definition at line 213 of file Execution.cc.

214{
215 // TODO Support dynamic shape
216 return _ctx.desc.inputs.at(ind.value())->info.total_size();
217}

References onert::exec::ExecutionContext::desc, onert::exec::IODescription::inputs, and onert::util::Index< T, DummyTag >::value().

◆ getLoss()

float onert::exec::Execution::getLoss ( const ir::IOIndex ind)

Get loss.

Note
It should be called after training
Parameters
[in]indOutput index
Returns
float Loss value

Definition at line 174 of file Execution.cc.

175{
176 auto execs = dynamic_cast<exec::train::TrainableExecutors *>(_executors.get());
177 if (!execs)
178 {
179 throw std::runtime_error{"Supported only TrainableExecutors"};
180 }
181
182 return execs->getLoss(ind);
183}

References onert::exec::train::TrainableExecutors::getLoss().

◆ getOutputBuffer()

void * onert::exec::Execution::getOutputBuffer ( ir::IOIndex  ind)

Get pointer of Output Buffer.

Parameters
[in]indexOutput index
Returns
Pointer of Output Buffer

Definition at line 229 of file Execution.cc.

230{
231 return _ctx.desc.outputs.at(ind.value())->buffer;
232}

References onert::exec::ExecutionContext::desc, onert::exec::IODescription::outputs, and onert::util::Index< T, DummyTag >::value().

◆ getOutputShape()

ir::Shape onert::exec::Execution::getOutputShape ( ir::IOIndex  ind) const

Definition at line 208 of file Execution.cc.

209{
210 return _ctx.desc.outputs.at(ind.value())->info.shape();
211}

References onert::exec::ExecutionContext::desc, onert::exec::IODescription::outputs, and onert::util::Index< T, DummyTag >::value().

◆ getOutputTotalSize()

size_t onert::exec::Execution::getOutputTotalSize ( ir::IOIndex  ind) const

Definition at line 219 of file Execution.cc.

220{
221 return _ctx.desc.outputs.at(ind.value())->info.total_size();
222}

References onert::exec::ExecutionContext::desc, onert::exec::IODescription::outputs, and onert::util::Index< T, DummyTag >::value().

◆ isFinished()

bool onert::exec::Execution::isFinished ( void  ) const

Check execution is finished.

Returns
true if execution is finished, otherwise false

Definition at line 160 of file Execution.cc.

160{ return finished; }

◆ iterateTrainableTensors()

void onert::exec::Execution::iterateTrainableTensors ( const std::function< void(const ir::OperandIndex &, const backend::train::ITrainableTensor *)> &  fn) const

Iterate trainable tensors.

Note
It should be called after training
Parameters
[in]fnfunction to be called with OperandIndex and a pointer to ITrainableTensor

Definition at line 185 of file Execution.cc.

188{
189 auto execs = dynamic_cast<exec::train::TrainableExecutors *>(_executors.get());
190 if (!execs)
191 {
192 throw std::runtime_error{"Supported only TrainableExecutors"};
193 }
194 execs->iterateTrainableTensors(fn);
195}

References onert::exec::train::TrainableExecutors::iterateTrainableTensors().

◆ primary_subgraph()

const ir::Graph & onert::exec::Execution::primary_subgraph ( ) const
inline

Returns primary graph object.

Returns
Graph object

Definition at line 55 of file Execution.h.

55{ return entryExecutor()->graph(); }
virtual const ir::Graph & graph() const =0
Returns graph object.

References onert::exec::IExecutor::graph().

◆ setInput() [1/2]

void onert::exec::Execution::setInput ( const ir::IOIndex index,
const ir::Shape shape,
const void *  buffer,
size_t  length 
)

Set input data's information, especially to specify unknown dimensions on model build time.

Parameters
[in]indexInput index
[in]shapeInput data's shape
[in]bufferInput data's buffer pointer
[in]lengthInput data's length

Definition at line 70 of file Execution.cc.

72{
73 changeInputShape(index, shape);
74 setInput(index, buffer, length);
75}
void changeInputShape(const ir::IOIndex &index, const ir::Shape &new_shape)
Change input shape.
Definition Execution.cc:45
void setInput(const ir::IOIndex &index, const void *buffer, size_t length)
Set input data's information.
Definition Execution.cc:62

References changeInputShape(), and setInput().

◆ setInput() [2/2]

void onert::exec::Execution::setInput ( const ir::IOIndex index,
const void *  buffer,
size_t  length 
)

Set input data's information.

Parameters
[in]indexInput index
[in]bufferInput data's buffer pointer
[in]lengthInput data's length

Definition at line 62 of file Execution.cc.

63{
64 // Length validation in execute(): datatype can be changed by API call
65 auto &input_desc = _ctx.desc.inputs.at(index.value());
66 input_desc->buffer = buffer;
67 input_desc->size = length;
68}

References onert::exec::ExecutionContext::desc, and onert::exec::IODescription::inputs.

Referenced by setInput().

◆ setInputLayout()

void onert::exec::Execution::setInputLayout ( const ir::IOIndex index,
ir::Layout  layout 
)

Set input data's data format.

Parameters
[in]indexInput index
[in]layoutInput data's data format

Definition at line 96 of file Execution.cc.

97{
98 _ctx.desc.inputs.at(index.value())->layout = layout;
99}

References onert::exec::ExecutionContext::desc, and onert::exec::IODescription::inputs.

◆ setInputType()

void onert::exec::Execution::setInputType ( const ir::IOIndex index,
const ir::TypeInfo typeInfo 
)

Set input type information.

Parameters
[in]indexInput index
[in]typeInfoInput type information

Definition at line 106 of file Execution.cc.

107{
108 _ctx.desc.inputs.at(index.value())->info.typeInfo(typeInfo);
109 _ctx.shape_updated = true;
110}

References onert::exec::ExecutionContext::desc, onert::exec::IODescription::inputs, and onert::exec::ExecutionContext::shape_updated.

◆ setOutput() [1/2]

void onert::exec::Execution::setOutput ( const ir::IOIndex index,
const ir::Shape shape,
void *  buffer,
size_t  length 
)

Set output data's information, especially to specify unknown dimensions on model build time.

Parameters
[in]indexOutput index
[in]shapeOutput data's shape
[in]bufferOutput data's buffer pointer
[in]lengthOutput data's length

Definition at line 87 of file Execution.cc.

89{
90 auto &output_desc = _ctx.desc.outputs.at(index.value());
91 output_desc->info.shape(shape);
92
93 setOutput(index, buffer, length);
94}
void setOutput(const ir::IOIndex &index, void *buffer, size_t length)
Set output data's information.
Definition Execution.cc:77

References onert::exec::ExecutionContext::desc, onert::exec::IODescription::outputs, and setOutput().

◆ setOutput() [2/2]

void onert::exec::Execution::setOutput ( const ir::IOIndex index,
void *  buffer,
size_t  length 
)

Set output data's information.

Parameters
[in]indexOutput index
[in]bufferOutput data's buffer pointer
[in]lengthOutput data's length

Definition at line 77 of file Execution.cc.

78{
79 // Length validation in execute()
80 // - datatype can be changed by API call
81 // - shape can be changed by dynamic shape inference
82 auto &output_desc = _ctx.desc.outputs.at(index.value());
83 output_desc->buffer = buffer;
84 output_desc->size = length;
85}

References onert::exec::ExecutionContext::desc, and onert::exec::IODescription::outputs.

Referenced by setOutput().

◆ setOutputLayout()

void onert::exec::Execution::setOutputLayout ( const ir::IOIndex index,
ir::Layout  layout 
)

Set output data's data format.

Parameters
[in]indexOutput index
[in]layoutOutput data's data format

Definition at line 101 of file Execution.cc.

102{
103 _ctx.desc.outputs.at(index.value())->layout = layout;
104}

References onert::exec::ExecutionContext::desc, and onert::exec::IODescription::outputs.

◆ setOutputType()

void onert::exec::Execution::setOutputType ( const ir::IOIndex index,
const ir::TypeInfo typeInfo 
)

Set output type information.

Parameters
[in]indexOutput index
[in]typeInfoOutput type information

Definition at line 112 of file Execution.cc.

113{
114 _ctx.desc.outputs.at(index.value())->info.typeInfo(typeInfo);
115 _ctx.shape_updated = true;
116}

References onert::exec::ExecutionContext::desc, onert::exec::IODescription::outputs, and onert::exec::ExecutionContext::shape_updated.

◆ startExecute()

void onert::exec::Execution::startExecute ( void  )

Start asynchronous execution.

Note
It returns after execution thread is started It should be called after setting input and output buffer

Definition at line 145 of file Execution.cc.

146{
147 VERBOSE(Execution) << "Create asynchronous execution thread" << std::endl;
148
149 _exec_thread = std::make_unique<std::thread>(&Execution::execute, this);
150}
void execute()
Execution.
Definition Execution.cc:118

References execute(), and VERBOSE.

◆ train()

void onert::exec::Execution::train ( uint32_t  training_step)

Train.

Note
It should be called after setting input and output buffer
Parameters
training_stepThe number of iterations of the training process. In other words, the number of gradient update.

Definition at line 162 of file Execution.cc.

163{
164 auto execs = dynamic_cast<exec::train::TrainableExecutors *>(_executors.get());
165 if (!execs)
166 {
167 throw std::runtime_error{"Supported only TrainableExecutors"};
168 }
169
170 execs->train(_ctx, training_step);
171 finished = true;
172}

References onert::exec::train::TrainableExecutors::train().

◆ waitFinish()

void onert::exec::Execution::waitFinish ( void  )

Return when execution is finished.

Note
It waits until execution is finished

Definition at line 152 of file Execution.cc.

153{
154 VERBOSE(Execution) << "Wait to finish execution" << std::endl;
155
156 _exec_thread->join();
157 finished = true;
158}

References VERBOSE.


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