3098be7ba0e15feaad89008a48657988939776e6
[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
28 namespace onert
29 {
30
31 namespace compiler
32 {
33
34 enum class State
35 {
36   CREATED, // Before compilation
37   COMPILED // Success compilation
38 };
39
40 struct ManualSchedulerOptions
41 {
42   std::string backend_for_all;
43   std::unordered_map<ir::OpCode, std::string> opcode_to_backend;
44   std::unordered_map<ir::OperationIndex, std::string> index_to_backend;
45 };
46
47 struct CompilerOptions
48 {
49   // GENERAL OPTIONS
50   std::vector<std::string> backend_list;
51   bool is_primary_subgraph; // TODO Remove this out of this struct as it is not user-given option
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
65 CompilerOptions fetchCompilerOptionsFromGlobalConfig(const ir::Subgraphs &subgs);
66
67 /**
68  * @brief Class to compile graph model
69  */
70 class Compiler
71 {
72 public:
73   /**
74    * @brief     Construct a new Compiler object
75    * @param[in] subgs All subgraphs of a model
76    */
77   Compiler(const std::shared_ptr<ir::Subgraphs> &subgs);
78
79 public:
80   /**
81    * @brief   Do compilation with the options
82    *
83    * @return std::shared_ptr<exec::ExecutorMap> Executors as a result of compilation
84    */
85   std::shared_ptr<exec::ExecutorMap> compile(void);
86
87   State state(void) const { return _state; }
88
89   /**
90    * @brief   Check if model can compile
91    * @return  @c true if model can compile, otherwise @c false
92    * @note    This method don't check model correctness,\n
93    *          so model verification should be done before calling this method
94    */
95   bool checkCompilable();
96   CompilerOptions &options() { return _options; }
97
98   /**
99    * @brief   Allow to compute float32 using float16 data type
100    */
101   void enableToFp16();
102
103 private:
104   void checkProfilerConditions();
105   std::shared_ptr<ir::Graph> &primary_subgraph() { return _subgraphs->at(ir::SubgraphIndex{0}); }
106
107 private:
108   std::shared_ptr<ir::Subgraphs> _subgraphs;
109   // NOTE These executors does not have duplicated subgraph. This mean they do not allow support
110   // subgraphs being called recursively because data of non-constant tensor of parent executor will
111   // be updated by child executor. If you want to support subgraphs being called recursively, you
112   // have to add allocate non-constant tensor memory of executors in execution time when each
113   // subgraph is called.
114   State _state;
115   CompilerOptions _options;
116 };
117
118 } // namespace compiler
119
120 } // namespace onert
121
122 #endif // __ONERT_COMPILER_COMPILE_H_