@@ -4,12 +4,12 @@ float rope_yarn_ramp(const float low, const float high, const uint i0) {
44 return 1 .0f - min (1 .0f, max (0 .0f, y));
55}
66
7- uint rope_a_coord(const uint i0, const uint i01, const uint i02, rope_params p) {
7+ uint rope_a_coord(const uint i0, const uint i01, const uint i02, const uint i03, rope_params p) {
88#if RMS_NORM_ROPE_FUSION
99 // Per-row offset in shared memory
1010 const uint ix = i0;
1111#else
12- const uint ix = i02* p.nb02 + i01* p.nb01 + i0;
12+ const uint ix = i03 * p.nb03 + i02* p.nb02 + i01* p.nb01 + i0;
1313#endif
1414 return ix;
1515}
@@ -34,26 +34,19 @@ void rope_yarn(const float theta_extrap, const uint i0, out float cos_theta, out
3434 sin_theta = sin (theta) * mscale;
3535}
3636
37- void rope_norm(const uint i0, const uint i1, rope_params p) {
38- uint ne0 = p.ncols;
39- uint ne1 = p.p_delta_rows;
40-
41- if (i0 >= ne0) {
37+ void rope_norm(const uint i0, const uint i1, const uint i2, const uint i3, rope_params p) {
38+ if (i0 >= p.ne00) {
4239 return ;
4340 }
4441
45- // i1 is actually i2*nb2+i1, but the rows are contiguous
46- const uint i01 = i1 % ne1;
47- const uint i02 = i1 / ne1;
48-
49- uint idst = i1* ne0 + i0;
50- const uint ix = rope_a_coord(i0, i01, i02, p);
42+ uint idst = i0 + i1 * p.nb11 + i2 * p.nb12 + i3 * p.nb13;
43+ const uint ix = rope_a_coord(i0, i1, i2, i3, p);
5144
5245 // Fusion optimization: ROPE + VIEW + SET_ROWS.
5346 // The rope output is viewed as a 1D tensor and offset based on a row index in rope_data_i.
5447 if (p.set_rows_stride != 0 ) {
55- idst = i01 * ne0 + i0;
56- idst += rope_data_i[i02 ].x * p.set_rows_stride;
48+ idst = i1 * p.nb11 + i0;
49+ idst += rope_data_i[i2 ].x * p.set_rows_stride;
5750 }
5851
5952 if (i0 >= p.n_dims) {
@@ -63,7 +56,7 @@ void rope_norm(const uint i0, const uint i1, rope_params p) {
6356 return ;
6457 }
6558
66- const float theta_base = rope_data_pos[i02 ] * pow (p.theta_scale, i0/ 2 .0f);
59+ const float theta_base = rope_data_pos[i2 ] * pow (p.theta_scale, i0/ 2 .0f);
6760
6861 const float freq_factor = p.has_ff != 0 ? rope_data_ff[i0/ 2 ] : 1 .0f;
6962
@@ -77,25 +70,19 @@ void rope_norm(const uint i0, const uint i1, rope_params p) {
7770 rope_data_d[idst + 1 ] = ROPE_D_TYPE(x0* sin_theta + x1* cos_theta);
7871}
7972
80- void rope_neox(const uint i0, const uint i1, rope_params p) {
81- uint ne0 = p.ncols;
82- uint ne1 = p.p_delta_rows;
83-
84- if (i0 >= ne0) {
73+ void rope_neox(const uint i0, const uint i1, const uint i2, const uint i3, rope_params p) {
74+ if (i0 >= p.ne00) {
8575 return ;
8676 }
8777
88- const uint i01 = i1 % ne1;
89- const uint i02 = i1 / ne1;
90-
91- uint idst = i1* ne0 + i0/ 2 ;
92- const uint ix = rope_a_coord(i0/ 2 , i01, i02, p);
78+ uint idst = i0/ 2 + i1 * p.nb11 + i2 * p.nb12 + i3 * p.nb13;
79+ const uint ix = rope_a_coord(i0/ 2 , i1, i2, i3, p);
9380
9481 // Fusion optimization: ROPE + VIEW + SET_ROWS.
9582 // The rope output is viewed as a 1D tensor and offset based on a row index in rope_data_i.
9683 if (p.set_rows_stride != 0 ) {
97- idst = i01 * ne0 + i0/ 2 ;
98- idst += rope_data_i[i02 ].x * p.set_rows_stride;
84+ idst = i1 * p.nb11 + i0/ 2 ;
85+ idst += rope_data_i[i2 ].x * p.set_rows_stride;
9986 }
10087
10188 if (i0 >= p.n_dims) {
@@ -105,7 +92,7 @@ void rope_neox(const uint i0, const uint i1, rope_params p) {
10592 return ;
10693 }
10794
108- const float theta_base = rope_data_pos[i02 ] * pow (p.theta_scale, i0/ 2 .0f);
95+ const float theta_base = rope_data_pos[i2 ] * pow (p.theta_scale, i0/ 2 .0f);
10996
11097 const float freq_factor = p.has_ff != 0 ? rope_data_ff[i0/ 2 ] : 1 .0f;
11198
@@ -120,26 +107,19 @@ void rope_neox(const uint i0, const uint i1, rope_params p) {
120107}
121108
122109
123- void rope_multi(const uint i0, const uint i1, rope_params p) {
124- uint ne0 = p.ncols;
125- uint ne1 = p.p_delta_rows;
126- uint ne2 = p.ne02;
127-
128- if (i0 >= ne0) {
110+ void rope_multi(const uint i0, const uint i1, const uint i2, const uint i3, rope_params p) {
111+ if (i0 >= p.ne00) {
129112 return ;
130113 }
131114
132- const uint i01 = i1 % ne1;
133- const uint i02 = i1 / ne1;
134-
135- uint idst = i1* ne0 + i0/ 2 ;
136- const uint ix = rope_a_coord(i0/ 2 , i01, i02, p);
115+ uint idst = i0/ 2 + i1 * p.nb11 + i2 * p.nb12 + i3 * p.nb13;
116+ const uint ix = rope_a_coord(i0/ 2 , i1, i2, i3, p);
137117
138118 // Fusion optimization: ROPE + VIEW + SET_ROWS.
139119 // The rope output is viewed as a 1D tensor and offset based on a row index in rope_data_i.
140120 if (p.set_rows_stride != 0 ) {
141- idst = i01 * ne0 + i0/ 2 ;
142- idst += rope_data_i[i02 ].x * p.set_rows_stride;
121+ idst = i1 * p.nb11 + i0/ 2 ;
122+ idst += rope_data_i[i2 ].x * p.set_rows_stride;
143123 }
144124
145125 if (i0 >= p.n_dims) {
@@ -156,26 +136,26 @@ void rope_multi(const uint i0, const uint i1, rope_params p) {
156136 float theta_base = 0.0 ;
157137 if (p.is_imrope != 0 ) {
158138 if (sector % 3 == 1 && sector < 3 * p.sections[1 ]) {
159- theta_base = rope_data_pos[i02 + ne2 * 1 ]* pow (p.theta_scale, i0/ 2 .0f);
139+ theta_base = rope_data_pos[i2 + p.ne02 * 1 ]* pow (p.theta_scale, i0/ 2 .0f);
160140 } else if (sector % 3 == 2 && sector < 3 * p.sections[2 ]) {
161- theta_base = rope_data_pos[i02 + ne2 * 2 ]* pow (p.theta_scale, i0/ 2 .0f);
141+ theta_base = rope_data_pos[i2 + p.ne02 * 2 ]* pow (p.theta_scale, i0/ 2 .0f);
162142 } else if (sector % 3 == 0 && sector < 3 * p.sections[0 ]) {
163- theta_base = rope_data_pos[i02 ]* pow (p.theta_scale, i0/ 2 .0f);
143+ theta_base = rope_data_pos[i2 ]* pow (p.theta_scale, i0/ 2 .0f);
164144 } else {
165- theta_base = rope_data_pos[i02 + ne2 * 3 ]* pow (p.theta_scale, i0/ 2 .0f);
145+ theta_base = rope_data_pos[i2 + p.ne02 * 3 ]* pow (p.theta_scale, i0/ 2 .0f);
166146 }
167147 } else {
168148 if (sector < p.sections[0 ]) {
169- theta_base = rope_data_pos[i02 ]* pow (p.theta_scale, i0/ 2 .0f);
149+ theta_base = rope_data_pos[i2 ]* pow (p.theta_scale, i0/ 2 .0f);
170150 }
171151 else if (sector >= p.sections[0 ] && sector < sec_w) {
172- theta_base = rope_data_pos[i02 + ne2 * 1 ]* pow (p.theta_scale, i0/ 2 .0f);
152+ theta_base = rope_data_pos[i2 + p.ne02 * 1 ]* pow (p.theta_scale, i0/ 2 .0f);
173153 }
174154 else if (sector >= sec_w && sector < sec_w + p.sections[2 ]) {
175- theta_base = rope_data_pos[i02 + ne2 * 2 ]* pow (p.theta_scale, i0/ 2 .0f);
155+ theta_base = rope_data_pos[i2 + p.ne02 * 2 ]* pow (p.theta_scale, i0/ 2 .0f);
176156 }
177157 else if (sector >= sec_w + p.sections[2 ]) {
178- theta_base = rope_data_pos[i02 + ne2 * 3 ]* pow (p.theta_scale, i0/ 2 .0f);
158+ theta_base = rope_data_pos[i2 + p.ne02 * 3 ]* pow (p.theta_scale, i0/ 2 .0f);
179159 }
180160 }
181161
@@ -191,20 +171,13 @@ void rope_multi(const uint i0, const uint i1, rope_params p) {
191171 rope_data_d[idst + p.n_dims/ 2 ] = ROPE_D_TYPE(x0* sin_theta + x1* cos_theta);
192172}
193173
194- void rope_vision(const uint i0, const uint i1, rope_params p) {
195- uint ne0 = p.ncols;
196- uint ne1 = p.p_delta_rows;
197- uint ne2 = p.ne02;
198-
199- if (i0 >= ne0) {
174+ void rope_vision(const uint i0, const uint i1, const uint i2, const uint i3, rope_params p) {
175+ if (i0 >= p.ne00) {
200176 return ;
201177 }
202178
203- const uint i01 = i1 % ne1;
204- const uint i02 = i1 / ne1;
205-
206- const uint idst = i1* ne0 + i0/ 2 ;
207- const uint ix = rope_a_coord(i0/ 2 , i01, i02, p);
179+ const uint idst = i0/ 2 + i1 * p.nb11 + i2 * p.nb12 + i3 * p.nb13;
180+ const uint ix = rope_a_coord(i0/ 2 , i1, i2, i3, p);
208181
209182 const int sect_dims = p.sections[0 ] + p.sections[1 ];
210183 const int sec_w = p.sections[1 ] + p.sections[0 ];
@@ -213,11 +186,11 @@ void rope_vision(const uint i0, const uint i1, rope_params p) {
213186 float theta_base = 0.0 ;
214187 if (sector < p.sections[0 ]) {
215188 const uint p0 = sector;
216- theta_base = rope_data_pos[i02 ]* pow (p.theta_scale, p0);
189+ theta_base = rope_data_pos[i2 ]* pow (p.theta_scale, p0);
217190 }
218191 else if (sector >= p.sections[0 ] && sector < sec_w) {
219192 const uint p0 = sector - p.sections[0 ];
220- theta_base = rope_data_pos[i02 + ne2 ]* pow (p.theta_scale, p0);
193+ theta_base = rope_data_pos[i2 + p.ne02 ]* pow (p.theta_scale, p0);
221194 }
222195
223196 const float freq_factor = p.has_ff != 0 ? rope_data_ff[i0/ 2 ] : 1 .0f;
0 commit comments