2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __ONERT_BACKEND_BUILTIN_BACKEND_H__
18 #define __ONERT_BACKEND_BUILTIN_BACKEND_H__
20 #include "BackendContext.h"
22 #include "KernelGenerator.h"
23 #include "TensorBuilder.h"
26 #include "train/BackendContext.h"
27 #include "train/KernelGenerator.h"
28 #include "train/TensorRegistry.h"
31 #include <backend/Backend.h>
33 #include <backend/train/ITrainableBackend.h>
45 class Backend : public ::onert::backend::Backend
48 public backend::train::ITrainableBackend
52 Backend() : _config{std::make_shared<Config>()} {}
54 std::shared_ptr<IConfig> config() const override { return _config; }
56 std::unique_ptr<onert::backend::BackendContext> newContext(ContextData &&data) const override
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
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
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());
86 std::unique_ptr<backend::train::TrainableBackendContext>
87 newContext(backend::train::TrainableContextData &&tdata) const override
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);
96 std::make_shared<train::KernelGenerator>(tgraph, tr, context->external_context());
102 std::shared_ptr<IConfig> _config;
105 } // namespace builtin
106 } // namespace backend
109 #endif // __ONERT_BACKEND_BUILTIN_BACKEND_H__