| 79 | } |
| 80 | |
| 81 | void adam ( hvec* grad16, hvec* param16, vec* param, vec* exp_avg, vec* exp_avg_sq, uint n_elements, |
| 82 | float betta1, float betta2, float bias_correction2, float eps, float w_decay, float step_size, float combined_unscale ) |
| 83 | { |
| 84 | hvec g16[DATA_SIZE]; |
| 85 | vec g[DATA_SIZE]; |
| 86 | vec p[DATA_SIZE]; |
| 87 | vec v[DATA_SIZE]; |
| 88 | vec m[DATA_SIZE]; |
| 89 | |
| 90 | float _betta1 = betta1; |
| 91 | float _betta2 = betta2; |
| 92 | float _betta1_minus1 = (1.0f - _betta1); |
| 93 | float _betta2_minus1 = (1.0f - _betta2); |
| 94 | float _bias_correction2 = bias_correction2; |
| 95 | float _eps = eps; |
| 96 | float _w_decay_plus1 = (1.0f + w_decay); |
| 97 | float _step_size = step_size; |
| 98 | float _combined_unscale = combined_unscale; |
| 99 | |
| 100 | uint iteration = n_elements / VEC_SIZE; |
| 101 | vadd_pipeline: for (uint i = 0 ; i < iteration ; i += DATA_SIZE) |
| 102 | { |
| 103 | #pragma HLS PIPELINE rewind |
| 104 | uint size = DATA_SIZE; |
| 105 | //boundary check |
| 106 | if (i + size > iteration) size = iteration - i; |
| 107 | |
| 108 | uint size_h = size / 2; |
| 109 | uint i_h = i / 2; |
| 110 | |
| 111 | read_g: for ( uint x = 0; x < size_h; ++x) |
| 112 | { |
| 113 | #pragma HLS PIPELINE II=1 |
| 114 | inner_read_g1: for (uint y = 0 ; y < VEC_SIZE; ++y) |
| 115 | { |
| 116 | #pragma HLS UNROLL |
| 117 | g[ 2*x ][y ] = grad16[ i_h + x][y] * _combined_unscale; |
| 118 | } |
| 119 | inner_read_g2: for (uint y = 0 ; y < VEC_SIZE; ++y) |
| 120 | { |
| 121 | #pragma HLS UNROLL |
| 122 | g[ 2*x + 1 ][y ] = grad16[ i_h + x][VEC_SIZE + y] * _combined_unscale; |
| 123 | } |
| 124 | } |
| 125 | read_v: for (uint x = 0; x < size; ++x) |
| 126 | { |
| 127 | #pragma HLS PIPELINE II=1 |
| 128 | v[x] = g[x] * g[x] * _betta2_minus1 + exp_avg_sq[i + x] * _betta2; |
| 129 | } |
| 130 | write_v: for (uint x = 0; x < size; ++x) |
| 131 | { |
| 132 | #pragma HLS PIPELINE II=1 |
| 133 | exp_avg_sq[i + x] = v[x]; |
| 134 | } |
| 135 | read_m: for (uint x = 0; x < size; ++x) |
| 136 | { |
| 137 | #pragma HLS PIPELINE II=1 |
| 138 | m[x] = g[x] * _betta1_minus1 + exp_avg[i + x] * _betta1; |