ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Adam.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 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_MICRO_TRAIN_TRAIN_OPTIMIZERS_ADAM_H
18#define ONERT_MICRO_TRAIN_TRAIN_OPTIMIZERS_ADAM_H
19
20#include "OMStatus.h"
24
25#include <cstdint>
26#include <unordered_map>
27
28namespace onert_micro
29{
30namespace train
31{
32namespace optimizers
33{
34
35/*
36 * Class to handle Adam optimizer
37 */
38class Adam
39{
40private:
41 // Save mapping between tensor index and internal state data with calculated exponent average
42 // squares
43 std::unordered_map<uint16_t, uint8_t *> _tensor_to_exponent_avg_squares;
44 // Save mapping between tensor index and internal state data with calculated exponent average
45 std::unordered_map<uint16_t, uint8_t *> _tensor_to_exponent_avg;
46 // Save mapping between tensor index and internal state data with calculated gradients
47 std::unordered_map<uint16_t, uint8_t *> _tensor_index_to_gradient;
48
49public:
50 Adam() = default;
51 Adam(const Adam &) = delete;
52 Adam(Adam &&) = delete;
53 Adam &operator=(const Adam &) = delete;
54 Adam &&operator=(const Adam &&) = delete;
56
57#ifdef OM_MEMORY_ESTIMATE
58 // Reset and deallocate all internal states
60
61 // Reset only gradients
63#endif // OM_MEMORY_ESTIMATE
64
65 // Reset and deallocate all internal states
66 void fullReset();
67
68 // Reset only gradients
69 void reset();
70
71 // Check is contains current state or not
72 // Needed for saving checkpoints
73 bool isReset()
74 {
75 return _tensor_to_exponent_avg_squares.empty() or _tensor_to_exponent_avg.empty();
76 }
77
78 // Get exponent and exponent squares data be tensor index
79 uint8_t *getExponentAvgDataByTensorIndex(uint16_t tensor_index);
80 uint8_t *getExponentAvgSquaresDataByTensorIndex(uint16_t tensor_index);
81 // Set exponent and exponent squares data be tensor index
82 void setExponentAvgDataByTensorIndex(uint16_t tensor_index, uint8_t *data);
83 void setExponentAvgSquaresDataByTensorIndex(uint16_t tensor_index, uint8_t *data);
84
85 // Update internal states according to Adam theory
87 core::OMRuntimeStorage &storage);
88
89 // Update weights according to Adam theory
90 OMStatus updateWeights(const OMTrainingContext &training_config, core::OMRuntimeContext &context,
92 std::unordered_map<uint16_t, core::OpTrainableRankType> &);
93};
94
95} // namespace optimizers
96} // namespace train
97} // namespace onert_micro
98
99#endif // ONERT_MICRO_TRAIN_TRAIN_OPTIMIZERS_ADAM_H
Adam & operator=(const Adam &)=delete
uint8_t * getExponentAvgSquaresDataByTensorIndex(uint16_t tensor_index)
Definition Adam.cpp:194
void setExponentAvgDataByTensorIndex(uint16_t tensor_index, uint8_t *data)
Definition Adam.cpp:203
OMStatus handle(core::OMRuntimeStorage &backward_storage, core::OMRuntimeContext &context, core::OMRuntimeStorage &storage)
Definition Adam.cpp:224
void setExponentAvgSquaresDataByTensorIndex(uint16_t tensor_index, uint8_t *data)
Definition Adam.cpp:211
OMStatus updateWeights(const OMTrainingContext &training_config, core::OMRuntimeContext &context, core::OMRuntimeStorage &storage, std::unordered_map< uint16_t, core::OpTrainableRankType > &)
Definition Adam.cpp:328
uint8_t * getExponentAvgDataByTensorIndex(uint16_t tensor_index)
Definition Adam.cpp:185
Adam && operator=(const Adam &&)=delete