ONE - On-device Neural Engine
Loading...
Searching...
No Matches
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.
 
 Execution (const std::shared_ptr< IExecutors > &executors, const ir::SubgraphIndex &entry_index)
 Construct a new Execution object for signature.
 
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 setOutput (const ir::IOIndex &index, void *buffer, size_t length)
 Set output data's information.
 
const ir::OperandInfoinputInfo (const ir::IOIndex &index)
 Get the Input Info object.
 
const ir::OperandInfooutputInfo (const ir::IOIndex &index)
 Get the Output Info object.
 
const void * outputBuffer (const ir::IOIndex &index) const
 Get internally allocated output buffer.
 
uint32_t inputSize ()
 Get input size.
 
uint32_t outputSize ()
 Get output size.
 
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.
 
const ExecutionContextcontext () const
 Get context of execution.
 
void restoreContext (const ExecutionContext &ctx)
 Set context of execution at once.
 
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() [1/2]

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

Construct a new Execution object.

Parameters
[in]executorModel executor

Definition at line 30 of file Execution.cc.

30 : _executors{executors}
31{
32 assert(executors != nullptr);
33 assert(executors->entryExecutor() != nullptr);
34
35 // Initialize I/O description
36 for (uint32_t i = 0; i < _executors->inputSize(); ++i)
37 _ctx.desc.inputs.emplace_back(_executors->inputInfo(ir::IOIndex(i)));
38
39 for (uint32_t i = 0; i < _executors->outputSize(); ++i)
40 _ctx.desc.outputs.emplace_back(_executors->outputInfo(ir::IOIndex(i)));
41 _ctx.shape_updated = false;
42
43 _is_internal_output_tensor.resize(_executors->outputSize());
44 for (uint32_t i = 0; i < _executors->outputSize(); ++i)
45 {
46 const auto output_tensor =
47 dynamic_cast<const backend::builtin::IOTensor *>(_executors->outputTensor(ir::IOIndex{i}));
48 if (!output_tensor)
49 throw std::runtime_error("Output tensor must be IOTensor");
50
51 _is_internal_output_tensor.at(i) = output_tensor->hasBackendTensor();
52 }
53
54 // Initialize options
56}
::onert::util::Index< uint32_t, IOIndexTag > IOIndex
Definition Index.h:36
static void fromGlobalConfig(ExecutionOptions &options)
std::vector< OutputDesc > outputs
std::vector< 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.

◆ Execution() [2/2]

onert::exec::Execution::Execution ( const std::shared_ptr< IExecutors > &  executors,
const ir::SubgraphIndex entry_index 
)

Construct a new Execution object for signature.

Parameters
[in]executorsModel executors
[in]entry_indexEntry subgraph index

Definition at line 58 of file Execution.cc.

60 : Execution(std::make_shared<SignatureExecutors>(executors, entry_index))
61{
62 // DO NOTHING
63}
Execution(const std::shared_ptr< IExecutors > &executors)
Construct a new Execution object.
Definition Execution.cc:30

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 65 of file Execution.cc.

66{
67 // This will be used later to set input tensor dynamic
68 // Note that 'compiled' model will not be updated with new_shape
69 // but new_shape will change model input shape while 'running' the model
70 auto &input_desc = _ctx.desc.inputs.at(index.value());
71 if (new_shape != input_desc.info.shape())
72 {
73 input_desc.info.shape(new_shape);
74 _ctx.shape_updated = true;
75
76 VERBOSE(Execution) << "Model input shape will be changed at the start of execute()"
77 << "(index: " << index << ")" << std::endl;
78 }
79}
#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.

◆ context()

const ExecutionContext & onert::exec::Execution::context ( ) const
inline

Get context of execution.

Returns
Execution context

Definition at line 183 of file Execution.h.

183{ return _ctx; }

◆ execute()

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

Execution.

Note
It should be called after setting input and output buffer

Definition at line 100 of file Execution.cc.

101{
102 VERBOSE(Execution) << "Start execution" << std::endl;
103
104 // Input length validation check
105 for (const auto &input : _ctx.desc.inputs)
106 {
107 if (input.info.total_size() > input.size)
108 throw std::runtime_error{"Too small input buffer length"};
109 }
110
111 // Output length validation check
112 if (!_ctx.shape_updated)
113 {
114 assert(_ctx.desc.outputs.size() == _is_internal_output_tensor.size());
115 for (uint32_t i = 0; i < _ctx.desc.outputs.size(); ++i)
116 {
117 const bool is_managed_internally = _is_internal_output_tensor.at(i);
118 const auto &output = _ctx.desc.outputs.at(i);
119 if (!is_managed_internally && output.info.total_size() > output.size)
120 throw std::runtime_error{"Too small output buffer length"};
121 if (is_managed_internally && output.buffer != nullptr)
122 VERBOSE(Execution) << "Warning: Output buffer was set from API even though the output "
123 "tensor was allocated internally"
124 << std::endl;
125 }
126 }
127
128 _executors->execute(_ctx);
129 finished = true;
130
131 VERBOSE(Execution) << "Execution finished" << std::endl;
132}

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.

◆ 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 163 of file Execution.cc.

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

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

◆ inputInfo()

const ir::OperandInfo & onert::exec::Execution::inputInfo ( const ir::IOIndex index)
inline

Get the Input Info object.

Parameters
[in]indexInput index
Returns
Input info

Definition at line 92 of file Execution.h.

93 {
94 return _ctx.desc.inputs.at(index.value()).info;
95 }

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

◆ inputSize()

uint32_t onert::exec::Execution::inputSize ( )
inline

Get input size.

Returns
Input size

Definition at line 121 of file Execution.h.

121{ return _ctx.desc.inputs.size(); }

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

◆ isFinished()

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

Check execution is finished.

Returns
true if execution is finished, otherwise false

Definition at line 149 of file Execution.cc.

149{ 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 174 of file Execution.cc.

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

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

◆ outputBuffer()

const void * onert::exec::Execution::outputBuffer ( const ir::IOIndex index) const
inline

Get internally allocated output buffer.

Parameters
[in]indexOutput index
Returns
Buffer pointer

Definition at line 112 of file Execution.h.

113 {
114 return entryExecutor()->outputBuffer(index.value());
115 }
virtual const uint8_t * outputBuffer(uint32_t index) const =0
Get output buffer at index.

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

◆ outputInfo()

const ir::OperandInfo & onert::exec::Execution::outputInfo ( const ir::IOIndex index)
inline

Get the Output Info object.

Parameters
[in]indexOutput index
Returns
Output info

Definition at line 102 of file Execution.h.

103 {
104 return _ctx.desc.outputs.at(index.value()).info;
105 }

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

◆ outputSize()

uint32_t onert::exec::Execution::outputSize ( )
inline

Get output size.

Returns
Output size

Definition at line 127 of file Execution.h.

127{ return _ctx.desc.outputs.size(); }

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

◆ primary_subgraph()

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

Returns primary graph object.

Returns
Graph object

Definition at line 62 of file Execution.h.

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

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

◆ restoreContext()

void onert::exec::Execution::restoreContext ( const ExecutionContext ctx)
inline

Set context of execution at once.

Parameters
[in]ctxExecution context

Definition at line 189 of file Execution.h.

189{ _ctx = ctx; }

◆ setInput()

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 82 of file Execution.cc.

83{
84 // Length validation in execute(): datatype can be changed by API call
85 auto &input_desc = _ctx.desc.inputs.at(index.value());
86 input_desc.buffer = buffer;
87 input_desc.size = length;
88}

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

◆ setOutput()

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 90 of file Execution.cc.

91{
92 // Length validation in execute()
93 // - datatype can be changed by API call
94 // - shape can be changed by dynamic shape inference
95 auto &output_desc = _ctx.desc.outputs.at(index.value());
96 output_desc.buffer = buffer;
97 output_desc.size = length;
98}

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

◆ 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 134 of file Execution.cc.

135{
136 VERBOSE(Execution) << "Create asynchronous execution thread" << std::endl;
137
138 _exec_thread = std::make_unique<std::thread>(&Execution::execute, this);
139}
void execute()
Execution.
Definition Execution.cc:100

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 151 of file Execution.cc.

152{
153 auto execs = dynamic_cast<exec::train::TrainableExecutors *>(_executors.get());
154 if (!execs)
155 {
156 throw std::runtime_error{"Supported only TrainableExecutors"};
157 }
158
159 execs->train(_ctx, training_step);
160 finished = true;
161}

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 141 of file Execution.cc.

142{
143 VERBOSE(Execution) << "Wait to finish execution" << std::endl;
144
145 _exec_thread->join();
146 finished = true;
147}

References VERBOSE.


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