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

+17 -2
View File
@@ -45,6 +45,7 @@
#include "r4d_args.h"
#include "r4d_emit.h"
#include "r4d_common.h"
#include "rad_yarn.h"
#include "r4d_fp8_wmma.h"
#include <cstdlib>
@@ -356,7 +357,7 @@ __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 gptj,
float theta, float rscale, uint32_t row_pitch,
int mode, int s0, int s1, int s2, int n_sec, uint32_t plane) {
int mode, int s0, int s1, int s2, int n_sec, uint32_t plane, RadYarn yarn) {
const uint32_t m = blockIdx.x;
const uint32_t half = rot / 2;
const float div = rscale != 0.0f ? rscale : 1.0f;
@@ -376,9 +377,11 @@ __global__ __launch_bounds__(kThreads) void rope_kernel(
if (mode == 4)
inv = 1.0f / powf(theta, (float)(2 * j) / (float)(2 * sec[c]));
}
inv = yarn.frequency(inv, (int)i);
const float ang = p * inv;
float c, s;
sincosf(ang, &s, &c);
if (yarn.factor != 0.0f) { c *= yarn.magnitude; s *= yarn.magnitude; }
const uint32_t i0 = gptj ? 2 * i : i;
const uint32_t i1 = gptj ? 2 * i + 1 : i + half;
const float x0 = bf2f(hd[i0]), x1 = bf2f(hd[i1]);
@@ -787,7 +790,7 @@ extern "C" int r4d_rope_mc_bf16(long qkv, long pos, int M, int head_dim, int n_r
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, gptj, theta, rscale,
(uint32_t)row_pitch, mode, s0, s1, s2, n_sec, (uint32_t)plane);
(uint32_t)row_pitch, mode, s0, s1, s2, n_sec, (uint32_t)plane, RadYarn{});
return hipGetLastError() == hipSuccess ? 0 : -4;
}
@@ -1384,6 +1387,18 @@ extern "C" int r4d_rad_rope(const RadArgs* a, RadStream s) {
const float theta = (float)r4d_getf(a, "theta", 10000.0);
const float rscale = (float)r4d_getf(a, "scale", 1.0);
const RadYarn yarn = rad_yarn_args(a);
if (!rad_yarn_valid(yarn)) return R4D_NO(RAD_E_INVAL);
if (yarn.factor != 0.0f) {
if (mc == 4 || rscale != 1.0f) return R4D_NO(RAD_E_UNSUPPORTED);
const uint32_t heads = (uint32_t)(n_head + n_head_kv);
const uint32_t gy = (heads * (uint32_t)(rot / 2) + kThreads - 1) / kThreads;
hipLaunchKernelGGL(rope_kernel, dim3((uint32_t)Mp, gy), dim3(kThreads), 0, (hipStream_t)s,
(uint16_t*)r4d_addr(qkv), (const int32_t*)r4d_addr(pos),
(uint32_t)head_dim, heads, (uint32_t)rot, gptj, theta, rscale,
(uint32_t)pitch, mc, sec[0], sec[1], sec[2], n_sec, (uint32_t)plane, yarn);
return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE;
}
/* 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. */
return r4d_rope_mc_bf16(r4d_addr(qkv), r4d_addr(pos), (int)Mp, (int)head_dim,
+7 -3
View File
@@ -23,6 +23,7 @@
#include "r4d_args.h"
#include "r4d_emit.h"
#include "r4d_common.h"
#include "rad_yarn.h"
#include <hip/hip_bf16.h>
#include <cstdint>
#include <cmath>
@@ -205,7 +206,7 @@ __global__ __launch_bounds__(256) void r4d_qk_norm_rope_gate_kernel(
// those here silently changes the model's output -- a different rope is a different model.
__global__ __launch_bounds__(64) void r4d_qk_rope_table_kernel(
const void* __restrict__ POS, void* __restrict__ CS, long rows, int rot, float theta,
float rscale, int cs_f32, int pos_i64) {
float rscale, int cs_f32, int pos_i64, RadYarn yarn) {
const int m = blockIdx.x;
const long pp = pos_i64 ? ((const long*)POS)[m] : (long)((const int*)POS)[m];
// A padded row addresses no table row, and a position at or past the table's height writes
@@ -216,9 +217,10 @@ __global__ __launch_bounds__(64) void r4d_qk_rope_table_kernel(
const float p = (float)pp / (rscale != 0.0f ? rscale : 1.0f);
const size_t o = (size_t)pp * rot;
for (int i = threadIdx.x; i < half; i += (int)blockDim.x) {
const float inv = 1.0f / powf(theta, (float)(2 * i) / (float)rot);
const float inv = yarn.frequency(1.0f / powf(theta, (float)(2 * i) / (float)rot), i);
float c, s;
sincosf(p * inv, &s, &c);
if (yarn.factor != 0.0f) { c *= yarn.magnitude; s *= yarn.magnitude; }
if (cs_f32) {
((float*)CS)[o + i] = c;
((float*)CS)[o + half + i] = s;
@@ -386,8 +388,10 @@ static int rad_rope_table_body(const RadArgs* args, const R4dSink& sk) {
const float theta = (float)r4d_getf(args, "theta", 10000.0);
const float rscale = (float)r4d_getf(args, "scale", 1.0);
const RadYarn yarn = rad_yarn_args(args);
if (!rad_yarn_valid(yarn) || (yarn.factor != 0.0f && rscale != 1.0f)) return R4D_NO(RAD_E_INVAL);
return R4D_EMIT(sk, r4d_qk_rope_table_kernel, dim3((int)M), dim3(64), 0,
r4d_p(po), r4d_wp(cs), (long)(r4d_numel(cs) / rot), (int)rot, theta, rscale,
cs->dtype == RAD_F32 ? 1 : 0, po->dtype == RAD_I64 ? 1 : 0);
cs->dtype == RAD_F32 ? 1 : 0, po->dtype == RAD_I64 ? 1 : 0, yarn);
}
R4D_ENTRY(r4d_rad_rope_table, rad_rope_table_body)
+8
View File
@@ -276,6 +276,10 @@ static const RadParamSpec pRopeTable[] = {
P_F64("theta"),
P_F64("scale"),
{ "dtype", RAD_P_STR, RAD_REQUIRED },
{ "yarn_factor", RAD_P_F64, RAD_OPTIONAL },
{ "yarn_low", RAD_P_F64, RAD_OPTIONAL },
{ "yarn_high", RAD_P_F64, RAD_OPTIONAL },
{ "yarn_magnitude", RAD_P_F64, RAD_OPTIONAL },
};
static const RadOperandSpec oRopeTable[] = {
{ "positions", RAD_OPD_IN, 0 },
@@ -1574,6 +1578,10 @@ static const RadParamSpec pRope[] = {
{ "mode", RAD_P_STR, RAD_REQUIRED },
{ "rotary_dim", RAD_P_INT, RAD_OPTIONAL },
{ "sections", RAD_P_STR, RAD_OPTIONAL },
{ "yarn_factor", RAD_P_F64, RAD_OPTIONAL },
{ "yarn_low", RAD_P_F64, RAD_OPTIONAL },
{ "yarn_high", RAD_P_F64, RAD_OPTIONAL },
{ "yarn_magnitude", RAD_P_F64, RAD_OPTIONAL },
};
/* ---------------------------------------------------------------------- the vision tower */
+36 -3
View File
@@ -45,6 +45,8 @@
#include "rad_abi.h"
#include "rad_plugin.h"
#include "../vendor/nlohmann/json.hpp"
#include <fstream>
#include "r4d_args.h"
#include "r4d_hc_fp8.h"
#include "r4d_fp8_frag.h"
@@ -1195,7 +1197,7 @@ static bool restores(const RadKernelInfo* r, const std::vector<RadParam>& lp, in
* The q side reads a STRIDED view in the engine (the [q|gate] projection is interleaved per head)
* and the unfused norm cannot, so the reference is fed a contiguous copy of the same numbers. The
* arithmetic does not depend on the stride; the values are what is being compared. */
static void qk_prologue_case(const Plugin& pl, uint32_t gain_dt) {
static void qk_prologue_case(const Plugin& pl, uint32_t gain_dt, bool yarn = false, int64_t rot = 64) {
if (!sel("qk_norm_rope_gate") && !sel("rope_table_f32")) return;
/* THE GAIN'S WIDTH IS RUN BOTH WAYS, because the two callers differ: every architecture plugin
* here declares an f32 norm gain and vLLM's declares bf16, and one flag inside the kernel picks
@@ -1213,7 +1215,12 @@ static void qk_prologue_case(const Plugin& pl, uint32_t gain_dt) {
* one k buffer does, and 4 query heads is not 12: the wave count the launcher picks is a
* function of nq + nkv, so a head count the model does not have exercises a launch it does not
* make. */
const int64_t M = 512, nq = 12, nkv = 2, hd = 256, rot = 64, ctx = 4096;
const int64_t M = 512, nq = 12, nkv = 2, hd = 256, ctx = yarn ? 524288 : 4096;
auto add_yarn = [&](std::vector<RadParam>& ps) {
if (!yarn) return;
ps.push_back(PF("yarn_factor", 2)); ps.push_back(PF("yarn_low", rot == 64 ? 14 : 28));
ps.push_back(PF("yarn_high", rot == 64 ? 22 : 43)); ps.push_back(PF("yarn_magnitude", 1.069314718));
};
const double theta = 1e7, eps = 1e-6, wadd = 1.0;
/* MANY ROWS, because the difference this case is hunting is one bf16 in tens of thousands: two
* summation orders for the same mean-square agree to a ULP in f32 and that ULP almost never
@@ -1222,6 +1229,10 @@ static void qk_prologue_case(const Plugin& pl, uint32_t gain_dt) {
* near the top. */
std::vector<int32_t> pos((size_t)M);
for (int64_t i = 0; i < M; ++i) pos[(size_t)i] = (int32_t)((i * 7 + i * i) % ctx);
if (yarn) {
const int32_t edges[] = {0, 1, 4096, 262143, 262144, 500000, 524287};
std::copy(std::begin(edges), std::end(edges), pos.begin());
}
auto rnd = [](int64_t i) { /* deterministic, and spans the exponent */
const uint32_t x = (uint32_t)(i * 2654435761u + 12345u);
@@ -1311,6 +1322,7 @@ static void qk_prologue_case(const Plugin& pl, uint32_t gain_dt) {
std::vector<RadParam> ps = { PI("M", M), PI("head_dim", hd), PI("n_head", nh),
PI("n_head_kv", 0), PF("theta", theta), PF("scale", 1.0),
PS("mode", "neox"), PI("rotary_dim", rot) };
add_yarn(ps);
run_case(rr, ts, ps);
}
@@ -1319,7 +1331,26 @@ static void qk_prologue_case(const Plugin& pl, uint32_t gain_dt) {
std::vector<RadTensor> ts = { T(d_p, RAD_I32, { M }), T(d_cs, RAD_F32, { ctx, rot }) };
std::vector<RadParam> ps = { PI("M", M), PI("rot", rot), PF("theta", theta),
PF("scale", 1.0), PS("dtype", "f32") };
add_yarn(ps);
run_case(rt, ts, ps);
if (yarn) {
const std::string here = __FILE__;
std::ifstream input(here.substr(0, here.find_last_of('/')) + "/../tests/data/yarn_reference_rocm.json");
if (!input) { ok("YaRN independent vectors", false, "fixture missing"); return; }
const auto fixture = nlohmann::json::parse(input);
const auto& c = fixture["cases"][rot == 64 ? 0 : 1];
std::vector<float> row(rot);
double worst = 0.0;
for (size_t m = 0; m < fixture["positions"].size(); ++m) {
st_memcpy(row.data(), (float*)d_cs + (size_t)pos[m] * rot, rot * sizeof(float), hipMemcpyDeviceToHost);
for (int i = 0; i < rot / 2; ++i) {
worst = std::max(worst, std::abs((double)row[i] - c["cos"][m][i].get<double>()));
worst = std::max(worst, std::abs((double)row[rot/2+i] - c["sin"][m][i].get<double>()));
}
}
ok("YaRN GPU table vs independent vLLM through 524287", worst <= 0.004,
("worst absolute error " + std::to_string(worst)).c_str());
}
}
{
std::vector<RadTensor> ts = {
@@ -1381,7 +1412,7 @@ static void qk_prologue_case(const Plugin& pl, uint32_t gain_dt) {
" vs " + std::to_string(h_bf16(want[i]));
}
}
const std::string tag = std::string(gf32 ? "f32 gain, " : "bf16 gain, ") + what;
const std::string tag = std::string(yarn ? "YaRN, " : "") + (gf32 ? "f32 gain, " : "bf16 gain, ") + what;
char rate[128];
std::snprintf(rate, sizeof rate, "%lld of %zu differ, worst %.2e of full scale",
(long long)n_off, got.size(), worst);
@@ -10945,6 +10976,8 @@ static void device_phase(const Plugin& pl) {
}
qk_prologue_case(pl, RAD_BF16);
qk_prologue_case(pl, RAD_F32);
qk_prologue_case(pl, RAD_F32, true);
qk_prologue_case(pl, RAD_F32, true, 128);
qk_kv_fold_case(pl, false);
qk_kv_fold_case(pl, true);
qsa_qprep_case(pl);