Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / train / Backend.h
1 /*
2  * Copyright (c) 2023 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_TRAIN_BACKEND_H__
18 #define __ONERT_BACKEND_TRAIN_BACKEND_H__
19
20 #include "BackendContext.h"
21 #include "Config.h"
22 #include "KernelGenerator.h"
23
24 #include <backend/Backend.h>
25 #include <backend/train/ITrainableBackend.h>
26
27 #include <memory>
28
29 namespace onert
30 {
31 namespace backend
32 {
33 namespace train
34 {
35
36 // TODO Unify TensorBuilder
37 // TODO Unify TensorRegistry
38 class Backend : public ::onert::backend::Backend, public backend::train::ITrainableBackend
39 {
40 public:
41   Backend() : _config{std::make_shared<Config>()} {}
42
43   std::shared_ptr<IConfig> config() const override { return _config; }
44
45   std::unique_ptr<onert::backend::BackendContext> newContext(ContextData &&data) const override
46   {
47     return std::make_unique<DummyBackendContext>(this, std::move(data));
48   }
49
50   std::unique_ptr<backend::train::TrainableBackendContext>
51   newContext(backend::train::TrainableContextData &&tdata) const override
52   {
53     const auto &tgraph = *tdata.tgraph;
54     auto tr = std::make_shared<TensorRegistry>();
55     auto tb = std::make_shared<TensorBuilder>(tr, "Bump");
56     auto tdata_ptr = std::make_unique<backend::train::TrainableContextData>(std::move(tdata));
57     auto context = std::make_unique<train::BackendContext>(this, std::move(tdata_ptr), tr, tb);
58
59     context->kernel_gen = std::make_shared<train::KernelGenerator>(
60       tgraph, tr, context->external_context(), context->data()->optimizer);
61     return context;
62   }
63
64 private:
65   std::shared_ptr<IConfig> _config;
66 };
67
68 } // namespace train
69 } // namespace backend
70 } // namespace onert
71
72 #endif // __ONERT_BACKEND_TRAIN_BACKEND_H__