1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
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 #include <iostream>
18 #include "backend/kernel_compiler/gpu/cuda_impl/sgd_impl.cuh"
19
20 template <typename T>
SGDKernel(const int size,const T dampening,const T weight_decay,const bool nesterov,const T * grad,const T * momentum,const T * lr,T * param,T * accum,T * stat)21 __global__ void SGDKernel(const int size, const T dampening, const T weight_decay, const bool nesterov, const T *grad,
22 const T *momentum, const T *lr, T *param, T *accum, T *stat) {
23 for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < (size); i += blockDim.x * gridDim.x) {
24 T grad_new = grad[i];
25 if (weight_decay > static_cast<T>(0)) {
26 grad_new += param[i] * weight_decay;
27 }
28
29 if (momentum[0] > static_cast<T>(0)) {
30 if (stat[i] > static_cast<T>(0)) {
31 accum[i] = grad_new;
32 stat[i] = 0;
33 } else {
34 accum[i] = accum[i] * momentum[0] + (1.0 - dampening) * grad_new;
35 }
36
37 if (nesterov) {
38 grad_new += accum[i] * momentum[0];
39 } else {
40 grad_new = accum[i];
41 }
42 }
43
44 param[i] -= lr[0] * grad_new;
45 }
46 }
47
48 template <typename T>
SGD(const int size,const T dampening,const T weight_decay,const bool nesterov,const T * lr,const T * momentum,const T * grad,T * param,T * accum,T * stat,cudaStream_t cuda_stream)49 void SGD(const int size, const T dampening, const T weight_decay, const bool nesterov, const T *lr, const T *momentum,
50 const T *grad, T *param, T *accum, T *stat, cudaStream_t cuda_stream) {
51 SGDKernel<<<GET_BLOCKS(size), GET_THREADS, 0, cuda_stream>>>(size, dampening, weight_decay, nesterov, grad, momentum,
52 lr, param, accum, stat);
53 }
54
55 template void SGD(const int size, const float dampening, const float weight_decay, const bool nesterov, const float *lr,
56 const float *momentum, const float *grad, float *param, float *accum, float *stat,
57 cudaStream_t cuda_stream);
58