From c9845eed52b61ed35235da66066abbc3ca2de172 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Fri, 16 Oct 2026 12:49:53 +0200 Subject: [PATCH] fix(experts): split bank copies at page-locked boundaries; Original bench without the subcommand With part of a file-mapped bank page-locked (`pin_resident`), an H2D copy that starts in registered pages and runs into unregistered ones is refused by the driver (CUDA_ERROR_INVALID_VALUE, first seen on the laptop run). `MappedExpertBank::copy_pieces` cuts a `cache_source` slice at every registered boundary and the four copy sites (prompt copy ring, cache fill, swap fill, PCIe staging) issue one copy per piece; one piece when nothing is locked, so the default path is unchanged. original_bench.sh: the `strata` binary takes the generate options directly. Co-Authored-By: Claude Opus 5.5 --- crates/cuda-driver/src/mapped_expert_bank.rs | 20 +++++++++++++++++ crates/cuda-driver/src/mmq_original_ring.rs | 9 +++++++- .../cuda-driver/src/original_expert_cache.rs | 22 +++++++++++-------- .../src/original_verify_gpu_plan.rs | 4 +++- scripts/laptop/original_bench.sh | 2 +- 5 files changed, 45 insertions(+), 12 deletions(-) diff --git a/crates/cuda-driver/src/mapped_expert_bank.rs b/crates/cuda-driver/src/mapped_expert_bank.rs index 11edb2a1..53b7f48b 100644 --- a/crates/cuda-driver/src/mapped_expert_bank.rs +++ b/crates/cuda-driver/src/mapped_expert_bank.rs @@ -314,6 +314,26 @@ impl<'ctx> MappedExpertBank<'ctx> { a >= lo && b < hi }) } + /// `bytes` (a `cache_source` slice) cut at every boundary of a `pin_resident` range, as (offset, length) + /// pieces: one H2D copy must not start in registered pages and run into unregistered ones (the driver + /// refuses it with CUDA_ERROR_INVALID_VALUE), so copy sites issue one copy per piece. One piece when nothing + /// is page-locked. + pub(crate) fn copy_pieces(&self, bytes: &[u8]) -> Vec<(usize, usize)> { + let resident = self.resident.borrow(); + let start = bytes.as_ptr() as usize; + let end = start + bytes.len(); + let mut cuts = vec![start, end]; + for &(a, n) in resident.iter() { + for c in [a, a + n] { + if c > start && c < end { + cuts.push(c); + } + } + } + cuts.sort_unstable(); + cuts.dedup(); + cuts.windows(2).map(|w| (w[0] - start, w[1] - w[0])).collect() + } /// Bytes page-locked by `pin_resident`. pub fn resident_bytes(&self) -> u64 { self.resident.borrow().iter().map(|&(_, n)| n as u64).sum() diff --git a/crates/cuda-driver/src/mmq_original_ring.rs b/crates/cuda-driver/src/mmq_original_ring.rs index 322c850b..a7871b94 100644 --- a/crates/cuda-driver/src/mmq_original_ring.rs +++ b/crates/cuda-driver/src/mmq_original_ring.rs @@ -474,7 +474,14 @@ impl<'read, 'ctx> Ring<'read, 'ctx> { // Physical role-major bank layout requires three H2Ds. Same source // bytes and literal blob G/U/D layout; no decode/conversion/copy tmp. for (destination, source) in destinations.into_iter().zip(sources) { - api.copy_registered(destination, source, stream)?; + // one copy per registered/unregistered piece (`MappedExpertBank::copy_pieces`) + for (offset, bytes) in bank.copy_pieces(source) { + api.copy_registered( + destination.checked_subspan(source.len(), offset, bytes)?, + &source[offset..offset + bytes], + stream, + )?; + } } Ok(()) })?; diff --git a/crates/cuda-driver/src/original_expert_cache.rs b/crates/cuda-driver/src/original_expert_cache.rs index 7cf2f3ff..71b26ca3 100644 --- a/crates/cuda-driver/src/original_expert_cache.rs +++ b/crates/cuda-driver/src/original_expert_cache.rs @@ -313,8 +313,10 @@ impl<'ctx> OriginalExpertCache<'ctx> { let mut offset = 0usize; for span in spans { let source = bank.cache_source(span)?; - let destination = handle.checked_subspan(capacity, offset, source.len())?; - api.copy(destination.qwen_address(), source, stream)?; + for (at, bytes) in bank.copy_pieces(source) { + let destination = handle.checked_subspan(capacity, offset + at, bytes)?; + api.copy(destination.qwen_address(), &source[at..at + bytes], stream)?; + } offset += source.len(); } Ok(()) @@ -523,13 +525,15 @@ impl<'ctx> OriginalExpertCache<'ctx> { let mut offset = 0usize; for span in spans { let source = bank.cache_source(span)?; - let destination = handle.checked_subspan(capacity, offset, source.len())?; - self.attempted = true; - self.api.as_ref().ok_or(Error::ContextPoisoned)?.copy( - destination.qwen_address(), - source, - api::Stream(0), - )?; + for (at, bytes) in bank.copy_pieces(source) { + let destination = handle.checked_subspan(capacity, offset + at, bytes)?; + self.attempted = true; + self.api.as_ref().ok_or(Error::ContextPoisoned)?.copy( + destination.qwen_address(), + &source[at..at + bytes], + api::Stream(0), + )?; + } offset += source.len(); } Ok(()) diff --git a/crates/cuda-driver/src/original_verify_gpu_plan.rs b/crates/cuda-driver/src/original_verify_gpu_plan.rs index 57680a58..ac538157 100644 --- a/crates/cuda-driver/src/original_verify_gpu_plan.rs +++ b/crates/cuda-driver/src/original_verify_gpu_plan.rs @@ -244,7 +244,9 @@ impl<'ctx> schedule::OriginalVerifyCpuSink<'_, 'ctx> { let mut offset = staging + (q * stride) as u64; for part in s { let bytes = source.bank.cache_source(part)?; - copy.enqueue_copy_h2d(offset, bytes)?; + for (at, n) in source.bank.copy_pieces(bytes) { + copy.enqueue_copy_h2d(offset + at as u64, &bytes[at..at + n])?; + } offset += bytes.len() as u64; } } diff --git a/scripts/laptop/original_bench.sh b/scripts/laptop/original_bench.sh index 96ec36bd..39c20a01 100644 --- a/scripts/laptop/original_bench.sh +++ b/scripts/laptop/original_bench.sh @@ -17,7 +17,7 @@ MAX_NEW="${MAX_NEW:-128}"; REPEAT="${REPEAT:-2}"; MAX_CONTEXT="${MAX_CONTEXT:-81 mkdir -p "$OUT" shard1="$MODEL_DIR/Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00001-of-00002.gguf" shard2="$MODEL_DIR/Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00002-of-00002.gguf" -args=(generate --serve --pack "$PACK" --native "$shard1" --ple-gguf "$shard2" --expert-profile "$PROFILE" +args=(--serve --pack "$PACK" --native "$shard1" --ple-gguf "$shard2" --expert-profile "$PROFILE" --expert-cache auto --prefill auto --spec 4 --spec-min-p 0.5 --mtp "$MTP" --max-context "$MAX_CONTEXT" --kv int8 --prompt-cache 0 "$@") echo "strata ${args[*]}" > "$OUT/$label.cmd"