STRA-338 params q02: queue_span_copies takes SlotRegion, Api::issue takes StreamEvent, plan block encoders take PlanEntryShape

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-11-08 12:54:21 +01:00
co-authored by Claude Sonnet 5.5
parent 57be6ffebc
commit 740a4761ba
9 changed files with 48 additions and 23 deletions
@@ -221,7 +221,7 @@ impl<'ctx> OriginalExpertCache<'ctx> {
let RetainedFill::Mapped { bank, spans } = self.swap_sources.last().ok_or(Error::ContextPoisoned)? else {
return Err(Error::InvalidSize);
};
queue_span_copies(api, bank, spans, handle, capacity, stream)
queue_span_copies(api, bank, spans, SlotRegion { handle, capacity }, stream)
}
}
/// Where a swap moves a slot: the expert it holds now, the expert it receives, and the slot's bytes.
@@ -231,6 +231,12 @@ struct SwapPlan {
handle: ffi::BufferHandle,
capacity: usize,
}
/// The device bytes of one expert slot: its buffer handle and the capacity the three spans must fit.
#[derive(Clone, Copy)]
struct SlotRegion {
handle: ffi::BufferHandle,
capacity: usize,
}
/// The layer all three spans of one expert belong to.
pub(super) fn span_layer(spans: &[ResidentExpertBankSpan; 3]) -> Result<usize> {
match spans[0].role() {
@@ -259,10 +265,10 @@ fn queue_span_copies(
api: &api::AsyncApi,
bank: &MappedExpertBank<'_>,
spans: &[ResidentExpertBankSpan; 3],
handle: ffi::BufferHandle,
capacity: usize,
slot: SlotRegion,
stream: api::Stream,
) -> Result<()> {
let SlotRegion { handle, capacity } = slot;
let mut offset = 0usize;
for span in spans {
let source = bank.cache_source(span)?;
@@ -24,7 +24,9 @@ impl<'ctx> OriginalStagerBatch<'_, 'ctx> {
let event = self.stager.slots[slot].event;
let api = &self.stager.api;
let flight = self.stager.flight.as_mut().ok_or(Error::InvalidSize)?;
let result = flight.stream.with_enqueue(|stream| api.issue(target, host, raw.bytes(), stream, event));
let result = flight
.stream
.with_enqueue(|stream| api.issue(target, host, raw.bytes(), abi::StreamEvent { stream, event }));
if result.is_err() {
self.stager.shared.failed.store(true, Ordering::Release);
self.stager.context.poison();
@@ -44,6 +44,12 @@ pub(crate) struct WorkerSlot {
bytes: usize,
pub(crate) event: usize,
}
/// A stream handle and the event recorded on it right after the copy it carries.
#[derive(Clone, Copy)]
pub(crate) struct StreamEvent {
pub(crate) stream: usize,
pub(crate) event: usize,
}
#[derive(Clone, Copy)]
pub(crate) struct CopyJob {
source: [(usize, usize); 3],
@@ -185,9 +185,9 @@ impl Api {
destination: BufferHandle,
source: &HostBuffer,
bytes: usize,
stream: usize,
event: usize,
completion: StreamEvent,
) -> Result<()> {
let StreamEvent { stream, event } = completion;
destination.validate_extent(bytes)?;
if bytes == 0 || bytes > source.bytes || source.address == 0 {
return Err(Error::InvalidSize);
@@ -55,9 +55,9 @@ mod tests;
#[path = "original_verify_gpu_plan/impl_originalverifygpuplan.rs"]
mod impl_originalverifygpuplan;
#[allow(unused_imports)]
pub(crate) use impl_originalverifygpuplan::encode_plan_block;
#[allow(unused_imports)]
use impl_originalverifygpuplan::span_layer;
#[allow(unused_imports)]
pub(crate) use impl_originalverifygpuplan::{PlanEntryShape, encode_plan_block};
#[path = "original_verify_gpu_plan/impl_schedule_originalverifycpusink.rs"]
mod impl_schedule_originalverifycpusink;
#[path = "original_verify_gpu_plan/plan_phases.rs"]
@@ -28,18 +28,26 @@ impl OriginalVerifyGpuPlan {
}
}
/// The entry geometry of one group's plan block: the most entries the block holds (`cap`), the routed experts per row
/// (`k`) and the entries the group's rows route (`n_entries`).
#[derive(Clone, Copy, Debug)]
pub(crate) struct PlanEntryShape {
pub cap: usize,
pub k: usize,
pub n_entries: usize,
}
/// The literal `GpuPlanSink` writes of one group into a `layout.words` int32 block (little-endian bytes):
/// VRAM groups first (`ptr`, `start`), `start[groups] = entries`, then the PCIe groups (`ptr2`, `start2`),
/// `start2[fetches] = entries`, and the counts. `dst[e] = i`, `tok[e] = i / k`. Refuses an entry outside the
/// group's `n_entries`, a repeated entry, an empty group, and more groups or entries than the block holds.
pub(crate) fn encode_plan_block(
layout: &PlanLayout,
cap: usize,
k: usize,
n_entries: usize,
shape: PlanEntryShape,
vram: &[(u64, &[u16])],
pcie: &[(u64, &[u16])],
) -> Result<Vec<u8>> {
let PlanEntryShape { cap, k, n_entries } = shape;
if k == 0 || n_entries > cap || n_entries > 128 || vram.len() > cap || pcie.len() > cap.min(MAX_PCIE_FETCHES) {
return Err(Error::InvalidQwenShape);
}
@@ -49,7 +49,7 @@ impl<'ctx> schedule::OriginalVerifyCpuSink<'_, 'ctx> {
let staging = self.staging_base(plan)?;
let kernel = self.bank.pcie_mode == 2;
let aliases = self.kernel_aliases(plan, pcie.as_ref(), cap, kernel)?;
let block = self.build_plan_block(plan, &layout, cap, (&vram, &aliases), (kernel, staging, stride))?;
let block = self.build_plan_block(plan, &layout, (&vram, &aliases), (kernel, staging, stride))?;
self.publish_plan_block(&layout, &block)?;
self.raise_after_staging(plan, pcie, copy, (kernel, staging, stride))
}
@@ -164,18 +164,19 @@ impl<'ctx> schedule::OriginalVerifyCpuSink<'_, 'ctx> {
&self,
plan: &OriginalVerifyGpuPlan,
layout: &PlanLayout,
cap: usize,
(vram, aliases): (&[(u64, &[u16])], &[[u64; 3]]),
(kernel, staging, stride): (bool, u64, usize),
) -> Result<Vec<u8>> {
let k = self.bank.geometry().k;
let g = self.bank.geometry();
let (cap, k) = (g.max_t * g.k, g.k);
let pcie_ptrs: Vec<(u64, &[u16])> = plan
.pcie
.iter()
.enumerate()
.map(|(q, e)| (if kernel { aliases[q][0] } else { staging + (q * stride) as u64 }, e.as_slice()))
.collect();
let mut block = encode_plan_block(layout, cap, k, self.rows * k, vram, &pcie_ptrs)?;
let mut block =
encode_plan_block(layout, PlanEntryShape { cap, k, n_entries: self.rows * k }, vram, &pcie_ptrs)?;
for (q, a) in aliases.iter().enumerate() {
for (r, &alias) in a.iter().enumerate() {
let at = (layout.ptr2 + 2 * (3 * q + r)) * 4;
@@ -37,7 +37,7 @@ fn the_block_is_the_literal_gpu_plan_sink_carve() {
let layout = PlanLayout::new(40);
let vram: Vec<(u64, &[u16])> = vec![(0x1000, &[0, 13]), (0x2000, &[5])];
let pcie: Vec<(u64, &[u16])> = vec![(0x9000, &[2, 21, 27]), (0x9800, &[29])];
let w = words(&encode_plan_block(&layout, 40, 10, 30, &vram, &pcie).unwrap());
let w = words(&encode_plan_block(&layout, PlanEntryShape { cap: 40, k: 10, n_entries: 30 }, &vram, &pcie).unwrap());
assert_eq!(w.len(), layout.words);
assert_eq!(&w[..4], &[2, 7, 2, 0], "vram groups, gpu entries, pcie groups");
assert_eq!(&w[layout.start..layout.start + 3], &[0, 2, 3]);
@@ -52,7 +52,7 @@ fn the_block_is_the_literal_gpu_plan_sink_carve() {
#[test]
fn an_empty_plan_is_the_run_loops_empty_plan() {
let layout = PlanLayout::new(80);
let w = words(&encode_plan_block(&layout, 80, 10, 80, &[], &[]).unwrap());
let w = words(&encode_plan_block(&layout, PlanEntryShape { cap: 80, k: 10, n_entries: 80 }, &[], &[]).unwrap());
assert!(w.iter().all(|&x| x == 0));
}
@@ -61,11 +61,13 @@ fn a_corrupt_plan_is_refused_not_repaired() {
let layout = PlanLayout::new(40);
let one = |e: &'static [u16]| vec![(0x1000u64, e)];
// entry outside the group's rows, repeated within and across groups, empty group, null pointer
assert!(encode_plan_block(&layout, 40, 10, 20, &one(&[20]), &[]).is_err());
assert!(encode_plan_block(&layout, 40, 10, 20, &one(&[3, 3]), &[]).is_err());
assert!(encode_plan_block(&layout, 40, 10, 20, &one(&[3]), &one(&[3])).is_err());
assert!(encode_plan_block(&layout, 40, 10, 20, &one(&[]), &[]).is_err());
assert!(encode_plan_block(&layout, 40, 10, 20, &[(0, &[1])], &[]).is_err());
assert!(encode_plan_block(&layout, PlanEntryShape { cap: 40, k: 10, n_entries: 20 }, &one(&[20]), &[]).is_err());
assert!(encode_plan_block(&layout, PlanEntryShape { cap: 40, k: 10, n_entries: 20 }, &one(&[3, 3]), &[]).is_err());
assert!(
encode_plan_block(&layout, PlanEntryShape { cap: 40, k: 10, n_entries: 20 }, &one(&[3]), &one(&[3])).is_err()
);
assert!(encode_plan_block(&layout, PlanEntryShape { cap: 40, k: 10, n_entries: 20 }, &one(&[]), &[]).is_err());
assert!(encode_plan_block(&layout, PlanEntryShape { cap: 40, k: 10, n_entries: 20 }, &[(0, &[1])], &[]).is_err());
// more entries than the block holds
assert!(encode_plan_block(&layout, 40, 10, 50, &[], &[]).is_err());
assert!(encode_plan_block(&layout, PlanEntryShape { cap: 40, k: 10, n_entries: 50 }, &[], &[]).is_err());
}