diff --git a/docs/ARCHITECTURES.md b/docs/ARCHITECTURES.md index dd33d470..85f20895 100644 --- a/docs/ARCHITECTURES.md +++ b/docs/ARCHITECTURES.md @@ -2130,7 +2130,12 @@ Traps: scratch of per-token records: decay, `dt' x`, B), at most 31 drafts a step. NVIDIA's LiLiCorr block drafter proposes 7: 32 query heads over 2 KV heads of 128, which four ranks replicate as the trunk's attention does (`drafter_geom`'s `kv_repl_ok`: each rank reads its head's k and v rows at `kv_off`, -and `attn_paged_sink` runs at gqa 8 instead of 16). +and `attn_paged_sink` runs at gqa 8 instead of 16). A verify's 8 query rows run the trunk's +attention in the split-KV decode kernel a plain decode runs, at gqa 16 (one and two ranks) in eight +waves as at gqa 8 (four) in four, so each row is the single-row decode at its position bit for bit +while each of the split law's segments holds one 16-key tile (a context up to 2048 tokens: 256 +segments at a decode, 128 at a verify). Past that the partition differs and a near-tie can flip +greedy text, as `ident.sh` allows ([TOOLS](TOOLS.md)). **Vision.** `arch/nemotronh/radio.h` serves the C-RADIO (v4-H) tower and its `mlp1` projector when the container holds them (`vision_config.model_type` `radio`): pictures, and video in tubelets of diff --git a/libr4d/r4d_attn_paged_gqa8.hip b/libr4d/r4d_attn_paged_gqa8.hip index 6ef8f8e1..34d6efbc 100644 --- a/libr4d/r4d_attn_paged_gqa8.hip +++ b/libr4d/r4d_attn_paged_gqa8.hip @@ -72,7 +72,11 @@ using std::max; #ifndef G8_BLOCK_SIZE #define G8_BLOCK_SIZE 16 #endif -#define G8_MAX_DECODE_ROWS 64 // q_len * gqa a single decode workgroup can hold: q_len <= 8 +/* q_len * gqa a single decode workgroup can hold: q_len <= 8 here. An including unit may raise it + * to 128 (eight waves): gqa 16 then verifies 7 drafts in the decode kernel, as gqa 8 does. */ +#ifndef G8_MAX_DECODE_ROWS +#define G8_MAX_DECODE_ROWS 64 +#endif #define G8_P_NWARPS 24 #define G8_P_TILE 48 @@ -152,6 +156,11 @@ static int g8_decode_launch(const R4DArgs* a, RadStream stream, dim3(splits, a->kv_heads, a->num_seqs), dim3(32), 0, *a, splits); else if (nw == 2) G8_LAUNCH(2); else if (nw == 3) G8_LAUNCH(3); +#if G8_MAX_DECODE_ROWS > 64 + /* Five to eight waves take eight; a wave past the last query row computes a copy of it and + * stores nothing. */ + else if (nw > 4) G8_LAUNCH(8); +#endif else G8_LAUNCH(4); #undef G8_LAUNCH if (emit_rc != RAD_OK) return -5; diff --git a/libr4d/r4d_attn_paged_h128_gqa16.hip b/libr4d/r4d_attn_paged_h128_gqa16.hip index 65fbdf97..edba4999 100644 --- a/libr4d/r4d_attn_paged_h128_gqa16.hip +++ b/libr4d/r4d_attn_paged_h128_gqa16.hip @@ -2,11 +2,18 @@ // query heads over 2 KV heads) at one and two ranks. Geometry only; r4d_attn_paged_gqa8.hip is the // body. The ratio changes two bounds, both stated in the row table: // -// decode verify width 64 / gqa = 4 (a workgroup holds 64 (query, head) rows). +// decode verify width 128 / gqa = 8 (a workgroup of eight waves holds 128 (query, head) rows). // prefill block width 384 / gqa = 24 query positions. // +// The verify width is the drafter's: LiLiCorr proposes 7, so a verify is 8 query rows. At 64 +// rows (q_len <= 4) those rows ran the query-tiled prefill kernel while a plain decode runs this +// split-KV one, so no verify row was the single-row decode at its position, and greedy text with +// the drafter left the text without it at one and two ranks within a few hundred characters (gqa +// 8, four ranks, already verified here). A wave holds one query's 16 heads in both launches. +// // No head_dim 256 leg (R4D_ATTN_H256 0): nothing in scope is gqa 16 at 256. Page size 16. #define R4D_ATTN_GQA 16 +#define G8_MAX_DECODE_ROWS 128 #define R4D_ATTN_H128 1 #define R4D_ATTN_H256 0 diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 1467fc9a..8a2d0a99 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -4631,7 +4631,8 @@ static const RadConstraint cAttnDecodeH128G2Bf16[] = { C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 2), C_EQ("block_size", 16), C_GE("causal", 0), C_LE("q_len", 32), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"), }; -/* Sixteen queries per KV head at head_dim 128 (Nemotron-H's 32 over 2): q_len <= 4 at decode. */ +/* Sixteen queries per KV head at head_dim 128 (Nemotron-H's 32 over 2): q_len <= 8 at decode, eight + * waves, so a verify of LiLiCorr's 7 drafts runs the decode kernel a plain decode runs. */ static const RadConstraint cAttnPrefillH128G16Fp8[] = { C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 16), C_EQ("block_size", 16), C_EQ("causal", 1), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_e4m3"), @@ -4642,11 +4643,11 @@ static const RadConstraint cAttnPrefillH128G16Bf16[] = { }; static const RadConstraint cAttnDecodeH128G16Fp8[] = { C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 16), C_EQ("block_size", 16), - C_GE("causal", 0), C_LE("q_len", 4), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_e4m3"), + C_GE("causal", 0), C_LE("q_len", 8), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_e4m3"), }; static const RadConstraint cAttnDecodeH128G16Bf16[] = { C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 16), C_EQ("block_size", 16), - C_GE("causal", 0), C_LE("q_len", 4), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"), + C_GE("causal", 0), C_LE("q_len", 8), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"), }; /* Key rename: libr4d writes C_IN("q_dtype","bf16") and C_IN("kv_dtype","bf16"); docs/OPS.md's * `attn_dense` has one `dtype`. The predicate is unchanged. @@ -7960,7 +7961,7 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8) { "attn_decode_h128_gqa16_fp8kv", "attn_paged", "attn", "paged attention, causal or not, split-KV (decode / speculative verify)", - "head_dim 128, 16 queries per kv head, paged block 16, q_len*gqa <= 64, no window", + "head_dim 128, 16 queries per kv head, paged block 16, q_len*gqa <= 128, no window", "bf16 query, fp8-e4m3 kv cache with per-(seq,head) descales, bf16 out, f16 partials", RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_DECODE, ROW(cAttnDecodeH128G16Fp8), ROW(tAttnSplit), nullptr, @@ -7970,7 +7971,7 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8) { "attn_decode_h128_gqa16_bf16kv", "attn_paged", "attn", "paged attention, causal or not, split-KV (decode / speculative verify)", - "head_dim 128, 16 queries per kv head, paged block 16, q_len*gqa <= 64, no window", + "head_dim 128, 16 queries per kv head, paged block 16, q_len*gqa <= 128, no window", "bf16 query, bf16 kv cache, bf16 out, f16 partials", RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_DECODE, ROW(cAttnDecodeH128G16Bf16), ROW(tAttnSplit), nullptr, diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 5f4d3bb1..7f8dc436 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -1348,6 +1348,88 @@ static void attn_numeric_case(const Plugin& pl, const char* name, int64_t head_d fmt("corrupted ref must fail: max_err=%.5g bad=%d", nmax, nbadc)); } +/* A speculative verify against the decodes it replaces: `q_len` query rows of one sequence whose + * context after the step is `ctx`, launched together, must give bit for bit the `q_len` single-row + * launches at the same positions (context ctx - q_len + 1 + j for row j), or greedy text with a + * drafter differs from the text without one. One KV head a rank, as Nemotron-H's trunk at two + * (gqa 16) and four (gqa 8) ranks, ragged as the engine issues it, with the engine's max_ctx and + * max_seqs (--max-model-len 8192, --max-num-seqs 4). Holds while every segment of the split law is + * one tile at both launches: up to 2048 keys at one sequence, where the decode targets 256 + * segments and an eight-row verify 128 (r4d_decode_splits). */ +static void attn_verify_bits_case(const Plugin& pl, const char* name, int64_t gqa, + const char* kv_dtype, int64_t q_len, int64_t ctx) { + if (!sel(name)) return; + const RadKernelInfo* r = find(pl, name); + std::string tag = fmt("%s verify %lld rows = %lld decodes at %lld keys", name, + (long long)q_len, (long long)q_len, (long long)ctx); + if (!r) { ok(tag.c_str(), false, "row absent"); return; } + const int64_t head_dim = 128, bs = 16, q_heads = gqa, max_ctx = 8192; + const int64_t max_blocks = max_ctx / bs, blocks = (ctx + bs - 1) / bs; + const int kv_is_fp8 = std::strcmp(kv_dtype, "bf16") != 0; + const uint32_t kvdt = kv_is_fp8 ? RAD_F8E4M3 : RAD_BF16; + const int ew = kv_is_fp8 ? 1 : 2; + const int64_t nelem = blocks * bs * 2 * head_dim; + std::vector kv((size_t)nelem * ew); + for (int64_t e = 0; e < nelem; ++e) { + if (kv_is_fp8) { kv[(size_t)e] = st_rand_e4m3((uint32_t)e); continue; } + const uint16_t h = st_rand_bf16((uint32_t)e); + kv[(size_t)e * 2] = (uint8_t)h; kv[(size_t)e * 2 + 1] = (uint8_t)(h >> 8); + } + /* the pages in reverse, so a row reading a neighbour's page shows */ + std::vector bt((size_t)max_blocks, -1); + for (int64_t b = 0; b < blocks; ++b) bt[(size_t)b] = (int32_t)(blocks - 1 - b); + const int64_t row = q_heads * head_dim; + std::vector qh((size_t)(q_len * row)); + for (size_t i = 0; i < qh.size(); ++i) qh[i] = st_rand_bf16((uint32_t)(i * 7 + 3)); + void* d_kv = dalloc(kv.size()); + st_memcpy(d_kv, kv.data(), kv.size(), hipMemcpyHostToDevice); + void* d_q = dalloc(qh.size() * 2); + st_memcpy(d_q, qh.data(), qh.size() * 2, hipMemcpyHostToDevice); + void* d_out = dalloc(qh.size() * 2); + /* rows [j0, j0 + n) at context `used`; returns the launch's code */ + auto launch = [&](int64_t j0, int64_t n, int64_t used) { + std::vector ts = { + T((char*)d_q + j0 * row * 2, RAD_BF16, { n, q_heads, head_dim }), + T(d_kv, kvdt, { blocks, 1, bs, 2 * head_dim }), + TI32({ 1, max_blocks }, bt), + TI32({ 1 }, { (int32_t)used }), + TF32({ 1, 1 }, 1.0f), + TF32({ 1, 1 }, 1.0f), + TI32({ 2 }, { 0, (int32_t)n }), + T((char*)d_out + j0 * row * 2, RAD_BF16, { n, q_heads, head_dim }), + }; + if (!kv_is_fp8) { absent(ts[4]); absent(ts[5]); } + std::vector ps = { + PI("q_len", q_len), PI("head_dim", head_dim), PI("gqa", gqa), PI("block_size", bs), + PI("causal", 1), PI("window", 0), PI("n_head", q_heads), PI("max_seqs", 4), + PI("max_ctx", max_ctx), PS("q_dtype", "bf16"), PS("kv_dtype", kv_dtype), + }; + RadArgs a; + std::memset(&a, 0, sizeof(a)); + a.t = ts.data(); a.n_t = (int)ts.size(); + a.p = ps.data(); a.n_p = (int)ps.size(); + a.world_size = 1; + if (r->scratch) { + const int64_t need = r->scratch(&a); + if (need < 0) return (int)need; + if (need > 0) { a.scratch = dalloc((size_t)need); a.scratch_bytes = need; } + } + const int rc = r->launch(&a, (RadStream) nullptr); + const hipError_t he = (hipError_t)st_stream_sync(nullptr); + return he != hipSuccess ? -1000 - (int)he : rc; + }; + int rc = launch(0, q_len, ctx); + std::vector ver(qh.size()), one(qh.size()); + st_memcpy(ver.data(), d_out, ver.size() * 2, hipMemcpyDeviceToHost); + for (int64_t j = 0; j < q_len && rc == RAD_OK; ++j) rc = launch(j, 1, ctx - q_len + 1 + j); + st_memcpy(one.data(), d_out, one.size() * 2, hipMemcpyDeviceToHost); + int64_t eq = 0; + for (int64_t j = 0; j < q_len; ++j) + eq += !std::memcmp(&ver[(size_t)(j * row)], &one[(size_t)(j * row)], (size_t)row * 2); + ok(tag.c_str(), rc == RAD_OK && eq == q_len, + fmt("rc=%d, rows bit-equal %lld/%lld", rc, (long long)eq, (long long)q_len)); +} + /* A head-256 prefill geometry the KV split serves: queries per KV head, KV heads (a rank's), page * size, and the unit's test hooks' prefix (`_splits`, the law; `_last_splits`, the * count the unit's last launch used). The workgroup holds 384 / gqa query positions. */ @@ -31070,6 +31152,15 @@ static void device_phase(const Plugin& pl) { attn_numeric_case(pl, "attn_decode_h128_gqa16_fp8kv", 128, 16, 1, "fp8_e4m3", 0, 256); attn_numeric_case(pl, "attn_decode_h128_gqa16_bf16kv", 128, 16, 1, "bf16", 0, 256); attn_numeric_case(pl, "attn_decode_h128_gqa16_bf16kv", 128, 16, 4, "bf16", 0, 256); + /* LiLiCorr's verify: 7 drafts and the anchor, eight waves. */ + attn_numeric_case(pl, "attn_decode_h128_gqa16_fp8kv", 128, 16, 8, "fp8_e4m3", 0, 256); + attn_numeric_case(pl, "attn_decode_h128_gqa16_bf16kv", 128, 16, 8, "bf16", 0, 256); + /* ... and gives, row for row, the decodes it replaces; gqa 8 is the trunk at four ranks */ + for (int64_t keys : { 120, 700, 2048 }) { + attn_verify_bits_case(pl, "attn_decode_h128_gqa16_bf16kv", 16, "bf16", 8, keys); + attn_verify_bits_case(pl, "attn_decode_h128_gqa16_fp8kv", 16, "fp8_e4m3", 8, keys); + attn_verify_bits_case(pl, "attn_decode_h128_gqa8_bf16kv", 8, "bf16", 8, keys); + } attn_gq_fold_case(pl, 3, false); attn_gq_fold_case(pl, 24, false); /* And over the fp8_v4 and k6_v4 caches (Flash-Next's --kv-cache-dtype fp8_v4 / k6_v4). */