diff --git a/abi/rad_builder.h b/abi/rad_builder.h index 16a69977..947b595f 100644 --- a/abi/rad_builder.h +++ b/abi/rad_builder.h @@ -1055,6 +1055,10 @@ typedef struct RadVideoGenDecl { int64_t dbg_dit_mod_bytes; rad_buf dbg_dit_blk; /* [n_layer, DBG_ROWS, D] f32: a row window after every block */ int64_t dbg_dit_blk_bytes; + rad_buf dbg_dit_hn; /* [DBG_VID_ROWS, D] f32: norm_out's stream, video rows head */ + int64_t dbg_dit_hn_bytes; + rad_buf dbg_dit_nout; /* [1, 3 * 2 * D] f32: this step's norm_out shift/scale row */ + int64_t dbg_dit_nout_bytes; } RadVideoGenDecl; int rad_declare_video_generator(RadBuilder* b, const RadVideoGenDecl* d); diff --git a/arch/minimaxh3/minimaxh3.cpp b/arch/minimaxh3/minimaxh3.cpp index 1a47f37b..fda148e4 100644 --- a/arch/minimaxh3/minimaxh3.cpp +++ b/arch/minimaxh3/minimaxh3.cpp @@ -171,6 +171,9 @@ enum { kMaxSegs = 8 }; static const int kRefConsume = 1; /* Rows captured after every DiT block by the R4D_DIT_SEL window shadow (see b_dbgl). */ enum { kDbgL = 64 }; +/* Rows of the velocity-head isolation shadow (b_hnd): norm_out's stream over the first video + * rows, enough to tell a faithful head from one fed the wrong modulation. */ +enum { kVidDbgL = 256 }; /* ================================================================== geometry, on the host */ /* diffusers' AutoencoderKLMiniMaxH3._split_tiles in latents: the first latent of each tile across @@ -593,6 +596,12 @@ struct Model { * diverges first. [L * kDbgL, D] f32 PERSIST. */ rad_buf b_dbgl = 0; rad_op op_dbgl = 0; + /* Velocity-head isolation (#62): norm_out's stream over the first kVidDbgL video rows, and + * this step's norm_out shift/scale table row (3 segments x 2 vectors of D). */ + rad_buf b_hnd = 0; + rad_op op_hnd = 0; + rad_buf b_noutd = 0; + rad_op op_noutd = 0; /* The step's gathered modulation table (b_modg) cast to f32: every block's and segment's cell, * both ranks' column halves, the exact vectors the norms consume. PERSIST f32 twin of a bf16 * scratch (the b_ax2 lesson). [world * L * kMaxSegs, Ws] f32. */ @@ -1274,16 +1283,29 @@ static int declare_dit(RadBuilder* b, const RadModelMeta* meta, Model& m) { RAD_NOWEIGHTS), { m.b_temb }, { m.b_tembd }); } - /* The per-block window shadow: the stream's first kDbgL rows after every block. Full-width - * per-block capture is gigabytes; a 64-row fingerprint tells whose forward diverges first. */ + /* The per-block window shadow: the stream's first kDbgL rows after every block (issued off + * b_h after the fold -- see step_dit). Full-width per-block capture is gigabytes; a 64-row + * fingerprint tells whose forward diverges first. */ m.b_dbgl = decl_b(b, nm.f("dit.blkdbg"), RAD_F32, { (int64_t)L * kDbgL, D }, RAD_BUF_PERSIST); m.op_dbgl = rw(b, RAD_OP(b, "cast", RAD_PARAMS(RAD_RANGE("M", 1, kDbgL), RAD_INT("n", D), RAD_STR("from", "bf16"), RAD_STR("to", "f32")), RAD_NOWEIGHTS), - { m.b_ao }, { m.b_dbgl }); - if (!m.op_dh0 || !m.op_modd || !m.op_dbgl || (m.curve > 0 && !m.op_tembd)) + { m.b_h }, { m.b_dbgl }); + /* The velocity-head isolation: this step's norm_out shift/scale table row (three + * segments, two D-vectors each). The RMS norm makes the output heads scale-invariant, so + * replaying the heads over it and over b_hnd (declared with b_hn below) -- checkpoint + * weights, exact math -- either reproduces the step's velocity or convicts the head's + * modulation itself. */ + m.b_noutd = decl_b(b, nm.f("dit.noutdbg"), RAD_F32, { 1, (int64_t)kMaxSegs * 2 * D }, RAD_BUF_PERSIST); + m.op_noutd = rw(b, RAD_OP(b, "cast", + RAD_PARAMS(RAD_RANGE("M", 1, 1), RAD_INT("n", (int64_t)kMaxSegs * 2 * D), + RAD_STR("from", "bf16"), RAD_STR("to", "f32")), + RAD_NOWEIGHTS), + { m.b_nout }, { m.b_noutd }); + if (!m.op_dh0 || !m.op_modd || !m.op_dbgl || !m.op_noutd || + (m.curve > 0 && !m.op_tembd)) return RAD_E_INVAL; } /* Learn the trunk's weight format from one linear before declaring the activation code planes. @@ -1482,6 +1504,17 @@ static int declare_dit(RadBuilder* b, const RadModelMeta* meta, Model& m) { /* ---- the output: norm_out's modulated norm (the last block's branch gated in), the heads in * f32 over parts of the rows, and the two Euler steps into the lane */ m.b_hn = decl_b(b, nm.f("dit.hn"), RAD_BF16, { T, D }); + /* The velocity-head isolation shadow over b_hn (see b_noutd above): the head's normalised + * input, first kVidDbgL video rows. Declared here because it reads b_hn. */ + if (m.dit_dbg) { + m.b_hnd = decl_b(b, nm.f("dit.hndbg"), RAD_F32, { kVidDbgL, D }, RAD_BUF_PERSIST); + m.op_hnd = rw(b, RAD_OP(b, "cast", + RAD_PARAMS(RAD_RANGE("M", 1, kVidDbgL), RAD_INT("n", D), + RAD_STR("from", "bf16"), RAD_STR("to", "f32")), + RAD_NOWEIGHTS), + { m.b_hn }, { m.b_hnd }); + if (!m.op_hnd) return RAD_E_INVAL; + } m.w_no_n = wcopy(b, nm.f("dit.nout.norm.weight"), nm.ckpt("transformer.norm_out.norm.weight"), RAD_BF16, { D }, RAD_ACCESS_PER_TOKEN, gx); if (!m.w_no_n) return RAD_E_INVAL; @@ -3253,6 +3286,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c m.b_modd ? (int64_t)gd.world * gd.n_layer * kMaxSegs * m.Ws * 4 : 0; d.dbg_dit_blk = m.b_dbgl; d.dbg_dit_blk_bytes = m.b_dbgl ? gd.n_layer * kDbgL * m.D * 4 : 0; + d.dbg_dit_hn = m.b_hnd; + d.dbg_dit_hn_bytes = m.b_hnd ? kVidDbgL * m.D * 4 : 0; + d.dbg_dit_nout = m.b_noutd; + d.dbg_dit_nout_bytes = m.b_noutd ? (int64_t)kMaxSegs * 2 * m.D * 4 : 0; } RAD_ARCH_TRY(rad_declare_video_generator(b, &d)); } @@ -3689,17 +3726,20 @@ 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(); - /* The dump's per-block window (R4D_DIT_SEL): block l-1's output rows [0, kDbgL) -- - * exactly the rows and the ordering point this block's own norm reads them at, so no - * extra lane dance is needed. */ - if (m.dit_dbg && V->step == 0 && l > 0 && h == 0 && R >= kDbgL) - RAD_ISSUE_N(c, m.op_dbgl, kDbgL, brow_slice(m.b_ao, 0, kDbgL, D), - brow_slice(m.b_dbgl, (l - 1) * kDbgL, kDbgL, D)); 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); + /* The dump's per-block window (R4D_DIT_SEL): block l-1's residual STREAM rows + * [0, kDbgL). b_ao is not the stream -- it holds block l-1's raw MLP delta until this + * block's norm folds it -- but op_mq1 writes the folded stream back to b_h, so right + * after its issue b_h equals the stream the reference holds after block l-1. Rows + * [0, kDbgL) belong to half 0, whose op_mq1 this follows; the deferred stream orders + * the cast after the fold it reads. */ + if (m.dit_dbg && V->step == 0 && l > 0 && h == 0 && R >= kDbgL) + RAD_ISSUE_N(c, m.op_dbgl, kDbgL, brow_slice(m.b_h, 0, kDbgL, D), + brow_slice(m.b_dbgl, (l - 1) * kDbgL, kDbgL, D)); 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), @@ -3742,14 +3782,26 @@ static void step_dit(RadCtx* c, const RadBatch* batch, const Model& m) { } } wait_ar(); - if (m.dit_dbg && V->step == 0 && R >= kDbgL) - RAD_ISSUE_N(c, m.op_dbgl, kDbgL, brow_slice(m.b_ao, 0, kDbgL, D), - brow_slice(m.b_dbgl, (L - 1) * kDbgL, kDbgL, D)); const char* nout = (const char*)rad_buf_ptr(c, m.b_nout) + (lane * m.S + V->step) * ng * 2 * D * (int64_t)sizeof(uint16_t); RAD_ISSUE_N(c, m.op_nout, R, brows(m.b_ao, R), mod(L - 1, 5, 1), RAD_P_T2(nout, RAD_BF16, ng, 2 * D), RAD_W(m.w_no_n), mod_segs, brows(m.b_h, R), brows(m.b_hn, R)); + /* Block 49's stream, slot L-1: only op_nout folds the last MLP delta into b_h (the reference + * FinalLayer norms the same post-fold stream), so this follows it. Nothing between here and + * the pass' end writes b_h. */ + if (m.dit_dbg && V->step == 0 && R >= kDbgL) + RAD_ISSUE_N(c, m.op_dbgl, kDbgL, brow_slice(m.b_h, 0, kDbgL, D), + brow_slice(m.b_dbgl, (L - 1) * kDbgL, kDbgL, D)); + /* The velocity-head isolation: the head's own operands -- norm_out's stream over the video + * rows this request's heads run on, and this step's shift/scale table row -- so the CPU + * replay can apply the checkpoint head to them and say whether it reproduces b_vel. */ + if (m.dit_dbg && V->step == 0 && vrows >= kVidDbgL) { + RAD_ISSUE_N(c, m.op_hnd, kVidDbgL, brow_slice(m.b_hn, vseg, kVidDbgL, D), + brows(m.b_hnd, kVidDbgL)); + RAD_ISSUE_N(c, m.op_noutd, 1, brow_slice(m.b_nout, lane * m.S + V->step, 1, (int64_t)kMaxSegs * 2 * D), + brows(m.b_noutd, 1)); + } /* The heads, in f32 over parts of the rows. */ auto heads = [&](rad_op op, rad_weight w, rad_weight bias, int64_t first, int64_t n, int64_t nout_c, diff --git a/core/gen/video_driver.cpp b/core/gen/video_driver.cpp index e972cee2..0dd146c2 100644 --- a/core/gen/video_driver.cpp +++ b/core/gen/video_driver.cpp @@ -324,7 +324,10 @@ int VideoDriver::init(const Program& prog, PositionsFn positions, dit_temb_b_ = decl_.dbg_dit_temb_bytes; dit_mod_b_ = decl_.dbg_dit_mod_bytes; dit_blk_b_ = decl_.dbg_dit_blk_bytes; - const int64_t sum = dit_h0_b_ + dit_vel_b_ + dit_temb_b_ + dit_mod_b_ + dit_blk_b_; + dit_hn_b_ = decl_.dbg_dit_hn_bytes; + dit_nout_b_ = decl_.dbg_dit_nout_bytes; + const int64_t sum = dit_h0_b_ + dit_vel_b_ + dit_temb_b_ + dit_mod_b_ + dit_blk_b_ + + dit_hn_b_ + dit_nout_b_; dit_host_ = static_cast(rad_dev_alloc(sum, RAD_MEM_HOST_PINNED)); dit_dbg_ = dit_host_ != nullptr; if (!dit_dbg_) @@ -1204,10 +1207,12 @@ const RadBatch* VideoDriver::build_step(Job& j, int step) { if (dit_dbg_ && j.step == 0 && !small && !j.ref_wide && j.ref_tab.empty()) { char* p = dit_host_; int64_t off = 0; - const rad_buf hh[5] = { decl_.dbg_dit_h0, decl_.dbg_dit_temb, decl_.dbg_dit_mod, - decl_.dbg_dit_blk, decl_.dbg_dit_vel }; - const int64_t nn[5] = { dit_h0_b_, dit_temb_b_, dit_mod_b_, dit_blk_b_, dit_vel_b_ }; - for (int i = 0; i < 5; ++i) + const rad_buf hh[7] = { decl_.dbg_dit_h0, decl_.dbg_dit_temb, decl_.dbg_dit_mod, + decl_.dbg_dit_blk, decl_.dbg_dit_hn, decl_.dbg_dit_nout, + decl_.dbg_dit_vel }; + const int64_t nn[7] = { dit_h0_b_, dit_temb_b_, dit_mod_b_, dit_blk_b_, dit_hn_b_, + dit_nout_b_, dit_vel_b_ }; + for (int i = 0; i < 7; ++i) if (hh[i] && nn[i] > 0) { rb_.push_back(Readback{ hh[i], nn[i], p + off }); off += nn[i]; @@ -1505,9 +1510,9 @@ int VideoDriver::done() { case RAD_GEN_PASS_VSTEP: { ++j->step; if (dit_used_) { - /* The step-0 dump landed: slab (h0 | temb | mod | blk | vel) to raw files, plus the few - * facts the driver knows (R4D_ENC_DUMPDIR; the prep script supplies D=5376 and the - * harness .meta files). */ + /* The step-0 dump landed: slab (h0 | temb | mod | blk | hn | nout | vel) to raw + * files, plus the few facts the driver knows (R4D_ENC_DUMPDIR; the prep script + * supplies D=5376 and the harness .meta files). */ char* p = dit_host_; dit_dump_file("dit_h0.f32", p, (size_t)dit_h0_b_); p += dit_h0_b_; @@ -1517,6 +1522,10 @@ int VideoDriver::done() { p += dit_mod_b_; dit_dump_file("dit_blk.f32", p, (size_t)dit_blk_b_); p += dit_blk_b_; + dit_dump_file("dit_hn.f32", p, (size_t)dit_hn_b_); + p += dit_hn_b_; + dit_dump_file("dit_nout.f32", p, (size_t)dit_nout_b_); + p += dit_nout_b_; dit_dump_file("dit_vel.f32", p, (size_t)dit_vel_b_); if (std::getenv("R4D_ENC_DUMPDIR")) { const std::string path = @@ -1530,9 +1539,10 @@ int VideoDriver::done() { } } RAD_ERR("dit_dbg: dumped step 0 -- rows %lld h0 %lld temb %lld mod %lld blk %lld " - "vel %lld", + "hn %lld nout %lld vel %lld", (long long)dit_rows_, (long long)dit_h0_b_, (long long)dit_temb_b_, - (long long)dit_mod_b_, (long long)dit_blk_b_, (long long)dit_vel_b_); + (long long)dit_mod_b_, (long long)dit_blk_b_, (long long)dit_hn_b_, + (long long)dit_nout_b_, (long long)dit_vel_b_); dit_used_ = false; } VideoEvent e; diff --git a/core/gen/video_driver.h b/core/gen/video_driver.h index 53f630a8..9c315c89 100644 --- a/core/gen/video_driver.h +++ b/core/gen/video_driver.h @@ -234,7 +234,7 @@ private: bool dit_dbg_ = false; char* dit_host_ = nullptr; int64_t dit_h0_b_ = 0, dit_vel_b_ = 0, dit_temb_b_ = 0, dit_mod_b_ = 0, - dit_blk_b_ = 0; + dit_blk_b_ = 0, dit_hn_b_ = 0, dit_nout_b_ = 0; bool dit_used_ = false; /* the running pass staged a dump */ int64_t dit_rows_ = 0; /* packed rows R of the dumped step */