char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
fe8156f78
char/aliceai-t5-moe
1 2float rope_yarn_ramp (const float low ,const float high ,const uint i0 ) { 3const float y = (i0 /2 - low ) /max (0.001f ,high - low ); 4return 1.0f - min (1.0f ,max (0.0f ,y )); 5} 6 7uint rope_a_coord (const uint i0 ,const uint i01 ,const uint i02 ,const uint i03 ,rope_params p ) { 8#if RMS_NORM_ROPE_FUSION 9// Per-row offset in shared memory 10const uint ix = i0 ; 11#else 12const uint ix = p .a_offset + i03 * p .nb03 + i02 * p .nb02 + i01 * p .nb01 + i0 ; 13#endif 14return ix ; 15} 16 17void rope_yarn (const float theta_extrap ,const uint i0 ,out float cos_theta ,out float sin_theta ,rope_params p ) { 18float mscale = p .attn_factor ; 19// Get n-d rotational scaling corrected for extrapolation 20float theta_interp = p .freq_scale * theta_extrap ; 21float theta = theta_interp ; 22if (p .ext_factor != 0.0f ) { 23float ramp_mix = rope_yarn_ramp (p .corr_dims [0 ],p .corr_dims [1 ],i0 )* p .ext_factor ; 24theta = theta_interp * (1 - ramp_mix )+ theta_extrap * ramp_mix ; 25 26// Get n-d magnitude scaling corrected for interpolation 27mscale *=1.0f + 0.1f * log (1.0f /p .freq_scale ); 28 } 29// Backprogagation uses inverted rotation 30if (p .is_back != 0 ) { 31theta = - theta ; 32 } 33cos_theta = cos (theta )* mscale ; 34sin_theta = sin (theta )* mscale ; 35} 36 37void rope_norm (const uint i0 ,const uint i1 ,const uint i2 ,const uint i3 ,rope_params p ) { 38if (i0 >=p .ne00 ) { 39return ; 40 } 41 42uint idst = i0 + i1 * p .nb11 + i2 * p .nb12 + i3 * p .nb13 ; 43const uint ix = rope_a_coord (i0 ,i1 ,i2 ,i3 ,p ); 44 45// Fusion optimization: ROPE + VIEW + SET_ROWS. 46// The rope output is viewed as a 1D tensor and offset based on a row index in rope_data_i. 47if (p .set_rows_stride != 0 ) { 48idst = i1 * p .nb11 + i0 ; 49idst += rope_data_i [i2 ].x * p .set_rows_stride ; 50 } 51idst += p .d_offset ; 52 53if (i0 < p .n_offs || i0 >=p .n_offs + p .n_dims ) { 54rope_data_d [idst + 0 ]= ROPE_D_TYPE (rope_data_a [ix + 0 ]); 55rope_data_d [idst + 1 ]= ROPE_D_TYPE (rope_data_a [ix + 1 ]); 56 57return ; 58 } 59 60const uint iw = i0 - p .n_offs ;// relative idx 61 62const float theta_base = rope_data_pos [i2 ]* pow (p .theta_scale ,iw /2.0f ); 63 64const float freq_factor = p .has_ff != 0 ?rope_data_ff [iw /2 ] :1.0f ; 65 66float cos_theta ,sin_theta ; 67rope_yarn (theta_base /freq_factor ,iw ,cos_theta ,sin_theta ,p ); 68 69const float x0 = float (rope_data_a [ix + 0 ]); 70const float x1 = float (rope_data_a [ix + 1 ]); 71 72rope_data_d [idst + 0 ]= ROPE_D_TYPE (x0 * cos_theta - x1 * sin_theta ); 73rope_data_d [idst + 1 ]= ROPE_D_TYPE (x0 * sin_theta + x1 * cos_theta ); 74} 75 76void rope_neox (const uint i0 ,const uint i1 ,const uint i2 ,const uint i3 ,rope_params p ) { 77if (i0 >=p .ne00 ) { 78return ; 79 } 80 81uint idst = i0 /2 + i1 * p .nb11 + i2 * p .nb12 + i3 * p .nb13 ; 82const uint ix = rope_a_coord (i0 /2 ,i1 ,i2 ,i3 ,p ); 83 84// Fusion optimization: ROPE + VIEW + SET_ROWS. 85// The rope output is viewed as a 1D tensor and offset based on a row index in rope_data_i. 86if (p .set_rows_stride != 0 ) { 87idst = i1 * p .nb11 + i0 /2 ; 88idst += rope_data_i [i2 ].x * p .set_rows_stride ; 89 } 90idst += p .d_offset ; 91 92if (i0 < p .n_offs || i0 >=p .n_offs + p .n_dims ) { 93rope_data_d [idst + i0 /2 + 0 ]= ROPE_D_TYPE (rope_data_a [ix + i0 /2 + 0 ]); 94rope_data_d [idst + i0 /2 + 1 ]= ROPE_D_TYPE (rope_data_a [ix + i0 /2 + 1 ]); 95 96return ; 97 } 98 99const uint iw = i0 - p .n_offs ;// relative idx 100 101const float theta_base = rope_data_pos [i2 ]* pow (p .theta_scale ,iw /2.0f ); 102 103const float freq_factor = p .has_ff != 0 ?rope_data_ff [iw /2 ] :1.0f ; 104 105float cos_theta ,sin_theta ; 106rope_yarn (theta_base /freq_factor ,iw ,cos_theta ,sin_theta ,p ); 107 108// idst/ix point at channel i0/2; the first channel of the rotated pair is p.n_offs + iw/2 = i0/2 + p.n_offs/2 109const float x0 = float (rope_data_a [ix + p .n_offs /2 + 0 ]); 110const float x1 = float (rope_data_a [ix + p .n_offs /2 + p .n_dims /2 ]); 111 112rope_data_d [idst + p .n_offs /2 + 0 ]= ROPE_D_TYPE (x0 * cos_theta - x1 * sin_theta ); 113rope_data_d [idst + p .n_offs /2 + p .n_dims /2 ]= ROPE_D_TYPE (x0 * sin_theta + x1 * cos_theta ); 114} 115 116 117void rope_multi (const uint i0 ,const uint i1 ,const uint i2 ,const uint i3 ,rope_params p ) { 118if (i0 >=p .ne00 ) { 119return ; 120 } 121 122uint idst = i0 /2 + i1 * p .nb11 + i2 * p .nb12 + i3 * p .nb13 ; 123const uint ix = rope_a_coord (i0 /2 ,i1 ,i2 ,i3 ,p ); 124 125// Fusion optimization: ROPE + VIEW + SET_ROWS. 126// The rope output is viewed as a 1D tensor and offset based on a row index in rope_data_i. 127if (p .set_rows_stride != 0 ) { 128idst = i1 * p .nb11 + i0 /2 ; 129idst += rope_data_i [i2 ].x * p .set_rows_stride ; 130 } 131idst += p .d_offset ; 132 133if (i0 < p .n_offs || i0 >=p .n_offs + p .n_dims ) { 134rope_data_d [idst + i0 /2 + 0 ]= ROPE_D_TYPE (rope_data_a [ix + i0 /2 + 0 ]); 135rope_data_d [idst + i0 /2 + 1 ]= ROPE_D_TYPE (rope_data_a [ix + i0 /2 + 1 ]); 136 137return ; 138 } 139 140const uint iw = i0 - p .n_offs ;// relative idx 141 142const int sect_dims = p .sections [0 ]+ p .sections [1 ]+ p .sections [2 ]+ p .sections [3 ]; 143const int sec_w = p .sections [1 ]+ p .sections [0 ]; 144const uint sector = (iw /2 ) %sect_dims ; 145 146float theta_base = 0.0 ; 147if (p .is_imrope != 0 ) { 148if (sector %3 == 1 && sector < 3 * p .sections [1 ]) { 149theta_base = rope_data_pos [i2 + p .ne02 * 1 ]* pow (p .theta_scale ,iw /2.0f ); 150 }else if (sector %3 == 2 && sector < 3 * p .sections [2 ]) { 151theta_base = rope_data_pos [i2 + p .ne02 * 2 ]* pow (p .theta_scale ,iw /2.0f ); 152 }else if (sector %3 == 0 && sector < 3 * p .sections [0 ]) { 153theta_base = rope_data_pos [i2 ]* pow (p .theta_scale ,iw /2.0f ); 154 }else { 155theta_base = rope_data_pos [i2 + p .ne02 * 3 ]* pow (p .theta_scale ,iw /2.0f ); 156 } 157 }else { 158if (sector < p .sections [0 ]) { 159theta_base = rope_data_pos [i2 ]* pow (p .theta_scale ,iw /2.0f ); 160 } 161else if (sector >=p .sections [0 ]&& sector < sec_w ) { 162theta_base = rope_data_pos [i2 + p .ne02 * 1 ]* pow (p .theta_scale ,iw /2.0f ); 163 } 164else if (sector >=sec_w && sector < sec_w + p .sections [2 ]) { 165theta_base = rope_data_pos [i2 + p .ne02 * 2 ]* pow (p .theta_scale ,iw /2.0f ); 166 } 167else if (sector >=sec_w + p .sections [2 ]) { 168theta_base = rope_data_pos [i2 + p .ne02 * 3 ]* pow (p .theta_scale ,iw /2.0f ); 169 } 170 } 171 172const float freq_factor = p .has_ff != 0 ?rope_data_ff [iw /2 ] :1.0f ; 173 174float cos_theta ,sin_theta ; 175rope_yarn (theta_base /freq_factor ,iw ,cos_theta ,sin_theta ,p ); 176 177// idst/ix point at channel i0/2; the first channel of the rotated pair is p.n_offs + iw/2 = i0/2 + p.n_offs/2 178const float x0 = float (rope_data_a [ix + p .n_offs /2 + 0 ]); 179const float x1 = float (rope_data_a [ix + p .n_offs /2 + p .n_dims /2 ]); 180 181rope_data_d [idst + p .n_offs /2 + 0 ]= ROPE_D_TYPE (x0 * cos_theta - x1 * sin_theta ); 182rope_data_d [idst + p .n_offs /2 + p .n_dims /2 ]= ROPE_D_TYPE (x0 * sin_theta + x1 * cos_theta ); 183} 184 185void rope_vision (const uint i0 ,const uint i1 ,const uint i2 ,const uint i3 ,rope_params p ) { 186if (i0 >=p .ne00 ) { 187return ; 188 } 189 190const uint idst = p .d_offset + i0 /2 + i1 * p .nb11 + i2 * p .nb12 + i3 * p .nb13 ; 191const uint ix = rope_a_coord (i0 /2 ,i1 ,i2 ,i3 ,p ); 192 193const int sect_dims = p .sections [0 ]+ p .sections [1 ]; 194const int sec_w = p .sections [1 ]+ p .sections [0 ]; 195const uint sector = (i0 /2 ) %sect_dims ; 196 197float theta_base = 0.0 ; 198if (sector < p .sections [0 ]) { 199const uint p0 = sector ; 200theta_base = rope_data_pos [i2 ]* pow (p .theta_scale ,p0 ); 201 } 202else if (sector >=p .sections [0 ]&& sector < sec_w ) { 203const uint p0 = sector - p .sections [0 ]; 204theta_base = rope_data_pos [i2 + p .ne02 ]* pow (p .theta_scale ,p0 ); 205 } 206 207const float freq_factor = p .has_ff != 0 ?rope_data_ff [i0 /2 ] :1.0f ; 208 209float cos_theta ,sin_theta ; 210rope_yarn (theta_base /freq_factor ,i0 ,cos_theta ,sin_theta ,p ); 211 212const float x0 = float (rope_data_a [ix + 0 ]); 213const float x1 = float (rope_data_a [ix + p .n_dims ]); 214 215rope_data_d [idst + 0 ]= ROPE_D_TYPE (x0 * cos_theta - x1 * sin_theta ); 216rope_data_d [idst + p .n_dims ]= ROPE_D_TYPE (x0 * sin_theta + x1 * cos_theta ); 217} 218