| 23 | typedef hls::vector<half, D_VEC_SIZE> hvec; // 256 bits |
| 24 | |
| 25 | void adam ( vec* grad, vec* param, vec* exp_avg, vec* exp_avg_sq, hvec* param16, uint n_elements, |
| 26 | float betta1, float betta2, float bias_correction2, float eps, float w_decay, float step_size, float combined_unscale ) |
| 27 | { |
| 28 | vec g[DATA_SIZE]; |
| 29 | vec p[DATA_SIZE]; |
| 30 | vec v[DATA_SIZE]; |
| 31 | vec m[DATA_SIZE]; |
| 32 | |
| 33 | float _betta1 = betta1; |
| 34 | float _betta2 = betta2; |
| 35 | float _betta1_minus1 = (1.0f - _betta1); |
| 36 | float _betta2_minus1 = (1.0f - _betta2); |
| 37 | float _bias_correction2 = bias_correction2; |
| 38 | float _eps = eps; |
| 39 | float _w_decay_plus1 = (1.0f + w_decay); |
| 40 | float _step_size = step_size; |
| 41 | float _combined_unscale = combined_unscale; |
| 42 | |
| 43 | uint iteration = n_elements / VEC_SIZE; |
| 44 | vadd_pipeline: for (uint i = 0 ; i < iteration ; i += DATA_SIZE) |
| 45 | { |
| 46 | #pragma HLS PIPELINE rewind |
| 47 | uint size = DATA_SIZE; |
| 48 | //boundary check |
| 49 | if (i + size > iteration) size = iteration - i; |
| 50 | |
| 51 | uint size_h = size / 2; |
| 52 | uint i_h = i / 2; |
| 53 | |
| 54 | read_g: for ( uint x = 0; x < size; ++x) |
| 55 | { |
| 56 | #pragma HLS PIPELINE II=1 |
| 57 | g[ x ] = grad[ i + x] * _combined_unscale; |
| 58 | } |
| 59 | read_v: for (uint x = 0; x < size; ++x) |
| 60 | { |
| 61 | #pragma HLS PIPELINE II=1 |
| 62 | v[x] = g[x] * g[x] * _betta2_minus1 + exp_avg_sq[i + x] * _betta2; |
| 63 | } |
| 64 | write_v: for (uint x = 0; x < size; ++x) |
| 65 | { |
| 66 | #pragma HLS PIPELINE II=1 |
| 67 | exp_avg_sq[i + x] = v[x]; |
| 68 | } |
| 69 | read_m: for (uint x = 0; x < size; ++x) |
| 70 | { |
| 71 | #pragma HLS PIPELINE II=1 |
| 72 | m[x] = g[x] * _betta1_minus1 + exp_avg[i + x] * _betta1; |
| 73 | } |
| 74 | write_m: for (uint x = 0; x < size; ++x) |
| 75 | { |
| 76 | #pragma HLS PIPELINE II=1 |
| 77 | exp_avg[i + x] = m[x]; |
| 78 | } |
| 79 | read_p: for (uint x = 0 ; x < size; ++x) |
| 80 | { |
| 81 | #pragma HLS PIPELINE II=1 |
| 82 | inner_read_p: for (uint y = 0 ; y < VEC_SIZE; ++y) |