From d9313a95d37fc978e27f21169a7e075613402d42 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Fri, 16 Oct 2026 16:27:26 +0200 Subject: [PATCH] test(driver): device proof of the PCIe span copy (staging layout and ptr2 rebase) Root-only (LELEKA_VERIFY_WINDOW_GPU=yes): verify_fetch_spans_kernel and verify_rebase_ptrs_kernel with pcie_stage's launch geometry stage five experts' gate/up/down spans into staging slots exactly as the DMA path lays them out and point ptr2[q] at each slot, leaving later table entries alone. Co-Authored-By: Claude Opus 5.5 --- .../src/original_verify_window_gpu.rs | 84 +++++++++++++++++++ docs/original-serve.md | 12 ++- 2 files changed, 93 insertions(+), 3 deletions(-) diff --git a/crates/cuda-driver/src/original_verify_window_gpu.rs b/crates/cuda-driver/src/original_verify_window_gpu.rs index 3cd00957..fbaa89cc 100644 --- a/crates/cuda-driver/src/original_verify_window_gpu.rs +++ b/crates/cuda-driver/src/original_verify_window_gpu.rs @@ -35,3 +35,87 @@ fn sealed_window_assets_load_on_the_device_and_export_every_named_entry() { assert_eq!(bank.physical.window.borrow().len(), loaded.len()); bank.close().expect("close"); } + +/// The PCIe span copy of pcie mode 2 (`verify_fetch_spans_kernel` + `verify_rebase_ptrs_kernel`, the launches of +/// `pcie_stage`) on the device: three spans per expert land in staging slot q as gate|up|down, byte for byte the DMA +/// layout of `publish_gpu_plan` (`staging + q * stride`, the spans in role order), and `ptr2[q]` points at the slot. +/// The sources here are device buffers (the kernel reads through whatever address the plan names; the window reads +/// registered host aliases). +#[test] +#[ignore = "Root-only: needs a CUDA device"] +fn the_pcie_span_copy_stages_gate_up_down_like_the_dma_path() { + use super::abi::{Arg, Command, Module}; + assert_eq!(std::env::var(ENABLE).ok().as_deref(), Some("yes")); + let driver = Driver::load().expect("driver"); + let device = driver.device(0).expect("device"); + let context = device.create_context(1 << 30).expect("context"); + let roles = [1_126_400usize, 1_126_400, 655_360]; + let stride: usize = roles.iter().sum(); + let fetches = 5usize; + let mut sources = Vec::new(); + let mut expected = vec![0u8; fetches * stride]; + for q in 0..fetches { + let mut at = q * stride; + for (r, &n) in roles.iter().enumerate() { + let bytes: Vec = (0..n).map(|i| ((i * 31 + q * 7 + r * 101) % 251) as u8).collect(); + expected[at..at + n].copy_from_slice(&bytes); + at += n; + let mut buffer = context.allocate(n).expect("source"); + buffer.copy_from(&bytes).expect("upload"); + sources.push(buffer); + } + } + let address = |b: &DeviceBuffer<'_>| b.handle.expect("handle").qwen_address(); + // ptr2 holds 3 * fetches aliases (a 60-entry table like a max_t = 6 window's plan block) + let mut table = context.allocate(60 * 8).expect("table"); + let mut words = vec![0u8; 60 * 8]; + for (i, s) in sources.iter().enumerate() { + words[i * 8..i * 8 + 8].copy_from_slice(&address(s).to_le_bytes()); + } + table.copy_from(&words).expect("table upload"); + let mut count = context.allocate(4).expect("count"); + count.copy_from(&(fetches as i32).to_le_bytes()).expect("count upload"); + let staging = context.allocate(16 * stride).expect("staging"); + let guard = context.enter().expect("enter"); + let pcie = Module::load(moe::PCIE.sealed().expect("sealed"), moe::PCIE.entries).expect("pcie module"); + let verify = gather_rows_module().expect("verify module"); + let mut copy = Command::new( + "verify_fetch_spans_kernel", + [48 * 8, 1, 1], + [256, 1, 1], + 0, + vec![ + Arg::Address(address(&table)), + Arg::Address(address(&count)), + Arg::Address(address(&staging)), + Arg::I64((roles[0] / 16) as i64), + Arg::I64((roles[1] / 16) as i64), + Arg::I64((roles[2] / 16) as i64), + ], + ) + .expect("command"); + pcie.launch(0, &mut copy).expect("launch copy"); + let mut rebase = Command::new( + "verify_rebase_ptrs_kernel", + [1, 1, 1], + [128, 1, 1], + 0, + vec![Arg::Address(address(&table)), Arg::Address(address(&count)), Arg::U64(address(&staging)), Arg::I64(stride as i64)], + ) + .expect("command"); + verify.launch(0, &mut rebase).expect("launch rebase"); + drop((pcie, verify)); + guard.finish().expect("pop"); + context.synchronize().expect("sync"); + let staged = staging.copy_to_owned(16 * stride).expect("read staging"); + assert!(staged[..fetches * stride] == expected[..], "staging slots are gate|up|down per expert"); + let table_after = table.copy_to_owned(60 * 8).expect("read table"); + for q in 0..fetches { + let ptr = u64::from_le_bytes(table_after[q * 8..q * 8 + 8].try_into().unwrap()); + assert_eq!(ptr, address(&staging) + (q * stride) as u64, "ptr2[{q}] rebased to its slot"); + } + // entries past the rebased ones keep their aliases + let tail = u64::from_le_bytes(table_after[fetches * 8..fetches * 8 + 8].try_into().unwrap()); + assert_eq!(tail, address(&sources[fetches])); + eprintln!("pcie span copy: {fetches} experts x {stride} bytes staged bit-identical, ptr2 rebased"); +} diff --git a/docs/original-serve.md b/docs/original-serve.md index 0f1a76b1..f409258e 100644 --- a/docs/original-serve.md +++ b/docs/original-serve.md @@ -50,6 +50,7 @@ The load must be the explicit KAR PLE MTP load, as for the windowed256 modes. | `LELEKA_ORIGINAL_SERVE_EXPERT_PER_LAYER=yes` | `--expert-cache-per-layer` | global | | `LELEKA_ORIGINAL_SERVE_EXPERT_PROFILE=P` | `--expert-profile` | none | | `LELEKA_ORIGINAL_SERVE_PCIE_FRAC` | `--pcie-frac` | 0 (see below) | +| `LELEKA_ORIGINAL_SERVE_PCIE_MODE` = `auto` \| `dma` \| `kernel` | `--pcie-mode` | auto (the copy kernel when the bank has device aliases) | | `LELEKA_ORIGINAL_SERVE_ADAPT_EVERY` / `_ADAPT_SWAPS` | `--adapt-every` / `--adapt-swaps` | 4 / 96 | | `LELEKA_ORIGINAL_SERVE_VRAM_RESERVE_MIB` / `_DECODE_RESERVE_MIB` | `--vram-reserve-mib` / port only | 700 / 1024 (auto sizing) | | `LELEKA_ORIGINAL_SERVE_PREFILL_BORROW=no` | `--no-prefill-borrow` | borrow (with a profile) | @@ -60,9 +61,14 @@ What runs, literally after generate.cpp / expert_source.cpp: 3/4 retry), filled from the profile in rank order through the registered model bank (per-layer skips a full layer, global stops at the first refusal) and slot 0 is read back. * Each verify-window layer group is split on its own routed ids (`split_group`): resident experts are VRAM groups, - the last `nmiss * pcie_num >> 8` misses (at most the staging blobs, at most 64) are staged by DMA on the copy - stream (flag B raised by a host function after the copies), the rest go to the CPU pool, which computes only its - entries and zeroes the GPU's. The plan is published (flag A) before the CPU starts. Every VRAM pointer is a + the last `nmiss * pcie_num >> 8` misses (at most the staging blobs, at most 64) are staged over PCIe, the rest go + to the CPU pool, which computes only its entries and zeroes the GPU's. Staging (`--pcie-mode`): the copy kernel + (mode 2, generate.cpp's default) - the plan carries the device aliases of each PCIe expert's three registered spans + (`ptr2[3q+r]`), flag B rises at once, and the window graph copies them into staging slot `q` after wait B + (`verify_fetch_spans_kernel`, `original_verify_window_pcie.cu`: fetch_blobs over three spans, gate|up|down exactly as + the DMA wrote them) and rebases `ptr2[q]`; no host CUDA call sits inside the window. `dma` (mode 0) keeps the + per-span `cuMemcpyHtoDAsync` on the copy stream and the host-function flag B. The pageable low-RAM bank has no + aliases: `auto` takes DMA there. The plan is published (flag A) before the CPU starts. Every VRAM pointer is a fenced slot whose owner is checked against the doorbell's ids. * Adaptive tier: per (layer, expert) routed counts; every `adapt_every` rounds the most-routed misses (count >= 2) replace the least-routed residents of their layer while the gain is >= 1.5, best `adapt_swaps`, then counts