1eba295508de1b0e235331a00096245390d6b9bb
[platform/core/ml/nnfw.git] / runtime / onert / core / include / backend / BackendContext.h
1 /*
2  * Copyright (c) 2020 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 #ifndef __ONERT_BACKEND_BACKEND_CONTEXT_H__
18 #define __ONERT_BACKEND_BACKEND_CONTEXT_H__
19
20 #include <memory>
21 #include "ir/Graph.h"
22
23 namespace onert
24 {
25 namespace backend
26 {
27
28 class Backend;
29 class IConstantInitializer;
30 class IKernelGenerator;
31 class ITensorRegister;
32 struct ITensorRegistry;
33 struct ITensorBuilder;
34 struct IOptimizer;
35
36 class BackendContext
37 {
38 public:
39   struct OperationInfo
40   {
41     ir::OperationIndex index;
42     ir::Layout layout;
43
44     OperationInfo(ir::OperationIndex index, ir::Layout layout) : index{index}, layout{layout} {}
45   };
46
47 public:
48   BackendContext(const Backend *backend, const ir::Graph *graph,
49                  std::shared_ptr<ITensorRegistry> tensor_registry = nullptr,
50                  std::shared_ptr<ITensorBuilder> tensor_builder = nullptr,
51                  std::shared_ptr<IConstantInitializer> constant_initializer = nullptr,
52                  std::shared_ptr<IKernelGenerator> kernel_gen = nullptr,
53                  std::shared_ptr<ITensorRegister> tensor_register = nullptr,
54                  std::shared_ptr<IOptimizer> optimizer = nullptr)
55       : _backend{backend}, _graph{graph}, tensor_registry{tensor_registry},
56         tensor_builder{tensor_builder}, constant_initializer{constant_initializer},
57         kernel_gen{kernel_gen}, tensor_register{tensor_register}, optimizer{optimizer}
58   {
59   }
60
61   virtual ~BackendContext() = default;
62
63   void initialize(const std::vector<OperationInfo> &operation_list,
64                   const std::vector<ir::OperandIndex> &operand_list);
65   void initConsts();
66
67   const Backend *backend() const { return _backend; }
68   const ir::Graph *graph() const { return _graph; }
69   const std::vector<OperationInfo> &operation_list() { return _operation_list; }
70   const std::vector<ir::OperandIndex> &operand_list() { return _operand_list; }
71
72 private:
73   const Backend *_backend{nullptr};
74   const ir::Graph *_graph{nullptr};
75   std::vector<OperationInfo> _operation_list;
76   std::vector<ir::OperandIndex> _operand_list;
77
78 public:
79   std::shared_ptr<ITensorRegistry> tensor_registry;
80   std::shared_ptr<ITensorBuilder> tensor_builder;
81   std::shared_ptr<IConstantInitializer> constant_initializer;
82   std::shared_ptr<IKernelGenerator> kernel_gen;
83   std::shared_ptr<ITensorRegister> tensor_register;
84   std::shared_ptr<IOptimizer> optimizer;
85 };
86
87 using BackendContexts = std::unordered_map<const Backend *, std::unique_ptr<BackendContext>>;
88
89 } // namespace backend
90 } // namespace onert
91
92 #endif // __ONERT_BACKEND_BACKEND_CONTEXT_H__