Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-hexagon/htp/rope-ops.c # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-hexagon / htp / rope-ops.c` 813 lines ยท 35385 bytes Raw Blame History 1 #pragma clang diagnostic ignored "-Wunused-variable" 2 #pragma clang diagnostic ignored "-Wunused-function" 3 #pragma clang diagnostic ignored "-Wunused-but-set-variable" 4 5 #include 6 #include 7 8 #include 9 #include 10 #include 11 12 #include "dma-queue.h" 13 #include "hvx-utils.h" 14 #include "hex-fastdiv.h" 15 16 #define GGML_COMMON_DECL_C 17 #include "ggml-common.h" 18 #include "htp-ctx.h" 19 #include "htp-ops.h" 20 #include "htp-tensor.h" 21 #include "rope-ops.h" 22 23 // Redefined the rope type constants as we can't include ggml.h 24 #define HTP_ROPE_TYPE_NORMAL 0 25 #define HTP_ROPE_TYPE_NEOX 2 26 #define HTP_ROPE_TYPE_MROPE 8 27 #define HTP_ROPE_TYPE_VISION 24 28 #define HTP_ROPE_TYPE_IMROPE 40 29 30 #define htp_rope_preamble \ 31 const uint32_t ne00 = src0->ne[0]; \ 32 const uint32_t ne01 = src0->ne[1]; \ 33 const uint32_t ne02 = src0->ne[2]; \ 34 const uint32_t ne03 = src0->ne[3]; \ 35 \ 36 const uint32_t ne0 = dst->ne[0]; \ 37 const uint32_t ne1 = dst->ne[1]; \ 38 const uint32_t ne2 = dst->ne[2]; \ 39 const uint32_t ne3 = dst->ne[3]; \ 40 \ 41 const uint32_t nb00 = src0->nb[0]; \ 42 const uint32_t nb01 = src0->nb[1]; \ 43 const uint32_t nb02 = src0->nb[2]; \ 44 const uint32_t nb03 = src0->nb[3]; \ 45 \ 46 const uint32_t nb0 = dst->nb[0]; \ 47 const uint32_t nb1 = dst->nb[1]; \ 48 const uint32_t nb2 = dst->nb[2]; \ 49 const uint32_t nb3 = dst->nb[3]; 50 51 struct htp_rope_context { 52 int32_t n_dims; 53 int32_t n_offs; 54 int32_t mode; 55 int32_t n_ctx_orig; 56 int32_t sections[4]; 57 58 float freq_base; 59 float freq_scale; 60 float ext_factor; 61 float attn_factor; 62 float beta_fast; 63 float beta_slow; 64 float theta_scale; 65 float theta_scale_32; 66 float theta_powers[32]; 67 float corr_dims[2]; 68 69 uint32_t src0_nrows_per_thread; 70 71 struct htp_ops_context * octx; 72 73 uint8_t * vtcm_base; 74 size_t spad_per_thread; 75 size_t theta_cache_offset; 76 77 size_t src0_row_size; 78 size_t src0_row_stride; 79 size_t dst_row_size; 80 size_t dst_row_stride; 81 size_t src0_row_size_aligned; 82 uint32_t src0_nrows; 83 uint32_t row_start; 84 uint32_t nrows; 85 86 struct fastdiv_values div_ne2_ne1; 87 struct fastdiv_values div_ne1; 88 89 const float * freq_factors; 90 }; 91 92 static float rope_yarn_ramp(const float low, const float high, const int i0) { 93 const float y = (i0 / 2 - low) / MAX(0.001f, high - low); 94 95 return (1 - MIN(1, MAX(0, y))); 96 } 97 98 // Compute one (cos, sin) pair into cache[i0], cache[i0+1] applying YaRN scaling. 99 static inline void rope_yarn_one(float theta, float freq_scale, float * corr_dims, 100 uint32_t i0, float ext_factor, float mscale, 101 float * cache) { 102 float theta_extrap = theta; 103 104 // Get n-d rotational scaling corrected for extrapolation 105 float theta_interp = freq_scale * theta_extrap; 106 float theta_final = theta_interp; 107 float mscale_final = mscale; 108 109 if (ext_factor != 0.0f) { 110 float ramp_mix = rope_yarn_ramp(corr_dims[0], corr_dims[1], i0) * ext_factor; 111 theta_final = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix; 112 113 // Get n-d magnitude scaling corrected for interpolation 114 mscale_final *= 1.0f + 0.1f * logf(1.0f / freq_scale); 115 } 116 117 const uint32_t b = i0 / 64; 118 const uint32_t k = (i0 % 64) / 2; 119 cache[b * 64 + k] = cosf(theta_final) * mscale_final; 120 cache[b * 64 + 32 + k] = sinf(theta_final) * mscale_final; 121 } 122 123 // 32 thetas -> 32 deinterleaved pairs [cos[32] | sin[32]] at cache[i0]. 124 static inline void rope_cache_hvx_32(float * cache, uint32_t i0, 125 HVX_Vector v_theta, 126 const float * freq_factors, 127 HVX_Vector v_freq_scale, 128 HVX_Vector v_mscale) { 129 if (freq_factors) { 130 HVX_Vector v_ff = hvx_vmemu(freq_factors + i0 / 2); 131 v_theta = hvx_vec_mul_f32_f32(v_theta, hvx_vec_inverse_f32(v_ff)); 132 } 133 134 HVX_Vector v_theta_final = hvx_vec_mul_f32_f32(v_theta, v_freq_scale); 135 HVX_Vector vcos; 136 HVX_Vector vsin; 137 hvx_vec_sincos_f32(v_theta_final, &vcos, &vsin); 138 vcos = hvx_vec_mul_f32_f32(vcos, v_mscale); 139 vsin = hvx_vec_mul_f32_f32(vsin, v_mscale); 140 141 if (((uintptr_t) (cache + i0)) % 128 == 0) { 142 hvx_vmem(cache + i0 + 0) = vcos; 143 hvx_vmem(cache + i0 + 32) = vsin; 144 } else { 145 hvx_vec_store_u(cache + i0 + 0, 32 * sizeof(float), vcos); 146 hvx_vec_store_u(cache + i0 + 32, 32 * sizeof(float), vsin); 147 } 148 } 149 150 static __attribute__((noinline)) void rope_cache_init(const float theta_base, 151 const float freq_scale, 152 const float * freq_factors, 153 float * corr_dims, 154 const uint32_t n_cache, 155 const float ext_factor, 156 const float mscale, 157 float * cache, 158 const float theta_scale, 159 const float * theta_powers, 160 const float theta_scale_32) { 161 // ref: https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaYaRNScaledRotaryEmbedding.py 162 if (ext_factor == 0.0f) { 163 // Fast path: fully vectorized 164 // We process 32 pairs (64 elements) per iteration. 165 const uint32_t n_blocks = n_cache / 64; 166 167 HVX_Vector v_theta_powers = hvx_vmemu(theta_powers); 168 HVX_Vector v_freq_scale = hvx_vec_splat_f32(freq_scale); 169 HVX_Vector v_mscale = hvx_vec_splat_f32(mscale); 170 171 float theta_block = theta_base; 172 173 for (uint32_t b = 0; b < n_blocks; b++) { 174 uint32_t i0 = b * 64; 175 HVX_Vector v_theta_base = hvx_vec_splat_f32(theta_block); 176 HVX_Vector v_theta = hvx_vec_mul_f32_f32(v_theta_base, v_theta_powers); 177 rope_cache_hvx_32(cache, i0, v_theta, freq_factors, v_freq_scale, v_mscale); 178 theta_block *= theta_scale_32; 179 } 180 181 // Leftovers 182 float theta = theta_block; 183 for (uint32_t i0 = n_blocks * 64; i0 < n_cache; i0 += 2) { 184 const float ff = freq_factors ? freq_factors[i0 / 2] : 1.0f; 185 rope_yarn_one(theta / ff, freq_scale, corr_dims, i0, ext_factor, mscale, cache); 186 theta *= theta_scale; 187 } 188 } else { 189 float theta = theta_base; 190 for (uint32_t i0 = 0; i0 < n_cache; i0 += 2) { 191 const float ff = freq_factors ? freq_factors[i0 / 2] : 1.0f; 192 rope_yarn_one(theta / ff, freq_scale, corr_dims, i0, ext_factor, mscale, cache); 193 theta *= theta_scale; 194 } 195 } 196 } 197 198 static inline float mrope_pick_theta(float theta_t, float theta_h, float theta_w, float theta_e, 199 int sector, const int32_t sections[4], int sec_w, int sec_e, 200 bool is_imrope) { 201 if (is_imrope) { 202 if (sector % 3 == 0 && sector < 3 * sections[0]) { return theta_t; } 203 else if (sector % 3 == 1 && sector < 3 * sections[1]) { return theta_h; } 204 else if (sector % 3 == 2 && sector < 3 * sections[2]) { return theta_w; } 205 else { return theta_e; } 206 } 207 if (sector < sections[0]) { return theta_t; } 208 else if (sector < sec_w) { return theta_h; } 209 else if (sector < sec_e) { return theta_w; } 210 else { return theta_e; } 211 } 212 213 // lane j is 1 when (j % 3) == rem 214 static const float __attribute__((aligned(128))) mrope_mod3_eq0[32] = { 215 1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0 216 }; 217 static const float __attribute__((aligned(128))) mrope_mod3_eq1[32] = { 218 0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1 219 }; 220 static const float __attribute__((aligned(128))) mrope_mod3_eq2[32] = { 221 0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0 222 }; 223 224 static const float __attribute__((aligned(128))) mrope_k_ramp[32] = { 225 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 226 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 227 }; 228 229 static inline HVX_VectorPred mrope_mask_eq1(const float * m) { 230 return Q6_Q_vcmp_gt_VsfVsf(hvx_vmemu(m), Q6_V_vzero()); 231 } 232 233 // IMROPE without wrap: theta[k] = pos[k % 3] * scale^k 234 static inline HVX_Vector mrope_thetas_imrope_mod3(float pos_t, float pos_h, float pos_w, 235 uint32_t k0, HVX_Vector v_powers, float scale_block) { 236 const int r = (int) (k0 % 3); 237 const float * mt = (r == 0) ? mrope_mod3_eq0 : (r == 1) ? mrope_mod3_eq2 : mrope_mod3_eq1; 238 const float * mh = (r == 0) ? mrope_mod3_eq1 : (r == 1) ? mrope_mod3_eq0 : mrope_mod3_eq2; 239 240 HVX_Vector v = hvx_vec_splat_f32(pos_w); 241 v = Q6_V_vmux_QVV(mrope_mask_eq1(mh), hvx_vec_splat_f32(pos_h), v); 242 v = Q6_V_vmux_QVV(mrope_mask_eq1(mt), hvx_vec_splat_f32(pos_t), v); 243 v = hvx_vec_mul_f32_f32(v, v_powers); 244 return hvx_vec_mul_f32_f32(v, hvx_vec_splat_f32(scale_block)); 245 } 246 247 // Contiguous MROPE without wrap: theta[k] = pos[section(k)] * scale^k 248 static inline HVX_Vector mrope_thetas_contig(float pos_t, float pos_h, float pos_w, float pos_e, 249 uint32_t k0, int s0, int sec_w, int sec_e, 250 HVX_Vector v_powers, float scale_block) { 251 HVX_Vector v_k = hvx_vec_add_f32_f32(hvx_vec_splat_f32((float) k0), hvx_vmemu(mrope_k_ramp)); 252 HVX_VectorPred lt_s0 = Q6_Q_vcmp_gt_VsfVsf(hvx_vec_splat_f32((float) s0), v_k); 253 HVX_VectorPred lt_sw = Q6_Q_vcmp_gt_VsfVsf(hvx_vec_splat_f32((float) sec_w), v_k); 254 HVX_VectorPred lt_se = Q6_Q_vcmp_gt_VsfVsf(hvx_vec_splat_f32((float) sec_e), v_k); 255 256 HVX_Vector v = hvx_vec_splat_f32(pos_e); 257 v = Q6_V_vmux_QVV(lt_se, hvx_vec_splat_f32(pos_w), v); 258 v = Q6_V_vmux_QVV(lt_sw, hvx_vec_splat_f32(pos_h), v); 259 v = Q6_V_vmux_QVV(lt_s0, hvx_vec_splat_f32(pos_t), v); 260 v = hvx_vec_mul_f32_f32(v, v_powers); 261 return hvx_vec_mul_f32_f32(v, hvx_vec_splat_f32(scale_block)); 262 } 263 264 // pos_t/h/w/e: the four position ids for this sequence step (t=time, h=height, w=width, e=extra). 265 // sections[4]: number of head dims assigned to each position component. 266 static __attribute__((noinline)) void mrope_cache_init(const float pos_t, 267 const float pos_h, 268 const float pos_w, 269 const float pos_e, 270 const int32_t sections[4], 271 const bool is_imrope, 272 const bool indep_sects, 273 const float freq_scale, 274 const float * freq_factors, 275 float * corr_dims, 276 const uint32_t n_cache, 277 const float ext_factor, 278 const float mscale, 279 float * cache, 280 const float theta_scale, 281 const float * theta_powers, 282 const float theta_scale_32) { 283 const int sect_dims = sections[0] + sections[1] + sections[2] + sections[3]; 284 const int sec_w = sections[0] + sections[1]; 285 const int sec_e = sec_w + sections[2]; 286 const uint32_t n_pairs = n_cache / 2; 287 288 const bool no_wrap = (sect_dims > 0) && (n_pairs <= (uint32_t) sect_dims); 289 const bool imrope_mod3 = is_imrope && !indep_sects && no_wrap 290 && sections[0] > 0 && sections[1] > 0 && sections[2] > 0 291 && n_pairs <= (uint32_t) (3 * sections[0]) 292 && n_pairs <= (uint32_t) (3 * sections[1]) 293 && n_pairs <= (uint32_t) (3 * sections[2]); 294 const bool contig = !is_imrope && !indep_sects && no_wrap; 295 296 if (ext_factor == 0.0f && (imrope_mod3 || contig)) { 297 HVX_Vector v_powers = hvx_vmemu(theta_powers); 298 HVX_Vector v_freq_scale = hvx_vec_splat_f32(freq_scale); 299 HVX_Vector v_mscale = hvx_vec_splat_f32(mscale); 300 float scale_block = 1.0f; 301 const uint32_t n_blocks = n_cache / 64; 302 303 for (uint32_t b = 0; b < n_blocks; b++) { 304 const uint32_t i0 = b * 64; 305 const uint32_t k0 = b * 32; 306 HVX_Vector v_theta = imrope_mod3 307 ? mrope_thetas_imrope_mod3(pos_t, pos_h, pos_w, k0, v_powers, scale_block) 308 : mrope_thetas_contig(pos_t, pos_h, pos_w, pos_e, k0, sections[0], sec_w, sec_e, 309 v_powers, scale_block); 310 rope_cache_hvx_32(cache, i0, v_theta, freq_factors, v_freq_scale, v_mscale); 311 scale_block *= theta_scale_32; 312 } 313 314 float theta_k = scale_block; 315 for (uint32_t k = n_blocks * 32; k < n_pairs; k++) { 316 const uint32_t i0 = 2 * k; 317 const float pos = mrope_pick_theta(pos_t, pos_h, pos_w, pos_e, 318 (int) k, sections, sec_w, sec_e, is_imrope); 319 const float ff = freq_factors ? freq_factors[k] : 1.0f; 320 rope_yarn_one(pos * theta_k / ff, freq_scale, corr_dims, i0, ext_factor, mscale, cache); 321 theta_k *= theta_scale; 322 } 323 return; 324 } 325 326 float theta_t = pos_t; 327 float theta_h = pos_h; 328 float theta_w = pos_w; 329 float theta_e = pos_e; 330 331 const bool use_hvx = (ext_factor == 0.0f); 332 float __attribute__((aligned(128))) thetas[32]; 333 uint32_t n_thetas = 0; 334 uint32_t block_i0 = 0; 335 336 HVX_Vector v_freq_scale = hvx_vec_splat_f32(freq_scale); 337 HVX_Vector v_mscale = hvx_vec_splat_f32(mscale); 338 339 for (uint32_t i0 = 0; i0 < n_cache; i0 += 2) { 340 const int sector = (i0 / 2) % sect_dims; 341 342 if (indep_sects) { 343 // Reset theta when crossing into a new section. 344 if (sector == 0) { theta_t = pos_t; } 345 else if (sector == sections[0]) { theta_h = pos_h; } 346 else if (sector == sec_w) { theta_w = pos_w; } 347 else if (sector == sec_e) { theta_e = pos_e; } 348 } 349 350 const float theta = mrope_pick_theta(theta_t, theta_h, theta_w, theta_e, 351 sector, sections, sec_w, sec_e, is_imrope); 352 353 if (use_hvx) { 354 if (n_thetas == 0) { 355 block_i0 = i0; 356 } 357 thetas[n_thetas++] = theta; 358 if (n_thetas == 32) { 359 rope_cache_hvx_32(cache, block_i0, hvx_vmemu(thetas), freq_factors, v_freq_scale, v_mscale); 360 n_thetas = 0; 361 } 362 } else { 363 const float ff = freq_factors ? freq_factors[i0 / 2] : 1.0f; 364 rope_yarn_one(theta / ff, freq_scale, corr_dims, i0, ext_factor, mscale, cache); 365 } 366 367 theta_t *= theta_scale; 368 theta_h *= theta_scale; 369 theta_w *= theta_scale; 370 theta_e *= theta_scale; 371 } 372 373 for (uint32_t k = 0; k < n_thetas; k++) { 374 const uint32_t i0 = block_i0 + 2 * k; 375 const float ff = freq_factors ? freq_factors[i0 / 2] : 1.0f; 376 rope_yarn_one(thetas[k] / ff, freq_scale, corr_dims, i0, ext_factor, mscale, cache); 377 } 378 } 379 380 #define M_PI 3.`1415926535897932384626433` 381 382 static void rope_corr_dims(int n_dims, 383 int n_ctx_orig, 384 float freq_base, 385 float beta_fast, 386 float beta_slow, 387 float * dims) { 388 float start = floorf(n_dims * logf(n_ctx_orig / (beta_fast * 2 * (float) M_PI)) / (2 * logf(freq_base))); 389 float end = ceilf(n_dims * logf(n_ctx_orig / (beta_slow * 2 * (float) M_PI)) / (2 * logf(freq_base))); 390 dims[0] = MAX(0, start); 391 dims[1] = MIN(n_dims - 1, end); 392 } 393 394 static inline void hvx_rope_neox_mul(HVX_Vector v0, HVX_Vector v1, HVX_Vector vcos, HVX_Vector vsin, 395 HVX_Vector * o0, HVX_Vector * o1) { 396 HVX_Vector vx0_c = Q6_Vqf32_vmpy_VsfVsf(v0, vcos); 397 HVX_Vector vx0_s = Q6_Vqf32_vmpy_VsfVsf(v0, vsin); 398 HVX_Vector vx1_c = Q6_Vqf32_vmpy_VsfVsf(v1, vcos); 399 HVX_Vector vx1_s = Q6_Vqf32_vmpy_VsfVsf(v1, vsin); 400 *o0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_Vqf32Vqf32(vx0_c, vx1_s)); 401 *o1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(vx0_s, vx1_c)); 402 } 403 404 // theta_cache full 32-pair blocks are deinterleaved [cos | sin]. 405 static inline void hvx_rope_neox_f32_aa(float * restrict dst, const float * restrict src0, uint32_t ne, const float * restrict theta_cache) { 406 const uint32_t he = ne / 2; 407 const uint32_t nvec = he / 32; 408 const uint32_t nloe = he % 32; 409 410 if (nloe == 0) { 411 const HVX_Vector * vs = (const HVX_Vector *) src0; 412 const HVX_Vector * vt = (const HVX_Vector *) theta_cache; 413 HVX_Vector * vd = (HVX_Vector *) dst; 414 for (uint32_t i = 0; i < nvec; i++) { 415 HVX_Vector o0, o1; 416 hvx_rope_neox_mul(vs[i], vs[nvec + i], vt[i * 2 + 0], vt[i * 2 + 1], &o0, &o1); 417 vd[i] = o0; 418 vd[nvec + i] = o1; 419 } 420 return; 421 } 422 423 for (uint32_t i = 0; i < nvec; i++) { 424 HVX_Vector o0, o1; 425 hvx_rope_neox_mul(((const HVX_Vector *) src0)[i], 426 hvx_vmemu(src0 + he + i * 32), 427 ((const HVX_Vector *) theta_cache)[i * 2 + 0], 428 ((const HVX_Vector *) theta_cache)[i * 2 + 1], 429 &o0, &o1); 430 ((HVX_Vector *) dst)[i] = o0; 431 hvx_vmemu(dst + he + i * 32) = o1; 432 } 433 434 HVX_Vector v0 = hvx_vmemu(src0 + nvec * 32); 435 HVX_Vector v1 = hvx_vmemu(src0 + he + nvec * 32); 436 HVX_Vector vcos = hvx_vmemu(theta_cache + nvec * 64); 437 HVX_Vector vsin = hvx_vmemu(theta_cache + nvec * 64 + 32); 438 HVX_Vector o0, o1; 439 hvx_rope_neox_mul(v0, v1, vcos, vsin, &o0, &o1); 440 hvx_vec_store_u(dst + nvec * 32, nloe * sizeof(float), o0); 441 hvx_vec_store_u(dst + he + nvec * 32, nloe * sizeof(float), o1); 442 } 443 444 static inline void hvx_rope_f32_aa(float * restrict dst, const float * restrict src0, uint32_t ne, const float * restrict theta_cache) { 445 const uint32_t nvec = ne / 64; 446 const uint32_t nloe = ne % 64; 447 448 for (uint32_t i = 0; i < nvec; i++) { 449 HVX_Vector v0 = ((const HVX_Vector *) src0)[i * 2 + 0]; 450 HVX_Vector v1 = ((const HVX_Vector *) src0)[i * 2 + 1]; 451 452 HVX_Vector vcos = ((const HVX_Vector *) theta_cache)[i * 2 + 0]; 453 HVX_Vector vsin = ((const HVX_Vector *) theta_cache)[i * 2 + 1]; 454 455 HVX_VectorPair vx0_x1 = Q6_W_vdeal_VVR(v1, v0, -4); 456 457 HVX_Vector vx0_c = Q6_Vqf32_vmpy_VsfVsf(Q6_V_lo_W(vx0_x1), vcos); 458 HVX_Vector vx0_s = Q6_Vqf32_vmpy_VsfVsf(Q6_V_lo_W(vx0_x1), vsin); 459 HVX_Vector vx1_c = Q6_Vqf32_vmpy_VsfVsf(Q6_V_hi_W(vx0_x1), vcos); 460 HVX_Vector vx1_s = Q6_Vqf32_vmpy_VsfVsf(Q6_V_hi_W(vx0_x1), vsin); 461 462 HVX_Vector v4 = Q6_Vqf32_vsub_Vqf32Vqf32(vx0_c, vx1_s); 463 HVX_Vector v5 = Q6_Vqf32_vadd_Vqf32Vqf32(vx0_s, vx1_c); 464 465 HVX_VectorPair vstore = Q6_W_vshuff_VVR(Q6_Vsf_equals_Vqf32(v5), Q6_Vsf_equals_Vqf32(v4), -4); 466 467 ((HVX_Vector *) dst)[i * 2 + 0] = Q6_V_lo_W(vstore); 468 ((HVX_Vector *) dst)[i * 2 + 1] = Q6_V_hi_W(vstore); 469 } 470 471 if (nloe > 0) { 472 if (nloe <= 32) { 473 HVX_Vector v0 = hvx_vmemu(src0 + nvec * 64); 474 HVX_Vector vcos = hvx_vmemu(theta_cache + nvec * 64); 475 HVX_Vector vsin = hvx_vmemu(theta_cache + nvec * 64 + 32); 476 477 HVX_VectorPair vx0_x1 = Q6_W_vdeal_VVR(Q6_V_vzero(), v0, -4); 478 479 HVX_Vector vx0_c = Q6_Vqf32_vmpy_VsfVsf(Q6_V_lo_W(vx0_x1), vcos); 480 HVX_Vector vx0_s = Q6_Vqf32_vmpy_VsfVsf(Q6_V_lo_W(vx0_x1), vsin); 481 HVX_Vector vx1_c = Q6_Vqf32_vmpy_VsfVsf(Q6_V_hi_W(vx0_x1), vcos); 482 HVX_Vector vx1_s = Q6_Vqf32_vmpy_VsfVsf(Q6_V_hi_W(vx0_x1), vsin); 483 484 HVX_Vector v4 = Q6_Vqf32_vsub_Vqf32Vqf32(vx0_c, vx1_s); 485 HVX_Vector v5 = Q6_Vqf32_vadd_Vqf32Vqf32(vx0_s, vx1_c); 486 487 HVX_VectorPair vstore = Q6_W_vshuff_VVR(Q6_Vsf_equals_Vqf32(v5), Q6_Vsf_equals_Vqf32(v4), -4); 488 489 hvx_vec_store_u(dst + nvec * 64, nloe * sizeof(float), Q6_V_lo_W(vstore)); 490 } else { 491 HVX_Vector v0 = hvx_vmemu(src0 + nvec * 64); 492 HVX_Vector v1 = hvx_vmemu(src0 + nvec * 64 + 32); 493 494 HVX_Vector vcos = hvx_vmemu(theta_cache + nvec * 64); 495 HVX_Vector vsin = hvx_vmemu(theta_cache + nvec * 64 + 32); 496 497 HVX_VectorPair vx0_x1 = Q6_W_vdeal_VVR(v1, v0, -4); 498 499 HVX_Vector vx0_c = Q6_Vqf32_vmpy_VsfVsf(Q6_V_lo_W(vx0_x1), vcos); 500 HVX_Vector vx0_s = Q6_Vqf32_vmpy_VsfVsf(Q6_V_lo_W(vx0_x1), vsin); 501 HVX_Vector vx1_c = Q6_Vqf32_vmpy_VsfVsf(Q6_V_hi_W(vx0_x1), vcos); 502 HVX_Vector vx1_s = Q6_Vqf32_vmpy_VsfVsf(Q6_V_hi_W(vx0_x1), vsin); 503 504 HVX_Vector v4 = Q6_Vqf32_vsub_Vqf32Vqf32(vx0_c, vx1_s); 505 HVX_Vector v5 = Q6_Vqf32_vadd_Vqf32Vqf32(vx0_s, vx1_c); 506 507 HVX_VectorPair vstore = Q6_W_vshuff_VVR(Q6_Vsf_equals_Vqf32(v5), Q6_Vsf_equals_Vqf32(v4), -4); 508 509 ((HVX_Vector *) dst)[nvec * 2 + 0] = Q6_V_lo_W(vstore); 510 hvx_vec_store_u(dst + nvec * 64 + 32, (nloe - 32) * sizeof(float), Q6_V_hi_W(vstore)); 511 } 512 } 513 } 514 515 static void inline rope_basic_f32_inplace(struct htp_rope_context * rctx, uint8_t * src, 516 uint32_t nr, const float * restrict theta_cache) { 517 const uint32_t n_offs = rctx->n_offs; 518 #pragma unroll(4) 519 for (uint32_t i = 0; i < nr; i++) { 520 float * s = (float *) (src + i * rctx->src0_row_size_aligned); 521 hvx_rope_f32_aa(s + n_offs, s + n_offs, rctx->n_dims, theta_cache); 522 } 523 } 524 525 static void inline rope_neox_f32_inplace(struct htp_rope_context * rctx, uint8_t * src, 526 uint32_t nr, uint32_t ne, const float * restrict theta_cache) { 527 const uint32_t n_offs = rctx->n_offs; 528 #pragma unroll(4) 529 for (uint32_t i = 0; i < nr; i++) { 530 float * s = (float *) (src + i * rctx->src0_row_size_aligned); 531 hvx_rope_neox_f32_aa(s + n_offs, s + n_offs, ne, theta_cache); 532 } 533 } 534 535 static void rope_job_f32(unsigned int nth, unsigned int ith, void * data) { 536 struct htp_rope_context * rctx = (struct htp_rope_context *) data; 537 struct htp_ops_context * octx = rctx->octx; 538 539 const struct htp_tensor * src0 = octx->src[0]; 540 const struct htp_tensor * src1 = octx->src[1]; 541 const struct htp_tensor * src2 = octx->src[2]; 542 const struct htp_tensor * dst = octx->dst; 543 544 htp_rope_preamble; 545 546 const uint32_t src0_nrows = rctx->nrows; 547 const uint32_t src0_nrows_per_thread = rctx->src0_nrows_per_thread; 548 549 const uint32_t src0_start_row = rctx->row_start + src0_nrows_per_thread * ith; 550 const uint32_t src0_end_row = MIN(src0_start_row + src0_nrows_per_thread, rctx->row_start + src0_nrows); 551 552 // no work for this thread 553 if (src0_start_row >= src0_end_row) { 554 return; 555 } 556 557 const int32_t mode = rctx->mode; 558 // MROPE, IMROPE and VISION use NEOX-style pairing for the rotation 559 const bool is_neox = (mode & HTP_ROPE_TYPE_NEOX) || (mode & HTP_ROPE_TYPE_MROPE); 560 const bool is_vision = (mode == HTP_ROPE_TYPE_VISION); 561 562 // VTCM setup 563 uint8_t * src0_spad_base = rctx->vtcm_base + (ith * rctx->spad_per_thread); 564 float * theta_cache = (float *) (src0_spad_base); 565 src0_spad_base = src0_spad_base + rctx->theta_cache_offset; 566 567 dma_queue * dma_q = octx->ctx->dma[ith]; 568 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 569 const int32_t * pos = (const int32_t *) (uintptr_t) src1->data; 570 const float * freq_factors = rctx->freq_factors; 571 572 const uint32_t i3_start = fastdiv(src0_start_row, &rctx->div_ne2_ne1); 573 const uint32_t rem = fastmodulo(src0_start_row, ne2 * ne1, &rctx->div_ne2_ne1); 574 const uint32_t i2_start = fastdiv(rem, &rctx->div_ne1); 575 const uint32_t i1_start = fastmodulo(rem, ne1, &rctx->div_ne1); 576 577 uint32_t ir = src0_start_row; 578 uint32_t prev_i2 = (uint32_t) -1; 579 uint32_t cur_slot = 0; 580 581 for (uint32_t i3 = i3_start; i3 < ne3; i3++) { // batch 582 const uint32_t i2_init = (i3 == i3_start) ? i2_start : 0; 583 for (uint32_t i2 = i2_init; i2 < ne2; i2++) { // seq-len 584 const uint32_t i1_init = (i3 == i3_start && i2 == i2_start) ? i1_start : 0; 585 for (uint32_t i1 = i1_init; i1 < ne1; ) { // attn-heads 586 if (ir >= src0_end_row) goto done; 587 588 // Rows in this block 589 const uint32_t nrows = MIN(src0_end_row - ir, ne1 - i1); 590 591 // Depth before prefetch 592 const uint32_t dma_depth = dma_queue_depth(dma_q); 593 594 // Prefetch up to 2 blocks 595 const uint32_t p_nrows = MIN(nrows, 2 * HTP_ROPE_SPAD_BLOCK); 596 for (uint32_t pr = 0; pr < p_nrows; pr += HTP_ROPE_SPAD_BLOCK) { 597 const uint32_t pnr = MIN(nrows - pr, HTP_ROPE_SPAD_BLOCK); 598 const uint32_t slot = (cur_slot + pr / HTP_ROPE_SPAD_BLOCK) % HTP_ROPE_SPAD_NSLOTS; 599 uint8_t * spad_slot = rope_spad_slot(src0_spad_base, slot, rctx->src0_row_size_aligned); 600 const dma_addr_t src0_data = src0->data + i3 * nb03 + i2 * nb02 + (i1 + pr) * nb01; 601 602 // Dummy DMA transaction for sequencing (interleaving wr, rd, wr, rd, ...) 603 dma_queue_push(dma_q, dma_make_data(dst->data, spad_slot), 0, 0, 0, 0); 604 605 dma_queue_push(dma_q, dma_make_data(spad_slot, src0_data), 606 rctx->src0_row_size_aligned, rctx->src0_row_stride, rctx->src0_row_size, pnr); 607 } 608 609 // Update theta cache 610 if (i2 != prev_i2) { 611 prev_i2 = i2; 612 613 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_A_PREP, i2); 614 // VISION rotates the full row; other modes only rotate n_dims. 615 const uint32_t n_cache = is_vision ? ne0 : (uint32_t) rctx->n_dims; 616 const bool is_mrope = (rctx->mode & HTP_ROPE_TYPE_MROPE) != 0; 617 if (is_mrope) { 618 // src1 holds four position arrays stacked along ne0: 619 // pos[i2], pos[i2+ne2], pos[i2+ne2*2], pos[i2+ne2*3] 620 const bool is_imrope = (rctx->mode == HTP_ROPE_TYPE_IMROPE); 621 mrope_cache_init( 622 (float) pos[i2], 623 (float) pos[i2 + ne2], 624 (float) pos[i2 + ne2 * 2], 625 (float) pos[i2 + ne2 * 3], 626 rctx->sections, is_imrope, is_vision, 627 rctx->freq_scale, freq_factors, rctx->corr_dims, 628 n_cache, rctx->ext_factor, rctx->attn_factor, 629 theta_cache, rctx->theta_scale, rctx->theta_powers, rctx->theta_scale_32); 630 } else { 631 rope_cache_init(pos[i2], rctx->freq_scale, freq_factors, rctx->corr_dims, 632 n_cache, rctx->ext_factor, rctx->attn_factor, 633 theta_cache, rctx->theta_scale, rctx->theta_powers, rctx->theta_scale_32); 634 } 635 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_A_PREP, i2); 636 } 637 638 // Skip output DMA transactions from prev block (if any) 639 for (uint32_t d = 0; d < dma_depth; d++) { dma_queue_pop_nowait(dma_q); } 640 641 // Compute loop 642 const uint32_t ne = is_vision ? ne0 : rctx->n_dims; 643 const uint32_t base_i1 = i1; 644 const uint32_t base_ir = ir; 645 646 for (uint32_t cnr = 0, cr = 0; cr < nrows; cr += cnr) { 647 cnr = MIN(nrows - cr, HTP_ROPE_SPAD_BLOCK); 648 const uint32_t slot = (cur_slot + cr / HTP_ROPE_SPAD_BLOCK) % HTP_ROPE_SPAD_NSLOTS; 649 const uint32_t cur_ir = base_ir + cr; 650 const uint32_t cur_i1 = base_i1 + cr; 651 652 dma_queue_pop(dma_q); 653 uint8_t * cur_spad = (uint8_t *) dma_queue_pop(dma_q).dst; 654 655 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, cur_ir); 656 if (is_neox || is_vision) { 657 rope_neox_f32_inplace(rctx, cur_spad, cnr, ne, theta_cache); 658 } else { 659 rope_basic_f32_inplace(rctx, cur_spad, cnr, theta_cache); 660 } 661 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, cur_ir); 662 663 const dma_addr_t dst_data = dst->data + i3 * nb3 + i2 * nb2 + cur_i1 * nb1; 664 dma_queue_push(dma_q, dma_make_data(dst_data, cur_spad), 665 rctx->dst_row_stride, rctx->src0_row_size_aligned, rctx->dst_row_size, cnr); 666 667 // Prefetch 2 blocks ahead into the slot just freed 668 if ((cr + 2 * HTP_ROPE_SPAD_BLOCK) < nrows) { 669 const uint32_t p_cr = cr + 2 * HTP_ROPE_SPAD_BLOCK; 670 const uint32_t pnr = MIN(nrows - p_cr, HTP_ROPE_SPAD_BLOCK); 671 const uint32_t p_slot = (cur_slot + p_cr / HTP_ROPE_SPAD_BLOCK) % HTP_ROPE_SPAD_NSLOTS; 672 uint8_t * p_spad = rope_spad_slot(src0_spad_base, p_slot, rctx->src0_row_size_aligned); 673 const dma_addr_t p_src0_data = src0->data + i3 * nb03 + i2 * nb02 + (base_i1 + p_cr) * nb01; 674 675 dma_queue_push(dma_q, dma_make_data(p_spad, p_src0_data), 676 rctx->src0_row_size_aligned, rctx->src0_row_stride, rctx->src0_row_size, pnr); 677 } 678 } 679 680 const uint32_t n_chunks = (nrows + HTP_ROPE_SPAD_BLOCK - 1) / HTP_ROPE_SPAD_BLOCK; 681 cur_slot = (cur_slot + n_chunks) % HTP_ROPE_SPAD_NSLOTS; 682 683 ir += nrows; 684 i1 += nrows; 685 } 686 } 687 } 688 689 done: 690 dma_queue_flush(dma_q); 691 692 FARF(HIGH, "rope-f32: %d/%d: (%u:%u)\n", ith, nth, src0_start_row, src0_end_row); 693 } 694 695 static int execute_op_rope_f32(struct htp_ops_context * octx) { 696 int err = HTP_STATUS_OK; 697 698 const struct htp_tensor * src0 = octx->src[0]; 699 const struct htp_tensor * src1 = octx->src[1]; 700 const struct htp_tensor * src2 = octx->src[2]; 701 const struct htp_tensor * dst = octx->dst; 702 703 const struct htp_rope_kernel_params * kparams = (const struct htp_rope_kernel_params *) octx->kernel_params; 704 if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) { 705 return HTP_STATUS_INVAL_PARAMS; 706 } 707 assert(octx->ctx->vtcm_size >= kparams->vtcm_size); 708 709 if (htp_tensor_is_extended(src1)) { 710 return HTP_STATUS_NO_SUPPORT; 711 } 712 713 const uint32_t total_rows = src0->ne[1] * src0->ne[2] * src0->ne[3]; 714 const size_t dst_data_row_size = dst->ne[0] * sizeof(float); 715 716 uint32_t row_start = 0; 717 uint32_t nrows = total_rows; 718 719 if (octx->ctx->mdev.count > 1) { 720 uint32_t rows_per_chunk = 0; 721 htp_tensor_mdev_rows_per_chunk(dst, sizeof(float), (uint32_t) dst_data_row_size, &rows_per_chunk); 722 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition( 723 total_rows, rows_per_chunk, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); 724 row_start = range.start; 725 nrows = range.count; 726 } 727 728 if (nrows == 0) { 729 return HTP_STATUS_OK; 730 } 731 732 const uint32_t n_threads = octx->n_threads; 733 734 const uint32_t ne0 = dst->ne[0]; 735 const size_t src0_row_size = src0->ne[0] * sizeof(float); 736 const size_t src0_row_stride = src0->nb[1]; 737 const size_t dst_row_size = dst->ne[0] * sizeof(float); 738 const size_t dst_row_stride = dst->nb[1]; 739 740 struct htp_rope_vtcm_layout layout; 741 htp_rope_vtcm_layout_build(&layout, src0->ne[0], kparams->n_threads, src2 ? src2->ne[0] : 0); 742 743 struct htp_rope_context rctx; 744 memset(&rctx, 0, sizeof(struct htp_rope_context)); 745 746 rctx.octx = octx; 747 rctx.vtcm_base = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, layout.off_spad); 748 rctx.spad_per_thread = layout.bytes_per_thread; 749 rctx.theta_cache_offset = layout.theta_cache_size_aligned; 750 751 if (src2) { 752 dma_queue * dma_q = octx->ctx->dma[0]; 753 const size_t ff_size = src2->ne[0] * sizeof(float); 754 float * vtcm_freq_factors = VTCM_LAYOUT_PTR(float, octx->ctx->vtcm_base, layout.off_freq_factors); 755 dma_queue_push(dma_q, dma_make_data(vtcm_freq_factors, src2->data), 756 layout.freq_factors_size_aligned, 0, ff_size, 1); 757 dma_queue_pop(dma_q); 758 rctx.freq_factors = vtcm_freq_factors; 759 } 760 761 const int32_t * op_params = &octx->op_params[0]; 762 rctx.n_dims = ((const int32_t *) op_params)[1]; 763 rctx.mode = ((const int32_t *) op_params)[2]; 764 rctx.n_ctx_orig = ((const int32_t *) op_params)[4]; 765 rctx.n_offs = ((const int32_t *) op_params)[15]; 766 767 memcpy(&rctx.freq_base, (int32_t *) op_params + 5, sizeof(float)); 768 memcpy(&rctx.freq_scale, (int32_t *) op_params + 6, sizeof(float)); 769 memcpy(&rctx.ext_factor, (int32_t *) op_params + 7, sizeof(float)); 770 memcpy(&rctx.attn_factor, (int32_t *) op_params + 8, sizeof(float)); 771 memcpy(&rctx.beta_fast, (int32_t *) op_params + 9, sizeof(float)); 772 memcpy(&rctx.beta_slow, (int32_t *) op_params + 10, sizeof(float)); 773 memcpy(&rctx.sections, (int32_t *) op_params + 11, sizeof(int) * 4); 774 775 rctx.theta_scale = powf(rctx.freq_base, -2.0f / rctx.n_dims); 776 rctx.theta_powers[0] = 1.0f; 777 for (int j = 1; j < 32; j++) { 778 rctx.theta_powers[j] = rctx.theta_powers[j - 1] * rctx.theta_scale; 779 } 780 rctx.theta_scale_32 = rctx.theta_powers[31] * rctx.theta_scale; 781 782 rope_corr_dims(rctx.n_dims, rctx.n_ctx_orig, rctx.freq_base, rctx.beta_fast, rctx.beta_slow, rctx.corr_dims); 783 784 rctx.src0_row_size = src0_row_size; 785 rctx.src0_row_stride = src0_row_stride; 786 rctx.dst_row_size = dst_row_size; 787 rctx.dst_row_stride = dst_row_stride; 788 rctx.src0_row_size_aligned = layout.src0_row_size_aligned; 789 790 rctx.src0_nrows = nrows; 791 rctx.nrows = nrows; 792 rctx.row_start = row_start; 793 rctx.src0_nrows_per_thread = fastdiv(nrows + n_threads - 1, &octx->n_threads_div); 794 rctx.div_ne2_ne1 = kparams->div_ne2_ne1; 795 rctx.div_ne1 = kparams->div_ne1; 796 797 FARF(HIGH, "rope-f32 n-rows %u n-dims %d ne0 %u ext-factor %.6f theta-scale %.6f attn-factor %.6f\n", rctx.src0_nrows, rctx.n_dims, ne0, 798 rctx.ext_factor, rctx.theta_scale, rctx.attn_factor); 799 800 work_queue_run(octx->ctx->work_queue, rope_job_f32, &rctx, n_threads); 801 802 return err; 803 } 804 805 int op_rope(struct htp_ops_context * octx) { 806 switch (octx->src[0]->type) { 807 case HTP_TYPE_F32: 808 return execute_op_rope_f32(octx); 809 810 default: 811 return HTP_STATUS_NO_SUPPORT; 812 } 813 }