ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onert::exec::SingleModelExecutors Class Reference

Class to gather executor set for single model NN package. More...

#include <SingleModelExecutors.h>

Collaboration diagram for onert::exec::SingleModelExecutors:

Public Member Functions

 SingleModelExecutors (void)=default
 Construct a new SingleModelExecutors object.
 
 SingleModelExecutors (const SingleModelExecutors &)=delete
 
 SingleModelExecutors (SingleModelExecutors &&)=default
 
 ~SingleModelExecutors ()=default
 Destroy the SingleModelExecutors object.
 
void emplace (const ir::ModelIndex &model_index, const ir::SubgraphIndex &subg_index, std::unique_ptr< IExecutor > exec) override
 Insert executor in executor set.
 
IExecutorat (const ir::ModelIndex &model_index, const ir::SubgraphIndex &subg_index) const override
 Return executor of index.
 
uint32_t inputSize () const override
 Return executor set's number of input.
 
uint32_t outputSize () const override
 Return executor set's number of output.
 
const ir::OperandInfoinputInfo (const ir::IOIndex &index) const override
 Return NN package input tensor info.
 
const ir::OperandInfooutputInfo (const ir::IOIndex &index) const override
 Return NN package output tensor info.
 
void execute (const ExecutionContext &ctx) override
 Execute NN package executor set.
 
- Public Member Functions inherited from onert::exec::IExecutors
virtual ~IExecutors ()=default
 Virtual IExecutors destructor.
 
IExecutorentryExecutor () const
 

Detailed Description

Class to gather executor set for single model NN package.

Definition at line 31 of file SingleModelExecutors.h.

Constructor & Destructor Documentation

◆ SingleModelExecutors() [1/3]

onert::exec::SingleModelExecutors::SingleModelExecutors ( void  )
default

Construct a new SingleModelExecutors object.

◆ SingleModelExecutors() [2/3]

onert::exec::SingleModelExecutors::SingleModelExecutors ( const SingleModelExecutors )
delete

◆ SingleModelExecutors() [3/3]

onert::exec::SingleModelExecutors::SingleModelExecutors ( SingleModelExecutors &&  )
default

◆ ~SingleModelExecutors()

onert::exec::SingleModelExecutors::~SingleModelExecutors ( )
default

Destroy the SingleModelExecutors object.

Member Function Documentation

◆ at()

IExecutor * onert::exec::SingleModelExecutors::at ( const ir::ModelIndex model_index,
const ir::SubgraphIndex subg_index 
) const
overridevirtual

Return executor of index.

Parameters
[in]model_indexModel index
[in]subg_indexSubgraph index
Returns
Executor

Implements onert::exec::IExecutors.

Definition at line 34 of file SingleModelExecutors.cc.

36{
37 return _executors.at(subg_index).get();
38}

◆ emplace()

void onert::exec::SingleModelExecutors::emplace ( const ir::ModelIndex model_index,
const ir::SubgraphIndex subg_index,
std::unique_ptr< IExecutor exec 
)
overridevirtual

Insert executor in executor set.

Parameters
[in]model_indexModel index
[in]subg_indexSubgraph index
[in]execExecutor to insert

Implements onert::exec::IExecutors.

Definition at line 28 of file SingleModelExecutors.cc.

30{
31 _executors.emplace(subg_index, std::move(exec));
32}

◆ execute()

void onert::exec::SingleModelExecutors::execute ( const ExecutionContext ctx)
overridevirtual

Execute NN package executor set.

Parameters
[in]ctxExecution context

Implements onert::exec::IExecutors.

Definition at line 54 of file SingleModelExecutors.cc.

55{
56 // UserTensor for Input/Output
57 std::vector<std::unique_ptr<backend::builtin::UserTensor>> tensorpool;
58
59 // EdgeTensor for Input Quantization / Output Dequantization
60 std::vector<std::unique_ptr<EdgeTensor>> qtensorpool;
61
62 // Input/Output Tensor vector for executor
63 std::vector<backend::IPortableTensor *> inputs(ctx.desc.inputs.size());
64 std::vector<backend::IPortableTensor *> outputs(ctx.desc.outputs.size());
65
66 // Vector for input quantization I/O
67 std::vector<backend::ITensor *> input_tensors;
68 std::vector<backend::ITensor *> input_qtensors;
69 std::vector<ir::PermuteType> input_permute_types;
70
71 // Vector for output dequantization I/O
72 std::vector<backend::ITensor *> output_qtensors;
73 std::vector<backend::ITensor *> output_tensors;
74 std::vector<ir::PermuteType> output_permute_types;
75
76 // Prepare UserTensor and EdgeTensor for input quantization
77 for (uint32_t i = 0; i < inputs.size(); i++)
78 {
79 auto &desc = ctx.desc.inputs[i];
80
81 // Input is optional if buffer is nullptr, and optional input's size is 0
82 if (desc->buffer == nullptr && (desc->size != 0 || desc->info.total_size() != 0))
83 throw std::runtime_error{"Input " + std::to_string(i) + "'s buffer is not set."};
84
85 tensorpool.emplace_back(std::make_unique<backend::builtin::UserTensor>(
86 desc->info, desc->layout, const_cast<uint8_t *>(static_cast<const uint8_t *>(desc->buffer)),
87 desc->size));
88
89 auto user_type = desc->info.typeInfo().type();
90 auto &model_info = entryExecutor()->inputInfo(i).typeInfo();
91 auto model_type = model_info.type();
92 if ((user_type != model_type && user_type == ir::DataType::FLOAT32) ||
93 (desc->layout == ir::Layout::NCHW))
94 {
95 auto quantized_info = desc->info;
96 quantized_info.typeInfo(model_info);
97 qtensorpool.emplace_back(
98 std::make_unique<EdgeTensor>(quantized_info, entryExecutor()->inputLayout(i)));
99 qtensorpool.back()->allocate_buffer();
100
101 input_tensors.push_back(tensorpool.back().get());
102 input_qtensors.push_back(qtensorpool.back().get());
103 inputs[i] = qtensorpool.back().get();
104 if (desc->layout == ir::Layout::NCHW)
105 input_permute_types.push_back(ir::PermuteType::NCHW_TO_NHWC);
106 else
107 input_permute_types.push_back(ir::PermuteType::COPY);
108 }
109 else
110 inputs[i] = tensorpool.back().get();
111 }
112
113 // Prepare UserTensor and EdgeTensor for output dequantization
114 for (uint32_t i = 0; i < outputs.size(); i++)
115 {
116 auto &desc = ctx.desc.outputs[i];
117
118 // Output is optional if buffer is nullptr, and optional output's size is 0
119 if (desc->buffer == nullptr && (desc->size != 0 || desc->info.total_size() != 0))
120 throw std::runtime_error{"Output " + std::to_string(i) + "'s buffer is not set."};
121
122 tensorpool.emplace_back(std::make_unique<backend::builtin::UserTensor>(
123 desc->info, desc->layout, static_cast<uint8_t *>(desc->buffer), desc->size));
124
125 auto user_type = desc->info.typeInfo().type();
126 auto &model_info = entryExecutor()->outputInfo(i).typeInfo();
127 auto model_type = model_info.type();
128 if ((user_type != model_type && user_type == ir::DataType::FLOAT32) ||
129 (desc->layout == ir::Layout::NCHW))
130 {
131 auto quantized_info = desc->info;
132 quantized_info.typeInfo(model_info);
133 qtensorpool.emplace_back(
134 std::make_unique<EdgeTensor>(quantized_info, entryExecutor()->outputLayout(i)));
135 qtensorpool.back()->allocate_buffer();
136
137 output_qtensors.push_back(qtensorpool.back().get());
138 output_tensors.push_back(tensorpool.back().get());
139 outputs[i] = qtensorpool.back().get();
140 if (desc->layout == ir::Layout::NCHW)
141 output_permute_types.push_back(ir::PermuteType::NHWC_TO_NCHW);
142 else
143 output_permute_types.push_back(ir::PermuteType::COPY);
144 }
145 else
146 outputs[i] = tensorpool.back().get();
147 }
148
149 // Run quantization
150 if (input_tensors.size() > 0)
151 {
152 auto input_quantize_layer = PermuteLayer(input_tensors, input_qtensors, input_permute_types);
153 input_quantize_layer.prepare();
154 input_quantize_layer.run();
155 }
156
157 // Executor
158 entryExecutor()->execute(inputs, outputs, ctx.options);
159
160 // Run dequantization
161 if (output_tensors.size() != 0)
162 {
163 auto output_dequantize_layer =
164 PermuteLayer(output_qtensors, output_tensors, output_permute_types);
165 output_dequantize_layer.prepare();
166 output_dequantize_layer.run();
167 }
168
169 // Get dynamic shape inference result
170 for (uint32_t i = 0; i < outputs.size(); i++)
171 {
172 if (ctx.desc.outputs[i]->buffer == nullptr)
173 {
174 // Output is optional if buffer is nullptr
175 continue;
176 }
177
178 ctx.desc.outputs[i]->info.shape(outputs[i]->getShape());
179 }
180}
IExecutor * entryExecutor() const
Definition IExecutors.h:60
const TypeInfo & typeInfo() const
Return tensor data type info.
DataType type() const
Definition TypeInfo.h:52
nnfw::cker::Shape getShape(const IPortableTensor *tensor)
virtual void execute(const std::vector< backend::IPortableTensor * > &inputs, const std::vector< backend::IPortableTensor * > &outputs, const ExecutionOptions &options)=0
Execute with given input/output tensors.
virtual const ir::OperandInfo & inputInfo(uint32_t index) const =0
Get input info at index.
virtual const ir::OperandInfo & outputInfo(uint32_t index) const =0
Get output info at index.

References onert::ir::COPY, onert::exec::ExecutionContext::desc, onert::exec::IExecutors::entryExecutor(), onert::exec::IExecutor::execute(), onert::exec::IExecutor::inputInfo(), onert::exec::IODescription::inputs, onert::ir::NCHW, onert::ir::NCHW_TO_NHWC, onert::ir::NHWC_TO_NCHW, onert::exec::ExecutionContext::options, onert::exec::IExecutor::outputInfo(), onert::exec::IODescription::outputs, onert::ir::TypeInfo::type(), and onert::ir::OperandInfo::typeInfo().

◆ inputInfo()

const ir::OperandInfo & onert::exec::SingleModelExecutors::inputInfo ( const ir::IOIndex index) const
overridevirtual

Return NN package input tensor info.

Parameters
[in]indexInput index
Returns
Tensor info

Implements onert::exec::IExecutors.

Definition at line 44 of file SingleModelExecutors.cc.

45{
46 return entryExecutor()->inputInfo(index.value());
47}
loco::GraphInputIndex index(const TFPlaceholder *node)
Definition TFNode.cpp:54

References onert::exec::IExecutors::entryExecutor(), and onert::exec::IExecutor::inputInfo().

◆ inputSize()

uint32_t onert::exec::SingleModelExecutors::inputSize ( ) const
overridevirtual

Return executor set's number of input.

Returns
Number of input

Implements onert::exec::IExecutors.

Definition at line 40 of file SingleModelExecutors.cc.

40{ return entryExecutor()->inputSize(); }
virtual uint32_t inputSize() const =0
Get input size.

References onert::exec::IExecutors::entryExecutor(), and onert::exec::IExecutor::inputSize().

◆ outputInfo()

const ir::OperandInfo & onert::exec::SingleModelExecutors::outputInfo ( const ir::IOIndex index) const
overridevirtual

Return NN package output tensor info.

Parameters
[in]indexOutput index
Returns
Tensor info

Implements onert::exec::IExecutors.

Definition at line 49 of file SingleModelExecutors.cc.

50{
51 return entryExecutor()->outputInfo(index.value());
52}

References onert::exec::IExecutors::entryExecutor(), and onert::exec::IExecutor::outputInfo().

◆ outputSize()

uint32_t onert::exec::SingleModelExecutors::outputSize ( ) const
overridevirtual

Return executor set's number of output.

Returns
Number of output

Implements onert::exec::IExecutors.

Definition at line 42 of file SingleModelExecutors.cc.

42{ return entryExecutor()->outputSize(); }
virtual uint32_t outputSize() const =0
Get output size.

References onert::exec::IExecutors::entryExecutor(), and onert::exec::IExecutor::outputSize().


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