Verify GPU plan: flag B route (at once or behind the staging copies) as a named pure function with a truth-table test

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-11-09 23:00:52 +01:00
co-authored by Claude Sonnet 5.5
parent 91a130da6a
commit 05e9acdaa5
2 changed files with 30 additions and 5 deletions
@@ -219,15 +219,13 @@ impl<'ctx> schedule::OriginalVerifyCpuSink<'_, 'ctx> {
(kernel, staging, stride): (bool, u64, usize),
) -> Result<()> {
let flag_b = self.flag_b_host_address()?;
if kernel {
if !plan.pcie.is_empty() {
if flag_b_route(kernel, pcie.is_some()) == FlagBRoute::AtOnce {
if kernel && !plan.pcie.is_empty() {
self.bank.retain_pcie_source(Rc::clone(pcie.as_ref().ok_or(Error::InvalidQwenShape)?.bank))?;
}
return self.raise_flag_b();
}
let Some(source) = pcie else {
return self.raise_flag_b();
};
let source = pcie.ok_or(Error::InvalidQwenShape)?;
if !plan.pcie.is_empty() {
// The bank's registered bytes are read by the copy engine after this call returns: the bank
// stays retained by the verifier until the window's copy-stream fence.
@@ -266,6 +264,24 @@ impl<'ctx> schedule::OriginalVerifyCpuSink<'_, 'ctx> {
}
}
/// Where a group's flag B is raised from.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum FlagBRoute {
/// From the host at once.
AtOnce,
/// Queued on the copy stream behind the group's staging copies, so it lands only after them.
BehindStagingCopies,
}
/// The route of flag B for one group. Only a group staged by DMA waits for its copies; the copy kernel and a group
/// without a PCIe source raise at once. Flag B is one monotone ring word shared by the groups of a window, so a window
/// must not mix a staged group with an at-once group: the at-once raise of a later ring value would announce the
/// earlier group's copies as landed. The product never mixes them: whether a PCIe source exists is `pcie_num > 0` of
/// the round (`decode_parts.rs`), the same for every group of every window of that round.
pub(super) const fn flag_b_route(copy_kernel: bool, has_pcie_source: bool) -> FlagBRoute {
if copy_kernel || !has_pcie_source { FlagBRoute::AtOnce } else { FlagBRoute::BehindStagingCopies }
}
/// Enqueues the host-to-device copies of every PCIe expert's spans into its staging slot: one batched call for all
/// pieces (`many`), or one per piece.
fn stage_pcie<'ctx>(
@@ -71,3 +71,12 @@ fn a_corrupt_plan_is_refused_not_repaired() {
// more entries than the block holds
assert!(encode_plan_block(&layout, PlanEntryShape { cap: 40, k: 10, n_entries: 50 }, &[], &[]).is_err());
}
#[test]
fn flag_b_waits_for_the_staging_copies_only_for_a_dma_group_with_a_pcie_source() {
use plan_phases::{FlagBRoute::*, flag_b_route};
assert_eq!(flag_b_route(false, true), BehindStagingCopies, "DMA with a source: behind the copies");
assert_eq!(flag_b_route(false, false), AtOnce, "no PCIe source: at once");
assert_eq!(flag_b_route(true, true), AtOnce, "copy kernel: the graph stages, flag B at once");
assert_eq!(flag_b_route(true, false), AtOnce);
}