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