From 32a4aef83cdd9a80a7ea3a9b52e02fb9751eb59d Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Wed, 14 Oct 2026 16:37:02 +0200 Subject: [PATCH] fix(mtp): feed full-forward the MLP hyper-connection, shared-expert gate/up/down and final mixer roles in driver order full_forward_weights handed OriginalMtpRetainedFullWeights::retain the roles in the wrong order: slots 3..6 (read by mlp_gr as the MLP hyper-connection: norm, down, up, inject) got the ATTENTION hyper-connection tensors, slots 12..14 (final mixer norm/down/up) got the MLP hyper-connection tensors, and slots 8..10 (shared expert gate, up, down per record_forward) got down, gate, up. The driver only checks byte extents and allows identical aliases, and these tensors have equal sizes, so the permutation passed every check and would run silently with the wrong weights: garbage drafts (zero acceptance, only speed lost, never correctness). mlp_hyper_connection inject and hyper_connection_mixer tensors were never used at all. Pin the order in one table (FULL_FORWARD_ROLES) with a name-level test against mtp.cpp record_forward. Co-Authored-By: Claude Sonnet 5.5 --- .../cache_original_mtp_weights.rs | 66 ++++++++++++++----- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/crates/leleka-runtime/src/qwen_forward/cache_original_mtp_weights.rs b/crates/leleka-runtime/src/qwen_forward/cache_original_mtp_weights.rs index 6a996ffd..00789bd4 100644 --- a/crates/leleka-runtime/src/qwen_forward/cache_original_mtp_weights.rs +++ b/crates/leleka-runtime/src/qwen_forward/cache_original_mtp_weights.rs @@ -149,23 +149,10 @@ impl<'ctx> RetainedOriginalMtpWeights<'ctx>{ if (kind,rows,columns)!=expected{return Err(Error::InvalidDimension);} Ok(owner) }; - let roles=[ - acquire(MtpRole::QueryProjection,(MtpKind::Q8_0,12288,2560))?, - acquire(MtpRole::QueryNorm,(MtpKind::F32,1,256))?, - acquire(MtpRole::AttentionOutput,(MtpKind::Q8_0,2560,6144))?, - acquire(MtpRole::AttentionNorm,(MtpKind::F32,1,10240))?, - acquire(MtpRole::AttentionDown,(MtpKind::Bf16,320,10240))?, - acquire(MtpRole::AttentionUp,(MtpKind::Bf16,10240,320))?, - acquire(MtpRole::AttentionInject,(MtpKind::Bf16,4,10240))?, - acquire(MtpRole::Router,(MtpKind::Bf16,512,2560))?, - acquire(MtpRole::SharedDown,(MtpKind::Q8_0,2560,640))?, - acquire(MtpRole::SharedGate,(MtpKind::Q8_0,640,2560))?, - acquire(MtpRole::SharedUp,(MtpKind::Q8_0,640,2560))?, - acquire(MtpRole::SharedScalar,(MtpKind::Bf16,1,2560))?, - acquire(MtpRole::FfnNorm,(MtpKind::F32,1,10240))?, - acquire(MtpRole::FfnDown,(MtpKind::Bf16,320,10240))?, - acquire(MtpRole::FfnUp,(MtpKind::Bf16,10240,320))?, - ]; + let roles=FULL_FORWARD_ROLES.map(|(role,expected)|acquire(role,expected)); + let mut acquired=Vec::new();acquired.try_reserve_exact(roles.len()).map_err(|_|Error::InputAllocation)?; + for role in roles{acquired.push(role?);} + let roles:[Rc>;15]=acquired.try_into().map_err(|_|Error::InvalidDimension)?; let experts=self.expert_bank(binding,context)?; // Literal bind: with a draft_vocab subset the draft head is the gathered // rows (`dhead_`, `n_dvocab_` of them), else the native head itself. @@ -185,6 +172,32 @@ impl<'ctx> RetainedOriginalMtpWeights<'ctx>{ } } +/// Role order of `OriginalMtpRetainedFullWeights::retain` (driver `FULL_EXTENTS`; +/// consumed by `plan`/`mlp_gr`/`final_gr`), i.e. mtp.cpp `record_forward(full)`: +/// 0 q_proj, 1 q_norm, 2 o_proj, 3..6 the MLP hyper-connection (hc_norm, down, +/// up, block_inject), 7 router, 8 shared gate, 9 shared up, 10 shared down, +/// 11 shared-expert gate scalar, 12..14 the final mixer (hc_norm, down, up). +/// The attention hyper-connection is the prefix's. Several of these have equal +/// byte extents (gate/up/down, the two hyper-connections), so the driver's +/// extent and alias checks cannot catch a permutation: the order is pinned here. +const FULL_FORWARD_ROLES:[(MtpRole,(MtpKind,usize,usize));15]=[ + (MtpRole::QueryProjection,(MtpKind::Q8_0,12288,2560)), + (MtpRole::QueryNorm,(MtpKind::F32,1,256)), + (MtpRole::AttentionOutput,(MtpKind::Q8_0,2560,6144)), + (MtpRole::FfnNorm,(MtpKind::F32,1,10240)), + (MtpRole::FfnDown,(MtpKind::Bf16,320,10240)), + (MtpRole::FfnUp,(MtpKind::Bf16,10240,320)), + (MtpRole::FfnInject,(MtpKind::Bf16,4,10240)), + (MtpRole::Router,(MtpKind::Bf16,512,2560)), + (MtpRole::SharedGate,(MtpKind::Q8_0,640,2560)), + (MtpRole::SharedUp,(MtpKind::Q8_0,640,2560)), + (MtpRole::SharedDown,(MtpKind::Q8_0,2560,640)), + (MtpRole::SharedScalar,(MtpKind::Bf16,1,2560)), + (MtpRole::FinalNorm,(MtpKind::F32,1,10240)), + (MtpRole::FinalDown,(MtpKind::Bf16,320,10240)), + (MtpRole::FinalUp,(MtpKind::Bf16,10240,320)), +]; + fn rt_kind(kind:MtpKind)->Result{ use leleka_cuda_driver::OriginalMtpTensorKind as K; match kind{MtpKind::Q8_0=>Ok(K::Q8_0),MtpKind::Bf16=>Ok(K::Bf16),MtpKind::F32=>Ok(K::F32),MtpKind::Q2_0=>Err(Error::InvalidDimension)} @@ -203,6 +216,25 @@ mod rt_contract_tests{ assert_eq!(rt_kind(MtpKind::Q2_0).err(),Some(Error::InvalidDimension)); } + /// mtp.cpp record_forward(full) tensor names, in the driver's role order. + #[test] + fn full_forward_role_order_matches_mtp_cpp_tensor_names(){ + let names:Vec<&str>=FULL_FORWARD_ROLES.iter().map(|(role,_)|rt_dense_name(*role).unwrap()).collect(); + assert_eq!(names,[ + "self_attn.q_proj.weight","self_attn.q_norm.weight","self_attn.o_proj.weight", + "mlp_hyper_connection.hc_norm.weight","mlp_hyper_connection.input_mix_weight_down.weight", + "mlp_hyper_connection.input_mix_weight_up.weight","mlp_hyper_connection.block_inject_weight.weight", + "mlp.gate.weight", + "mlp.shared_expert.gate_proj.weight","mlp.shared_expert.up_proj.weight","mlp.shared_expert.down_proj.weight", + "mlp.shared_expert_gate.weight", + "hyper_connection_mixer.hc_norm.weight","hyper_connection_mixer.input_mix_weight_down.weight", + "hyper_connection_mixer.input_mix_weight_up.weight", + ]); + for (role,expected) in FULL_FORWARD_ROLES{ + assert_eq!(rt_dense_shape(role),Some(expected)); + } + } + #[test] fn every_contract_role_has_a_unique_rt_name_kind_and_shape(){ let roles=rt_dense_roles();