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
+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) {
|
||||
|
||||
Reference in new issue
Block a user