ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
BackendContext.h
Go to the documentation of this file.
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_CONTEXT_H__
18#define __ONERT_BACKEND_TRAIN_BACKEND_CONTEXT_H__
19
21
22#include "ExternalContext.h"
23#include "KernelGenerator.h"
24#include "TensorBuilder.h"
25
27{
28
29// TODO Remove this class if ExecutorFactory creates trainable context only once instead of
30// replacing BackendContext
32{
33public:
35 std::shared_ptr<backend::ITensorRegistry> tensor_registry = nullptr)
37 {
38 }
39
40 backend::ITensorRegistry *genTensors() override { return nullptr; }
41
43};
44
45// TODO Unify TensorBuilder
46// TODO Unify TensorRegistry
48{
49public:
50 BackendContext(const ITrainableBackend *backend, std::unique_ptr<TrainableContextData> &&tdata,
51 std::shared_ptr<backend::train::ITensorRegistry> tensor_registry = nullptr,
52 std::shared_ptr<TensorBuilder> tensor_builder = nullptr,
53 std::unique_ptr<exec::train::optimizer::Optimizer> optimizer = nullptr,
54 std::shared_ptr<KernelGenerator> kernel_gen = nullptr)
56 kernel_gen{kernel_gen}, _external_context(new ExternalContext),
57 _tensor_builder{tensor_builder}, _optimizer{std::move(optimizer)}
58 {
59 }
60 BackendContext(const BackendContext &) = delete;
61 ~BackendContext() = default;
62
63public:
65
66public:
67 FunctionMap gen() override;
68
69private:
70 void planForwardTensors();
71 void planBackwardTensors();
72 void planLayerScopeTensors(const FunctionMap &fn_map);
73
74public:
75 std::shared_ptr<ExternalContext> external_context() { return _external_context; }
76
77 const exec::train::optimizer::Optimizer *optimizer() const { return _optimizer.get(); }
78
79private:
80 FunctionMap generateFunctionMap();
81
82public:
83 // TODO Make it private
84 std::shared_ptr<KernelGenerator> kernel_gen;
85
86private:
87 // NOTE ruy context has a thread pool, and when multiple ruy contexts are created,
88 // the thread pool is also created in duplicate
89 // TODO Create one ruy context for session
90 std::shared_ptr<ExternalContext> _external_context;
91
92private:
93 std::shared_ptr<TensorBuilder> _tensor_builder;
94
95private:
96 std::unique_ptr<exec::train::optimizer::Optimizer> _optimizer;
97};
98
99} // namespace onert::backend::train
100
101#endif // __ONERT_BACKEND_TRAIN_BACKEND_CONTEXT_H__
std::shared_ptr< ITensorRegistry > tensor_registry
const ContextData & data() const
const Backend * backend() const
std::shared_ptr< ExternalContext > external_context()
BackendContext & operator=(const BackendContext &)=delete
std::shared_ptr< KernelGenerator > kernel_gen
BackendContext(const ITrainableBackend *backend, std::unique_ptr< TrainableContextData > &&tdata, std::shared_ptr< backend::train::ITensorRegistry > tensor_registry=nullptr, std::shared_ptr< TensorBuilder > tensor_builder=nullptr, std::unique_ptr< exec::train::optimizer::Optimizer > optimizer=nullptr, std::shared_ptr< KernelGenerator > kernel_gen=nullptr)
const exec::train::optimizer::Optimizer * optimizer() const
BackendContext(const BackendContext &)=delete
backend::ITensorRegistry * genTensors() override
backend::FunctionMap genKernels() override
DummyBackendContext(const Backend *backend, ContextData &&data, std::shared_ptr< backend::ITensorRegistry > tensor_registry=nullptr)
std::shared_ptr< ITensorRegistry > tensor_registry()
Base class for all optimizers.
Definition Optimizer.h:37
std::unordered_map< ir::OperationIndex, std::unique_ptr< exec::train::TrainableFnSequence > > FunctionMap
std::unordered_map< ir::OperationIndex, std::unique_ptr< exec::FunctionSequence > > FunctionMap