diff --git a/crates/cuda-driver/src/mtp_original_full_forward/impl_originalmtpfullforward_2.rs b/crates/cuda-driver/src/mtp_original_full_forward/impl_originalmtpfullforward_2.rs index f210fb9b..3dc82348 100644 --- a/crates/cuda-driver/src/mtp_original_full_forward/impl_originalmtpfullforward_2.rs +++ b/crates/cuda-driver/src/mtp_original_full_forward/impl_originalmtpfullforward_2.rs @@ -9,3 +9,5 @@ mod impl_originalmtpfullforward; mod impl_originalmtpfullforward_2_2; #[path = "impl_originalmtpfullforward_2/impl_originalmtpfullforward_3.rs"] mod impl_originalmtpfullforward_3; +#[path = "impl_originalmtpfullforward_2/impl_originalmtpfullforward_4.rs"] +mod impl_originalmtpfullforward_4; diff --git a/crates/cuda-driver/src/mtp_original_full_forward/impl_originalmtpfullforward_2/impl_originalmtpfullforward.rs b/crates/cuda-driver/src/mtp_original_full_forward/impl_originalmtpfullforward_2/impl_originalmtpfullforward.rs index 3e71412d..36f0cafe 100644 --- a/crates/cuda-driver/src/mtp_original_full_forward/impl_originalmtpfullforward_2/impl_originalmtpfullforward.rs +++ b/crates/cuda-driver/src/mtp_original_full_forward/impl_originalmtpfullforward_2/impl_originalmtpfullforward.rs @@ -1,62 +1,90 @@ -//! Moved out of the parent module by STRA-338: impl OriginalMtpFullForward. +//! Moved out of the parent module by STRA-338: impl OriginalMtpFullForward. `plan` records the commands of one draft +//! step in the order of the former single function; the attention block is here, the MoE and the tail blocks are in +//! `impl_originalmtpfullforward_4.rs`. #[allow(unused_imports)] use super::*; +/// The scratch addresses the attention block shares. +struct PlanBufs { + qfull: u64, + qcur: u64, + attn: u64, + gated: u64, + bo: u64, +} + impl OriginalMtpFullForward<'_> { pub(in super::super) fn plan( &self, p: &ForwardPrefix<'_, '_>, policy: &OriginalMtpFullPolicy, ) -> Result> { + self.validate_plan(p, policy)?; + let mut v = Vec::new(); + v.try_reserve_exact(110).map_err(|_| Error::HostAllocation)?; + self.plan_attention_block(&mut v, p, policy)?; + self.plan_moe_block(&mut v, p, policy)?; + self.plan_tail_block(&mut v, p, policy)?; + Ok(v) + } + fn validate_plan(&self, p: &ForwardPrefix<'_, '_>, policy: &OriginalMtpFullPolicy) -> Result<()> { self.inner.validate(p.scalar.context)?; validate_scalar_prefix(p.scalar)?; p.ring.validate_context(p.scalar.context)?; if policy.legacy_router_mode > 2 { return Err(Error::InvalidProjectionShape); } - let o = &self.inner; - let w = &o.weights; - let wa = |i: usize| -> Result { Ok(w.roles[i].handle.ok_or(Error::ContextPoisoned)?.qwen_address()) }; - let a = |i| o.address(i); - let mut v = Vec::new(); - v.try_reserve_exact(110).map_err(|_| Error::HostAllocation)?; - // Prefix quantized mixed remains live: Q uses same Q8 image as K and V. - // Original Q split is 24 distinct width256 rows, equivalent to its 2D D2D. - let qfull = a(0)?; - let qcur = a(1)?; - let attn = a(2)?; - let gated = a(3)?; - let bo = a(4)?; + Ok(()) + } + pub(in super::super) fn slot(&self, i: usize) -> Result { + self.inner.address(i) + } + /// The addresses of several scratch slots, resolved in the order given. + pub(in super::super) fn slots(&self, ids: [usize; N]) -> Result<[u64; N]> { + let mut out = [0; N]; + for (k, i) in ids.into_iter().enumerate() { + out[k] = self.inner.address(i)?; + } + Ok(out) + } + pub(in super::super) fn role_address(&self, i: usize) -> Result { + Ok(self.inner.weights.roles[i].handle.ok_or(Error::ContextPoisoned)?.qwen_address()) + } + fn plan_attention_block( + &self, + v: &mut Vec, + p: &ForwardPrefix<'_, '_>, + policy: &OriginalMtpFullPolicy, + ) -> Result<()> { + let b = self.plan_q(v, p)?; + self.plan_rope(v, &b, p.pos, &policy.kv)?; + self.plan_attention(v, p, &b)?; + self.plan_gate_out(v, p, &b)?; + self.plan_residual(v, p, policy, &b) + } + /// Prefix quantized mixed remains live: Q uses same Q8 image as K and V. Original Q split is 24 distinct width256 + /// rows, equivalent to its 2D D2D. + fn plan_q(&self, v: &mut Vec, p: &ForwardPrefix<'_, '_>) -> Result { + let [qfull, qcur, attn, gated, bo] = self.slots([0, 1, 2, 3, 4])?; + let b = PlanBufs { qfull, qcur, attn, gated, bo }; // Native Q8_0 body is shared with prefix's literal MMVQ module; its command // is retained in ForwardProgram, not submitted through an old default API. // Slot0 below is a full native MMVQ asset; explicit input/output geometry. - let qweight = wa(0)?; + let qweight = self.role_address(0)?; let qentry = fixed_entry(13, "head_n8_s0")?; - push( - &mut v, - 13, - qentry, - [12288, 1, 1], - [32, 4, 1], - 0, - vec![F::U64(qweight), F::U64(p.q), F::U64(qfull), F::I32(2560), F::I32(12288)], - )?; - self.finite_tail(&mut v, qfull, 12288, 0)?; + let q = vec![F::U64(qweight), F::U64(p.q), F::U64(qfull), F::I32(2560), F::I32(12288)]; + push(v, 13, qentry, [12288, 1, 1], [32, 4, 1], 0, q)?; + self.finite_tail(v, qfull, 12288, 0)?; // Copy commands remain distinct byte transports (24 literal rows); their // execution is interleaved in ForwardProgram immediately before Q norm. let norm_entry = 0; - push( - &mut v, - 16, - norm_entry, - [24, 1, 1], - [256, 1, 1], - 0, - vec![F::U64(qcur), F::U64(wa(1)?), F::U64(qcur), F::I32(256), F::F32(1e-6)], - )?; - self.finite_tail(&mut v, qcur, 6144, 1)?; - let kv = &policy.kv; + let norm = vec![F::U64(qcur), F::U64(self.role_address(1)?), F::U64(qcur), F::I32(256), F::F32(1e-6)]; + push(v, 16, norm_entry, [24, 1, 1], [256, 1, 1], 0, norm)?; + self.finite_tail(v, qcur, 6144, 1)?; + Ok(b) + } + fn reject_unsupported_kv(kv: &DraftKvPolicy) -> Result<()> { if kv.disabled || kv.hybrid || kv.rotated @@ -68,453 +96,113 @@ impl OriginalMtpFullForward<'_> { { return Err(Error::UnsupportedQwenOperation); } - push( - &mut v, - 16, - 1, - [4, 24, 1], - [32, 1, 1], - 0, - vec![ - F::U64(qcur), - F::U64(qcur), - F::I32(24), - F::I32(256), - F::I32(64), - F::F32(kv.theta_scale), - F::F32(kv.freq_scale), - F::F32(kv.corr_low), - F::F32(kv.corr_high), - F::F32(kv.ext_factor), - F::F32(kv.mscale), - F::U64(p.pos), - F::U64(0), - F::Words3([0; 3]), - ], - )?; - self.finite_tail(&mut v, qcur, 6144, 2)?; - // Original window_ids on actual private step, followed by same INT8 decode - // chunk and merge entries. Ring owns pools/table/ident/scratch throughout. - // window command uses ring's actual KV module in the captured program. - let (pa, pm, pl, ident) = o.attention(p.ring)?; + Ok(()) + } + fn plan_rope(&self, v: &mut Vec, b: &PlanBufs, pos: u64, kv: &DraftKvPolicy) -> Result<()> { + Self::reject_unsupported_kv(kv)?; + let qcur = b.qcur; + let rope = vec![ + F::U64(qcur), + F::U64(qcur), + F::I32(24), + F::I32(256), + F::I32(64), + F::F32(kv.theta_scale), + F::F32(kv.freq_scale), + F::F32(kv.corr_low), + F::F32(kv.corr_high), + F::F32(kv.ext_factor), + F::F32(kv.mscale), + F::U64(pos), + F::U64(0), + F::Words3([0; 3]), + ]; + push(v, 16, 1, [4, 24, 1], [32, 1, 1], 0, rope)?; + self.finite_tail(v, qcur, 6144, 2) + } + /// Original window_ids on actual private step, followed by same INT8 decode chunk and merge entries. Ring owns + /// pools/table/ident/scratch throughout. The window command uses the ring's actual KV module in the captured + /// program. + fn plan_attention(&self, v: &mut Vec, p: &ForwardPrefix<'_, '_>, b: &PlanBufs) -> Result<()> { + let (pa, pm, pl, ident) = self.inner.attention(p.ring)?; let chain_mtp = crate::original_verify::chain::flags().mtp; // the Original's chunk kernel in both modes (the load-hoisted variant measured no faster); only the merge changes let daily = daily_mtp(); - push( - &mut v, - if daily { symbols::DAILY_ASSET } else { 19 }, - if daily { symbols::DAILY_CHUNK } else { 0 }, - [CHUNKS as u32, 2, 1], - [256, 1, 1], - 0, - vec![ - F::U64(qcur), - F::U64(p.ring.address(0)?), - F::U64(p.ring.address(1)?), - F::U64(p.ring.address(2)?), - F::U64(p.ring.address(3)?), - F::U64(p.ring.address(4)?), - F::U64(ident), - F::U64(p.step), - F::I32(2), - F::I32(4), - F::F32(1.0 / 16.0), - F::U64(pa), - F::U64(pm), - F::U64(pl), - F::I32(CHUNKS as i32), - F::I32(32768), - F::I64(SCRATCH_FLOATS as i64), - ], - )?; - let mut merge_args = vec![ + self.push_chunk(v, p, (b.qcur, [pa, pm, pl, ident], daily))?; + Self::push_merge(v, p.step, b.attn, [pa, pm, pl], (daily, chain_mtp))?; + self.finite_tail(v, b.attn, 6144, 3) + } + fn push_chunk(&self, v: &mut Vec, p: &ForwardPrefix<'_, '_>, io: (u64, [u64; 4], bool)) -> Result<()> { + let (qcur, window, daily) = io; + let [pa, pm, pl, ident] = window; + let ring = [p.ring.address(0)?, p.ring.address(1)?, p.ring.address(2)?, p.ring.address(3)?, p.ring.address(4)?]; + let args = vec![ + F::U64(qcur), + F::U64(ring[0]), + F::U64(ring[1]), + F::U64(ring[2]), + F::U64(ring[3]), + F::U64(ring[4]), + F::U64(ident), + F::U64(p.step), + F::I32(2), + F::I32(4), + F::F32(1.0 / 16.0), F::U64(pa), F::U64(pm), F::U64(pl), F::I32(CHUNKS as i32), - F::U64(attn), + F::I32(32768), F::I64(SCRATCH_FLOATS as i64), ]; - if daily { - // merge_v2 takes the step words after n_chunks: (acc, m, l, n_chunks, steps, attn, stride) - merge_args = vec![ - F::U64(pa), - F::U64(pm), - F::U64(pl), - F::I32(CHUNKS as i32), - F::U64(p.step), - F::U64(attn), - F::I64(SCRATCH_FLOATS as i64), - ]; + let (asset, index) = if daily { (symbols::DAILY_ASSET, symbols::DAILY_CHUNK) } else { (19, 0) }; + push(v, asset, index, [CHUNKS as u32, 2, 1], [256, 1, 1], 0, args) + } + fn push_merge(v: &mut Vec, step: u64, attn: u64, window: [u64; 3], flags: (bool, bool)) -> Result<()> { + let (daily, chain_mtp) = flags; + let (asset, index) = if daily { + (symbols::DAILY_ASSET, symbols::DAILY_MERGE_V2) } else if chain_mtp { - merge_args.push(F::U64(p.step)); // the row's step words: n_ids bounds the merge to the chunks that hold cells - } - push( - &mut v, - if daily { - symbols::DAILY_ASSET - } else if chain_mtp { - symbols::CHAIN_ASSET - } else { - 19 - }, - if daily { - symbols::DAILY_MERGE_V2 - } else if chain_mtp { - symbols::CHAIN_QSA_MERGE_N - } else { - 1 - }, - [24, 1, 1], - [256, 1, 1], - 0, - merge_args, - )?; - self.finite_tail(&mut v, attn, 6144, 3)?; - push( - &mut v, - 11, - 0, - [24, 1, 1], - [256, 1, 1], - 0, - vec![F::U64(attn), F::U64(qfull), F::U64(gated), F::I32(24), F::I32(256)], - )?; - self.finite_tail(&mut v, gated, 6144, 4)?; - self.quant(&mut v, gated, p.q, 6144)?; - self.q8_tail(&mut v, p.q, 6144, 5)?; - self.mmvq(&mut v, wa(2)?, p.q, bo, 6144, 2560)?; - self.finite_tail(&mut v, bo, 2560, 6)?; - self.mlp_gr(&mut v, p, bo, policy)?; - self.finite_tail(&mut v, p.r, 10240, 7)?; - self.finite_tail(&mut v, p.scalar.address(7, p.scalar.max_t * 40960)?, 10240, 8)?; - self.finite_tail(&mut v, p.scalar.address(8, p.scalar.max_t * 1280)?, 320, 9)?; - self.finite_tail(&mut v, a(20)?, 4, 10)?; - self.finite_tail(&mut v, p.mixed, 2560, 11)?; - let router = a(5)?; - self.mmvf(&mut v, p.mixed, wa(7)?, router, 2560, 512)?; - self.finite_tail(&mut v, router, 512, 12)?; - if policy.native_router { - push(&mut v, 3, 0, [1, 1, 1], [32, 8, 1], 0, vec![F::U64(router), F::U64(a(7)?), F::U64(a(6)?)])?; + (symbols::CHAIN_ASSET, symbols::CHAIN_QSA_MERGE_N) } else { - push( - &mut v, - 4, - policy.legacy_router_mode as usize, - [1, 1, 1], - [512, 1, 1], - (512 + 512 * 8 + 512 * 4) as u32, - vec![F::U64(router), F::I32(1), F::I32(512), F::I32(10), F::U64(a(7)?), F::U64(a(6)?)], - )?; + (19, 1) + }; + push(v, asset, index, [24, 1, 1], [256, 1, 1], 0, Self::merge_args(step, attn, window, flags)) + } + fn merge_args(step: u64, attn: u64, window: [u64; 3], flags: (bool, bool)) -> Vec { + let [pa, pm, pl] = window; + let (n, scratch) = (F::I32(CHUNKS as i32), F::I64(SCRATCH_FLOATS as i64)); + if flags.0 { + // merge_v2 takes the step words after n_chunks: (acc, m, l, n_chunks, steps, attn, stride) + return vec![F::U64(pa), F::U64(pm), F::U64(pl), n, F::U64(step), F::U64(attn), scratch]; } - self.finite_tail(&mut v, a(6)?, 10, 13)?; - let expert = w.experts.handle.ok_or(Error::ContextPoisoned)?.qwen_address(); - push( - &mut v, - 0, - 0, - [1, 1, 1], - [128, 1, 1], - 0, - vec![ - F::U64(a(7)?), - F::I32(10), - F::I32(10), - F::U64(expert), - F::I64(1382400), - F::U64(a(12)?), - F::U64(a(13)?), - F::U64(a(14)?), - F::U64(a(8)?), - F::U64(a(10)?), - ], - )?; - push( - &mut v, - 1, - 0, - [1, 1, 1], - [128, 1, 1], - 0, - vec![F::U64(p.mixed), F::U64(a(15)?), F::U64(a(16)?), F::I64(80)], - )?; - let s2 = a(17)?; - let hiddenq = s2 + 51200; - let hscale = hiddenq + 6816; - push( - &mut v, - 0, - if policy.old_grouped_s2 { 1 } else { 2 }, - [40, 10, 1], - [256, 1, 1], - 0, - vec![ - F::U64(a(12)?), - F::U64(a(13)?), - F::U64(a(14)?), - F::U64(a(10)?), - F::U64(a(15)?), - F::U64(a(16)?), - F::U64(s2), - F::I32(10), - ], - )?; - self.finite_tail(&mut v, s2, 12800, 14)?; - push(&mut v, 0, 3, [6400usize.div_ceil(256) as u32, 1, 1], [256, 1, 1], 0, vec![F::U64(s2), F::I64(6400)])?; - self.finite_tail(&mut v, s2, 6400, 15)?; - push( - &mut v, - 1, - 0, - [200usize.div_ceil(128) as u32, 1, 1], - [128, 1, 1], - 0, - vec![F::U64(s2), F::U64(hiddenq), F::U64(hscale), F::I64(200)], - )?; - push( - &mut v, - 0, - if policy.old_grouped_s2 { 4 } else { 5 }, - [40, 10, 1], - [256, 1, 1], - 0, - vec![ - F::U64(a(12)?), - F::U64(a(13)?), - F::U64(a(14)?), - F::U64(a(8)?), - F::U64(hiddenq), - F::U64(hscale), - F::U64(a(9)?), - ], - )?; - self.finite_tail(&mut v, a(9)?, 25600, 16)?; - // Shared projection consumes original mixed, not its unconditional BF16 image. - push(&mut v, 10, 0, [10, 1, 1], [256, 1, 1], 0, vec![F::U64(p.mixed), F::U64(a(23)?), F::I64(2560)])?; - self.quant(&mut v, p.mixed, p.q, 2560)?; - self.q8_tail(&mut v, p.q, 2560, 17)?; - self.mmvq(&mut v, wa(8)?, p.q, a(18)?, 2560, 640)?; - self.mmvq(&mut v, wa(9)?, p.q, a(19)?, 2560, 640)?; - self.finite_tail(&mut v, a(18)?, 640, 18)?; - self.finite_tail(&mut v, a(19)?, 640, 19)?; - push( - &mut v, - 2, - 0, - [5, 1, 1], - [128, 1, 1], - 0, - vec![F::U64(a(18)?), F::U64(a(19)?), F::U64(a(18)?), F::I32(640)], - )?; - self.finite_tail(&mut v, a(18)?, 640, 20)?; - self.quant(&mut v, a(18)?, p.q, 640)?; - self.q8_tail(&mut v, p.q, 640, 21)?; - self.mmvq(&mut v, wa(10)?, p.q, a(11)?, 640, 2560)?; - self.finite_tail(&mut v, a(11)?, 2560, 22)?; - if policy.native_shared_scalar { - self.mmvf(&mut v, p.mixed, wa(11)?, a(22)?, 2560, 1)?; - push(&mut v, 2, 2, [1, 1, 1], [1, 1, 1], 0, vec![F::U64(a(22)?)])?; - } else { - push( - &mut v, - 2, - 1, - [1, 1, 1], - [256, 1, 1], - 0, - vec![F::U64(a(23)?), F::U64(wa(11)?), F::U64(a(22)?), F::I32(2560)], - )?; + let mut args = vec![F::U64(pa), F::U64(pm), F::U64(pl), n, F::U64(attn), scratch]; + if flags.1 { + args.push(F::U64(step)); // the row's step words: n_ids bounds the merge to the chunks that hold cells } - self.finite_tail(&mut v, a(22)?, 1, 23)?; - push(&mut v, 2, 3, [20, 1, 1], [128, 1, 1], 0, vec![F::U64(a(11)?), F::U64(a(22)?), F::I32(2560)])?; - self.finite_tail(&mut v, a(11)?, 2560, 24)?; - let mut combine_args = vec![F::U64(a(9)?), F::U64(a(6)?), F::U64(a(11)?), F::U64(o.combined()?)]; - if policy.native_combine { - combine_args.extend([F::I64(2560), F::I32(10)]); - } else { - combine_args.extend([F::I32(2560), F::I32(10), F::I32(1)]); - } - push( - &mut v, - if policy.native_combine { 5 } else { 2 }, - if policy.native_combine { 0 } else { 4 }, - [if policy.native_combine { 10 } else { 20 }, 1, 1], - [if policy.native_combine { 256 } else { 128 }, 1, 1], - 0, - combine_args, - )?; - self.finite_tail(&mut v, o.combined()?, 2560, 25)?; - if policy.native_final_gr { - push( - &mut v, - 8, - 2, - [40, 1, 1], - [256, 1, 1], - 0, - vec![ - F::U64(p.r), - F::U64(o.combined()?), - F::U64(a(20)?), - F::U64(p.r), - F::I32(2560), - F::I32(4), - F::F32(0.25), - ], - )?; - } else { - push( - &mut v, - 9, - 0, - [40, 1, 1], - [256, 1, 1], - 16, - vec![F::U64(p.r), F::U64(o.combined()?), F::U64(a(20)?), F::I32(2560), F::I32(4), F::U64(p.r)], - )?; - } - self.finite_tail(&mut v, p.r, 10240, 26)?; - self.final_gr(&mut v, p, policy)?; - self.finite_tail(&mut v, a(24)?, 2560, 27)?; - self.quant(&mut v, a(24)?, p.q, 2560)?; - self.q8_tail(&mut v, p.q, 2560, 28)?; - let (name, grid, block, iq) = head_dispatch::head_dispatch(w.head_kind, 2560, w.vocab, 1, w.dispatch)?; - let asset = if iq { 14 } else { 13 }; - let index = fixed_entry(asset, name)?; - let mut args = vec![F::U64(w.head.handle.ok_or(Error::ContextPoisoned)?.qwen_address())]; - if iq { - args.push(F::U64(crate::native_head_row_bytes(w.head_kind, 2560)? as u64)); - } - args.extend([F::U64(p.q), F::U64(o.logits()?), F::I32(2560), F::I32(w.vocab as i32)]); - if iq { - args.push(F::I32(1)); - } - push(&mut v, asset, index, grid, block, 0, args)?; - self.finite_tail(&mut v, o.logits()?, w.vocab, 29)?; - if let Some(j) = policy.coupled_j { - if !o.coupled || j >= 8 { - return Err(Error::InvalidQwenShape); - } - let blocks = w.vocab.div_ceil(4096); - let kpart = w.vocab.min(64); - let sub = w - .subset - .as_ref() - .map(|(f, _)| f.handle.ok_or(Error::ContextPoisoned).map(|h| h.qwen_address())) - .transpose()? - .unwrap_or(0); - let inv = w - .subset - .as_ref() - .map(|(_, i)| i.handle.ok_or(Error::ContextPoisoned).map(|h| h.qwen_address())) - .transpose()? - .unwrap_or(0); - push( - &mut v, - 20, - 2, - [1, 1, 1], - [1024, 1, 1], - (w.vocab.div_ceil(32) * 4) as u32, - vec![ - F::U64(o.logits()?), - F::I32(w.vocab as i32), - F::U64(inv), - F::I32(w.id_vocab as i32), - F::U64(a(28)?), - F::U64(a(29)?), - F::I32(4096), - F::I32(j as i32), - ], - )?; - push( - &mut v, - 20, - 0, - [blocks as u32, 1, 1], - [128, 1, 1], - 0, - vec![ - F::U64(o.logits()?), - F::I32(w.vocab as i32), - F::U64(0), - F::I32(0), - F::Params(SamplerParams::default()), - F::I32(kpart as i32), - F::I32(blocks as i32), - F::U64(a(30)?), - ], - )?; - push( - &mut v, - 20, - 3, - [1, 1, 1], - [32, 1, 1], - 0, - vec![ - F::U64(a(30)?), - F::I32(blocks as i32), - F::I32(w.vocab as i32), - F::I32(kpart as i32), - F::U64(a(28)?), - F::U64(p.step), - F::U64(sub), - F::U64(a(29)?), - F::I32(4096), - F::I32(j as i32), - F::U64(o.address(34)?), - F::U64(o.address(35)?), - ], - )?; - } else { - let mut greedy = SamplerParams::default(); - greedy.greedy = true; - greedy.temperature = 0.0; - let chain_head = crate::original_verify::chain::flags().head; - push( - &mut v, - if chain_head { symbols::CHAIN_ASSET } else { 15 }, - if chain_head { symbols::CHAIN_SAMPLER_GREEDY } else { 0 }, - [1, 1, 1], - [1024, 1, 1], - 0, - vec![ - F::U64(o.logits()?), - F::I32(w.vocab as i32), - F::U64(0), - F::I32(0), - F::Params(greedy), - F::I32(0), - F::I32(0), - F::U64(o.address(34)?), - ], - )?; - push( - &mut v, - if chain_head { symbols::CHAIN_ASSET } else { 12 }, - if chain_head { symbols::CHAIN_ROW_TOP_PROB } else { 0 }, - [1, 1, 1], - [1024, 1, 1], - 0, - vec![F::U64(o.logits()?), F::I32(w.vocab as i32), F::U64(o.address(34)?), F::U64(o.address(35)?)], - )?; - if let Some((forward, _)) = &w.subset { - push( - &mut v, - 12, - 1, - [1, 1, 1], - [64, 1, 1], - 0, - vec![ - F::U64(o.address(34)?), - F::U64(forward.handle.ok_or(Error::ContextPoisoned)?.qwen_address()), - F::I32(1), - ], - )?; - } - } - Ok(v) + args + } + fn plan_gate_out(&self, v: &mut Vec, p: &ForwardPrefix<'_, '_>, b: &PlanBufs) -> Result<()> { + let gate = vec![F::U64(b.attn), F::U64(b.qfull), F::U64(b.gated), F::I32(24), F::I32(256)]; + push(v, 11, 0, [24, 1, 1], [256, 1, 1], 0, gate)?; + self.finite_tail(v, b.gated, 6144, 4)?; + self.quant(v, b.gated, p.q, 6144)?; + self.q8_tail(v, p.q, 6144, 5)?; + self.mmvq(v, self.role_address(2)?, p.q, b.bo, 6144, 2560)?; + self.finite_tail(v, b.bo, 2560, 6) + } + fn plan_residual( + &self, + v: &mut Vec, + p: &ForwardPrefix<'_, '_>, + policy: &OriginalMtpFullPolicy, + b: &PlanBufs, + ) -> Result<()> { + self.mlp_gr(v, p, b.bo, policy)?; + self.finite_tail(v, p.r, 10240, 7)?; + self.finite_tail(v, p.scalar.address(7, p.scalar.max_t * 40960)?, 10240, 8)?; + self.finite_tail(v, p.scalar.address(8, p.scalar.max_t * 1280)?, 320, 9)?; + self.finite_tail(v, self.slot(20)?, 4, 10)?; + self.finite_tail(v, p.mixed, 2560, 11) } } diff --git a/crates/cuda-driver/src/mtp_original_full_forward/impl_originalmtpfullforward_2/impl_originalmtpfullforward_4.rs b/crates/cuda-driver/src/mtp_original_full_forward/impl_originalmtpfullforward_2/impl_originalmtpfullforward_4.rs new file mode 100644 index 00000000..1fc84e80 --- /dev/null +++ b/crates/cuda-driver/src/mtp_original_full_forward/impl_originalmtpfullforward_2/impl_originalmtpfullforward_4.rs @@ -0,0 +1,313 @@ +//! The MoE and tail blocks of `OriginalMtpFullForward::plan` (STRA-338), recorded in the order of the former single +//! function: router, experts, shared expert and combine; then the final GR, the head and the sampler. + +#[allow(unused_imports)] +use super::*; + +/// What the coupled sampler needs besides the logits. +struct Coupled { + logits: u64, + sub: u64, + inv: u64, + j: u8, + slots: [u64; 5], +} + +impl OriginalMtpFullForward<'_> { + pub(in super::super) fn plan_moe_block( + &self, + v: &mut Vec, + p: &ForwardPrefix<'_, '_>, + policy: &OriginalMtpFullPolicy, + ) -> Result<()> { + self.plan_router(v, p, policy)?; + self.plan_experts(v, p.mixed, policy)?; + self.plan_shared(v, p, policy)?; + self.plan_combine(v, policy) + } + pub(in super::super) fn plan_tail_block( + &self, + v: &mut Vec, + p: &ForwardPrefix<'_, '_>, + policy: &OriginalMtpFullPolicy, + ) -> Result<()> { + self.plan_final_gr(v, p, policy)?; + self.plan_head(v, p)?; + self.plan_sampler(v, p.step, policy) + } + fn plan_router( + &self, + v: &mut Vec, + p: &ForwardPrefix<'_, '_>, + policy: &OriginalMtpFullPolicy, + ) -> Result<()> { + let router = self.slot(5)?; + self.mmvf(v, p.mixed, self.role_address(7)?, router, 2560, 512)?; + self.finite_tail(v, router, 512, 12)?; + let [ids, probs] = self.slots([7, 6])?; + Self::push_router(v, router, [ids, probs], policy)?; + self.finite_tail(v, probs, 10, 13) + } + fn push_router( + v: &mut Vec, + router: u64, + outputs: [u64; 2], + policy: &OriginalMtpFullPolicy, + ) -> Result<()> { + let [ids, probs] = outputs; + if policy.native_router { + return push(v, 3, 0, [1, 1, 1], [32, 8, 1], 0, vec![F::U64(router), F::U64(ids), F::U64(probs)]); + } + let shared = (512 + 512 * 8 + 512 * 4) as u32; + let args = vec![F::U64(router), F::I32(1), F::I32(512), F::I32(10), F::U64(ids), F::U64(probs)]; + push(v, 4, policy.legacy_router_mode as usize, [1, 1, 1], [512, 1, 1], shared, args) + } + fn plan_experts(&self, v: &mut Vec, mixed: u64, policy: &OriginalMtpFullPolicy) -> Result<()> { + let expert = self.inner.weights.experts.handle.ok_or(Error::ContextPoisoned)?.qwen_address(); + let [a7, a8, a10, a12, a13, a14, a15, a16] = self.slots([7, 8, 10, 12, 13, 14, 15, 16])?; + let select = vec![ + F::U64(a7), + F::I32(10), + F::I32(10), + F::U64(expert), + F::I64(1382400), + F::U64(a12), + F::U64(a13), + F::U64(a14), + F::U64(a8), + F::U64(a10), + ]; + push(v, 0, 0, [1, 1, 1], [128, 1, 1], 0, select)?; + push(v, 1, 0, [1, 1, 1], [128, 1, 1], 0, vec![F::U64(mixed), F::U64(a15), F::U64(a16), F::I64(80)])?; + self.plan_s2(v, policy, [a8, a10, a12, a13, a14, a15, a16])?; + self.plan_expert_down(v, policy, [a8, a12, a13, a14]) + } + /// The grouped S2 matrix products of the selected experts and their requantization. + fn plan_s2(&self, v: &mut Vec, policy: &OriginalMtpFullPolicy, a: [u64; 7]) -> Result<()> { + let [_, a10, a12, a13, a14, a15, a16] = a; + let s2 = self.slot(17)?; + let hiddenq = s2 + 51200; + let hscale = hiddenq + 6816; + let index = if policy.old_grouped_s2 { 1 } else { 2 }; + let args = + vec![F::U64(a12), F::U64(a13), F::U64(a14), F::U64(a10), F::U64(a15), F::U64(a16), F::U64(s2), F::I32(10)]; + push(v, 0, index, [40, 10, 1], [256, 1, 1], 0, args)?; + self.finite_tail(v, s2, 12800, 14)?; + push(v, 0, 3, [6400usize.div_ceil(256) as u32, 1, 1], [256, 1, 1], 0, vec![F::U64(s2), F::I64(6400)])?; + self.finite_tail(v, s2, 6400, 15)?; + let requant = vec![F::U64(s2), F::U64(hiddenq), F::U64(hscale), F::I64(200)]; + push(v, 1, 0, [200usize.div_ceil(128) as u32, 1, 1], [128, 1, 1], 0, requant) + } + fn plan_expert_down(&self, v: &mut Vec, policy: &OriginalMtpFullPolicy, a: [u64; 4]) -> Result<()> { + let [a8, a12, a13, a14] = a; + let hiddenq = self.slot(17)? + 51200; + let hscale = hiddenq + 6816; + let a9 = self.slot(9)?; + let index = if policy.old_grouped_s2 { 4 } else { 5 }; + let args = vec![F::U64(a12), F::U64(a13), F::U64(a14), F::U64(a8), F::U64(hiddenq), F::U64(hscale), F::U64(a9)]; + push(v, 0, index, [40, 10, 1], [256, 1, 1], 0, args)?; + self.finite_tail(v, a9, 25600, 16) + } + fn plan_shared( + &self, + v: &mut Vec, + p: &ForwardPrefix<'_, '_>, + policy: &OriginalMtpFullPolicy, + ) -> Result<()> { + self.plan_shared_input(v, p)?; + self.plan_shared_gate_up(v, p)?; + self.plan_shared_down(v, p)?; + self.plan_shared_scalar(v, p.mixed, policy) + } + /// Shared projection consumes original mixed, not its unconditional BF16 image. + fn plan_shared_input(&self, v: &mut Vec, p: &ForwardPrefix<'_, '_>) -> Result<()> { + let a23 = self.slot(23)?; + push(v, 10, 0, [10, 1, 1], [256, 1, 1], 0, vec![F::U64(p.mixed), F::U64(a23), F::I64(2560)])?; + self.quant(v, p.mixed, p.q, 2560)?; + self.q8_tail(v, p.q, 2560, 17) + } + fn plan_shared_gate_up(&self, v: &mut Vec, p: &ForwardPrefix<'_, '_>) -> Result<()> { + let [a18, a19] = self.slots([18, 19])?; + self.mmvq(v, self.role_address(8)?, p.q, a18, 2560, 640)?; + self.mmvq(v, self.role_address(9)?, p.q, a19, 2560, 640)?; + self.finite_tail(v, a18, 640, 18)?; + self.finite_tail(v, a19, 640, 19)?; + push(v, 2, 0, [5, 1, 1], [128, 1, 1], 0, vec![F::U64(a18), F::U64(a19), F::U64(a18), F::I32(640)])?; + self.finite_tail(v, a18, 640, 20) + } + fn plan_shared_down(&self, v: &mut Vec, p: &ForwardPrefix<'_, '_>) -> Result<()> { + let [a18, a11] = self.slots([18, 11])?; + self.quant(v, a18, p.q, 640)?; + self.q8_tail(v, p.q, 640, 21)?; + self.mmvq(v, self.role_address(10)?, p.q, a11, 640, 2560)?; + self.finite_tail(v, a11, 2560, 22) + } + fn plan_shared_scalar(&self, v: &mut Vec, mixed: u64, policy: &OriginalMtpFullPolicy) -> Result<()> { + let [a11, a22, a23] = self.slots([11, 22, 23])?; + if policy.native_shared_scalar { + self.mmvf(v, mixed, self.role_address(11)?, a22, 2560, 1)?; + push(v, 2, 2, [1, 1, 1], [1, 1, 1], 0, vec![F::U64(a22)])?; + } else { + let args = vec![F::U64(a23), F::U64(self.role_address(11)?), F::U64(a22), F::I32(2560)]; + push(v, 2, 1, [1, 1, 1], [256, 1, 1], 0, args)?; + } + self.finite_tail(v, a22, 1, 23)?; + push(v, 2, 3, [20, 1, 1], [128, 1, 1], 0, vec![F::U64(a11), F::U64(a22), F::I32(2560)])?; + self.finite_tail(v, a11, 2560, 24) + } + fn plan_combine(&self, v: &mut Vec, policy: &OriginalMtpFullPolicy) -> Result<()> { + let [a9, a6, a11] = self.slots([9, 6, 11])?; + let combined = self.inner.combined()?; + let mut args = vec![F::U64(a9), F::U64(a6), F::U64(a11), F::U64(combined)]; + let native = policy.native_combine; + if native { + args.extend([F::I64(2560), F::I32(10)]); + } else { + args.extend([F::I32(2560), F::I32(10), F::I32(1)]); + } + let (asset, index) = if native { (5, 0) } else { (2, 4) }; + let (grid, block) = if native { (10, 256) } else { (20, 128) }; + push(v, asset, index, [grid, 1, 1], [block, 1, 1], 0, args)?; + self.finite_tail(v, combined, 2560, 25) + } + fn plan_final_gr( + &self, + v: &mut Vec, + p: &ForwardPrefix<'_, '_>, + policy: &OriginalMtpFullPolicy, + ) -> Result<()> { + self.push_final_gr(v, p.r, policy.native_final_gr)?; + self.finite_tail(v, p.r, 10240, 26)?; + self.final_gr(v, p, policy)?; + let a24 = self.slot(24)?; + self.finite_tail(v, a24, 2560, 27)?; + self.quant(v, a24, p.q, 2560)?; + self.q8_tail(v, p.q, 2560, 28) + } + fn push_final_gr(&self, v: &mut Vec, r: u64, native: bool) -> Result<()> { + let combined = self.inner.combined()?; + let a20 = self.slot(20)?; + if native { + let args = vec![F::U64(r), F::U64(combined), F::U64(a20), F::U64(r), F::I32(2560), F::I32(4), F::F32(0.25)]; + return push(v, 8, 2, [40, 1, 1], [256, 1, 1], 0, args); + } + let args = vec![F::U64(r), F::U64(combined), F::U64(a20), F::I32(2560), F::I32(4), F::U64(r)]; + push(v, 9, 0, [40, 1, 1], [256, 1, 1], 16, args) + } + fn plan_head(&self, v: &mut Vec, p: &ForwardPrefix<'_, '_>) -> Result<()> { + let w = &self.inner.weights; + let (name, grid, block, iq) = head_dispatch::head_dispatch(w.head_kind, 2560, w.vocab, 1, w.dispatch)?; + let asset = if iq { 14 } else { 13 }; + let index = fixed_entry(asset, name)?; + push(v, asset, index, grid, block, 0, self.head_args(p.q, iq)?)?; + self.finite_tail(v, self.inner.logits()?, w.vocab, 29) + } + fn head_args(&self, q: u64, iq: bool) -> Result> { + let w = &self.inner.weights; + let mut args = vec![F::U64(w.head.handle.ok_or(Error::ContextPoisoned)?.qwen_address())]; + if iq { + args.push(F::U64(crate::native_head_row_bytes(w.head_kind, 2560)? as u64)); + } + args.extend([F::U64(q), F::U64(self.inner.logits()?), F::I32(2560), F::I32(w.vocab as i32)]); + if iq { + args.push(F::I32(1)); + } + Ok(args) + } + fn plan_sampler(&self, v: &mut Vec, step: u64, policy: &OriginalMtpFullPolicy) -> Result<()> { + match policy.coupled_j { + Some(j) => self.plan_coupled(v, step, j), + None => self.plan_greedy(v), + } + } + fn plan_coupled(&self, v: &mut Vec, step: u64, j: u8) -> Result<()> { + let o = &self.inner; + if !o.coupled || j >= 8 { + return Err(Error::InvalidQwenShape); + } + let w = &o.weights; + let handle = |f: &DeviceBuffer<'_>| f.handle.ok_or(Error::ContextPoisoned).map(|h| h.qwen_address()); + let sub = w.subset.as_ref().map(|(f, _)| handle(f)).transpose()?.unwrap_or(0); + let inv = w.subset.as_ref().map(|(_, i)| handle(i)).transpose()?.unwrap_or(0); + let slots = self.slots([28, 29, 30, 34, 35])?; + let coupled = Coupled { logits: o.logits()?, sub, inv, j, slots }; + self.push_coupled_filter(v, &coupled)?; + self.push_coupled_blocks(v, &coupled)?; + self.push_coupled_merge(v, &coupled, step) + } + fn push_coupled_filter(&self, v: &mut Vec, c: &Coupled) -> Result<()> { + let w = &self.inner.weights; + let args = vec![ + F::U64(c.logits), + F::I32(w.vocab as i32), + F::U64(c.inv), + F::I32(w.id_vocab as i32), + F::U64(c.slots[0]), + F::U64(c.slots[1]), + F::I32(4096), + F::I32(c.j as i32), + ]; + push(v, 20, 2, [1, 1, 1], [1024, 1, 1], (w.vocab.div_ceil(32) * 4) as u32, args) + } + fn push_coupled_blocks(&self, v: &mut Vec, c: &Coupled) -> Result<()> { + let w = &self.inner.weights; + let blocks = w.vocab.div_ceil(4096); + let kpart = w.vocab.min(64); + let args = vec![ + F::U64(c.logits), + F::I32(w.vocab as i32), + F::U64(0), + F::I32(0), + F::Params(SamplerParams::default()), + F::I32(kpart as i32), + F::I32(blocks as i32), + F::U64(c.slots[2]), + ]; + push(v, 20, 0, [blocks as u32, 1, 1], [128, 1, 1], 0, args) + } + fn push_coupled_merge(&self, v: &mut Vec, c: &Coupled, step: u64) -> Result<()> { + let w = &self.inner.weights; + let blocks = w.vocab.div_ceil(4096); + let kpart = w.vocab.min(64); + let args = vec![ + F::U64(c.slots[2]), + F::I32(blocks as i32), + F::I32(w.vocab as i32), + F::I32(kpart as i32), + F::U64(c.slots[0]), + F::U64(step), + F::U64(c.sub), + F::U64(c.slots[1]), + F::I32(4096), + F::I32(c.j as i32), + F::U64(c.slots[3]), + F::U64(c.slots[4]), + ]; + push(v, 20, 3, [1, 1, 1], [32, 1, 1], 0, args) + } + fn plan_greedy(&self, v: &mut Vec) -> Result<()> { + let o = &self.inner; + let mut greedy = SamplerParams::default(); + greedy.greedy = true; + greedy.temperature = 0.0; + let chain_head = crate::original_verify::chain::flags().head; + let logits = o.logits()?; + let [top, prob] = self.slots([34, 35])?; + let vocab = F::I32(o.weights.vocab as i32); + let (asset, index) = if chain_head { (symbols::CHAIN_ASSET, symbols::CHAIN_SAMPLER_GREEDY) } else { (15, 0) }; + let sample = + vec![F::U64(logits), vocab, F::U64(0), F::I32(0), F::Params(greedy), F::I32(0), F::I32(0), F::U64(top)]; + push(v, asset, index, [1, 1, 1], [1024, 1, 1], 0, sample)?; + let (asset, index) = if chain_head { (symbols::CHAIN_ASSET, symbols::CHAIN_ROW_TOP_PROB) } else { (12, 0) }; + let rows = vec![F::U64(logits), F::I32(o.weights.vocab as i32), F::U64(top), F::U64(prob)]; + push(v, asset, index, [1, 1, 1], [1024, 1, 1], 0, rows)?; + self.push_subset_map(v, top) + } + fn push_subset_map(&self, v: &mut Vec, top: u64) -> Result<()> { + let Some((forward, _)) = &self.inner.weights.subset else { + return Ok(()); + }; + let forward = forward.handle.ok_or(Error::ContextPoisoned)?.qwen_address(); + push(v, 12, 1, [1, 1, 1], [64, 1, 1], 0, vec![F::U64(top), F::U64(forward), F::I32(1)]) + } +}