ONE - On-device Neural Engine
Loading...
Searching...
No Matches
TensorRegistry.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_TENSOR_REGISTRY__
18#define __ONERT_BACKEND_TRAIN_TENSOR_REGISTRY__
19
22
25#include "Tensor.h"
26
27namespace onert
28{
29namespace backend
30{
31namespace train
32{
33
35 : public PortableTensorRegistryTemplate<Tensor, TrainableTensor, BackPropTensor, GradientTensor>
36{
37public:
39 {
40 auto itr = _disposable_back_prop.find(index);
41 if (itr != _disposable_back_prop.end())
42 return itr->second.get();
43
44 return nullptr;
45 }
46
48 std::unique_ptr<BackPropTensor> tensor)
49 {
50 assert(tensor != nullptr);
51 auto itr = _disposable_back_prop.find(index);
52 if (itr != _disposable_back_prop.end())
53 throw std::runtime_error{
54 "Tried to set a disposable tensor but another disposable tensor already exists."};
55
56 _disposable_back_prop[index] = std::move(tensor);
57 }
58
59 const std::unordered_map<DisposableTensorIndex, std::unique_ptr<BackPropTensor>> &
61 {
62 return _disposable_back_prop;
63 }
64
65 std::shared_ptr<LayerScopeTensor> getLayerScopeTensor(const LayerScopeTensorIndex &index)
66 {
67 auto itr = _layer_scope.find(index);
68 if (itr != _layer_scope.end())
69 return itr->second;
70
71 return nullptr;
72 }
73
75 std::shared_ptr<LayerScopeTensor> &tensor)
76 {
77 assert(tensor != nullptr);
78 auto itr = _layer_scope.find(index);
79 if (itr != _layer_scope.end())
80 throw std::runtime_error{
81 "Tried to set a layer scope tensor but another layer scope tensor already exists."};
82
83 _layer_scope[index] = tensor;
84 }
85
86 const std::unordered_map<LayerScopeTensorIndex, std::shared_ptr<LayerScopeTensor>> &
88 {
89 return _layer_scope;
90 }
91
92private:
93 // Disposable Tensors to be accumulated to BackPropTensor
94 std::unordered_map<DisposableTensorIndex, std::unique_ptr<BackPropTensor>> _disposable_back_prop;
95 std::unordered_map<LayerScopeTensorIndex, std::shared_ptr<LayerScopeTensor>> _layer_scope;
96};
97
98} // namespace train
99} // namespace backend
100} // namespace onert
101
102#endif // __ONERT_BACKEND_TRAIN_TENSOR_REGISTRY__
Class that is index of DisposableTensor.
std::shared_ptr< LayerScopeTensor > getLayerScopeTensor(const LayerScopeTensorIndex &index)
BackPropTensor * getDisposableBackPropTensor(const DisposableTensorIndex &index)
const std::unordered_map< LayerScopeTensorIndex, std::shared_ptr< LayerScopeTensor > > & layerscope_tensors()
void setLayerScopeTensor(const LayerScopeTensorIndex &index, std::shared_ptr< LayerScopeTensor > &tensor)
void setDisposableBackPropTensor(const DisposableTensorIndex &index, std::unique_ptr< BackPropTensor > tensor)
const std::unordered_map< DisposableTensorIndex, std::unique_ptr< BackPropTensor > > & disposable_back_prop_tensors()