ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onert::compiler::Compiler Class Reference

Class to compile NN package. More...

#include <Compiler.h>

Collaboration diagram for onert::compiler::Compiler:

Public Member Functions

 Compiler (const std::shared_ptr< ir::Model > &model, CompilerOptions *copts)
 Construct a new Compiler object for single model.
 
 Compiler (const std::shared_ptr< ir::NNPkg > &nnpkg, CompilerOptions *copts)
 Construct a new Compiler object for NN package.
 
 ~Compiler ()=default
 Destroy the Compiler object.
 
std::shared_ptr< CompilerArtifactcompile (void)
 Do compilation with the options.
 
- Public Member Functions inherited from onert::compiler::ICompiler
virtual ~ICompiler ()=default
 Virtual ICompiler destructor.
 

Detailed Description

Class to compile NN package.

Definition at line 37 of file Compiler.h.

Constructor & Destructor Documentation

◆ Compiler() [1/2]

onert::compiler::Compiler::Compiler ( const std::shared_ptr< ir::Model > &  model,
CompilerOptions copts 
)

Construct a new Compiler object for single model.

Parameters
[in]modelmodel to compile
[in]coptsCompiler Options

Definition at line 41 of file Compiler.cc.

42 : _model{model}, _options{copts}
43{
44 // DO NOTHING
45}

◆ Compiler() [2/2]

onert::compiler::Compiler::Compiler ( const std::shared_ptr< ir::NNPkg > &  nnpkg,
CompilerOptions copts 
)

Construct a new Compiler object for NN package.

Parameters
[in]nnpkgNN package to compile
[in]coptsCompiler option for package

Definition at line 47 of file Compiler.cc.

48 : _model{nnpkg->primary_model()}, _options{copts}
49{
50 // Use for single model only
51 assert(nnpkg->model_count() == 1);
52}

◆ ~Compiler()

onert::compiler::Compiler::~Compiler ( )
default

Destroy the Compiler object.

Member Function Documentation

◆ compile()

std::shared_ptr< CompilerArtifact > onert::compiler::Compiler::compile ( void  )
virtual

Do compilation with the options.

Returns
std::shared_ptr<CompilerArtifact> Executors as a result of compilation

Implements onert::compiler::ICompiler.

Definition at line 54 of file Compiler.cc.

55{
56 /***************************************************
57 * Prepare compilation phase
58 ***************************************************/
59 if (!_options)
60 throw std::runtime_error{"Empty compile option"};
61
62 // Mode check
63 // TODO handle option for each model
64 if (_options->he_profiling_mode)
65 {
66 if (!_options->he_scheduler)
67 throw std::runtime_error("Heterogeneous scheduler must be enabled during profiling.");
68
69 if (_options->executor != "Dataflow")
70 throw std::runtime_error("Profiling mode works only with 'Dataflow' executor");
71 }
72
73 if (!_model->hasOnly<ir::Graph>())
74 {
75 throw std::runtime_error("Compiler can only compile models for inference.");
76 }
77
78 _options->forceInternalOptions();
79 _options->verboseOptions();
80
81 auto custom_kernel_builder = _model->getKernelBuilder();
82
83 _model->iterate([&](const ir::SubgraphIndex &, ir::IGraph &graph) {
84 auto &subg = nnfw::misc::polymorphic_downcast<ir::Graph &>(graph);
85
86 // Mandatory passes
87 pass::PassRunner{}
88 .append(std::make_unique<pass::ConstantOutputPass>(subg))
89 .append(std::make_unique<pass::OddOutputPass>(subg))
90 .run();
91
92 // Optimizations
93 pass::PassRunner{}.append(std::make_unique<pass::UnusedOperandEliminationPass>(subg)).run();
94 });
95
96 /***************************************************
97 * Backend independent analysis & optimization phase
98 ***************************************************/
99 // TODO Handle dump level for each model
100 auto dump_level = static_cast<dumper::dot::DotDumper::Level>(_options->graph_dump_level);
101 onert::dumper::dot::DotDumper dot_dumper(dump_level);
102
103 // Tracing context
104 auto tracing_ctx = std::make_unique<util::TracingCtx>();
105
106 // Lower: Assign backend
107 std::unordered_map<ir::SubgraphIndex, std::unique_ptr<compiler::LoweredGraph>> lowered_subgs;
108 {
109 _model->iterate([&](const ir::SubgraphIndex &subg_index, ir::IGraph &graph) {
110 auto &subg = nnfw::misc::polymorphic_downcast<ir::Graph &>(graph);
111
112 // Lower: Assign backend
113 lowered_subgs[subg_index] = std::make_unique<compiler::LoweredGraph>(subg, *_options);
114 // Set tracing_ctx for copied graph
115 tracing_ctx->setSubgraphIndex(&(lowered_subgs[subg_index]->graph()), subg_index.value());
116 });
117 }
118
119 _model.reset();
120
121 for (const auto &[subg_index, lowered_subg] : lowered_subgs)
122 {
123 dot_dumper.dump(*lowered_subg, nnfw::misc::str("after_lower_subg-", subg_index.value()));
124 }
125
126 // Shape inference.
127 {
128 // Run the StaticShapeInfer of primary subg. All child StaticShapeInferers are called
129 // recursively
130 std::unordered_map<ir::SubgraphIndex, std::unique_ptr<StaticShapeInferer>> inferers =
131 createStaticShapeInferers(lowered_subgs);
132
133 const auto primary_subg_idx = ir::SubgraphIndex{0};
134 inferers.at(primary_subg_idx)->infer();
135
136 for (const auto &pair_inferer : inferers)
137 {
138 const auto inferer = pair_inferer.second.get();
139 inferer->dump();
140 }
141 }
142
143 // Shape validation
144 // TODO Move shape independent feature check from ShapeValidator to OperationValidator
145 // TODO Move ShapeValidator into shape inference
146 // - Check input tensor shape validation
147 // - Check parameter value validation which valid value is depend on input tensor shape
148 // - Output tensor shape validation check is needless because
149 // static/dynamic shape inferer will make valid output shape
150 for (const auto &pair : lowered_subgs)
151 {
152 auto &lowered_subg = pair.second;
153 compiler::ShapeValidator{lowered_subg->graph()}();
154 }
155
156 /*************************************************************
157 * Backend independent analysis & optimization phase finished
158 *************************************************************/
159 auto executors = std::make_shared<exec::SingleModelExecutors>();
160 for (auto &&[subg_index, lowered_subg] : lowered_subgs)
161 {
162 auto const model_index = ir::ModelIndex{0};
163 auto const indexed_ranks = lowered_subg->indexed_ranks();
164
165 ir::OperationDumper dumper("Executor generation of Subgraph " +
166 std::to_string(subg_index.value()));
167 lowered_subg->graph().operations().iterate(
168 [&](const ir::OperationIndex &, const ir::IOperation &op) { op.accept(dumper); });
169
170 ExecutorFactoryArgs args;
171 args.tracing_ctx = tracing_ctx.get();
172 args.options = _options;
173 args.model_index = model_index;
174 args.custom_kernel_builder = custom_kernel_builder;
175 auto executor = std::unique_ptr<exec::IExecutor>{
176 ExecutorFactory::get().create(std::move(lowered_subg), executors, args)};
177 executor->setIndexedRanks(indexed_ranks);
178 executors->emplace(model_index, subg_index, std::move(executor));
179 }
180
181 /********************************
182 * Code generation phase finished
183 ********************************/
184 return std::make_shared<CompilerArtifact>(executors, std::move(tracing_ctx));
185}
exec::IExecutor * create(std::unique_ptr< compiler::LoweredGraph > lowered_graph, const std::shared_ptr< exec::IExecutors > &executors, const ExecutorFactoryArgs &args)
static ExecutorFactory & get()
args
Definition infer.py:21
std::string str(Args &&...args)
::onert::util::Index< uint32_t, OperationIndexTag > OperationIndex
Definition Index.h:32
::onert::util::Index< uint16_t, ModelIndexTag > ModelIndex
Definition Index.h:44
::onert::util::Index< uint16_t, SubgraphIndexTag > SubgraphIndex
Definition Index.h:41
void forceInternalOptions()
Force default values of CompilerOptions for correct compilations.
void verboseOptions()
Print option value.
virtual void setIndexedRanks(std::shared_ptr< ir::OperationIndexMap< int64_t > >)=0
Set an ordering on operations.

References onert::ir::IOperation::accept(), onert::compiler::pass::PassRunner::append(), onert::compiler::ExecutorFactory::create(), onert::dumper::dot::DotDumper::dump(), onert::compiler::CompilerOptions::executor, onert::compiler::CompilerOptions::forceInternalOptions(), onert::compiler::ExecutorFactory::get(), onert::compiler::CompilerOptions::graph_dump_level, onert::compiler::CompilerOptions::he_profiling_mode, onert::compiler::CompilerOptions::he_scheduler, onert::compiler::pass::PassRunner::run(), onert::exec::IExecutor::setIndexedRanks(), nnfw::misc::str(), onert::util::Index< T, DummyTag >::value(), and onert::compiler::CompilerOptions::verboseOptions().


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