Archived
Extend YaRN to Qwen4 attention and compressed index keys
This commit is contained in:
1 parent
2af7fa4022
commit
59d36ae928
8 files changed
+112
-11
No files matched your search
@@ -31,6 +31,7 @@
|
||||
|
||||
#include "r4d.h"
|
||||
#include "r4d_args.h"
|
||||
#include "rad_yarn.h"
|
||||
#include "r4d_emit.h"
|
||||
#include "r4d_common.h" // r4d_wave_sum
|
||||
|
||||
@@ -61,7 +62,7 @@ __global__ __launch_bounds__(kMaxN) void qsa_block_key_kernel(
|
||||
const int32_t* __restrict__ page, const int32_t* __restrict__ bpos,
|
||||
uint16_t* __restrict__ out,
|
||||
int n, int ratio, int rot, float eps, float wadd, float theta, float rscale,
|
||||
long long pos0, int k_ld, int o_ld, int mode, int s0, int s1, int s2, int bstep, int mc) {
|
||||
long long pos0, int k_ld, int o_ld, int mode, int s0, int s1, int s2, int bstep, int mc, RadYarn yarn) {
|
||||
|
||||
__shared__ float sh[kMaxN];
|
||||
__shared__ float red[kMaxN / 32];
|
||||
@@ -114,9 +115,10 @@ __global__ __launch_bounds__(kMaxN) void qsa_block_key_kernel(
|
||||
const long long bp = bpos ? (long long)bpos[(size_t)b * bstep + comp]
|
||||
: (pos0 + (long long)b * ratio);
|
||||
const float p = (float)bp / (rscale != 0.0f ? rscale : 1.0f);
|
||||
const float iv = 1.0f / powf(theta, (float)(2 * ci) / (float)rot);
|
||||
const float iv = yarn.frequency(1.0f / powf(theta, (float)(2 * ci) / (float)rot), ci);
|
||||
float s, c;
|
||||
sincosf(p * iv, &s, &c);
|
||||
if (yarn.factor != 0.0f) { c *= yarn.magnitude; s *= yarn.magnitude; }
|
||||
const float xp = sh[pt];
|
||||
res = first ? (res * c - xp * s) : (xp * s + res * c);
|
||||
}
|
||||
@@ -148,7 +150,7 @@ static int r4d_qsa_block_key_bf16_emit(long k, long w, long page, long bpos, lon
|
||||
int n, int ratio, int rot, float eps, float wadd,
|
||||
float theta, float rscale, long long pos0, int k_ld,
|
||||
int o_ld, int mode, int s0, int s1, int s2, int bstep, int mc,
|
||||
const R4dSink& sk) {
|
||||
const R4dSink& sk, RadYarn yarn = {}) {
|
||||
int emit_rc = RAD_OK;
|
||||
if (!k || !w || !out) return -1;
|
||||
if (M <= 0 || n <= 0 || n > kMaxN || ratio <= 0) return -2;
|
||||
@@ -162,7 +164,7 @@ static int r4d_qsa_block_key_bf16_emit(long k, long w, long page, long bpos, lon
|
||||
emit_rc = R4D_EMIT(sk, qsa_block_key_kernel, dim3((unsigned)M), dim3((unsigned)thr), 0, (const uint16_t*)k, (const float*)w,
|
||||
(const int32_t*)page, (const int32_t*)bpos, (uint16_t*)out,
|
||||
n, ratio, rot, eps, wadd, theta, rscale, pos0, k_ld, o_ld, mode, s0, s1,
|
||||
s2, bstep, mc);
|
||||
s2, bstep, mc, yarn);
|
||||
return emit_rc == RAD_OK && (sk.desc || hipGetLastError() == hipSuccess) ? 0 : -4;
|
||||
}
|
||||
|
||||
@@ -244,12 +246,16 @@ static int r4d_rad_qsa_block_key_body(const RadArgs* a, const R4dSink& sk) {
|
||||
if (r4d_numel(w) < n) return R4D_NO(RAD_E_SHAPE);
|
||||
const long long kp = r4d_row_pitch(k), op = r4d_row_pitch(o);
|
||||
if (kp < 0 || op < 0) return R4D_NO(RAD_E_STRIDE);
|
||||
const RadYarn yarn = rad_yarn_args(a);
|
||||
if (!rad_yarn_valid(yarn) ||
|
||||
(yarn.factor != 0.0f && (rot <= 0 || r4d_getf(a, "scale", 1.0) != 1.0)))
|
||||
return R4D_NO(RAD_E_INVAL);
|
||||
const int e = r4d_qsa_block_key_bf16_emit(
|
||||
r4d_addr(k), r4d_addr(w), r4d_addr(pg), r4d_addr(bp), r4d_addr(o),
|
||||
(int)M, (int)n, (int)ratio, (int)rot,
|
||||
(float)r4d_getf(a, "eps", 1e-6), (float)r4d_getf(a, "wadd", 1.0),
|
||||
(float)r4d_getf(a, "theta", 10000.0), (float)r4d_getf(a, "scale", 1.0),
|
||||
pos0, (int)kp, (int)op, mode, sec[0], sec[1], sec[2], bstep, mc, sk);
|
||||
pos0, (int)kp, (int)op, mode, sec[0], sec[1], sec[2], bstep, mc, sk, yarn);
|
||||
return e == 0 ? RAD_OK : RAD_E_SHAPE;
|
||||
}
|
||||
|
||||
|
||||
@@ -1100,6 +1100,10 @@ static const RadParamSpec pQsaBlockKey[] = {
|
||||
* its `sections`, read against a [3, M] component-major `pos` list. Absent is NeoX. */
|
||||
{ "mode", RAD_P_STR, 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 },
|
||||
};
|
||||
static const RadOperandSpec oQsaBlockKey[] = {
|
||||
{ "k", RAD_OPD_IN, 0 },
|
||||
|
||||
@@ -8658,6 +8658,51 @@ static void qsa_score_rows_case(const Plugin& pl) {
|
||||
* block's worth of angle, which is the mistake a reader makes when they take the position of the
|
||||
* block's LAST token instead of its first, and it is invisible in magnitude: the vectors stay the
|
||||
* same length and point somewhere else. rel_l2 is what sees it. */
|
||||
static void qsa_block_key_yarn_case(const Plugin& pl) {
|
||||
const char* what = "qsa_block_key YaRN vs independent ROCm vectors through 524287";
|
||||
const RadKernelInfo* r = find(pl, "qsa_block_key_bf16");
|
||||
if (!r) { ok(what, false, "row absent"); return; }
|
||||
const std::string here = __FILE__;
|
||||
std::ifstream input(here.substr(0, here.find_last_of('/')) + "/../tests/data/yarn_reference_rocm.json");
|
||||
if (!input) { ok(what, false, "fixture absent"); return; }
|
||||
const auto fixture = nlohmann::json::parse(input);
|
||||
const auto positions = fixture["positions"].get<std::vector<int32_t>>();
|
||||
const auto& c = fixture["cases"][0];
|
||||
const int64_t M = positions.size(), n = 128, ratio = 4;
|
||||
/* Identical unit keys make the block mean exactly one. Unit gain and eps=0
|
||||
* make the norm exactly one too, isolating the compressed-key rotation. */
|
||||
std::vector<uint16_t> keys(M * ratio * n, h_to_bf16(1.0f));
|
||||
std::vector<float> gains(n, 1.0f);
|
||||
void* dk = dalloc(keys.size()*2), *dw = dalloc(gains.size()*4);
|
||||
void* dp = dalloc(M*4), *dout = dalloc(M*n*2);
|
||||
st_memcpy(dk, keys.data(), keys.size()*2, hipMemcpyHostToDevice);
|
||||
st_memcpy(dw, gains.data(), gains.size()*4, hipMemcpyHostToDevice);
|
||||
st_memcpy(dp, positions.data(), M*4, hipMemcpyHostToDevice);
|
||||
std::vector<RadTensor> ts = {T(dk, RAD_BF16, {M*ratio,n}), T(dw, RAD_F32, {n}),
|
||||
T(nullptr, RAD_I32, {M}), T(dp, RAD_I32, {M}), T(dout, RAD_BF16, {M,n})};
|
||||
std::vector<RadParam> ps = {PI("M",M), PI("n",n), PI("ratio",ratio), PI("rotary_dim",64),
|
||||
PF("eps",0), PF("theta",1e7), PF("scale",1), PF("wadd",0), PS("dtype","bf16"),
|
||||
PF("yarn_factor",2), PF("yarn_low",14), PF("yarn_high",22), PF("yarn_magnitude",1.069314718)};
|
||||
RadArgs a{}; a.t=ts.data(); a.n_t=ts.size(); a.p=ps.data(); a.n_p=ps.size();
|
||||
bool passed = r->launch(&a,nullptr) == RAD_OK;
|
||||
st_stream_sync(nullptr);
|
||||
std::vector<uint16_t> got(M*n);
|
||||
st_memcpy(got.data(),dout,got.size()*2,hipMemcpyDeviceToHost);
|
||||
double worst=0;
|
||||
for (int64_t m=0;m<M;++m) for (int i=0;i<n;++i) {
|
||||
double expected=1;
|
||||
if (i<64) {
|
||||
const int j=i%32;
|
||||
const double cosine=c["cos"][m][j], sine=c["sin"][m][j];
|
||||
expected=i<32 ? cosine-sine : cosine+sine;
|
||||
}
|
||||
const double actual=h_bf16(got[m*n+i]);
|
||||
passed = passed && std::isfinite(actual);
|
||||
worst=std::max(worst,std::abs(actual-expected));
|
||||
}
|
||||
ok(what,passed && worst<=0.008,"worst absolute BF16 error "+std::to_string(worst));
|
||||
}
|
||||
|
||||
static void qsa_block_key_case(const Plugin& pl) {
|
||||
const char* what = "qsa_block_key vs an f64 evaluation of the same formula";
|
||||
const RadKernelInfo* r = find(pl, "qsa_block_key_bf16");
|
||||
@@ -11057,6 +11102,7 @@ static void device_phase(const Plugin& pl) {
|
||||
if (sel("qsa_block_key_bf16")) {
|
||||
if (!g_case) std::printf("\nthe QSA compressed key, against the formula it implements\n");
|
||||
qsa_block_key_case(pl);
|
||||
qsa_block_key_yarn_case(pl);
|
||||
}
|
||||
|
||||
if (sel("moe_gemm_w4a8")) {
|
||||
|
||||
Reference in new issue
Block a user