diff --git a/arch/common/rad_fp8.h b/arch/common/rad_fp8.h index 42edfa39..90183b2c 100644 --- a/arch/common/rad_fp8.h +++ b/arch/common/rad_fp8.h @@ -993,10 +993,14 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha * set reads_x). The model must have declared the pair (ActFP8::declare_qs's `i8`). */ i8 = known && linear_is_w8(enc); if (i8) { - if (!a.x || !a.q8 || !a.s8) { - fprintf(stderr, "radiance: '%s' is an int8 weight and its input lacks the bf16 plane " - "or the int8 code pair (the model declared its input before learning " - "the trunk is int8)\n", wn); + /* The codes come from the producer (`q8_fed`, which declares no `op_q` and so needs no bf16 + * plane) or from this linear's own `op_q` over `a.x`. Either way the int8 pair must exist; + * the bf16 plane is required only when this linear quantises, exactly as the fp8 branch + * needs `a.q`/`a.s` without demanding `a.x`. */ + if (!a.q8 || !a.s8 || (!a.q8_fed && !a.x)) { + fprintf(stderr, "radiance: '%s' is an int8 weight and its input lacks the int8 code " + "pair%s (the model declared its input before learning the trunk is " + "int8)\n", wn, a.q8_fed ? "" : " or the bf16 plane it quantises"); return RAD_E_DTYPE; } if (N % 16 || K % RAD_FP8_BLOCK) { diff --git a/arch/minimaxh3/minimaxh3.cpp b/arch/minimaxh3/minimaxh3.cpp index cadba8dc..d52b9801 100644 --- a/arch/minimaxh3/minimaxh3.cpp +++ b/arch/minimaxh3/minimaxh3.cpp @@ -450,7 +450,7 @@ struct Model { rad_buf b_v8 = 0, b_vs = 0; /* its values at E4M3 codes and their column scales */ /* The trained schedule's base times (a few-step checkpoint's), or none. */ std::vector rungs; - rad_buf b_h = 0, b_hm = 0, b_qkv = 0, b_ao = 0, b_gu = 0, b_hn = 0, b_hf = 0, b_vel = 0, + rad_buf b_h = 0, b_qkv = 0, b_ao = 0, b_gu = 0, b_hn = 0, b_hf = 0, b_vel = 0, b_avel = 0; ActFP8 a_hm{}, a_attn{}, a_ffn{}; /* The DiT's trunk linears are int8 (a W8A8 container). Learned by probing one linear before @@ -1141,19 +1141,13 @@ static int declare_dit(RadBuilder* b, const RadModelMeta* meta, Model& m) { /* Learn the trunk's weight format from one linear before declaring the activation code planes. * An int8 (W8A8) denoiser quantises its per-token activations to int8 too -- E4M3's three * mantissa bits band the smooth activations that carry detail, and its 448 clamp clips - * highlights -- so its producers write a bf16 plane and each int8 linear quantises that plane - * into its own codes (LinearFP8::i8's op_q). Probe block 0's down projection: a single-source - * linear whose name map declare() will not re-make. */ + * highlights. The int8 path mirrors the E4M3 one exactly: the fused modulated-norm producer + * writes the int8 codes and scale (`rms_mod_quant_i8`, fed into `a_hm` via its q8 pair) and each + * int8 linear reads those codes directly, with no bf16 plane and no per-linear quantiser. Probe + * block 0's down projection: a single-source linear whose name map declare() will not re-make. */ RAD_ARCH_TRY(m.dit[0].down.probe(b, nm, g, nm.f("dit.blk.0.ffn_down"), { nm.ckpt("transformer.transformer_blocks.0.ff.net.2.weight") })); m.dit_i8 = m.dit[0].down.probed_i8; - if (m.dit_i8) { - /* The bf16 modulated-norm plane the producers write for the int8 trunk; the E4M3 pair is - * never read, so a_hm is declared without it below. */ - m.b_hm = decl_b(b, nm.f("dit.hmb"), RAD_BF16, { T, D }); - if (!m.b_hm) return RAD_E_INVAL; - m.a_hm.x = m.b_hm; - } m.op_text = rw(b, RAD_OP(b, "lane_gather", RAD_PARAMS(RAD_RANGE("M", 1, (int64_t)kMaxText), RAD_INT("n", D), RAD_INT("stride", (int64_t)kMaxText), @@ -1171,6 +1165,10 @@ static int declare_dit(RadBuilder* b, const RadModelMeta* meta, Model& m) { /* ---- the blocks */ RAD_ARCH_TRY(m.a_hm.declare_qs(b, nm, g, "dit.hm", D, 0, m.dit_i8, !m.dit_i8)); + /* The fused `rms_mod_quant*` producer writes a_hm's code pair (int8 here), and qkv / gate_up + * read it directly -- so it is `q8_fed` and declares no quantiser of its own, exactly as its + * E4M3 sibling is fed by `rms_mod_quant_fp8`. */ + m.a_hm.q8_fed = m.dit_i8; /* With video sparse attention a fourth projection beside q, k and v gates the pooled-tile * attention into each row's output. */ const int64_t nparts = m.vsa ? 4 : 3; @@ -1197,36 +1195,22 @@ static int declare_dit(RadBuilder* b, const RadModelMeta* meta, Model& m) { const char* sections = nm.f("%lld %lld %lld", (long long)rf, (long long)rf, (long long)rf); const int64_t ff_all = g.n_ff_all; - /* The block's modulated norm of `b_h`. For the E4M3 container it is fused with the quantiser: - * `rms_mod_quant_fp8` folds the previous block's all-reduced delta in, renormalises with the - * timestep/segment modulation, and writes `a_hm`'s E4M3 codes plus the updated residual. For the - * int8 trunk the quantiser is the whole point of the change -- E4M3 banded these activations -- - * so it comes apart: plain `rms_mod` (no `group` param) writes only the bf16 plane `b_hm`, and - * each int8 linear reading `a_hm` (qkv, gate_up) quantises that plane into its own int8 codes. */ + /* The block's modulated norm of `b_h`, fused with the quantiser: it folds the previous block's + * all-reduced delta in, renormalises with the timestep/segment modulation, and writes `a_hm`'s + * code pair plus the updated residual. Both trunks take the same path; only the op -- and hence + * the code dtype -- differs (`a_hm.cq()` / `cs()` resolve to the int8 pair for a W8A8 trunk, the + * E4M3 pair otherwise), and the two schemas share a shape and a group of 128. */ auto mod_q = [&](rad_weight w, bool gated) { - rad_op op; - if (m.dit_i8) { - op = RAD_OP(b, "rms_mod", - RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n", D), RAD_F64("eps", eps), - RAD_INT("gated", gated ? 1 : 0), RAD_STR("dtype", "bf16")), - RAD_WEIGHTS(w)); - } else { - op = RAD_OP(b, "rms_mod_quant_fp8", - RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n", D), RAD_F64("eps", eps), - RAD_INT("group", RAD_FP8_BLOCK), - RAD_INT("gated", gated ? 1 : 0), RAD_STR("dtype", "bf16")), - RAD_WEIGHTS(w)); - } + rad_op op = RAD_OP(b, m.dit_i8 ? "rms_mod_quant_i8" : "rms_mod_quant_fp8", + RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n", D), RAD_F64("eps", eps), + RAD_INT("group", RAD_FP8_BLOCK), + RAD_INT("gated", gated ? 1 : 0), RAD_STR("dtype", "bf16")), + RAD_WEIGHTS(w)); if (!op) return op; rad_buf rd[3] = { m.b_modg, m.b_h, m.b_ao }; rad_op_reads(b, op, rd, gated ? 3 : 2); - if (m.dit_i8) { - rad_buf wr[2] = { m.b_hm, m.b_h }; - rad_op_writes(b, op, wr, 2); - } else { - rad_buf wr[3] = { m.a_hm.q, m.a_hm.s, m.b_h }; - rad_op_writes(b, op, wr, 3); - } + rad_buf wr[3] = { m.a_hm.cq(), m.a_hm.cs(), m.b_h }; + rad_op_writes(b, op, wr, 3); return op; }; @@ -3475,18 +3459,11 @@ static void step_dit(RadCtx* c, const RadBatch* batch, const Model& m) { for (int h = 0; h < nh; ++h) { const int64_t r0 = hr0[h], n = hn[h]; if (h > 0 && l > 0) wait_ar(); - if (m.dit_i8) - /* rms_mod: z (the bf16 plane the int8 qkv quantises) bound, no q/scale. */ - RAD_ISSUE_N(c, B.op_mq1, n, l > 0 ? brow_slice(m.b_ao, r0, n, D) : RAD_NONE, - l > 0 ? modh(h, l - 1, 5, 1) : RAD_NONE, modh(h, l, 0, 2), - RAD_W(B.w_n1), segh[h], brow_slice(m.b_h, r0, n, D), - brow_slice(m.b_hm, r0, n, D)); - else - RAD_ISSUE_N(c, B.op_mq1, n, l > 0 ? brow_slice(m.b_ao, r0, n, D) : RAD_NONE, - l > 0 ? modh(h, l - 1, 5, 1) : RAD_NONE, modh(h, l, 0, 2), - RAD_W(B.w_n1), segh[h], brow_slice(m.b_h, r0, n, D), - brow_slice(m.a_hm.q, r0, n, D), brow_slice(m.a_hm.s, r0, n, nD), - RAD_NONE); + RAD_ISSUE_N(c, B.op_mq1, n, l > 0 ? brow_slice(m.b_ao, r0, n, D) : RAD_NONE, + l > 0 ? modh(h, l - 1, 5, 1) : RAD_NONE, modh(h, l, 0, 2), + RAD_W(B.w_n1), segh[h], brow_slice(m.b_h, r0, n, D), + brow_slice(m.a_hm.cq(), r0, n, D), brow_slice(m.a_hm.cs(), r0, n, nD), + RAD_NONE); B.qkv.step(c, m.a_hm, m.b_qkv, R, r0, n); } RAD_ISSUE_N(c, B.op_qkr, R, bcol(m.b_qkv, 0, 3 * qd, R), pos, RAD_W(B.w_qn), @@ -3517,15 +3494,10 @@ static void step_dit(RadCtx* c, const RadBatch* batch, const Model& m) { } for (int h = 0; h < nh; ++h) { const int64_t r0 = hr0[h], n = hn[h]; - if (m.dit_i8) - RAD_ISSUE_N(c, B.op_mq2, n, brow_slice(m.b_ao, r0, n, D), modh(h, l, 2, 1), - modh(h, l, 3, 2), RAD_W(B.w_n2), segh[h], brow_slice(m.b_h, r0, n, D), - brow_slice(m.b_hm, r0, n, D)); - else - RAD_ISSUE_N(c, B.op_mq2, n, brow_slice(m.b_ao, r0, n, D), modh(h, l, 2, 1), - modh(h, l, 3, 2), RAD_W(B.w_n2), segh[h], brow_slice(m.b_h, r0, n, D), - brow_slice(m.a_hm.q, r0, n, D), brow_slice(m.a_hm.s, r0, n, nD), - RAD_NONE); + RAD_ISSUE_N(c, B.op_mq2, n, brow_slice(m.b_ao, r0, n, D), modh(h, l, 2, 1), + modh(h, l, 3, 2), RAD_W(B.w_n2), segh[h], brow_slice(m.b_h, r0, n, D), + brow_slice(m.a_hm.cq(), r0, n, D), brow_slice(m.a_hm.cs(), r0, n, nD), + RAD_NONE); B.gate_up.step(c, m.a_hm, m.b_gu, R, r0, n); B.gq.step(c, m.b_gu, R, r0, n); B.down.step(c, m.a_ffn, m.b_ao, R, r0, n); diff --git a/libr4d/r4d_plugin.h b/libr4d/r4d_plugin.h index 9f6edc4b..a696154c 100644 --- a/libr4d/r4d_plugin.h +++ b/libr4d/r4d_plugin.h @@ -194,6 +194,7 @@ int r4d_rad_cu_repeat (const RadArgs*, RadStream); /* r4d_video.hip */ int r4d_rad_rms_mod (const RadArgs*, RadStream); int r4d_rad_rms_mod_quant_fp8 (const RadArgs*, RadStream); +int r4d_rad_rms_mod_quant_i8 (const RadArgs*, RadStream); int r4d_rad_flow_x0_step (const RadArgs*, RadStream); int r4d_rad_patch_unfold (const RadArgs*, RadStream); int r4d_rad_vtile_gather (const RadArgs*, RadStream); diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 90e4ea0e..495d3b50 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -3041,6 +3041,9 @@ static const RadOpSchema kSchemas[] = { SCHEMA("rms_mod_quant_fp8", pRmsModQ, oRmsModQ, "rms_mod with the chain in bf16, then (q, scale) = quant_act_fp8(z, group); z written " "when bound."), + SCHEMA("rms_mod_quant_i8", pRmsModQ, oRmsModQ, + "rms_mod with the chain in bf16, then (q, scale) = quant_act_i8g(z, group) -- int8 codes " + "a quarter of E4M3's error on the same byte; z written when bound."), SCHEMA("flow_x0_step", pGenMn, oFlowX0, "x = r x + (1 - r) (x + (1 - t) v), r = sigma_next / sigma, each step one f32 " "rounding."), @@ -7291,6 +7294,14 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, ROW(cRmsModQ), NOTUNE, nullptr, nullptr, r4d_rad_rms_mod_quant_fp8 }, +{ "rms_mod_quant_i8_bf16", "rms_mod_quant_i8", "norm", + "gated residual, modulated RMS norm and quant_act_i8g in one pass: a workgroup a row", + "n % 128 == 0, n <= 8192, group 128; s a column slice at its own pitch", + "bf16 rows, gain and table, int8 codes and f32 scales", + RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, + ROW(cRmsModQ), NOTUNE, + nullptr, nullptr, r4d_rad_rms_mod_quant_i8 }, + { "flow_x0_step_f32", "flow_x0_step", "elem", "a sampler step toward the clean estimate", "any M, row pitches honoured", "f32 state, f32 or bf16 velocity", diff --git a/libr4d/r4d_video.hip b/libr4d/r4d_video.hip index c1134cc6..c88e4c92 100644 --- a/libr4d/r4d_video.hip +++ b/libr4d/r4d_video.hip @@ -77,7 +77,9 @@ __device__ __forceinline__ float rdv(const float*, float v) { return v; } // r4d_q8.h's. `s` is a column slice of the modulation table, [shift | scale] in each entry's row. constexpr int kRmChunks = 4; // 8-wide chunks a thread keeps: n <= kT * 8 * 4 = 8192 -template +// QMODE: 0 writes the bf16 chain to `z`, 1 also quantises it to E4M3 (q, scale), 2 to int8. The two +// quantising modes differ only in r4d_q8.h's `q8_top` / `q8_pack4` specialisation. +template __global__ __launch_bounds__(kT) void rms_mod_kernel( const uint16_t* __restrict__ yd, const uint16_t* __restrict__ g, const uint16_t* __restrict__ s, const uint16_t* __restrict__ w, const int* __restrict__ cu, uint16_t* __restrict__ x, @@ -125,7 +127,7 @@ __global__ __launch_bounds__(kT) void rms_mod_kernel( o[j] = bfr(bfr(n2 * bfr(1.0f + scf[j])) + shf[j]); } if (z) st8(z + (size_t)r * z_ld + i, o); - if (QUANT) { + if (QMODE) { float m = 0.0f; #pragma unroll for (int j = 0; j < 8; ++j) m = fmaxf(m, fabsf(o[j])); @@ -133,12 +135,12 @@ __global__ __launch_bounds__(kT) void rms_mod_kernel( m = fmaxf(m, r4d_xor_lane<4>(m)); m = fmaxf(m, r4d_xor_lane<2>(m)); m = fmaxf(m, r4d_xor_lane<1>(m)); - const float sc = m > 0.0f ? m * (1.0f / q8_top()) : 1.0f; + const float sc = m > 0.0f ? m * (1.0f / q8_top()) : 1.0f; const float inv = 1.0f / sc; if ((threadIdx.x & 15) == 0) qs[(size_t)r * qs_ld + i / 128] = sc; *(uint2*)(q + (size_t)r * q_ld + i) = - make_uint2(q8_pack4(o[0], o[1], o[2], o[3], inv), - q8_pack4(o[4], o[5], o[6], o[7], inv)); + make_uint2(q8_pack4(o[0], o[1], o[2], o[3], inv), + q8_pack4(o[4], o[5], o[6], o[7], inv)); } } } @@ -425,22 +427,22 @@ int parse_f32_list(const char* s, float* out, int max) { // rms_mod (QUANT false: y, g, s, w, cu, x, z) and rms_mod_quant_fp8 (y, g, s, w, cu, x, q, scale, // z), one shim. -static int rms_mod_launch(const RadArgs* a, RadStream st, bool quant) { +static int rms_mod_launch(const RadArgs* a, RadStream st, int qmode) { const RadTensor *y = r4d_opt(a, 0), *g = r4d_opt(a, 1), *sc = r4d_opt(a, 2); const RadTensor *w = r4d_opt(a, 3), *cu = r4d_opt(a, 4), *x = r4d_opt(a, 5); - const RadTensor* q = quant ? r4d_opt(a, 6) : nullptr; - const RadTensor* qs = quant ? r4d_opt(a, 7) : nullptr; - const RadTensor* z = r4d_opt(a, quant ? 8 : 6); - if (!sc || !w || !cu || !x || (quant ? (!q || !qs) : !z)) return R4D_NO(RAD_E_INVAL); + const RadTensor* q = qmode ? r4d_opt(a, 6) : nullptr; + const RadTensor* qs = qmode ? r4d_opt(a, 7) : nullptr; + const RadTensor* z = r4d_opt(a, qmode ? 8 : 6); + if (!sc || !w || !cu || !x || (qmode ? (!q || !qs) : !z)) return R4D_NO(RAD_E_INVAL); const bool gated = r4d_geti_or(a, "gated", 0) != 0; if (gated && (!y || !g)) return R4D_NO(RAD_E_INVAL); if (!is_bf16(x) || !is_bf16(sc) || !is_bf16(w) || !is_i32(cu) || (gated && (!is_bf16(y) || !is_bf16(g))) || (z && !is_bf16(z)) || - (quant && (q->dtype != RAD_F8E4M3 || qs->dtype != RAD_F32))) + (qmode && (q->dtype != (qmode == 2 ? RAD_I8 : RAD_F8E4M3) || qs->dtype != RAD_F32))) return R4D_NO(RAD_E_DTYPE); const long long n = r4d_geti_or(a, "n", 0); if (n <= 0 || (n & 7) || n > (long long)kT * 8 * kRmChunks || - (quant && (n % 128 || r4d_geti_or(a, "group", 0) != 128))) + (qmode && (n % 128 || r4d_geti_or(a, "group", 0) != 128))) return R4D_NO(RAD_E_SHAPE); long long rx = 0, xl = 0, ry = 0, yl = 0, rg = 0, gl = 0, rs = 0, sl = 0, rz = 0, zl = 0; long long rq = 0, rqs = 0, ql = 0, qsl = 0; @@ -449,14 +451,14 @@ static int rms_mod_launch(const RadArgs* a, RadStream st, bool quant) { (gated && (!rows8(y, n, &ry, &yl) || !rows8(g, n, &rg, &gl))) || (z && !rows8(z, n, &rz, &zl))) return R4D_NO(RAD_E_STRIDE); - if (quant) { + if (qmode) { ql = r4d_rows_pitch(q, n, &rq); qsl = r4d_rows_pitch(qs, n / 128, &rqs); if (ql < 0 || (ql & 7) || ((uintptr_t)q->data & 7) || qsl < 0) return R4D_NO(RAD_E_STRIDE); } - const long long n_seq = r4d_numel(cu) - 1, rows = quant ? rq : rz; + const long long n_seq = r4d_numel(cu) - 1, rows = qmode ? rq : rz; if (rows <= 0 || n_seq <= 0 || rx < rows || rs < n_seq || r4d_numel(w) < n || - (quant && rqs < rows) || (gated && (ry < rows || rg < n_seq)) || (z && rz < rows)) + (qmode && rqs < rows) || (gated && (ry < rows || rg < n_seq)) || (z && rz < rows)) return R4D_NO(RAD_E_SHAPE); const float eps = (float)r4d_getf(a, "eps", 1e-6); #define R4D_RMQ(G, Q) \ @@ -464,24 +466,31 @@ static int rms_mod_launch(const RadArgs* a, RadStream st, bool quant) { (hipStream_t)st, G ? (const uint16_t*)y->data : nullptr, \ G ? (const uint16_t*)g->data : nullptr, (const uint16_t*)sc->data, \ (const uint16_t*)w->data, (const int*)cu->data, (uint16_t*)x->data, \ - Q ? (uint8_t*)q->data : nullptr, Q ? (float*)qs->data : nullptr, \ + (Q) ? (uint8_t*)q->data : nullptr, (Q) ? (float*)qs->data : nullptr, \ z ? (uint16_t*)z->data : nullptr, (int)n, (int)n_seq, eps, (int)yl, \ (int)gl, (int)sl, (int)xl, (int)ql, (int)qsl, (int)zl) - if (quant) { - if (gated) R4D_RMQ(true, true); - else R4D_RMQ(false, true); + if (qmode == 2) { + if (gated) R4D_RMQ(true, 2); + else R4D_RMQ(false, 2); + } else if (qmode == 1) { + if (gated) R4D_RMQ(true, 1); + else R4D_RMQ(false, 1); } else { - if (gated) R4D_RMQ(true, false); - else R4D_RMQ(false, false); + if (gated) R4D_RMQ(true, 0); + else R4D_RMQ(false, 0); } #undef R4D_RMQ return st_of(hipGetLastError()); } -extern "C" int r4d_rad_rms_mod(const RadArgs* a, RadStream s) { return rms_mod_launch(a, s, false); } +extern "C" int r4d_rad_rms_mod(const RadArgs* a, RadStream s) { return rms_mod_launch(a, s, 0); } extern "C" int r4d_rad_rms_mod_quant_fp8(const RadArgs* a, RadStream s) { - return rms_mod_launch(a, s, true); + return rms_mod_launch(a, s, 1); +} + +extern "C" int r4d_rad_rms_mod_quant_i8(const RadArgs* a, RadStream s) { + return rms_mod_launch(a, s, 2); } extern "C" int r4d_rad_flow_x0_step(const RadArgs* a, RadStream s) {