diff --git a/crates/cuda-driver/src/lib.rs b/crates/cuda-driver/src/lib.rs index 100d009f..1da65cc7 100644 --- a/crates/cuda-driver/src/lib.rs +++ b/crates/cuda-driver/src/lib.rs @@ -3155,6 +3155,15 @@ pub use original_verify::{ target_os = "linux", target_pointer_width = "64" ))] +pub use original_verify::{ + OriginalVerifyGdnProjections, OriginalVerifyNativeMatrix, OriginalVerifyProjBinding, + OriginalVerifyQsaProjections, +}; +#[cfg(all( + feature = "cuda-driver", + target_os = "linux", + target_pointer_width = "64" +))] pub use original_verify::{ ExpertTier, HcHalf, OriginalVerifyExpertLayout, OriginalVerifyWindowResources, PlanLayout, Projection, QuantSource, Rows as OriginalVerifyRows, UNBOUND_PRODUCERS, WINDOW_LAYERS, diff --git a/crates/cuda-driver/src/original_verify.rs b/crates/cuda-driver/src/original_verify.rs index 8ba7bee7..77459b55 100644 --- a/crates/cuda-driver/src/original_verify.rs +++ b/crates/cuda-driver/src/original_verify.rs @@ -354,6 +354,7 @@ pub use schedule::{OriginalVerifier,OriginalVerifyWindowResult,OriginalVerifyCpu #[path="original_verify_window_ops.rs"] mod window_ops; pub use window_ops::{OriginalVerifyExpertLayout,OriginalVerifyWindowResources,UNBOUND_PRODUCERS}; pub use window_record::{ExpertTier,HcHalf,Projection,QuantSource,Rows,WindowOps,WindowPolicy,record_window}; +pub use proj::{OriginalVerifyGdnProjections,OriginalVerifyNativeMatrix,OriginalVerifyProjBinding,OriginalVerifyQsaProjections}; pub use window_plan::{PlanLayout,WINDOW_LAYERS}; impl OriginalVerifyWindow<'_>{ diff --git a/crates/leleka-runtime/src/qwen_forward/dense_weight_cache.rs b/crates/leleka-runtime/src/qwen_forward/dense_weight_cache.rs index 3fcf368a..4ef07e8d 100644 --- a/crates/leleka-runtime/src/qwen_forward/dense_weight_cache.rs +++ b/crates/leleka-runtime/src/qwen_forward/dense_weight_cache.rs @@ -16,6 +16,11 @@ use crate::model_binding::qwen4exp_input::{ use leleka_cuda_driver::GrBatchInjectWorkspace; #[cfg(all(target_os = "linux", target_arch = "x86_64"))] use leleka_cuda_driver::OriginalVerifyGdnWeights; +#[cfg(all(target_os = "linux", target_arch = "x86_64"))] +use leleka_cuda_driver::{ + OriginalVerifyGdnProjections, OriginalVerifyGeometry, OriginalVerifyNativeMatrix, + OriginalVerifyProjBinding, OriginalVerifyQsaProjections, +}; use leleka_cuda_driver::{Context, DeviceBuffer}; use std::{ cell::{Cell, RefCell, RefMut}, @@ -1234,6 +1239,71 @@ impl<'ctx> DenseWeightCache<'ctx> { } retained.try_into().map_err(|_| Error::InvalidDimension) } + /// The native-format (GGUF block) MMVQ projection matrices of the 36 GDN and 12 QSA + /// layers for the literal Verifier window: the cache's own allocations, borrowed. + /// A matrix the cache holds as BF16 has no native MMVQ owner and is refused. + #[cfg(all(target_os = "linux", target_arch = "x86_64"))] + pub(super) fn original_verify_projections<'a>( + &'a self, + binding: &ModelBinding, + geometry: &OriginalVerifyGeometry, + ) -> Result> { + self.check(binding)?; + let gdn_type = |kind: GdnMatrixKind| -> Result { + match kind { + GdnMatrixKind::Q8_0 => Ok(8), + GdnMatrixKind::Q4K => Ok(12), + GdnMatrixKind::Q5K => Ok(13), + GdnMatrixKind::Q6K => Ok(14), + GdnMatrixKind::Iq4Nl => Ok(20), + GdnMatrixKind::Iq4Xs => Ok(23), + GdnMatrixKind::Bf16 => Err(Error::Unsupported(super::contract::Stage::Gdn)), + } + }; + let qsa_type = |kind: QsaMatrixKind| -> Result { + match kind { + QsaMatrixKind::Q4K => Ok(12), + QsaMatrixKind::Q5K => Ok(13), + QsaMatrixKind::Q6K => Ok(14), + QsaMatrixKind::Iq4Xs => Ok(23), + QsaMatrixKind::Bf16 => Err(Error::Unsupported(super::contract::Stage::QsaAttention)), + } + }; + let mut gdn = Vec::new(); + let mut qsa = Vec::new(); + gdn.try_reserve_exact(36).map_err(|_| Error::InputAllocation)?; + qsa.try_reserve_exact(12).map_err(|_| Error::InputAllocation)?; + for layer in binding.ordered_layers() { + match layer.kind() { + BoundLayerKind::Gdn => { + let resident = self.gdn(binding, layer.index())?; + let mut matrix = |role| -> Result> { + let (buffer, kind) = self.gdn_matrix(resident, role)?; + Ok(OriginalVerifyNativeMatrix { kind: gdn_type(kind)?, buffer }) + }; + gdn.push(OriginalVerifyGdnProjections { + qkv: matrix(GdnMatrixRole::Qkv)?, + gate: matrix(GdnMatrixRole::Z)?, + out: matrix(GdnMatrixRole::Out)?, + }); + } + BoundLayerKind::Qsa => { + let resident = self.qsa(binding, layer.index())?; + let mut matrix = |role| -> Result> { + let (buffer, kind) = self.qsa_matrix(resident, role)?; + Ok(OriginalVerifyNativeMatrix { kind: qsa_type(kind)?, buffer }) + }; + qsa.push(OriginalVerifyQsaProjections { + k: matrix(QsaMatrixRole::K)?, + v: matrix(QsaMatrixRole::V)?, + q: matrix(QsaMatrixRole::Q)?, + out: matrix(QsaMatrixRole::Out)?, + }); + } + } + } + OriginalVerifyProjBinding::new(self.context, geometry, gdn, qsa).map_err(driver_error) + } pub(super) fn qsa_matrix( &self, entry: &QsaResident,