ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
28{
29
31 : public PortableTensorRegistryTemplate<Tensor, TrainableTensor, BackPropTensor, GradientTensor>
32{
33public:
35 {
36 auto itr = _disposable_back_prop.find(index);
37 if (itr != _disposable_back_prop.end())
38 return itr->second.get();
39
40 return nullptr;
41 }
42
44 std::unique_ptr<BackPropTensor> tensor)
45 {
46 assert(tensor != nullptr);
47 auto itr = _disposable_back_prop.find(index);
48 if (itr != _disposable_back_prop.end())
49 throw std::runtime_error{
50 "Tried to set a disposable tensor but another disposable tensor already exists."};
51
52 _disposable_back_prop[index] = std::move(tensor);
53 }
54
55 const std::unordered_map<DisposableTensorIndex, std::unique_ptr<BackPropTensor>> &
57 {
58 return _disposable_back_prop;
59 }
60
61 std::shared_ptr<LayerScopeTensor> getLayerScopeTensor(const LayerScopeTensorIndex &index)
62 {
63 auto itr = _layer_scope.find(index);
64 if (itr != _layer_scope.end())
65 return itr->second;
66
67 return nullptr;
68 }
69
71 std::shared_ptr<LayerScopeTensor> &tensor)
72 {
73 assert(tensor != nullptr);
74 auto itr = _layer_scope.find(index);
75 if (itr != _layer_scope.end())
76 throw std::runtime_error{
77 "Tried to set a layer scope tensor but another layer scope tensor already exists."};
78
79 _layer_scope[index] = tensor;
80 }
81
82 const std::unordered_map<LayerScopeTensorIndex, std::shared_ptr<LayerScopeTensor>> &
84 {
85 return _layer_scope;
86 }
87
88private:
89 // Disposable Tensors to be accumulated to BackPropTensor
90 std::unordered_map<DisposableTensorIndex, std::unique_ptr<BackPropTensor>> _disposable_back_prop;
91 std::unordered_map<LayerScopeTensorIndex, std::shared_ptr<LayerScopeTensor>> _layer_scope;
92};
93
94} // namespace onert::backend::train
95
96#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()