Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / backend / builtin / Backend.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_BUILTIN_BACKEND_H__
18 #define __ONERT_BACKEND_BUILTIN_BACKEND_H__
19
20 #include "BackendContext.h"
21 #include "Config.h"
22 #include "KernelGenerator.h"
23 #include "TensorBuilder.h"
24 #include "Tensor.h"
25 #ifdef ONERT_TRAIN
26 #include "train/BackendContext.h"
27 #include "train/KernelGenerator.h"
28 #include "train/TensorRegistry.h"
29 #endif // ONERT_TRAIN
30
31 #include <backend/Backend.h>
32 #ifdef ONERT_TRAIN
33 #include <backend/train/ITrainableBackend.h>
34 #endif // ONERT_TRAIN
35
36 #include <memory>
37
38 namespace onert
39 {
40 namespace backend
41 {
42 namespace builtin
43 {
44
45 class Backend : public ::onert::backend::Backend
46 #ifdef ONERT_TRAIN
47   ,
48                 public backend::train::ITrainableBackend
49 #endif // ONERT_TRAIN
50 {
51 public:
52   Backend() : _config{std::make_shared<Config>()} {}
53
54   std::shared_ptr<IConfig> config() const override { return _config; }
55
56   std::unique_ptr<onert::backend::BackendContext> newContext(ContextData &&data) const override
57   {
58     auto context = std::make_unique<BackendContext>(this, std::move(data));
59     // ControlFlow backend may not build tensors for itself because the backend's operation uses
60     // tensors of other baceknd instead
61     // But the backend builds tensors in case of that the controlflow operation may have constant
62     // input or that consecutive controflow operations exist. We have to make them not to be built
63     // later
64     // 1. Constant input
65     //   These tensors cannot be dynamic tensor, so let's do it as follows:
66     //   - always skip copying
67     //   - if it is operation's input in child subgraph: register "use" as constant input of the
68     //   operations in child subgraph
69     //   - if it is child subgraph's output: register "use" as constant input of the operations
70     //   using it
71     // 2. Consecutive controflow operation's intermediate tensor
72     //   These tensors can be dynamic tensor and this is complicated to support without copying. But
73     //   there is no such case until now, let's support it later
74     // TODO Remove TensorBuilder and ConstantInitializer
75     // TODO Support Consecutive controflow operation's intermediate tensor
76     auto tr = std::make_shared<TensorRegistry>();
77     auto tb = std::make_shared<TensorBuilder>(tr);
78     context->tensor_registry = tr;
79     context->tensor_builder = tb;
80     context->kernel_gen = std::make_shared<KernelGenerator>(
81       *context->graph(), tb->dynamicTensorManager(), tr, context->external_context());
82     return context;
83   }
84
85 #ifdef ONERT_TRAIN
86   std::unique_ptr<backend::train::TrainableBackendContext>
87   newContext(backend::train::TrainableContextData &&tdata) const override
88   {
89     const auto &tgraph = *tdata.tgraph;
90     auto tr = std::make_shared<train::TensorRegistry>();
91     // TODO Create TensorBuilder if necessary
92     auto tdata_ptr = std::make_unique<backend::train::TrainableContextData>(std::move(tdata));
93     auto context = std::make_unique<train::BackendContext>(this, std::move(tdata_ptr), tr);
94
95     context->kernel_gen =
96       std::make_shared<train::KernelGenerator>(tgraph, tr, context->external_context());
97     return context;
98   }
99 #endif // ONERT_TRAIN
100
101 private:
102   std::shared_ptr<IConfig> _config;
103 };
104
105 } // namespace builtin
106 } // namespace backend
107 } // namespace onert
108
109 #endif // __ONERT_BACKEND_BUILTIN_BACKEND_H__