diff --git a/libavx/avx_attn.cpp b/libavx/avx_attn.cpp index 06494693..d26fbfb7 100644 --- a/libavx/avx_attn.cpp +++ b/libavx/avx_attn.cpp @@ -1186,7 +1186,7 @@ AVX_KERNEL(rope) { static const char* kModes[] = { "neox", "gptj", "mrope", "imrope", "axial" }; const int mode = p_enum(a, "mode", kModes, 5, -1); if (mode < 0) return RAD_E_UNSUPPORTED; - const int64_t rot = p_int(a, "rotary_dim", head_dim); + int64_t rot = p_int(a, "rotary_dim", head_dim); if (head_dim <= 0 || n_head <= 0 || rot <= 0 || rot > head_dim || (rot & 1)) return RAD_E_SHAPE; int64_t den; const int lrc = rope_ladder(a, rot, head_dim, &den); @@ -1214,6 +1214,10 @@ AVX_KERNEL(rope) { if (M <= 0 || numel(qkv) < M * stride_row) return RAD_E_SHAPE; const int64_t rs = qkv->rank >= 2 ? qkv->stride[qkv->rank - 2] : stride_row; const int64_t ds = laststride(qkv); + /* THE PROPORTIONAL LADDER (`head`) is HF's: the rotation spans the whole head -- pair i is + * (i, i + head_dim/2) -- and every frequency past rotary_dim/2 is zero (libref, ref_rope). */ + const int64_t nfreq = rot / 2; + if (den == head_dim && rot < head_dim && !multi && mode == ROPE_NEOX) rot = head_dim; const int64_t half = rot / 2; float* invf = inv_freqs(half, den, theta); int comp[512] = {}; @@ -1243,6 +1247,7 @@ AVX_KERNEL(rope) { 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); } + for (int64_t i = nfreq; i < half; ++i) invf[i] = 0.0f; 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) diff --git a/libr4d/r4d_model_bf16.hip b/libr4d/r4d_model_bf16.hip index 4f56fcec..b5fd739b 100644 --- a/libr4d/r4d_model_bf16.hip +++ b/libr4d/r4d_model_bf16.hip @@ -805,7 +805,7 @@ __device__ __forceinline__ int rope_comp(int mode, uint32_t i, const int* sec, i __global__ __launch_bounds__(kThreads) void rope_kernel( uint16_t* __restrict__ qkv, const int32_t* __restrict__ pos, - uint32_t head_dim, uint32_t n_rope_heads, uint32_t rot, int den, int gptj, + uint32_t head_dim, uint32_t n_rope_heads, uint32_t rot, uint32_t nfreq, int den, int gptj, float theta, float rscale, uint32_t row_pitch, int mode, int s0, int s1, int s2, int n_sec, uint32_t plane, RadYarn yarn) { const uint32_t m = blockIdx.x; @@ -828,6 +828,10 @@ __global__ __launch_bounds__(kThreads) void rope_kernel( inv = 1.0f / powf(theta, (float)(2 * j) / (float)(2 * sec[c])); } inv = yarn.frequency(inv, (int)i); + /* THE PROPORTIONAL LADDER rotates the whole head with every frequency past nfreq zero + * (HF's _compute_proportional_rope_parameters), so pair i is (i, i + head_dim/2) and the + * pairs past nfreq turn by 0: cos 1, sin 0, the value back exactly. */ + if (i >= nfreq) inv = 0.0f; const float ang = p * inv; float c, s; sincosf(ang, &s, &c); @@ -1320,7 +1324,8 @@ extern "C" int r4d_rope_bf16(long qkv, long pos, int M, int head_dim, int n_rope // the caller's, and the plain entry passes `rot`, as it always has. static int rope_launch(long qkv, long pos, int M, int head_dim, int n_rope_heads, int rot, int den, int gptj, float theta, float rscale, int row_pitch, int mode, - const int* sec, int n_sec, int plane, RadYarn yarn, long stream) { + const int* sec, int n_sec, int plane, RadYarn yarn, long stream, + int nfreq = -1) { if (!qkv || !pos) return -1; if (M <= 0 || head_dim <= 0 || n_rope_heads <= 0) return -2; if (rot <= 0 || rot > head_dim || (rot & 1)) return -3; @@ -1329,9 +1334,11 @@ static int rope_launch(long qkv, long pos, int M, int head_dim, int n_rope_heads const uint32_t pairs = (uint32_t)n_rope_heads * (uint32_t)(rot / 2); const uint32_t gy = (pairs + kThreads - 1) / kThreads; const int s0 = n_sec > 0 ? sec[0] : 0, s1 = n_sec > 1 ? sec[1] : 0, s2 = n_sec > 2 ? sec[2] : 0; + if (nfreq < 0) nfreq = rot / 2; hipLaunchKernelGGL(rope_kernel, dim3((uint32_t)M, gy), dim3(kThreads), 0, (hipStream_t)stream, (uint16_t*)qkv, (const int32_t*)pos, (uint32_t)head_dim, - (uint32_t)n_rope_heads, (uint32_t)rot, (int)den, gptj, theta, rscale, + (uint32_t)n_rope_heads, (uint32_t)rot, (uint32_t)nfreq, (int)den, gptj, theta, + rscale, (uint32_t)row_pitch, mode, s0, s1, s2, n_sec, (uint32_t)plane, yarn); return hipGetLastError() == hipSuccess ? 0 : -4; } @@ -2181,6 +2188,11 @@ extern "C" int r4d_rad_rope(const RadArgs* a, RadStream s) { else if (std::strcmp(lad, "rotary") && std::strcmp(lad, "head")) return R4D_NO(RAD_E_UNSUPPORTED); } + /* `head` is HF's proportional RoPE: the rotation spans the WHOLE head (rotate_half over + * head_dim, so pair i is (i, i + head_dim/2)) and only the first rotary_dim/2 frequencies are + * non-zero. Rotating the first rotary_dim dims as a rotary of their own pairs (i, i + rot/2) + * instead -- Gemma 4's global layers, 128 of 512 -- turns the wrong channels, and long-range + * attention degrades with distance. */ /* THE MODE, and with it how many components a position has. With [M] positions -- one * component a token -- mrope and imrope ARE neox: every frequency reads the same position and @@ -2231,9 +2243,12 @@ extern "C" int r4d_rad_rope(const RadArgs* a, RadStream s) { if (yarn.factor != 0.0f && (mc == 4 || rscale != 1.0f)) return R4D_NO(RAD_E_UNSUPPORTED); /* The Q heads and the K heads rotate; the V heads that follow them do not, which is the whole * reason this op takes the fused projection rather than two tensors. */ + long long rot_l = rot, nfreq = rot / 2; + if (den == head_dim && rot < head_dim && !multi && !gptj && mc == 0) rot_l = head_dim; return rope_launch(r4d_addr(qkv), r4d_addr(pos), (int)Mp, (int)head_dim, - (int)(n_head + n_head_kv), (int)rot, (int)den, gptj, theta, rscale, - (int)pitch, mc, sec, n_sec, plane, yarn, (long)(uintptr_t)s) == 0 + (int)(n_head + n_head_kv), (int)rot_l, (int)den, gptj, theta, rscale, + (int)pitch, mc, sec, n_sec, plane, yarn, (long)(uintptr_t)s, + (int)nfreq) == 0 ? RAD_OK : RAD_E_DEVICE; } diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index c651a66e..506567ba 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -4634,7 +4634,8 @@ static void rope_ladder_case(const Plugin& pl) { for (int64_t h = 0; h < n_rope; ++h) { for (int64_t i = 0; i < half; ++i) { const size_t i0 = base + (size_t)h * hd + i; - const size_t i1 = i0 + half; + /* `head` is HF's proportional RoPE: pair i is (i, i + head_dim/2). */ + const size_t i1 = i0 + ((lad && !std::strcmp(lad, "head")) ? hd / 2 : half); const float x0 = h_bf16(saved[(size_t)i0]), x1 = h_bf16(saved[(size_t)i1]); const float inv = 1.0f / powf(theta, (float)(2 * i) / (float)den); float s, c; diff --git a/libref/ref_attn.cpp b/libref/ref_attn.cpp index c1fa9018..1d59a547 100644 --- a/libref/ref_attn.cpp +++ b/libref/ref_attn.cpp @@ -499,6 +499,11 @@ int ref_rope(const RadArgs* a, RadStream) { const int64_t need = mode == ROPE_IMROPE ? 3 : n_sec; if (n_comp < need || n_comp > 8) return RAD_E_SHAPE; } + /* THE PROPORTIONAL LADDER (`head`) is HF's: the rotation spans the whole head -- pair i is + * (i, i + head_dim/2) -- and every frequency past rotary_dim/2 is zero, so those pairs turn by + * nothing. A rotary of rotary_dim's own width, pairing (i, i + rot/2), turns other channels. */ + const int64_t nfreq = rot / 2; + if (den == head_dim && rot < head_dim && !multi && mode == ROPE_NEOX) rot = head_dim; RopeFreq f; RAD_REF_TRY(rope_freqs(mode, rot, den, theta, sec, multi ? n_sec : 0, &f)); const RadYarn yarn = rad_yarn_args(a); @@ -507,6 +512,7 @@ int ref_rope(const RadArgs* a, RadStream) { if (mode == ROPE_AXIAL || rscale != 1.0f) return RAD_E_UNSUPPORTED; for (int64_t i = 0; i < rot / 2; ++i) f.inv[i] = yarn.frequency(f.inv[i], (int)i); } + for (int64_t i = nfreq; i < rot / 2; ++i) f.inv[i] = 0.0f; const int64_t stride_row = (n_head + 2 * n_head_kv) * head_dim; diff --git a/tests/ref_test.cpp b/tests/ref_test.cpp index 00b96962..d6d60278 100644 --- a/tests/ref_test.cpp +++ b/tests/ref_test.cpp @@ -2441,14 +2441,19 @@ TEST(op_rope_multi_component) { } /* THE PROPORTIONAL LADDER. With head_dim wider than rotary_dim, the two ladders give frequency 1 a - * different angle: `rotary` divides by rotary_dim, `head` by head_dim. Frequency 0 is theta^0 under - * both, and the tail past rotary_dim is never touched, so those pin the unchanged parts while - * frequency 1 pins the switch itself. */ + * different angle: `rotary` divides by rotary_dim, `head` by head_dim. And they pair different + * channels: `rotary` is a complete rotary of rotary_dim's own width, pair i = (i, i + rot/2) with + * the tail past rot untouched, while `head` is HF's proportional RoPE + * (_compute_proportional_rope_parameters + rotate_half over the WHOLE head): pair i is + * (i, i + head_dim/2) and every frequency past rot/2 is zero, so only those rot/2 pairs turn. + * Frequency 0 is theta^0 under both; frequency 1 pins the ladder, the partner index the pairing. */ TEST(op_rope_ladder) { const int64_t hd = 8, rot = 4; /* two frequencies, a partial rotary (hd > rot) */ const float pos = 7.0f; for (const char* lad : { "rotary", "head" }) { - const int64_t den = (lad[0] == 'h') ? hd : rot; + const bool head = lad[0] == 'h'; + const int64_t den = head ? hd : rot; + const int64_t poff = head ? hd / 2 : rot / 2; /* where pair i's partner sits */ T got(RAD_F32, { 1, hd }); got.f(std::vector(hd, 1.0f)); T p1(RAD_I32, { 1 }); p1.n({ 7 }); @@ -2456,13 +2461,16 @@ TEST(op_rope_ladder) { .d("theta", 100).d("scale", 1).s("mode", "neox").i("rotary_dim", rot) .s("ladder", lad); CHECK_OK(run("rope", a)); + std::vector turned(hd, false); for (int i = 0; i < rot / 2; ++i) { const float inv = 1.0f / std::pow(100.0f, (float)(2 * i) / (float)den); const float ang = pos * inv; CHECK_NEAR(got.at(i), std::cos(ang) - std::sin(ang), 1e-5); - CHECK_NEAR(got.at(i + rot / 2), std::sin(ang) + std::cos(ang), 1e-5); + CHECK_NEAR(got.at(i + poff), std::sin(ang) + std::cos(ang), 1e-5); + turned[i] = turned[i + poff] = true; } - for (int d = rot; d < hd; ++d) CHECK_EQ(got.at(d), 1.0f); /* untouched tail */ + for (int d = 0; d < hd; ++d) + if (!turned[d]) CHECK_EQ(got.at(d), 1.0f); /* every other channel untouched */ } /* absent ladder == rotary: the models that never set it keep the old angles bit-for-bit */ {