Imported Upstream version 1.12.0
[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 #include "ir/LowerInfoMap.h"
23 #include "exec/FunctionSequence.h"
24
25 namespace onert
26 {
27 namespace backend
28 {
29
30 class Backend;
31 struct ITensorRegistry;
32
33 using FunctionMap =
34     std::vector<std::pair<ir::OpSequenceIndex, std::unique_ptr<exec::FunctionSequence>>>;
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       : _backend{backend}, _graph{graph}, tensor_registry{tensor_registry}
51   {
52   }
53
54   virtual ~BackendContext() = default;
55
56   void initialize(const std::vector<OperationInfo> &operation_list,
57                   const std::vector<ir::OperandIndex> &operand_list);
58   void initConsts();
59
60   const Backend *backend() const { return _backend; }
61   const ir::Graph *graph() const { return _graph; }
62   const std::vector<OperationInfo> &operation_list() const { return _operation_list; }
63   const std::vector<ir::OperandIndex> &operand_list() const { return _operand_list; }
64
65   virtual ITensorRegistry *genTensors(const std::vector<onert::ir::OpSequenceIndex> &,
66                                       const ir::OpSequences &, const ir::LowerInfoMap &)
67   {
68     return nullptr;
69   }
70   virtual FunctionMap genKernels(const std::vector<onert::ir::OpSequenceIndex> &,
71                                  const ir::OpSequences &)
72   {
73     return {};
74   }
75
76 private:
77   const Backend *_backend{nullptr};
78   const ir::Graph *_graph{nullptr};
79   std::vector<OperationInfo> _operation_list;
80   std::vector<ir::OperandIndex> _operand_list;
81
82 public:
83   std::shared_ptr<ITensorRegistry> tensor_registry;
84 };
85
86 using BackendContexts = std::unordered_map<const Backend *, std::unique_ptr<BackendContext>>;
87
88 } // namespace backend
89 } // namespace onert
90
91 #endif // __ONERT_BACKEND_BACKEND_CONTEXT_H__