From 32ceea0fd9eadca0f008d8471373dc401212c4e0 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Sun, 8 Nov 2026 01:26:24 +0100 Subject: [PATCH] STRA-338 params h04: dense weight cache (PLE matrix expectation, QSA selection request) takes grouped operands --- .../src/qwen_forward/dense_weight_cache.rs | 7 +++++++ .../impl_denseweightcache_4.rs | 20 +++++++++++++------ .../impl_denseweightcache_5.rs | 6 ++---- .../src/qwen_forward/qsa_index.rs | 2 +- .../qsa_index/impl_qsaindexstage.rs | 6 +++++- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/dense_weight_cache.rs b/crates/engine-qwen35-cuda/src/qwen_forward/dense_weight_cache.rs index cd625884..73f571c8 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/dense_weight_cache.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/dense_weight_cache.rs @@ -38,6 +38,13 @@ pub(crate) struct DenseCacheCounters { pub read_bytes: u64, pub upload_bytes: u64, } +/// What a resident-key selection scores: the candidate prior whose keys are searched, the index query vector and the +/// token position being selected for. +pub(super) struct QsaSelectionRequest<'a> { + pub(super) candidate: &'a QsaPrior, + pub(super) index_query: &'a [f32], + pub(super) position: u32, +} pub(super) struct GdnResident { owner: Rc<()>, pub weights: Rc, diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/dense_weight_cache/impl_denseweightcache/impl_denseweightcache_4.rs b/crates/engine-qwen35-cuda/src/qwen_forward/dense_weight_cache/impl_denseweightcache/impl_denseweightcache_4.rs index d0ff090a..037de0d0 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/dense_weight_cache/impl_denseweightcache/impl_denseweightcache_4.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/dense_weight_cache/impl_denseweightcache/impl_denseweightcache_4.rs @@ -3,6 +3,13 @@ #[allow(unused_imports)] use super::*; +/// The role, row count and byte size a retained PLE matrix must have; any other matrix is refused. +struct ExpectedPleMatrix { + role: PleMatrixRole, + rows: usize, + size_bytes: usize, +} + impl<'ctx> DenseWeightCache<'ctx> { pub(in super::super) fn add(&mut self, bytes: &[u8], source_bytes: usize) -> Result { if self @@ -83,12 +90,11 @@ impl<'ctx> DenseWeightCache<'ctx> { &mut self, binding: &ModelBinding, weights: &PleWeights, - role: PleMatrixRole, - rows: usize, - bytes: usize, + expected: ExpectedPleMatrix, ) -> Result { + let ExpectedPleMatrix { role, rows, size_bytes } = expected; let matrix = binding.ple_matrix_full(weights, role).map_err(binding_error)?; - if matrix.rows() != rows || matrix.bytes().len() != bytes { + if matrix.rows() != rows || matrix.bytes().len() != size_bytes { return Err(Error::InvalidDimension); } let index = self.add(matrix.bytes(), matrix.bytes().len())?; @@ -99,9 +105,11 @@ impl<'ctx> DenseWeightCache<'ctx> { let weights = Rc::new(binding.ple_weights().map_err(binding_error)?); // Key retains one complete immutable matrix with the same total byte // charge as its former 160 tiles; per-row projection math is unchanged. - let key = self.add_ple_matrix(binding, &weights, PleMatrixRole::Key, 10240, 52_428_800)?; + let key_matrix = ExpectedPleMatrix { role: PleMatrixRole::Key, rows: 10240, size_bytes: 52_428_800 }; + let key = self.add_ple_matrix(binding, &weights, key_matrix)?; // Value retains one whole immutable matrix; forty CTAs keep the original row arithmetic. - let value = self.add_ple_matrix(binding, &weights, PleMatrixRole::Value, 2560, 2560 * 2560 * 2)?; + let value_matrix = ExpectedPleMatrix { role: PleMatrixRole::Value, rows: 2560, size_bytes: 2560 * 2560 * 2 }; + let value = self.add_ple_matrix(binding, &weights, value_matrix)?; let conv = Self::ple_conv_bytes(&weights)?; let aux = [ self.add_f32(weights.norm_key())?, diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/dense_weight_cache/impl_denseweightcache/impl_denseweightcache_5.rs b/crates/engine-qwen35-cuda/src/qwen_forward/dense_weight_cache/impl_denseweightcache/impl_denseweightcache_5.rs index 333a37e5..df58e912 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/dense_weight_cache/impl_denseweightcache/impl_denseweightcache_5.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/dense_weight_cache/impl_denseweightcache/impl_denseweightcache_5.rs @@ -135,9 +135,7 @@ impl<'ctx> DenseWeightCache<'ctx> { &self, binding: &ModelBinding, context: &Context<'_>, - candidate: &QsaPrior, - iq: &[f32], - position: u32, + request: QsaSelectionRequest<'_>, ) -> Result> { self.retained_context(binding, context)?; self.workspaces @@ -146,7 +144,7 @@ impl<'ctx> DenseWeightCache<'ctx> { .ok_or(Error::IdentityMismatch)? .try_borrow_mut() .map_err(|_| Error::IdentityMismatch)? - .select(&self.binding, context, candidate, iq, position) + .select(&self.binding, context, request.candidate, request.index_query, request.position) } pub(in super::super::super) fn gdn_input<'slot, 'source>( &'slot self, diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/qsa_index.rs b/crates/engine-qwen35-cuda/src/qwen_forward/qsa_index.rs index 07cfcfc8..2d316150 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/qsa_index.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/qsa_index.rs @@ -1,7 +1,7 @@ //! Artifact-bound QSA preparation. Immutable candidates publish only at token commit. #![allow(dead_code)] use super::contract::{Error, LayerId, Position, Result, Stage, TokenLayer}; -use super::dense_weight_cache::DenseWeightCache; +use super::dense_weight_cache::{DenseWeightCache, QsaSelectionRequest}; use super::gr::ResidualReady; use super::qsa_attention::QsaGated; use super::session::QwenStepTxn; diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/qsa_index/impl_qsaindexstage.rs b/crates/engine-qwen35-cuda/src/qwen_forward/qsa_index/impl_qsaindexstage.rs index 27ca65ed..bc3750c4 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/qsa_index/impl_qsaindexstage.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/qsa_index/impl_qsaindexstage.rs @@ -232,7 +232,11 @@ impl QsaPrep<'_, '_, '_> { #[cfg(all(test, any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))] let cache = if combined_qsa_observer::bypass() { None } else { cache }; let ids = if let Some(cache) = cache { - cache.qsa_select(self.binding, self.context, candidate, iq, self.p)? + cache.qsa_select( + self.binding, + self.context, + QsaSelectionRequest { candidate, index_query: iq, position: self.p }, + )? } else { select(self.context, &candidate.score_keys()?, &candidate.0.dead, iq, self.p)? };