cpu_adam.h 7.88 KB
Newer Older
aiss's avatar
aiss committed
1
2
3
4
/*
Copyright The Microsoft DeepSpeed Team
*/

aiss's avatar
aiss committed
5
6
7
8
9
10
11
#pragma once

#define NOMINMAX  // Windows idiosyncrasy
                  // https://stackoverflow.com/questions/4913922/possible-problems-with-nominmax-on-visual-c

#include <stdio.h>
#include <cassert>
aiss's avatar
aiss committed
12
13
14
15
16
#include "simd.h"

#if defined(__ENABLE_CUDA__)
#include <cuda_fp16.h>
#include <cuda_runtime_api.h>
aiss's avatar
aiss committed
17
18
#include "cuda.h"
#include "custom_cuda_layers.h"
aiss's avatar
aiss committed
19
20
21
22
23
typedef __half ds_half_precision_t;
#else
#include <cmath>
typedef unsigned short ds_half_precision_t;
#endif
aiss's avatar
aiss committed
24

aiss's avatar
aiss committed
25
26
27
28
29
30
31
#define STEP(SPAN)                                             \
    void Step_##SPAN(float* _params,                           \
                     float* grads,                             \
                     float* _exp_avg,                          \
                     float* _exp_avg_sq,                       \
                     size_t _param_size,                       \
                     ds_half_precision_t* dev_param = nullptr, \
aiss's avatar
aiss committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
                     bool half_precision = false);

class Adam_Optimizer {
public:
    Adam_Optimizer(float alpha = 1e-3,
                   float betta1 = 0.9,
                   float betta2 = 0.999,
                   float eps = 1e-8,
                   float weight_decay = 0,
                   bool adamw_mode = true)
        : _alpha(alpha),
          _betta1(betta1),
          _betta2(betta2),
          _eps(eps),
          _weight_decay(weight_decay),
          _betta1_t(1.0),
          _betta2_t(1.0),
          _step(0),
          _adamw_mode(adamw_mode)
    {
aiss's avatar
aiss committed
52
#if defined(__ENABLE_CUDA__)
aiss's avatar
aiss committed
53
54
55
56
57
        cudaMallocHost((void**)_doubled_buffer, TILE * sizeof(float));
        cudaMallocHost((void**)(_doubled_buffer + 1), TILE * sizeof(float));

        _streams[0] = Context::Instance().GetCurrentStream();
        _streams[1] = Context::Instance().GetNewStream();
aiss's avatar
aiss committed
58
59
        _buf_index = false;
#endif
aiss's avatar
aiss committed
60
61
62
    }
    ~Adam_Optimizer()
    {
aiss's avatar
aiss committed
63
#if defined(__ENABLE_CUDA__)
aiss's avatar
aiss committed
64
65
        cudaFreeHost(_doubled_buffer[0]);
        cudaFreeHost(_doubled_buffer[1]);
aiss's avatar
aiss committed
66
#endif
aiss's avatar
aiss committed
67
    }
aiss's avatar
aiss committed
68

aiss's avatar
aiss committed
69
70
71
72
73
74
75
76
#if defined(__AVX512__) or defined(__AVX256__)
    template <int span>
    void Step_AVX(size_t* rounded_size,
                  float* _params,
                  float* grads,
                  float* _exp_avg,
                  float* _exp_avg_sq,
                  size_t param_size,
aiss's avatar
aiss committed
77
                  ds_half_precision_t* dev_param = nullptr,
aiss's avatar
aiss committed
78
79
80
81
82
                  bool half_precision = false);
#endif
    STEP(1)
    STEP(4)
    STEP(8)
aiss's avatar
aiss committed
83
#if defined(__ENABLE_CUDA__)
aiss's avatar
aiss committed
84
85
86
87
    inline void SynchronizeStreams()
    {
        for (int i = 0; i < 2; i++) cudaStreamSynchronize(_streams[i]);
    }
aiss's avatar
aiss committed
88
#endif
aiss's avatar
aiss committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    inline void IncrementStep(size_t step, float beta1, float beta2)
    {
        if (beta1 != _betta1 || beta2 != _betta2) {
            _step = step;
            _betta1 = beta1;
            _betta2 = beta2;
            _betta1_t = std::pow(_betta1, step);
            _betta2_t = std::pow(_betta2, step);
        } else {
            _step++;
            if (_step != step) {
                _betta1_t = std::pow(_betta1, step);
                _betta2_t = std::pow(_betta2, step);
                _step = step;
            } else {
                _betta1_t *= _betta1;
                _betta2_t *= _betta2;
            }
        }
    }
    inline void update_state(float lr, float epsilon, float weight_decay, bool bias_correction)
    {
        _alpha = lr;
        _eps = epsilon;
        _weight_decay = weight_decay;

        _bias_correction1 = 1.0f;
        _bias_correction2 = 1.0f;
        if (bias_correction == 1) {
            _bias_correction1 = 1 - _betta1_t;
            _bias_correction2 = 1 / sqrt(1 - _betta2_t);
        }
    }

private:
    float _alpha;
    float _betta1;
    float _betta2;
    float _eps;
    float _weight_decay;

    float _betta1_t;
    float _betta2_t;
    size_t _step;

    float _bias_correction1;
    float _bias_correction2;

    bool _adamw_mode;

aiss's avatar
aiss committed
139
140
#if defined(__ENABLE_CUDA__)
    float* _doubled_buffer[2];
aiss's avatar
aiss committed
141
    cudaStream_t _streams[2];
aiss's avatar
aiss committed
142
143
    bool _buf_index;
#endif
aiss's avatar
aiss committed
144
145
146
147
148
149
150
151
152
153
};

#if defined(__AVX512__) or defined(__AVX256__)
template <int span>
void Adam_Optimizer::Step_AVX(size_t* rounded_size,
                              float* _params,
                              float* grads,
                              float* _exp_avg,
                              float* _exp_avg_sq,
                              size_t _param_size,
aiss's avatar
aiss committed
154
                              ds_half_precision_t* dev_params,
aiss's avatar
aiss committed
155
156
157
                              bool half_precision)
{
    size_t new_rounded_size = 0;
aiss's avatar
aiss committed
158
    int rshft = half_precision ? 1 : 0;
aiss's avatar
aiss committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

    AVX_Data betta1_4;
    betta1_4.data = SIMD_SET(_betta1);
    AVX_Data betta2_4;
    betta2_4.data = SIMD_SET(_betta2);

    float betta1_minus1 = 1 - _betta1;
    float betta2_minus1 = 1 - _betta2;
    AVX_Data betta1_minus1_4;
    betta1_minus1_4.data = SIMD_SET(betta1_minus1);
    AVX_Data betta2_minus1_4;
    betta2_minus1_4.data = SIMD_SET(betta2_minus1);

    AVX_Data bias2_sqrt;
    bias2_sqrt.data = SIMD_SET(_bias_correction2);

    AVX_Data eps_4;
    eps_4.data = SIMD_SET(_eps);

    float step_size = -1 * _alpha / _bias_correction1;
    AVX_Data step_size_4;
    step_size_4.data = SIMD_SET(step_size);

    float w_decay = -1 * _alpha * _weight_decay;
    AVX_Data weight_decay4;
    if (_weight_decay > 0)
        weight_decay4.data = (_adamw_mode ? SIMD_SET(w_decay) : SIMD_SET(_weight_decay));
    new_rounded_size = ROUND_DOWN(_param_size, SIMD_WIDTH * span);
    for (size_t t = 0; t < new_rounded_size; t += TILE) {
        size_t copy_size = TILE;
        if ((t + TILE) > new_rounded_size) copy_size = new_rounded_size - t;
        size_t offset = copy_size + t;
aiss's avatar
aiss committed
191
#if defined(__ENABLE_CUDA__)
aiss's avatar
aiss committed
192
        if ((t / TILE) >= 2) { cudaStreamSynchronize(_streams[_buf_index]); }
aiss's avatar
aiss committed
193
#endif
aiss's avatar
aiss committed
194
195
196
#pragma omp parallel for
        for (size_t i = t; i < offset; i += SIMD_WIDTH * span) {
            AVX_Data grad_4[span];
aiss's avatar
aiss committed
197
            simd_load<span>(grad_4, grads + (i >> rshft), half_precision);
aiss's avatar
aiss committed
198
199
200
201
202
203
204
205

            AVX_Data momentum_4[span];
            simd_load<span>(momentum_4, _exp_avg + i, false);

            AVX_Data variance_4[span];
            simd_load<span>(variance_4, _exp_avg_sq + i, false);

            AVX_Data param_4[span];
aiss's avatar
aiss committed
206
            simd_load<span>(param_4, _params + (i >> rshft), half_precision);
aiss's avatar
aiss committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

            if (_weight_decay > 0 && !_adamw_mode) {
                simd_fma<span>(grad_4, param_4, weight_decay4, grad_4);
            }

            simd_mul<span>(momentum_4, momentum_4, betta1_4);
            simd_fma<span>(momentum_4, grad_4, betta1_minus1_4, momentum_4);
            simd_mul<span>(variance_4, variance_4, betta2_4);
            simd_mul<span>(grad_4, grad_4, grad_4);
            simd_fma<span>(variance_4, grad_4, betta2_minus1_4, variance_4);
            simd_sqrt<span>(grad_4, variance_4);
            simd_fma<span>(grad_4, grad_4, bias2_sqrt, eps_4);
            simd_div<span>(grad_4, momentum_4, grad_4);

            if (_weight_decay > 0 && _adamw_mode) {
                simd_fma<span>(param_4, param_4, weight_decay4, param_4);
            }

            simd_fma<span>(param_4, grad_4, step_size_4, param_4);

aiss's avatar
aiss committed
227
228
            simd_store<span>(_params + (i >> rshft), param_4, half_precision);
#if defined(__ENABLE_CUDA__)
aiss's avatar
aiss committed
229
230
231
            if (dev_params) {
                simd_store<span>(_doubled_buffer[_buf_index] + (i - t), param_4, half_precision);
            }
aiss's avatar
aiss committed
232
#endif
aiss's avatar
aiss committed
233
234
235
            simd_store<span>(_exp_avg + i, momentum_4, false);
            simd_store<span>(_exp_avg_sq + i, variance_4, false);
        }
aiss's avatar
aiss committed
236
#if defined(__ENABLE_CUDA__)
aiss's avatar
aiss committed
237
238
239
240
241
242
243
244
245
246
        if (dev_params) {
            if (half_precision)
                launch_param_update_half(
                    _doubled_buffer[_buf_index], dev_params + t, copy_size, _streams[_buf_index]);
            else
                launch_param_update(
                    _doubled_buffer[_buf_index], dev_params + t, copy_size, _streams[_buf_index]);

            _buf_index = !_buf_index;
        }
aiss's avatar
aiss committed
247
#endif
aiss's avatar
aiss committed
248
249
250
251
    }
    *rounded_size = new_rounded_size;
}
#endif