STRA-338 params h04: dense weight cache (PLE matrix expectation, QSA selection request) takes grouped operands

This commit is contained in:
Pavlo Ratushnyi
2026-11-08 01:26:24 +01:00
parent f2aa4093d0
commit 32ceea0fd9
5 changed files with 29 additions and 12 deletions
@@ -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<GdnWeights>,
@@ -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<usize> {
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<usize> {
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())?,
@@ -135,9 +135,7 @@ impl<'ctx> DenseWeightCache<'ctx> {
&self,
binding: &ModelBinding,
context: &Context<'_>,
candidate: &QsaPrior,
iq: &[f32],
position: u32,
request: QsaSelectionRequest<'_>,
) -> Result<Box<[i32]>> {
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,
@@ -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;
@@ -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)?
};