Add static YaRN to Qwen rotary paths with independent CPU and ROCm vectors

This commit is contained in:
hazyumps authored and StillDeadcode committed 2026-10-07 11:09:27 +02:00
1 parent d23c3e0d17
commit 2af7fa4022
22 files changed
+7542 -27

No files matched your search

+16 -1
View File
@@ -54,6 +54,7 @@
*/
#include "avx_vec.h"
#include "avx_common.h"
#include "rad_yarn.h"
using namespace avx;
@@ -836,6 +837,12 @@ AVX_KERNEL(rope) {
}
}
const int pair_mode = mode == ROPE_GPTJ ? ROPE_GPTJ : ROPE_NEOX;
const RadYarn yarn = rad_yarn_args(a);
if (!rad_yarn_valid(yarn)) return RAD_E_INVAL;
if (yarn.factor != 0.0f) {
if (mode == ROPE_AXIAL || rscale != 1.0f) return RAD_E_UNSUPPORTED;
for (int64_t i = 0; i < half; ++i) invf[i] = yarn.frequency(invf[i], (int)i);
}
const int64_t pcs = multi ? pos->stride[0] : 0, pms = multi ? pos->stride[1] : 0;
AVX_PARALLEL_FOR_IF(M * (n_head + n_head_kv) * rot >= AVX_PAR_MIN)
@@ -851,6 +858,8 @@ AVX_KERNEL(rope) {
} else {
angles(cs, invf, half, (float)ldi(pos, offlin(pos, m)) / div);
}
if (yarn.factor != 0.0f)
for (int64_t i = 0; i < rot; ++i) cs[i] *= yarn.magnitude;
/* Q heads then K heads. The V heads that follow are left alone, which is the whole reason
* this op takes the fused qkv rather than two tensors. */
for (int64_t h = 0; h < n_head + n_head_kv; ++h)
@@ -953,7 +962,11 @@ AVX_KERNEL(rope_table) {
if (M <= 0 || numel(cs) % rot) return RAD_E_SHAPE;
const int64_t rows = numel(cs) / rot;
const int64_t half = rot / 2;
const float* invf = inv_freqs(half, rot, theta);
float* invf = inv_freqs(half, rot, theta);
const RadYarn yarn = rad_yarn_args(a);
if (!rad_yarn_valid(yarn) || (yarn.factor != 0.0f && rscale != 1.0f)) return RAD_E_INVAL;
if (yarn.factor != 0.0f)
for (int64_t i = 0; i < half; ++i) invf[i] = yarn.frequency(invf[i], (int)i);
AVX_PARALLEL_FOR_IF(M * rot >= AVX_PAR_MIN)
for (int64_t m = 0; m < M; ++m) {
@@ -962,6 +975,8 @@ AVX_KERNEL(rope_table) {
const float p = (float)pp / (rscale != 0.0f ? rscale : 1.0f);
float* buf = scratch_f32((size_t)rot);
angles(buf, invf, half, p);
if (yarn.factor != 0.0f)
for (int64_t i = 0; i < rot; ++i) buf[i] *= yarn.magnitude;
/* `cs` may be strided, and offlin has to be evaluated per element there -- which is what
* libref does unconditionally. The dense case is one row store. */
if (rad_tensor_is_contiguous(cs)) {
+11
View File
@@ -206,6 +206,17 @@ std::vector<Case> build_cases() {
}
add("rope_table", { HI("M", 12), HI("rot", 64), HF("theta", 10000.0), HF("scale", 1.0),
HS("dtype", "f32") });
for (const char* dt : { "f32", "bf16" }) {
for (const char* mode : { "neox", "gptj", "imrope" })
add("rope", { HI("M", 12), HI("head_dim", 128), HI("n_head", 4), HI("n_head_kv", 2),
HF("theta", 1e7), HF("scale", 1), HS("mode", mode),
HI("rotary_dim", 64), HS("sections", "11 11 10"), HS("dtype", dt),
HF("yarn_factor", 2), HF("yarn_low", 14), HF("yarn_high", 22),
HF("yarn_magnitude", 1.069314718) });
add("rope_table", { HI("M", 12), HI("rot", 64), HF("theta", 1e7), HF("scale", 1),
HS("dtype", dt), HF("yarn_factor", 2), HF("yarn_low", 14),
HF("yarn_high", 22), HF("yarn_magnitude", 1.069314718) });
}
/* The multi-component forms: Qwen3-VL's interleaved M-RoPE over a partial rotary, and a vision
* tower's 2-D axial rotary over a whole 72-wide head. */
for (const char* dt : { "f32", "bf16" }) {
+6 -2
View File
@@ -218,11 +218,15 @@ static const RadOperandSpec oKvStore[] = { OPD("k"), OPD("v"), OPD("slot_mapping
static const RadParamSpec pRope[] = { P_INT("M"), P_INT("head_dim"), P_INT("n_head"),
P_INT("n_head_kv"), P_F64("theta"), P_F64("scale"),
P_STR("mode"), P_INTO("rotary_dim"), P_STRO("sections") };
P_STR("mode"), P_INTO("rotary_dim"), P_STRO("sections"),
P_F64O("yarn_factor"), P_F64O("yarn_low"),
P_F64O("yarn_high"), P_F64O("yarn_magnitude") };
static const RadOperandSpec oRope[] = { INOUT("qkv"), OPD("positions") };
static const RadParamSpec pRopeTable[] = { P_INT("M"), P_INT("rot"), P_F64("theta"),
P_F64("scale"), P_STR("dtype") };
P_F64("scale"), P_STR("dtype"),
P_F64O("yarn_factor"), P_F64O("yarn_low"),
P_F64O("yarn_high"), P_F64O("yarn_magnitude") };
static const RadOperandSpec oRopeTable[] = { OPD("positions"), OUT("cos_sin") };
static const RadParamSpec pQkNormRope[] = { P_INT("M"), P_INT("head_dim"), P_INT("n_head"),