Archived
dsa_score: row_part -- score one run of a step's query rows, and rebase cu for the selection after it
With `row_part` p, `score` holds R rows and row i is query row p * R + i; `q`, `q_scale` and `gate` stay the whole step's, so a run is the unsplit op's rows bit for bit. A row past the last sequence scores -inf, so every rank's run can be the same length whatever the step's. The optional output `cu_part` receives cu - p * R, under which qsa_select runs over the run's own rows (pos offset by p * R) and finds each row's sequence unchanged; qsa_select itself is untouched. This is what an indexer split over tensor-parallel ranks by query rows needs from the op: the scoring is the only per-row work in GLM-5.3's indexer that grows with the context. libr4d offsets the query operands and compares row0 + r against cu; libref and libavx index the same way. Without `row_part` nothing changes. Tests: the selftest scores each case's rows in equal runs and holds every run to the unsplit rows' bytes and cu_part to the rebased bounds; ref_test checks a run, a run past the step and a short query plane; r4d_emu_dsa (r4d_dsa.hip now runs on the emulator) splits decode rows beside a prefill chunk at 2, 4 and 8 ranks, selects with the rebased bounds and joins the runs as the all_gather does, byte for byte against the unsplit scorer and selection, ties and signed gates included. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
5e8bae8ae6
commit
afe5318259
13 files changed
+451
-47
No files matched your search
+14
-5
@@ -2569,7 +2569,7 @@ order, not a slow one.
|
||||
| `qsa_block_key` | `M`(range) `n` `ratio` `rotary_dim` `pos0`/opt `block_size`/opt `eps`:f64 `theta`:f64 `scale`:f64/opt `wadd`:f64/opt `dtype` `mode`/opt `sections`/opt `yarn_factor`:f64/opt `yarn_low`:f64/opt `yarn_high`:f64/opt `yarn_magnitude`:f64/opt | `k`, `w`w, `page`?, `pos`? → `out` |
|
||||
| `dsa_pool` | `M`(range) `n` `ratio` `dtype` `block_size`/opt | `kg`, `ape`w → `q`, `scale`; `page`?, `cache`:inout? |
|
||||
| `dsa_query` | `M`(range) `n` `dtype` | `x` → `q`, `scale` |
|
||||
| `dsa_score` | `M`(range) `n` `heads` `blocks` `seqs` `dtype` | `q`, `q_scale`, `gate`, `k`, `k_scale`?, `bt`, `cu`, `nc` → `score` |
|
||||
| `dsa_score` | `M`(range) `n` `heads` `blocks` `seqs` `dtype` `row_part`/opt | `q`, `q_scale`, `gate`, `k`, `k_scale`?, `bt`, `cu`, `nc` → `score`, `cu_part`? |
|
||||
| `mla_attn` | `M`(range) `latent_dim` `heads` `blocks` `block_size` `scale`:f64 `dtype` | `q`, `kv`, `bt`, `length` → `out` |
|
||||
| `mla_project` | `M`(range) `heads` `head_dim` `latent_dim` `mode` `dtype` | `a`, `b`w → `y` |
|
||||
| `qsa_score` | `M`(range) `n` `heads` `blocks` `seqs` `dtype` | `q`, `bk`, `bt`, `cu`, `nc` → `score` |
|
||||
@@ -2813,10 +2813,19 @@ history makes no future block eligible.
|
||||
embedded scale are read in place. `q` may be flattened `[M*heads, n]` with `q_scale`
|
||||
`[M*heads, 1]`, each with its own row pitch, which is `dsa_query`'s output as written.
|
||||
|
||||
libr4d's `dsa_score_fp8` serves `n` 128 and 32 heads, `M` up to 65535, both key forms and both
|
||||
query forms. A block of two waves covers the 32 heads of one query row against one or two 16-key
|
||||
tiles (two past 32 blocks); the dot products run on fp8 WMMA, and scales and gates fold only after
|
||||
each head's ReLU.
|
||||
`row_part` scores one run of a step's query rows, for an indexer split over tensor-parallel ranks
|
||||
by rows. With `row_part` p, `score` holds R rows and score row i is query row `p * R + i`; `q`,
|
||||
`q_scale` and `gate` are the whole step's and hold at least `(p + 1) * R` rows, so a run is the
|
||||
unsplit op's rows, bit for bit. A row past the last sequence in `cu` scores `-inf` everywhere,
|
||||
which lets every rank's run be the same length whatever the step's. `cu_part`, `[seqs + 1]` i32
|
||||
and only with `row_part`, receives `cu - p * R`: the bounds rebased to the run's first row, under
|
||||
which an op that reads `cu` over the run's own rows (`qsa_select`, with `pos` offset by `p * R`)
|
||||
finds each row's sequence unchanged.
|
||||
|
||||
libr4d's `dsa_score_fp8` serves `n` 128 and 32 heads, `M` up to 65535, both key forms, both
|
||||
query forms and `row_part`. A block of two waves covers the 32 heads of one query row against one
|
||||
or two 16-key tiles (two past 32 blocks); the dot products run on fp8 WMMA, and scales and gates
|
||||
fold only after each head's ReLU.
|
||||
|
||||
`mla_attn` is absorbed latent attention: one latent vector per token is both key and value.
|
||||
`q` and `out` are `[M, heads, latent_dim]`, `kv` is `[pages, 1, block_size, latent_dim]`, and
|
||||
|
||||
+21
-7
@@ -64,17 +64,19 @@ AVX_KERNEL(dsa_score) {
|
||||
k.stride[1] = 1;
|
||||
}
|
||||
const auto &bt = a->t[5], &cu = a->t[6], &nc = a->t[7], &out = a->t[8];
|
||||
const RadTensor* cu_part = rad_arg_in(a, 9);
|
||||
const int64_t n = p_int(a, "n", 0), h = p_int(a, "heads", 0);
|
||||
if (h > 0 && q.rank == 2 && qs.rank == 2 && qs.shape[1] == 1 &&
|
||||
q.shape[0] == out.shape[0] * h && qs.shape[0] == q.shape[0]) {
|
||||
const int64_t part = p_int(a, "row_part", -1);
|
||||
if (h > 0 && q.rank == 2 && qs.rank == 2 && qs.shape[1] == 1 && g.rank == 2 &&
|
||||
q.shape[0] == g.shape[0] * h && qs.shape[0] == q.shape[0]) {
|
||||
q.rank = 3;
|
||||
q.shape[0] = out.shape[0];
|
||||
q.shape[0] = g.shape[0];
|
||||
q.shape[2] = q.shape[1];
|
||||
q.shape[1] = h;
|
||||
q.stride[2] = q.stride[1];
|
||||
q.stride[1] = q.stride[0];
|
||||
q.stride[0] *= h;
|
||||
qs.shape[0] = out.shape[0];
|
||||
qs.shape[0] = g.shape[0];
|
||||
qs.shape[1] = h;
|
||||
qs.stride[1] = qs.stride[0];
|
||||
qs.stride[0] *= h;
|
||||
@@ -84,9 +86,14 @@ AVX_KERNEL(dsa_score) {
|
||||
g.shape[0] != q.shape[0] || qs.shape[1] != h || g.shape[1] != h || k.rank != 2 ||
|
||||
k.shape[1] != n || ks.rank != 1 || ks.shape[0] != k.shape[0] || bt.rank != 2 ||
|
||||
cu.rank != 1 || nc.rank != 1 || cu.shape[0] != bt.shape[0] + 1 ||
|
||||
nc.shape[0] != bt.shape[0] || out.rank != 2 || out.shape[0] != q.shape[0] ||
|
||||
nc.shape[0] != bt.shape[0] || out.rank != 2 ||
|
||||
(part < 0 ? out.shape[0] != q.shape[0]
|
||||
: out.shape[0] < 1 || q.shape[0] < (part + 1) * out.shape[0]) ||
|
||||
out.shape[1] > bt.shape[1])
|
||||
return RAD_E_SHAPE;
|
||||
if (cu_part && (part < 0 || cu_part->rank != 1 || cu_part->shape[0] != cu.shape[0]))
|
||||
return part < 0 ? RAD_E_INVAL : RAD_E_SHAPE;
|
||||
if (cu_part && cu_part->dtype != RAD_I32) return RAD_E_DTYPE;
|
||||
if (q.dtype != RAD_F8E4M3 || k.dtype != RAD_F8E4M3 || qs.dtype != RAD_F32 ||
|
||||
ks.dtype != RAD_F32 || g.dtype != RAD_F32 || bt.dtype != RAD_I32 || cu.dtype != RAD_I32 ||
|
||||
nc.dtype != RAD_I32 || out.dtype != RAD_F32)
|
||||
@@ -94,8 +101,15 @@ AVX_KERNEL(dsa_score) {
|
||||
for (int i = 0; i < 9; ++i)
|
||||
if (!(packed && i == 4) && !a->t[i].data) return RAD_E_INVAL;
|
||||
const float norm = 1.0f / std::sqrt((float)(h * n));
|
||||
/* A part scores out's rows from step row part * rows; the query operands span the step. */
|
||||
const int64_t row0 = part > 0 ? part * out.shape[0] : 0;
|
||||
if (cu_part)
|
||||
for (int64_t s = 0; s < cu.shape[0]; ++s)
|
||||
st_int_dt(RAD_I32, cu_part->data, s * cu_part->stride[0],
|
||||
ld_int_dt(cu.dtype, cu.data, s * cu.stride[0]) - row0);
|
||||
AVX_PARALLEL_FOR_IF(q.shape[0] * out.shape[1] * h * n >= AVX_PAR_MIN)
|
||||
for (int64_t r = 0; r < q.shape[0]; ++r) {
|
||||
for (int64_t o = 0; o < out.shape[0]; ++o) {
|
||||
const int64_t r = row0 + o;
|
||||
int64_t seq = -1;
|
||||
for (int64_t s = 0; s < bt.shape[0]; ++s)
|
||||
if (r >= ld_int_dt(cu.dtype, cu.data, s * cu.stride[0]) &&
|
||||
@@ -123,7 +137,7 @@ AVX_KERNEL(dsa_score) {
|
||||
}
|
||||
result *= ldt(&ks, page * ks.stride[0]) * norm;
|
||||
}
|
||||
stt(&out, r * out.stride[0] + b * out.stride[1], result);
|
||||
stt(&out, o * out.stride[0] + b * out.stride[1], result);
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
|
||||
@@ -142,9 +142,9 @@ static const RadParamSpec pMlaProject[] = { P_INT("M"), P_INT("heads"), P_INT("h
|
||||
P_INT("latent_dim"), P_STR("mode"), P_STR("dtype") };
|
||||
static const RadOperandSpec oMlaAttn[] = { OPD("q"), OPD("kv"), OPD("bt"), OPD("length"), OUT("out") };
|
||||
static const RadParamSpec pDsaScore[] = { P_INT("M"), P_INT("n"), P_INT("heads"),
|
||||
P_INT("blocks"), P_INT("seqs"), P_STR("dtype") };
|
||||
P_INT("blocks"), P_INT("seqs"), P_STR("dtype"), P_INTO("row_part") };
|
||||
static const RadOperandSpec oDsaScore[] = { OPD("q"), OPD("q_scale"), OPD("gate"), OPD("k"),
|
||||
OPD_O("k_scale"), OPD("bt"), OPD("cu"), OPD("nc"), OUT("score") };
|
||||
OPD_O("k_scale"), OPD("bt"), OPD("cu"), OPD("nc"), OUT("score"), OUT_O("cu_part") };
|
||||
static const RadOperandSpec oDsaPool[] = { OPD("kg"), WGT("ape"), OUT("q"), OUT("scale"),
|
||||
OPD_O("page"), INOUT_O("cache") };
|
||||
static const RadParamSpec pRouterSigmoid[] = { P_INT("M"), P_INT("n_expert"), P_INT("top_k"),
|
||||
|
||||
+42
-10
@@ -53,7 +53,7 @@ int query_body(const RadArgs* a, RadStream stream) {
|
||||
}
|
||||
struct ScoreMeta {
|
||||
long qr, qh, qsr, qsc, gr, gc, kr, ksr, btr, btc, cur, ncr, op;
|
||||
int rows, blocks, seqs, pages;
|
||||
int rows, blocks, seqs, pages, row0;
|
||||
};
|
||||
__device__ uint32_t bytes4(const uint8_t* p) {
|
||||
if (!((uintptr_t)p & 3)) return *(const uint32_t*)p;
|
||||
@@ -61,7 +61,8 @@ __device__ uint32_t bytes4(const uint8_t* p) {
|
||||
}
|
||||
/* Two waves cover all 32 heads and reuse the query across `kScoreTiles` 16-key tiles, one up to 32
|
||||
* blocks and two past them. FP8 dot products stay on WMMA; scales and the signed per-head gates
|
||||
* fold only after the per-head ReLU. */
|
||||
* fold only after the per-head ReLU. The query operands start at a part's first row, so only the
|
||||
* sequence lookup sees the step row (`row0 + r`). */
|
||||
template <int kScoreTiles>
|
||||
__global__ void dsa_score_kernel(const uint8_t* q, const float* qs, const float* gate,
|
||||
const uint8_t* k, const float* ks, const int* bt, const int* cu,
|
||||
@@ -75,7 +76,7 @@ __global__ void dsa_score_kernel(const uint8_t* q, const float* qs, const float*
|
||||
if (!tid) {
|
||||
seq = -1;
|
||||
for (int s = 0; s < m.seqs; ++s)
|
||||
if (r >= cu[(long)s * m.cur] && r < cu[(long)(s + 1) * m.cur]) {
|
||||
if (m.row0 + r >= cu[(long)s * m.cur] && m.row0 + r < cu[(long)(s + 1) * m.cur]) {
|
||||
seq = s;
|
||||
break;
|
||||
}
|
||||
@@ -133,10 +134,14 @@ __global__ void dsa_score_kernel(const uint8_t* q, const float* qs, const float*
|
||||
out[(long)r * m.op + b0 + i] =
|
||||
pages[i] >= 0 ? (partial[0][i] + partial[1][i]) * scales[i] * 0.015625f : -INFINITY;
|
||||
}
|
||||
/* The step's cu rebased to a part's first row: an op that reads cu over the part's own rows
|
||||
* (qsa_select) then finds each row's sequence unchanged. */
|
||||
__global__ void dsa_rebase_kernel(const int* cu, int* out, long cs, long os, int n, int row0) {
|
||||
for (int i = threadIdx.x; i < n; i += blockDim.x) out[(long)i * os] = cu[(long)i * cs] - row0;
|
||||
}
|
||||
int score_body(const RadArgs* a, RadStream stream) {
|
||||
if (!a || a->n_t < 9) return RAD_E_INVAL;
|
||||
RadTensor q = a->t[0], qs = a->t[1];
|
||||
const auto& g = a->t[2];
|
||||
RadTensor q = a->t[0], qs = a->t[1], g = a->t[2];
|
||||
RadTensor k = a->t[3], ks = a->t[4];
|
||||
const bool packed = k.dtype == RAD_U8;
|
||||
if (packed) {
|
||||
@@ -156,20 +161,41 @@ int score_body(const RadArgs* a, RadStream stream) {
|
||||
k.stride[1] = 1;
|
||||
}
|
||||
const auto &bt = a->t[5], &cu = a->t[6], &nc = a->t[7], &o = a->t[8];
|
||||
if (q.rank == 2 && qs.rank == 2 && qs.shape[1] == 1 && q.shape[0] == o.shape[0] * 32 &&
|
||||
qs.shape[0] == q.shape[0]) {
|
||||
const RadTensor* cu_part = rad_arg_in(a, 9);
|
||||
const long long part = r4d_geti_or(a, "row_part", -1);
|
||||
if (q.rank == 2 && qs.rank == 2 && qs.shape[1] == 1 && g.rank == 2 &&
|
||||
q.shape[0] == g.shape[0] * 32 && qs.shape[0] == q.shape[0]) {
|
||||
q.rank = 3;
|
||||
q.shape[0] = o.shape[0];
|
||||
q.shape[0] = g.shape[0];
|
||||
q.shape[2] = q.shape[1];
|
||||
q.shape[1] = 32;
|
||||
q.stride[2] = q.stride[1];
|
||||
q.stride[1] = q.stride[0];
|
||||
q.stride[0] *= 32;
|
||||
qs.shape[0] = o.shape[0];
|
||||
qs.shape[0] = g.shape[0];
|
||||
qs.shape[1] = 32;
|
||||
qs.stride[1] = qs.stride[0];
|
||||
qs.stride[0] *= 32;
|
||||
}
|
||||
/* A part scores o's rows from step row part * rows; the query operands span the step. */
|
||||
const long long rows = o.rank == 2 ? o.shape[0] : 0, row0 = part > 0 ? part * rows : 0;
|
||||
if (part >= 0) {
|
||||
if (q.rank != 3 || qs.rank != 2 || g.rank != 2 || rows < 1 || part > INT_MAX / rows ||
|
||||
q.shape[0] < row0 + rows || qs.shape[0] != q.shape[0] || g.shape[0] != q.shape[0])
|
||||
return RAD_E_SHAPE;
|
||||
if (q.data && qs.data && g.data) {
|
||||
q.data = (uint8_t*)q.data + row0 * q.stride[0];
|
||||
qs.data = (float*)qs.data + row0 * qs.stride[0];
|
||||
g.data = (float*)g.data + row0 * g.stride[0];
|
||||
}
|
||||
q.shape[0] = qs.shape[0] = g.shape[0] = rows;
|
||||
}
|
||||
if (cu_part) {
|
||||
if (part < 0) return RAD_E_INVAL;
|
||||
if (cu_part->dtype != RAD_I32) return RAD_E_DTYPE;
|
||||
if (cu_part->rank != 1 || cu_part->shape[0] != cu.shape[0] || cu_part->stride[0] < 1)
|
||||
return RAD_E_SHAPE;
|
||||
}
|
||||
if (q.dtype != RAD_F8E4M3 || k.dtype != RAD_F8E4M3 || qs.dtype != RAD_F32 ||
|
||||
g.dtype != RAD_F32 || ks.dtype != RAD_F32 || o.dtype != RAD_F32 || bt.dtype != RAD_I32 ||
|
||||
cu.dtype != RAD_I32 || nc.dtype != RAD_I32)
|
||||
@@ -194,7 +220,13 @@ int score_body(const RadArgs* a, RadStream stream) {
|
||||
ScoreMeta m{ q.stride[0], q.stride[1], qs.stride[0], qs.stride[1], g.stride[0],
|
||||
g.stride[1], k.stride[0], ks.stride[0], bt.stride[0], bt.stride[1],
|
||||
cu.stride[0], nc.stride[0], o.stride[0], (int)q.shape[0], (int)o.shape[1],
|
||||
(int)bt.shape[0], (int)k.shape[0] };
|
||||
(int)bt.shape[0], (int)k.shape[0], (int)row0 };
|
||||
if (cu_part) {
|
||||
const int rc = R4D_EMIT(stream, dsa_rebase_kernel, dim3(1), dim3(64), 0,
|
||||
(const int*)cu.data, (int*)cu_part->data, cu.stride[0],
|
||||
cu_part->stride[0], (int)cu.shape[0], (int)row0);
|
||||
if (rc != RAD_OK) return rc;
|
||||
}
|
||||
#define SCORE(NT) \
|
||||
R4D_EMIT(stream, (dsa_score_kernel<NT>), dim3((m.blocks + 16 * NT - 1) / (16 * NT), m.rows), \
|
||||
dim3(64), 0, (const uint8_t*)q.data, (const float*)qs.data, (const float*)g.data, \
|
||||
|
||||
+4
-2
@@ -1748,11 +1748,13 @@ static const RadOperandSpec oMlaAttn[] = { { "q", RAD_OPD_IN, 0 },
|
||||
{ "length", RAD_OPD_IN, 0 },
|
||||
{ "out", RAD_OPD_OUT, 0 } };
|
||||
static const RadParamSpec pDsaScore[] = { P_INT("M"), P_INT("n"), P_INT("heads"),
|
||||
P_INT("blocks"), P_INT("seqs"), P_STR("dtype") };
|
||||
P_INT("blocks"), P_INT("seqs"), P_STR("dtype"),
|
||||
P_INTO("row_part") };
|
||||
static const RadOperandSpec oDsaScore[] = {
|
||||
{ "q", RAD_OPD_IN, 0 }, { "q_scale", RAD_OPD_IN, 0 }, { "gate", RAD_OPD_IN, 0 },
|
||||
{ "k", RAD_OPD_IN, 0 }, { "k_scale", RAD_OPD_IN, 1 }, { "bt", RAD_OPD_IN, 0 },
|
||||
{ "cu", RAD_OPD_IN, 0 }, { "nc", RAD_OPD_IN, 0 }, { "score", RAD_OPD_OUT, 0 }
|
||||
{ "cu", RAD_OPD_IN, 0 }, { "nc", RAD_OPD_IN, 0 }, { "score", RAD_OPD_OUT, 0 },
|
||||
{ "cu_part", RAD_OPD_OUT, 1 }
|
||||
};
|
||||
static const RadOperandSpec oDsaPool[] = {
|
||||
{ "kg", RAD_OPD_IN, 0 }, { "ape", RAD_OPD_WEIGHT, 0 }, { "q", RAD_OPD_OUT, 0 },
|
||||
|
||||
@@ -13481,6 +13481,43 @@ static void dsa_score_check(const Plugin& pl, int64_t rows, int64_t blocks, bool
|
||||
.c_str(),
|
||||
matches);
|
||||
ok("DSA scorer guards and pitched output", guards);
|
||||
if (matches) {
|
||||
/* The rows split into `parts` equal runs, each scored with `row_part` into its own
|
||||
* plane: every run must be the unsplit rows' bytes, and cu_part the rebased bounds. */
|
||||
int64_t parts = rows;
|
||||
for (int64_t d = 2; d < rows; ++d)
|
||||
if (rows % d == 0) {
|
||||
parts = d;
|
||||
break;
|
||||
}
|
||||
const int64_t R = rows / parts, S = host[6].shape[0];
|
||||
bool same = true;
|
||||
for (int64_t part = 0; part < parts && same; ++part) {
|
||||
std::vector<RadTensor> split = device;
|
||||
split.push_back(T(nullptr, RAD_I32, { S }));
|
||||
split[8] = T(nullptr, RAD_F32, { R, blocks });
|
||||
std::vector<RadParam> sp = ps;
|
||||
sp[0] = PI("M", R);
|
||||
sp.push_back(PI("row_part", part));
|
||||
run_case(row, split, sp);
|
||||
std::vector<float> plane((size_t)(R * blocks));
|
||||
std::vector<int32_t> cu_part((size_t)S);
|
||||
st_memcpy(plane.data(), split[8].data, plane.size() * 4, hipMemcpyDeviceToHost);
|
||||
st_memcpy(cu_part.data(), split[9].data, cu_part.size() * 4, hipMemcpyDeviceToHost);
|
||||
for (int64_t r = 0; r < R; ++r)
|
||||
same = same && std::memcmp(&plane[(size_t)(r * blocks)],
|
||||
got.data() + 128 +
|
||||
(size_t)((part * R + r) * out.stride[0]) * 4,
|
||||
(size_t)blocks * 4) == 0;
|
||||
for (int64_t i = 0; i < S; ++i)
|
||||
same = same && cu_part[(size_t)i] ==
|
||||
((int32_t*)host[6].data)[i * host[6].stride[0]] - part * R;
|
||||
}
|
||||
ok(fmt("DSA scorer split into %lld row parts: each the unsplit rows' bytes, cu rebased",
|
||||
(long long)parts)
|
||||
.c_str(),
|
||||
same);
|
||||
}
|
||||
if (packed) {
|
||||
std::vector<uint8_t> after(packed_bytes.size());
|
||||
st_memcpy(after.data(), packed_device, after.size(), hipMemcpyDeviceToHost);
|
||||
|
||||
@@ -674,7 +674,8 @@ extern "C" int r4d_shape_dsa_score(const RadParam* p, int n_p, int operand, RadO
|
||||
opd_idx(S, B, pages),
|
||||
opd_idx(S + 1, 0, M, -1, RAD_OPD_F_IDX_CU),
|
||||
opd_idx(S, 0, 0, B),
|
||||
opd((uint32_t)RAD_F32, M, B) };
|
||||
opd((uint32_t)RAD_F32, M, B),
|
||||
opd_none() };
|
||||
R4D_PICK(l);
|
||||
}
|
||||
extern "C" int r4d_shape_mla_attn(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
|
||||
+21
-7
@@ -67,17 +67,19 @@ int ref_dsa_score(const RadArgs* a, RadStream) {
|
||||
k.stride[1] = 1;
|
||||
}
|
||||
const auto &bt = a->t[5], &cu = a->t[6], &nc = a->t[7], &out = a->t[8];
|
||||
const RadTensor* cu_part = rad_arg_in(a, 9);
|
||||
const int64_t n = p_int(a, "n", 0), h = p_int(a, "heads", 0);
|
||||
if (h > 0 && q.rank == 2 && qs.rank == 2 && qs.shape[1] == 1 &&
|
||||
q.shape[0] == out.shape[0] * h && qs.shape[0] == q.shape[0]) {
|
||||
const int64_t part = p_int(a, "row_part", -1);
|
||||
if (h > 0 && q.rank == 2 && qs.rank == 2 && qs.shape[1] == 1 && gate.rank == 2 &&
|
||||
q.shape[0] == gate.shape[0] * h && qs.shape[0] == q.shape[0]) {
|
||||
q.rank = 3;
|
||||
q.shape[0] = out.shape[0];
|
||||
q.shape[0] = gate.shape[0];
|
||||
q.shape[2] = q.shape[1];
|
||||
q.shape[1] = h;
|
||||
q.stride[2] = q.stride[1];
|
||||
q.stride[1] = q.stride[0];
|
||||
q.stride[0] *= h;
|
||||
qs.shape[0] = out.shape[0];
|
||||
qs.shape[0] = gate.shape[0];
|
||||
qs.shape[1] = h;
|
||||
qs.stride[1] = qs.stride[0];
|
||||
qs.stride[0] *= h;
|
||||
@@ -87,9 +89,14 @@ int ref_dsa_score(const RadArgs* a, RadStream) {
|
||||
gate.shape[0] != q.shape[0] || qs.shape[1] != h || gate.shape[1] != h || k.rank != 2 ||
|
||||
k.shape[1] != n || ks.rank != 1 || ks.shape[0] != k.shape[0] || bt.rank != 2 ||
|
||||
cu.rank != 1 || nc.rank != 1 || cu.shape[0] != bt.shape[0] + 1 ||
|
||||
nc.shape[0] != bt.shape[0] || out.rank != 2 || out.shape[0] != q.shape[0] ||
|
||||
nc.shape[0] != bt.shape[0] || out.rank != 2 ||
|
||||
(part < 0 ? out.shape[0] != q.shape[0]
|
||||
: out.shape[0] < 1 || q.shape[0] < (part + 1) * out.shape[0]) ||
|
||||
out.shape[1] > bt.shape[1])
|
||||
return RAD_E_SHAPE;
|
||||
if (cu_part && (part < 0 || cu_part->rank != 1 || cu_part->shape[0] != cu.shape[0]))
|
||||
return part < 0 ? RAD_E_INVAL : RAD_E_SHAPE;
|
||||
if (cu_part && cu_part->dtype != RAD_I32) return RAD_E_DTYPE;
|
||||
if (q.dtype != RAD_F8E4M3 || k.dtype != RAD_F8E4M3 || qs.dtype != RAD_F32 ||
|
||||
ks.dtype != RAD_F32 || gate.dtype != RAD_F32 || bt.dtype != RAD_I32 ||
|
||||
cu.dtype != RAD_I32 || nc.dtype != RAD_I32 || out.dtype != RAD_F32)
|
||||
@@ -97,8 +104,15 @@ int ref_dsa_score(const RadArgs* a, RadStream) {
|
||||
for (int i = 0; i < 9; ++i)
|
||||
if (!(packed && i == 4) && !a->t[i].data) return RAD_E_INVAL;
|
||||
const float norm = 1.0f / std::sqrt((float)(h * n));
|
||||
/* A part scores out's rows from step row part * rows; the query operands span the step. */
|
||||
const int64_t row0 = part > 0 ? part * out.shape[0] : 0;
|
||||
if (cu_part)
|
||||
for (int64_t s = 0; s < cu.shape[0]; ++s)
|
||||
st_int(RAD_I32, cu_part->data, s * cu_part->stride[0],
|
||||
ld_int(cu.dtype, cu.data, s * cu.stride[0]) - row0);
|
||||
#pragma omp parallel for schedule(static)
|
||||
for (int64_t r = 0; r < q.shape[0]; ++r) {
|
||||
for (int64_t o = 0; o < out.shape[0]; ++o) {
|
||||
const int64_t r = row0 + o;
|
||||
int64_t seq = -1;
|
||||
for (int64_t s = 0; s < bt.shape[0]; ++s)
|
||||
if (r >= ld_int(cu.dtype, cu.data, s * cu.stride[0]) &&
|
||||
@@ -126,7 +140,7 @@ int ref_dsa_score(const RadArgs* a, RadStream) {
|
||||
}
|
||||
result *= ldt(&ks, page * ks.stride[0]) * norm;
|
||||
}
|
||||
stt(&out, r * out.stride[0] + b * out.stride[1], result);
|
||||
stt(&out, o * out.stride[0] + b * out.stride[1], result);
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
|
||||
@@ -170,9 +170,9 @@ static const RadParamSpec pMlaProject[] = { P_INT("M"), P_INT("heads"), P_INT("h
|
||||
P_INT("latent_dim"), P_STR("mode"), P_STR("dtype") };
|
||||
static const RadOperandSpec oMlaAttn[] = { OPD("q"), OPD("kv"), OPD("bt"), OPD("length"), OUT("out") };
|
||||
static const RadParamSpec pDsaScore[] = { P_INT("M"), P_INT("n"), P_INT("heads"),
|
||||
P_INT("blocks"), P_INT("seqs"), P_STR("dtype") };
|
||||
P_INT("blocks"), P_INT("seqs"), P_STR("dtype"), P_INTO("row_part") };
|
||||
static const RadOperandSpec oDsaScore[] = { OPD("q"), OPD("q_scale"), OPD("gate"), OPD("k"),
|
||||
OPD_O("k_scale"), OPD("bt"), OPD("cu"), OPD("nc"), OUT("score") };
|
||||
OPD_O("k_scale"), OPD("bt"), OPD("cu"), OPD("nc"), OUT("score"), OUT_O("cu_part") };
|
||||
static const RadOperandSpec oDsaPool[] = { OPD("kg"), WGT("ape"), OUT("q"), OUT("scale"),
|
||||
OPD_O("page"), INOUT_O("cache") };
|
||||
static const RadParamSpec pRouterSigmoid[] = { P_INT("M"), P_INT("n_expert"), P_INT("top_k"),
|
||||
|
||||
@@ -737,6 +737,7 @@ int ref_dsa_score_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out
|
||||
o.t_idx({ S + 1 }, M, -1, RAD_OPD_F_IDX_CU);
|
||||
o.t_idx({ S }, 0, B, 0);
|
||||
o.t(RAD_F32, { M, B });
|
||||
o.skip(); // cu_part: a part's rebased bounds, exercised by the split's own tests.
|
||||
return o.finish("dsa_score", operand, out);
|
||||
}
|
||||
int ref_mla_attn_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
|
||||
+15
-11
@@ -361,16 +361,18 @@ endif()
|
||||
# ================================================================== libr4d's kernels, on the host
|
||||
#
|
||||
# Some libr4d ops (the sandwich-norm joins, expert_scale) have no libref oracle by design
|
||||
# (docs/OPS.md), so rad-kbench never checks them. r4d_emu runs the source of thirteen units
|
||||
# (docs/OPS.md), so rad-kbench never checks them. r4d_emu runs the source of fourteen units
|
||||
# written against r4d_launch.h on the host: the windowed paged attention, pair_argmax, the joins,
|
||||
# expert_scale, the QSA block selection, the runtime-LoRA pair, the F32 routing projection's tile
|
||||
# forms, GLM's mHC read with its chunk split, the F32-scale E4M3 GEMM's forms, GLM's KDA
|
||||
# recurrence, the MXFP4 expert GEMM's forms, the MLA matrix row's pairings and GLM's MoE decode
|
||||
# fusions, whose kernels a card-less build otherwise never runs. emu/hip/hip_runtime.h supplies the
|
||||
# HIP names, each GPU thread is a fiber, and __syncthreads and cross-lane ops are barriers (one not
|
||||
# reached uniformly is reported as a deadlock). Checked against ports of libref's ref_attn_paged and
|
||||
# ref_qsa_select, the host arithmetic in ref_host.cpp, for the joins the unfused chain bit for bit,
|
||||
# for the LoRA pair libref itself when the test can load it, and for KDA libref itself.
|
||||
# recurrence, the MXFP4 expert GEMM's forms, the MLA matrix row's pairings, GLM's MoE decode
|
||||
# fusions and GLM's DSA scoring split by query rows, whose kernels a card-less build otherwise
|
||||
# never runs. emu/hip/hip_runtime.h supplies the HIP names, each GPU thread is a fiber, and
|
||||
# __syncthreads and cross-lane ops are barriers (one not reached uniformly is reported as a
|
||||
# deadlock). Checked against ports of libref's ref_attn_paged and ref_qsa_select, the host
|
||||
# arithmetic in ref_host.cpp, for the joins the unfused chain bit for bit, for the LoRA pair libref
|
||||
# itself when the test can load it, for KDA libref itself, and for the DSA split the unsplit
|
||||
# kernels.
|
||||
#
|
||||
# Requires clang (ext_vector_type) and x86-64 (SysV fiber switch in inline assembly). With a clang
|
||||
# C++ compiler the .hip files build here as C++ (`-x c++`; the property is per directory, so
|
||||
@@ -384,8 +386,9 @@ set(R4D_EMU_HIP ${R4D_DIR}/r4d_attn_paged_window.hip ${R4D_DIR}/r4d_pair_argmax_
|
||||
${R4D_DIR}/r4d_norm_join_bf16.hip ${R4D_DIR}/r4d_expert_scale_bf16.hip
|
||||
${R4D_DIR}/r4d_qsa_select_bf16.hip ${R4D_DIR}/r4d_lora_bf16.hip ${R4D_DIR}/r4d_glm.hip
|
||||
${R4D_DIR}/r4d_mhc.hip ${R4D_DIR}/r4d_fp8_f32scale.hip ${R4D_DIR}/r4d_kda.hip
|
||||
${R4D_DIR}/r4d_moe_mxfp4.hip ${R4D_DIR}/r4d_mla.hip ${R4D_DIR}/r4d_quant_act_fp8.hip)
|
||||
set(R4D_EMU_TESTS attn pick norm escale qsa lora f32 mhc fp8f32 kda moemx mla moefuse)
|
||||
${R4D_DIR}/r4d_moe_mxfp4.hip ${R4D_DIR}/r4d_mla.hip ${R4D_DIR}/r4d_quant_act_fp8.hip
|
||||
${R4D_DIR}/r4d_dsa.hip)
|
||||
set(R4D_EMU_TESTS attn pick norm escale qsa lora f32 mhc fp8f32 kda moemx mla moefuse dsa)
|
||||
set(R4D_EMU_BUILT OFF)
|
||||
if(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64)$")
|
||||
# No fiber switch for this ISA.
|
||||
@@ -424,7 +427,7 @@ else()
|
||||
${R4D_EMU}/test_escale.cpp ${R4D_EMU}/test_qsa.cpp ${R4D_EMU}/test_lora.cpp
|
||||
${R4D_EMU}/test_f32.cpp ${R4D_EMU}/test_mhc.cpp ${R4D_EMU}/test_fp8f32.cpp
|
||||
${R4D_EMU}/test_kda.cpp ${R4D_EMU}/test_moemx.cpp ${R4D_EMU}/test_mla.cpp
|
||||
${R4D_EMU}/test_moefuse.cpp)
|
||||
${R4D_EMU}/test_moefuse.cpp ${R4D_EMU}/test_dsa.cpp)
|
||||
get_filename_component(stem ${src} NAME_WE)
|
||||
set(lang "")
|
||||
if(src MATCHES "\\.hip$")
|
||||
@@ -461,7 +464,7 @@ else()
|
||||
endif()
|
||||
endif()
|
||||
if(R4D_EMU_BUILT)
|
||||
foreach(t pick norm escale qsa lora f32 mhc fp8f32 kda moemx mla moefuse)
|
||||
foreach(t pick norm escale qsa lora f32 mhc fp8f32 kda moemx mla moefuse dsa)
|
||||
add_test(NAME r4d_emu_${t} COMMAND ${CMAKE_BINARY_DIR}/tests/r4d_emu_${t})
|
||||
endforeach()
|
||||
# The attention cases take about 11 s, 7 of them in the two long prefills on the WMMA kernel,
|
||||
@@ -472,6 +475,7 @@ if(R4D_EMU_BUILT)
|
||||
endforeach()
|
||||
set_tests_properties(r4d_emu_attn_shard0 r4d_emu_attn_shard1 r4d_emu_attn_shard2 r4d_emu_pick
|
||||
r4d_emu_norm r4d_emu_escale r4d_emu_qsa r4d_emu_lora r4d_emu_mla r4d_emu_moefuse
|
||||
r4d_emu_dsa
|
||||
PROPERTIES LABELS "r4d_emu" TIMEOUT ${RAD_TEST_TIMEOUT})
|
||||
# The LoRA pair has a libref oracle, and the test holds the emulated kernels to it when it can
|
||||
# load one -- expand bit for bit, shrink to its bound. kda_update is held to libref's within
|
||||
|
||||
@@ -0,0 +1,247 @@
|
||||
/* tests/r4d_emu: GLM's DSA scoring and selection split over the tensor-parallel ranks by query
|
||||
* rows (r4d_dsa.hip, then r4d_qsa_select_bf16.hip) on the host SIMT emulator.
|
||||
*
|
||||
* Each rank scores its run of R = 4 * ceil(M / (4 * world)) rows with `row_part`, selects over
|
||||
* the rows of that run that exist with the rebased bounds the scorer wrote (`cu_part`), and the
|
||||
* runs are laid end to end as the all_gather concatenates them. The joined table, nsel and
|
||||
* seqused must equal the unsplit scorer and selection byte for byte on every row of the step; a
|
||||
* run's scores must equal the unsplit scores' rows, and its rows past the step score -inf. The
|
||||
* fixtures mix decode rows with a prefill chunk, a missing page, duplicated keys (exact ties) and
|
||||
* signed gates, at world sizes where every rank has rows and where the last ranks have none. */
|
||||
#include "rad_abi.h"
|
||||
#include "rad_plugin.h"
|
||||
#include "r4d_plugin.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int kHeads = 32, kN = 128, kRatio = 4, kPitch = 136;
|
||||
|
||||
RadTensor tens(void* p, uint32_t dt, std::vector<int64_t> shape) {
|
||||
RadTensor t{};
|
||||
t.data = p;
|
||||
t.dtype = dt;
|
||||
t.rank = (uint32_t)shape.size();
|
||||
int64_t st = 1;
|
||||
for (int i = (int)shape.size() - 1; i >= 0; --i) {
|
||||
t.shape[i] = shape[i];
|
||||
t.stride[i] = st;
|
||||
st *= shape[i];
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
struct Fix {
|
||||
int S = 0, M = 0, nb = 0, pages = 0, topk = 0;
|
||||
std::vector<int32_t> cu, nc, pos, bt;
|
||||
std::vector<uint8_t> q, cache; /* [M*32, 128] codes; [pages, 1, 4, 34] packed keys */
|
||||
std::vector<float> qs, gate; /* [M*32, 1], [M, 32] */
|
||||
};
|
||||
|
||||
/* `decode` one-row sequences, then one prefill chunk of `chunk` rows at `start`. */
|
||||
Fix make(uint32_t seed, int decode, int chunk, int start, int topk) {
|
||||
std::mt19937 rng(seed);
|
||||
std::uniform_int_distribution<int> byte(0, 255), coin(0, 99);
|
||||
std::normal_distribution<float> nd(0.f, 1.f);
|
||||
Fix f;
|
||||
f.S = decode + 1;
|
||||
f.M = decode + chunk;
|
||||
f.topk = topk;
|
||||
f.cu.push_back(0);
|
||||
for (int s = 0; s < decode; ++s) {
|
||||
const int p = 40 + 37 * s + byte(rng) % 50;
|
||||
f.pos.push_back(p);
|
||||
f.cu.push_back(f.cu.back() + 1);
|
||||
f.nc.push_back((p + 1) / kRatio);
|
||||
}
|
||||
for (int i = 0; i < chunk; ++i) f.pos.push_back(start + i);
|
||||
f.cu.push_back(f.M);
|
||||
f.nc.push_back((start + chunk) / kRatio);
|
||||
for (int c : f.nc) f.nb = std::max(f.nb, c + 2);
|
||||
f.pages = f.S * f.nb + 3;
|
||||
f.bt.assign((size_t)f.S * f.nb, -1);
|
||||
for (int s = 0; s < f.S; ++s)
|
||||
for (int b = 0; b <= f.nc[s] && b < f.nb; ++b)
|
||||
f.bt[(size_t)s * f.nb + b] = (s * f.nb + b * 7) % f.pages;
|
||||
f.bt[(size_t)(f.S - 1) * f.nb + 3] = -1;
|
||||
auto code = [&] {
|
||||
int v;
|
||||
do v = byte(rng);
|
||||
while ((v & 0x7f) == 0x7f);
|
||||
return (uint8_t)v;
|
||||
};
|
||||
f.cache.assign((size_t)f.pages * kPitch, 0);
|
||||
for (int p = 0; p < f.pages; ++p) {
|
||||
uint8_t* page = &f.cache[(size_t)p * kPitch];
|
||||
/* Every fifth page repeats its predecessor's key: equal scores on one row. */
|
||||
if (p % 5 == 4)
|
||||
std::memcpy(page, page - kPitch, kPitch);
|
||||
else {
|
||||
for (int d = 0; d < kN; ++d) page[d] = code();
|
||||
const float scale = std::ldexp(1.0f, -6 - p % 3);
|
||||
std::memcpy(page + kN, &scale, 4);
|
||||
}
|
||||
}
|
||||
f.q.resize((size_t)f.M * kHeads * kN);
|
||||
for (auto& v : f.q) v = code();
|
||||
f.qs.resize((size_t)f.M * kHeads);
|
||||
for (size_t i = 0; i < f.qs.size(); ++i) f.qs[i] = std::ldexp(1.0f, -5 - (int)(i % 3));
|
||||
f.gate.resize((size_t)f.M * kHeads);
|
||||
for (auto& v : f.gate) v = nd(rng) * (coin(rng) < 10 ? 0.0f : 1.0f);
|
||||
return f;
|
||||
}
|
||||
|
||||
RadParam pint(const char* k, long long v) { return { k, RAD_P_INT, v, 0, nullptr }; }
|
||||
|
||||
/* Scores `rows` score rows from step row `part * rows` (part < 0: the whole step, no row_part). */
|
||||
int score(const Fix& f, const uint8_t* q, const float* qs, const float* gate, int64_t qrows,
|
||||
int part, int rows, float* out, int32_t* cu_part) {
|
||||
RadTensor t[10] = {
|
||||
tens((void*)q, RAD_F8E4M3, { qrows * kHeads, kN }),
|
||||
tens((void*)qs, RAD_F32, { qrows * kHeads, 1 }),
|
||||
tens((void*)gate, RAD_F32, { qrows, kHeads }),
|
||||
tens((void*)f.cache.data(), RAD_U8, { f.pages, 1, 4, 34 }),
|
||||
RadTensor{},
|
||||
tens((void*)f.bt.data(), RAD_I32, { f.S, f.nb }),
|
||||
tens((void*)f.cu.data(), RAD_I32, { f.S + 1 }),
|
||||
tens((void*)f.nc.data(), RAD_I32, { f.S }),
|
||||
tens(out, RAD_F32, { rows, f.nb }),
|
||||
cu_part ? tens(cu_part, RAD_I32, { f.S + 1 }) : RadTensor{},
|
||||
};
|
||||
t[3].stride[0] = kPitch;
|
||||
RadParam p[7] = { pint("M", rows), pint("n", kN), pint("heads", kHeads), pint("blocks", f.nb),
|
||||
pint("seqs", f.S), { "dtype", RAD_P_STR, 0, 0, "fp8_e4m3" },
|
||||
pint("row_part", part) };
|
||||
RadArgs a{};
|
||||
a.t = t;
|
||||
a.n_t = 10;
|
||||
a.p = p;
|
||||
a.n_p = part < 0 ? 6 : 7;
|
||||
return r4d_rad_dsa_score(&a, nullptr);
|
||||
}
|
||||
|
||||
int select(const Fix& f, const float* sc, int rows, const int32_t* pos, const int32_t* cu,
|
||||
int32_t* sel, int32_t* nsel, int32_t* seqused) {
|
||||
RadTensor t[8] = {
|
||||
tens((void*)sc, RAD_F32, { rows, f.nb }),
|
||||
tens((void*)f.bt.data(), RAD_I32, { f.S, f.nb }),
|
||||
tens((void*)f.nc.data(), RAD_I32, { f.S }),
|
||||
tens((void*)pos, RAD_I32, { rows }),
|
||||
tens((void*)cu, RAD_I32, { f.S + 1 }),
|
||||
tens(sel, RAD_I32, { rows, f.topk + 1 }),
|
||||
tens(nsel, RAD_I32, { rows }),
|
||||
tens(seqused, RAD_I32, { rows }),
|
||||
};
|
||||
RadParam p[6] = { pint("M", rows), pint("blocks", f.nb), pint("topk", f.topk),
|
||||
pint("ratio", kRatio), pint("seqs", f.S), pint("signed", 1) };
|
||||
RadArgs a{};
|
||||
a.t = t;
|
||||
a.n_t = 8;
|
||||
a.p = p;
|
||||
a.n_p = 6;
|
||||
const int64_t need = r4d_rad_qsa_select_scratch(&a);
|
||||
std::vector<uint32_t> scratch((size_t)(need / 4 + 1), 0xA5A5A5A5u);
|
||||
a.scratch = scratch.data();
|
||||
a.scratch_bytes = need;
|
||||
return r4d_rad_qsa_select(&a, nullptr);
|
||||
}
|
||||
|
||||
int check(const char* name, const Fix& f) {
|
||||
const int M = f.M, K = f.topk + 1;
|
||||
std::vector<float> want_sc((size_t)M * f.nb);
|
||||
std::vector<int32_t> want_sel((size_t)M * K, 0x55555555), want_n(M), want_su(M);
|
||||
if (score(f, f.q.data(), f.qs.data(), f.gate.data(), M, -1, M, want_sc.data(), nullptr) ||
|
||||
select(f, want_sc.data(), M, f.pos.data(), f.cu.data(), want_sel.data(), want_n.data(),
|
||||
want_su.data())) {
|
||||
std::printf("FAIL %s: the unsplit launch\n", name);
|
||||
return 1;
|
||||
}
|
||||
int fails = 0;
|
||||
for (int world : { 2, 4, 8 }) {
|
||||
const int R = 4 * ((M + 4 * world - 1) / (4 * world));
|
||||
const int64_t padded = (int64_t)world * R;
|
||||
/* The step's query rows, then rows past the step that a last run reads no further than. */
|
||||
std::vector<uint8_t> q((size_t)padded * kHeads * kN, 0x38);
|
||||
std::vector<float> qs((size_t)padded * kHeads, 1.0f), gate((size_t)padded * kHeads, 3.0f);
|
||||
std::memcpy(q.data(), f.q.data(), f.q.size());
|
||||
std::memcpy(qs.data(), f.qs.data(), f.qs.size() * 4);
|
||||
std::memcpy(gate.data(), f.gate.data(), f.gate.size() * 4);
|
||||
std::vector<int32_t> sel((size_t)padded * K, 0x55555555), ns(padded, 0x55555555),
|
||||
su(padded, 0x55555555);
|
||||
bool ok = true;
|
||||
const char* why = "";
|
||||
for (int part = 0; part < world && ok; ++part) {
|
||||
const int r0 = part * R, have = std::min(R, M - r0);
|
||||
std::vector<float> sc((size_t)R * f.nb, 7.0f);
|
||||
std::vector<int32_t> cu_part(f.S + 1, 0x55555555);
|
||||
if (score(f, q.data(), qs.data(), gate.data(), padded, part, R, sc.data(),
|
||||
cu_part.data())) {
|
||||
ok = false, why = "score launch";
|
||||
break;
|
||||
}
|
||||
for (int s = 0; s <= f.S; ++s) ok = ok && cu_part[s] == f.cu[s] - r0;
|
||||
if (!ok) {
|
||||
why = "cu_part";
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < R && ok; ++i)
|
||||
for (int b = 0; b < f.nb && ok; ++b) {
|
||||
const float v = sc[(size_t)i * f.nb + b];
|
||||
ok = i < have ? std::memcmp(&v, &want_sc[(size_t)(r0 + i) * f.nb + b], 4) == 0
|
||||
: v == -INFINITY;
|
||||
}
|
||||
if (!ok) {
|
||||
why = "scores";
|
||||
break;
|
||||
}
|
||||
if (have <= 0) continue;
|
||||
if (select(f, sc.data(), have, f.pos.data() + r0, cu_part.data(), &sel[(size_t)r0 * K],
|
||||
&ns[r0], &su[r0])) {
|
||||
ok = false, why = "select launch";
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ok) {
|
||||
ok = std::memcmp(sel.data(), want_sel.data(), want_sel.size() * 4) == 0;
|
||||
why = "selection";
|
||||
}
|
||||
if (ok) {
|
||||
ok = std::memcmp(ns.data(), want_n.data(), (size_t)M * 4) == 0 &&
|
||||
std::memcmp(su.data(), want_su.data(), (size_t)M * 4) == 0;
|
||||
why = "nsel or seqused";
|
||||
}
|
||||
if (!ok) {
|
||||
std::printf("FAIL %s world %d (R %d): %s differs\n", name, world, R, why);
|
||||
++fails;
|
||||
}
|
||||
}
|
||||
return fails;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
int fails = 0, cases = 0;
|
||||
struct Case {
|
||||
const char* name;
|
||||
uint32_t seed;
|
||||
int decode, chunk, start, topk;
|
||||
} cases_[] = {
|
||||
{ "prefill chunk alone", 1, 0, 64, 300, 16 },
|
||||
{ "decode rows beside a chunk", 2, 6, 61, 517, 32 },
|
||||
{ "chunk from position 0, short history", 3, 2, 37, 0, 8 },
|
||||
{ "rows below topk blocks", 4, 3, 29, 20, 64 },
|
||||
};
|
||||
for (const auto& c : cases_) {
|
||||
fails += check(c.name, make(c.seed, c.decode, c.chunk, c.start, c.topk));
|
||||
++cases;
|
||||
}
|
||||
std::printf("dsa split: %d fixture(s), %d failure(s)\n", cases, fails);
|
||||
return fails ? 1 : 0;
|
||||
}
|
||||
@@ -486,6 +486,49 @@ TEST(dsa_score_relu_is_per_head_before_signed_weights_and_masks_invalid_history)
|
||||
CHECK_EQ(out.at(0), -INFINITY);
|
||||
}
|
||||
|
||||
TEST(dsa_score_row_part_scores_its_run_of_rows_and_rebases_cu) {
|
||||
T q(RAD_F8E4M3, { 2, 2, 2 }), qs(RAD_F32, { 2, 2 }), gate(RAD_F32, { 2, 2 });
|
||||
T k(RAD_F8E4M3, { 3, 2 }), ks(RAD_F32, { 3 }), bt(RAD_I32, { 2, 3 });
|
||||
T cu(RAD_I32, { 3 }), nc(RAD_I32, { 2 }), out(RAD_F32, { 1, 3 }), cu_part(RAD_I32, { 3 });
|
||||
q.f({ 1, 0, 0, 1, -1, 0, 0, -1 });
|
||||
qs.f({ 2, 0.5, 1, 1 });
|
||||
gate.f({ -1, 4, 2, -1 });
|
||||
k.f({ 1, -1, 2, 3, -2, -4 });
|
||||
ks.f({ 0.25, 0.5, 1 });
|
||||
bt.n({ 0, 1, -1, 2, 0, 1 });
|
||||
cu.n({ 0, 1, 2 });
|
||||
nc.n({ 2, 1 });
|
||||
A a;
|
||||
a.t(q).t(qs).t(gate).t(k).t(ks).t(bt).t(cu).t(nc).t(out).t(cu_part);
|
||||
a.i("M", 1).i("n", 2).i("heads", 2).i("blocks", 3).i("seqs", 2).s("dtype", "fp8_e4m3");
|
||||
a.i("row_part", 1);
|
||||
REQUIRE_EQ(run("dsa_score", a), RAD_OK);
|
||||
CHECK_EQ(out.at(0), 0);
|
||||
CHECK_EQ(out.at(1), -INFINITY);
|
||||
CHECK_EQ(out.at(2), -INFINITY);
|
||||
CHECK_EQ(cu_part.at(0), -1);
|
||||
CHECK_EQ(cu_part.at(1), 0);
|
||||
CHECK_EQ(cu_part.at(2), 1);
|
||||
/* Part 2 of one-row runs is past the step: no sequence holds its row. */
|
||||
T q3(RAD_F8E4M3, { 3, 2, 2 }), qs3(RAD_F32, { 3, 2 }), gate3(RAD_F32, { 3, 2 });
|
||||
q3.f({ 1, 0, 0, 1, -1, 0, 0, -1, 1, 1, 1, 1 });
|
||||
qs3.f({ 2, 0.5, 1, 1, 1, 1 });
|
||||
gate3.f({ -1, 4, 2, -1, 1, 1 });
|
||||
A past;
|
||||
past.t(q3).t(qs3).t(gate3).t(k).t(ks).t(bt).t(cu).t(nc).t(out).t(cu_part);
|
||||
past.i("M", 1).i("n", 2).i("heads", 2).i("blocks", 3).i("seqs", 2).s("dtype", "fp8_e4m3");
|
||||
past.i("row_part", 2);
|
||||
REQUIRE_EQ(run("dsa_score", past), RAD_OK);
|
||||
for (int i : { 0, 1, 2 }) CHECK_EQ(out.at(i), -INFINITY);
|
||||
CHECK_EQ(cu_part.at(2), 0);
|
||||
/* A part needs query rows for its whole run. */
|
||||
A short_q;
|
||||
short_q.t(q).t(qs).t(gate).t(k).t(ks).t(bt).t(cu).t(nc).t(out).t(cu_part);
|
||||
short_q.i("M", 1).i("n", 2).i("heads", 2).i("blocks", 3).i("seqs", 2).s("dtype", "fp8_e4m3");
|
||||
short_q.i("row_part", 2);
|
||||
CHECK_EQ(run("dsa_score", short_q), RAD_E_SHAPE);
|
||||
}
|
||||
|
||||
TEST(kda_anchor_replays_only_the_accepted_prefix_of_the_previous_window) {
|
||||
constexpr int D = 128;
|
||||
for (int accepted : { 1, 2, 3 }) {
|
||||
|
||||
Reference in new issue
Block a user