Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / onert / core / include / compiler / Compiler.h
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file  Compiler.h
19  * @brief This file contains Compiler class to define and run compilation phase
20  */
21
22 #ifndef __ONERT_COMPILER_COMPILE_H_
23 #define __ONERT_COMPILER_COMPILE_H_
24
25 #include "ir/Graph.h"
26 #include "exec/IExecutor.h"
27 #include "util/TracingCtx.h"
28
29 namespace onert
30 {
31
32 namespace compiler
33 {
34
35 enum class State
36 {
37   CREATED, // Before compilation
38   COMPILED // Success compilation
39 };
40
41 struct ManualSchedulerOptions
42 {
43   std::string backend_for_all;
44   std::unordered_map<ir::OpCode, std::string> opcode_to_backend;
45   std::unordered_map<ir::OperationIndex, std::string> index_to_backend;
46 };
47
48 struct CompilerOptions
49 {
50   // GENERAL OPTIONS
51   std::vector<std::string> backend_list;
52
53   // OPTIONS ONLY FOR DEBUGGING/PROFILING
54   std::string trace_filepath; //< File path to save trace records
55   int graph_dump_level;       //< Graph dump level, values between 0 and 2 are valid
56   int op_seq_max_node;        //< Number of nodes that can be
57   std::string executor;       //< Executor name to use
58   ManualSchedulerOptions manual_scheduler_options; //< Options for ManualScheduler
59   bool he_scheduler;      //< HEScheduler if true, ManualScheduler otherwise
60   bool he_profiling_mode; //< Whether HEScheduler profiling mode ON/OFF
61   bool disable_compile;   //< Run with Interpreter if true, try compilation otherwise
62   bool fp16_enable;       //< Whether fp16 mode ON/OFF
63
64   util::TracingCtx *tracing_ctx; //< Profiling information
65 };
66
67 CompilerOptions fetchCompilerOptionsFromGlobalConfig(const ir::Subgraphs &subgs);
68
69 /**
70  * @brief Class to compile graph model
71  */
72 class Compiler
73 {
74 public:
75   /**
76    * @brief     Construct a new Compiler object
77    * @param[in] subgs All subgraphs of a model
78    * @param[in] tracing_ctx Profiling information
79    */
80   Compiler(const std::shared_ptr<ir::Subgraphs> &subgs, util::TracingCtx *tracing_ctx);
81
82 public:
83   /**
84    * @brief   Do compilation with the options
85    *
86    * @return std::shared_ptr<exec::ExecutorMap> Executors as a result of compilation
87    */
88   std::shared_ptr<exec::ExecutorMap> compile(void);
89
90   State state(void) const { return _state; }
91
92   /**
93    * @brief   Check if model can compile
94    * @return  @c true if model can compile, otherwise @c false
95    * @note    This method don't check model correctness,\n
96    *          so model verification should be done before calling this method
97    */
98   bool checkCompilable();
99   CompilerOptions &options() { return _options; }
100
101   /**
102    * @brief   Allow to compute float32 using float16 data type
103    */
104   void enableToFp16();
105
106 private:
107   void checkProfilerConditions();
108   std::shared_ptr<ir::Graph> &primary_subgraph() { return _subgraphs->at(ir::SubgraphIndex{0}); }
109
110 private:
111   std::shared_ptr<ir::Subgraphs> _subgraphs;
112   // NOTE These executors does not have duplicated subgraph. This mean they do not allow support
113   // subgraphs being called recursively because data of non-constant tensor of parent executor will
114   // be updated by child executor. If you want to support subgraphs being called recursively, you
115   // have to add allocate non-constant tensor memory of executors in execution time when each
116   // subgraph is called.
117   State _state;
118   CompilerOptions _options;
119 };
120
121 } // namespace compiler
122
123 } // namespace onert
124
125 #endif // __ONERT_COMPILER_COMPILE_H_