ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 35 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 39 of file Compiler.cc.

40 : _model{model}, _options{copts}
41{
42 // DO NOTHING
43}

◆ 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 45 of file Compiler.cc.

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

◆ ~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 52 of file Compiler.cc.

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