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