ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Adam.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2016 The TensorFlow Authors. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef __NNFW_CKER_TRAIN_OPTIMIZER_ADAM_H__
19#define __NNFW_CKER_TRAIN_OPTIMIZER_ADAM_H__
20
23
24#include <vector>
25
26namespace nnfw
27{
28namespace cker
29{
30namespace train
31{
32
33inline void Adam(const Shape &trainable_shape, float *trainable_data, const Shape &grad_shape,
34 const float *grad_data, const Shape &m_shape, float *m_data, const Shape &v_shape,
35 float *v_data, float beta1_power, float beta2_power, float learning_rate,
36 float beta1, float beta2, float epsilon, bool use_nesterov)
37{
38 Tensor trainable_tensor;
39 Tensor grad_tensor;
40 Tensor m_tensor;
41 Tensor v_tensor;
42 Tensor beta1_power_tensor;
43 Tensor beta2_power_tensor;
44 Tensor lr_tensor;
45 Tensor beta1_tensor;
46 Tensor beta2_tensor;
47 Tensor epsilon_tensor;
48
49 trainable_tensor.shape.ReplaceWith(trainable_shape.DimensionsCount(), trainable_shape.DimsData());
50 trainable_tensor.buffer = trainable_data;
51
52 grad_tensor.shape.ReplaceWith(grad_shape.DimensionsCount(), grad_shape.DimsData());
53 grad_tensor.buffer = const_cast<float *>(grad_data);
54
55 m_tensor.shape.ReplaceWith(m_shape.DimensionsCount(), m_shape.DimsData());
56 m_tensor.buffer = m_data;
57
58 v_tensor.shape.ReplaceWith(v_shape.DimensionsCount(), v_shape.DimsData());
59 v_tensor.buffer = v_data;
60
61 std::vector<float> beta1_power_vec{beta1_power};
62 beta1_power_tensor.buffer = beta1_power_vec.data();
63
64 std::vector<float> beta2_power_vec{beta2_power};
65 beta2_power_tensor.buffer = beta2_power_vec.data();
66
67 std::vector<float> lr_vec{learning_rate};
68 lr_tensor.buffer = lr_vec.data();
69
70 std::vector<float> beta1_vec{beta1};
71 beta1_tensor.buffer = beta1_vec.data();
72
73 std::vector<float> beta2_vec{beta2};
74 beta2_tensor.buffer = beta2_vec.data();
75
76 std::vector<float> epsilon_vec{epsilon};
77 epsilon_tensor.buffer = epsilon_vec.data();
78
79 if (trainable_shape != m_shape)
80 throw std::runtime_error("cker::Adam: output and m do not have the same shape");
81
82 if (trainable_shape != v_shape)
83 throw std::runtime_error("cker::Adam: output and v do not have the same shape");
84
85 if (trainable_shape != grad_shape)
86 throw std::runtime_error("cker::Adam: output and gradient do not have the same shape");
87
90 device, trainable_tensor.flat<float>(), m_tensor.flat<float>(), v_tensor.flat<float>(),
91 beta1_power_tensor.scalar<float>(), beta2_power_tensor.scalar<float>(),
92 lr_tensor.scalar<float>(), beta1_tensor.scalar<float>(), beta2_tensor.scalar<float>(),
93 epsilon_tensor.scalar<float>(), static_cast<const Tensor &>(grad_tensor).flat<float>(),
94 use_nesterov);
95}
96
97} // namespace train
98} // namespace cker
99} // namespace nnfw
100
101#endif // __NNFW_CKER_TRAIN_OPTIMIZER_ADAM_H__
int32_t DimensionsCount() const
Definition Shape.h:91
void ReplaceWith(int dimensions_count, const int32_t *dims_data)
Definition Shape.h:130
int32_t * DimsData()
Definition Shape.h:112
const Eigen::ThreadPoolDevice * GetThreadPoolDevice()
void Adam(const Shape &trainable_shape, float *trainable_data, const Shape &grad_shape, const float *grad_data, const Shape &m_shape, float *m_data, const Shape &v_shape, float *v_data, float beta1_power, float beta2_power, float learning_rate, float beta1, float beta2, float epsilon, bool use_nesterov)
Definition Adam.h:33
Eigen::ThreadPoolDevice CPUDevice
Definition topk_v2.h:30
TTypes< T >::ConstScalar scalar() const
Definition Tensor.h:156
TTypes< T >::Flat flat()
Definition Tensor.h:127