diff --git a/crates/cuda-driver/src/original_pcie_copy_gpu_smoke.rs b/crates/cuda-driver/src/original_pcie_copy_gpu_smoke.rs index 6dd72e4b..e0151541 100644 --- a/crates/cuda-driver/src/original_pcie_copy_gpu_smoke.rs +++ b/crates/cuda-driver/src/original_pcie_copy_gpu_smoke.rs @@ -173,3 +173,63 @@ fn pcie_copy_gpu_raw_batch_page_locked() { fn pcie_copy_gpu_raw_batch_mixed() { run(true, Memory::Mixed); } + +/// Host-to-device throughput of the plain per-piece copy from pageable and from page-locked memory, with the piece sizes of an expert +/// (640 KiB) and of a bulk copy (32 MiB). Prints GB/s and the time one call keeps the calling thread: a pageable source is staged +/// by the driver inside the call, a page-locked one is queued and the call returns at once. No assertion on the numbers. +#[test] +#[ignore = "needs a CUDA device; prints a measurement"] +fn pcie_copy_gpu_bandwidth() { + let driver = Driver::load().unwrap(); + let ctx = driver.device(0).unwrap().create_context(256 << 20).unwrap(); + let dest = ctx.allocate(64 << 20).unwrap(); + let destination = dest.handle.expect("live buffer").qwen_address(); + let mut stream = ctx.original_gpu_stream(OriginalStreamKind::NonBlocking).unwrap(); + let size = 256usize << 20; + let mut heap = vec![0x5Au8; size + 2 * PAGE]; + let skew = heap.as_ptr().align_offset(PAGE); + let base = heap.as_mut_ptr() as usize + skew; + let source = &heap[skew..skew + size]; + let guard = ctx.enter().unwrap(); + let api = MappedApi::load().unwrap(); + guard.finish().unwrap(); + for locked in [false, true] { + if locked { + let guard = ctx.enter().unwrap(); + api.register_staging(base, size).unwrap(); + guard.finish().unwrap(); + } + for piece in [640usize << 10, 32 << 20] { + let copies = size / piece; + for pass in 0..3 { + let started = std::time::Instant::now(); + let mut in_calls = std::time::Duration::ZERO; + for i in 0..copies { + let t = std::time::Instant::now(); + stream.enqueue_copy_h2d(destination, &source[i * piece..(i + 1) * piece]).unwrap(); + in_calls += t.elapsed(); + } + stream.synchronize().unwrap(); + let total = started.elapsed().as_secs_f64(); + if pass > 0 { + println!( + "pcie copy bandwidth: {} source, {:>5} KiB pieces: {:.2} GB/s; the calls took {:.0}% of the time on the calling thread ({:.0} us per call)", + if locked { "page-locked" } else { "pageable " }, + piece >> 10, + size as f64 / total / 1e9, + 100.0 * in_calls.as_secs_f64() / total, + in_calls.as_secs_f64() * 1e6 / copies as f64 + ); + } + } + } + } + { + let guard = ctx.enter().unwrap(); + api.unregister_at(base).unwrap(); + guard.finish().unwrap(); + } + drop(stream); + drop(dest); + ctx.close().unwrap(); +} diff --git a/docs/windows.md b/docs/windows.md index 5ad01364..d3c259d6 100644 --- a/docs/windows.md +++ b/docs/windows.md @@ -528,3 +528,68 @@ The quality gate (teacher-forced top-16 log-probabilities, `scripts/quality/q_ta gate run matters more than the 5090's. On the 5090 (auto tier, 9e3 replay) the skip measured +15.2 % decode but FAILED the teacher-forced gate (p99 1.005 against a limit of 0.499), so a laptop speed number alone proves nothing: run its gate too, and keep the switch out of every profile until one passes. + +#### PCIe tier on the laptop (2026-10-27): the batch-copy fault, and whether PCIe beats the CPU + +Branch `claude/3080-pcie`, native engine, workstation preset, RTX 3080 Laptop (cc 8.6, 16 GiB, driver 617.42), `--context-tokens 262144`, model digest `sha256:80d5e333...66a3`, WSL off. On the preset the expert tier reads `vram 41-46% pcie 0 cpu 54-59%` because a pageable file-mapped bank takes no PCIe share unless `LELEKA_ORIGINAL_SERVE_PCIE_FRAC` is set (`pcie_num_for`). + +**The failure.** With a share set (0.45, 0.8, 1.0; 6 of 6 runs of the first A/B) every request died the same way: the prompt prefill finished (all 9 chunks of 17.9k tokens), the first decode window launched its graph, and the first `cuMemcpyHtoDAsync_v2` after it returned `CUDA_ERROR_ILLEGAL_ADDRESS` (700), then `class=CudaExecution` and a worker that is not ready. It is not a length effect: a 2,066-token prompt with 48 output tokens fails the same way, so the loop is one engine start (60 s) plus 20 s. The Windows System log has one `nvlddmkm` event **153** per failure (STRA-235's were id 13, "SM Warp Exception"); it carries no text. + +**Root cause, bisected in the engine and reproduced model-free.** + +* In the engine, `LELEKA_PCIE_DMA_BATCH=off` alone removes the failure (`MANY=off` and `FLAGB=host` are not needed); the reply hash of the 48-token request is the same with and without the other two switches. The window's DMA arm (`publish_gpu_plan`) gave all pieces of a group to one `cuMemcpyBatchAsync_v2`; `dma_options` turned that on everywhere except WSL2, and native Windows has no `/proc/sys/kernel/osrelease`, so it was on here. +* `crates/cuda-driver/src/original_pcie_copy_gpu_smoke.rs` (no model, 200 rounds, real piece sizes, every byte checked) on this laptop: + +| source memory | one call per piece | `cuMemcpyBatchAsync_v2` | +|---|---|---| +| pageable (heap) | exact | exact (stream-ordered access; also with the "during call" and "any" orders) | +| page-locked: `cuMemHostRegister` flags 0, READ_ONLY (8) or DEVICEMAP (2) | exact | **error 700** at the next synchronize (access order "during call": at the call itself) | +| half page-locked, half pageable | exact | **error 700** | + + The batch faults for a single 16-byte piece, and for every piece size, alignment, source access order and registration flag tried. So it is not a hazard of the engine (no stream, kernel, graph or flag is involved): it is the driver's batch entry with a page-locked host source on this machine. The older WSL2 note in `dma_options` ("the batch copy faulted in a microbenchmark on the RTX 3080 laptop") was the same bug. I cannot say why inside the driver. +* **Fix** (`7218a4f`): `dma_options_for(wsl, windows, env)` leaves the batch off by default on native Windows too (the stream write of flag B stays on: it ran in every engine run here); `LELEKA_PCIE_DMA_BATCH=on` still forces it. The tracer now also names `cuMemcpyBatchAsync_v2` (`batch-async copies N ...`). Tests: a unit test of the defaults; `pcie_copy_gpu_production_{pageable,page_locked,mixed}` pass; `pcie_copy_gpu_raw_batch_{page_locked,mixed}` FAIL on this laptop by design, they are the check to repeat on a new driver (`cargo test --release -p leleka-cuda-driver --features cuda-driver,sm86 --lib -- --ignored --nocapture pcie_copy_gpu_`, one case per process because a fault poisons the context). Not tried on the 5090 or on Linux, where the batch has measured +6 % with the registered bank (job 981); that path is unchanged. + +**Is this STRA-235?** No, as far as the evidence goes. STRA-235 is an SM warp exception inside a `mul_mat_q` kernel (event id 13, a repeating PC) during the prefill, with the PCIe share at 0, i.e. with no window copy and no batch call anywhere in the run; this one is a driver entry the prefill never calls, with a different event id. What this work adds to STRA-235 is negative: `mapped_file_dma_under_working_set_trimming_is_exact` (a 2 GiB read-only file mapping, the first GiB page-locked in place with `cuMemHostRegister(READ_ONLY)` as `pin_resident` does, random expert-sized copies out of the locked and the pageable halves, `EmptyWorkingSet` every 5 ms as the governor does, a thread churning the pages, every byte checked against the file's content function) found 0 wrong bytes in 205,235 copies (about 60 GB; half out of the locked pages, half out of pageable ones) over 5 minutes with 27,248 working-set trims. A page that was trimmed or reclaimed under a registration, which could have fed garbage weights to the MMQ, is therefore not what STRA-235 is, at least not in this form. The root cause of STRA-235 is still unknown. + +**Does PCIe beat the CPU here? Not with the pageable bank as it is.** Raw link (`pcie_copy_gpu_bandwidth`): a page-locked source copies at 11.6 GB/s (640 KiB pieces) to 12.4 GB/s (32 MiB) with the link at x8 gen4 (`nvidia-smi` reads width 8 of 16, also under load in every arm below), in 4 us per call without holding the calling thread; a pageable heap source copies at 8.1-8.9 GB/s but the call is the copy (79 us per 640 KiB piece, 100 % of the time on the calling thread). The window is driven by one thread, and the pageable bank's pieces come out of file-mapped pages that the governor's working-set trim keeps dropping, so the engine pays far more than the heap figure: with `LELEKA_SERVE_PROFILE=1` and share 0.45, one 200-token request spends **10.4-11.0 s in `pool.publish_plan`** (of 27-31 s of decode), against 0.0 s without a share, while the CPU pool's compute falls only from 7.6-8.2 s to 4.9-5.1 s. + +Decode tok/s of a 2,066-token prompt, 200 output tokens, temperature 0, the same request three times in one engine (request 1 is cold; requests 2 and 3 normally hit the prefix cache, the runs marked `*` lost it to memory pressure). One engine start per row, so read differences below about 10 % as noise: + +| share | 1 | 2 | 3 | PCIe / CPU share of the entries | +|---|---|---|---|---| +| unset (CPU only) | 6.46 | 9.08 | 8.80 | 0 / 55 % | +| 0.25 | 5.17 | 7.13 | 7.60 | 11 / 45 % | +| 0.45 | 4.49 | 6.92 | 7.50 | 20 / 34 % | +| 0.65 | 4.00 | 4.12* | 4.29* | 33 / 29 % | +| 0.8 | 3.59 | 5.90 | 6.33 | 36 / 21 % | +| 1.0 | 3.85 | 6.47 | 7.27 | 50 / 5 % | +| 0.45 + `LELEKA_PCIE_AUTO=on` | 4.57 | 7.51 | 7.51 | 16 / 39 % | + +No plain share beats the CPU. The pageable misses are the problem, so `LELEKA_PCIE_LOCKED_ONLY=on` (`d54f101`, off by default) lets only the experts that `pin_resident` locked (4,242 experts, 8 GiB, by profile rank) take the share; every other miss stays on the CPU pool. `pool.publish_plan` drops to 1.3-1.7 s per request. Same request, same method: + +| share + locked only | 1 | 2 | 3 | PCIe / CPU share | +|---|---|---|---|---| +| 0.3 | 6.88 | 9.62 | 9.07 | 10 / 44 % | +| 0.45 (two runs) | 6.79, 7.26 | 9.11, 7.95* | 10.02, 9.51 | 14-15 / 41-43 % | +| 0.6 | 6.54 | 9.48 | 9.79 | 15 / 41 % | +| 0.8 | 6.71 | 8.82 | 10.38 | 16 / 40 % | +| 1.0 | 6.91 | 7.76* | 8.01* | 17 / 44 % | +| unset, same build (two runs) | 6.75, 6.67 | 8.60, 7.50* | 8.50, 8.88 | 0 / 54-59 % | + +Once the share is set it hardly matters which value, because the eligible set (a bit over 15 % of the entries) is the cap. + +**Paired A/B on the agent-shaped workload** (`scripts/e2e/long_session.py`, 2 turns of one conversation, 17,907 then 46,787 input tokens with 16,384 (14,336 in two runs) reused from the prefix cache, 200 output tokens each; engine restarted per run after `wsl --shutdown`; 3 reps per arm in the rotated order A B C / B C A / C A B in three stretches of 31-34 minutes, a few minutes apart; GPU 55-64 C, no throttling, no P5 stall, no CUDA error in any of the 9 runs; build of `2a7e80a`'s code, arms differ only by the environment): + +| arm | environment | prefill tok/s, turn 1 / 2 | decode tok/s, turn 1 | decode tok/s, turn 2 | entries VRAM / PCIe / CPU | CPU utility during decode | PCIe width | +|---|---|---|---|---|---|---|---| +| A | none (the preset's default) | 105.3 / 108.9 | 6.73 (6.46-7.01) | 6.79 (6.58-7.19) | 42.2 / 0 / 57.8 % | 152 % | x8 | +| B | `PCIE_FRAC=0.45` | 106.3 / 109.7 | 5.02 (4.84-5.22) | 4.79 (4.67-4.96) | 43.7 / 20.4 / 36.0 % | 150 % | x8 | +| C | `PCIE_FRAC=0.6`, `LELEKA_PCIE_LOCKED_ONLY=on` | 106.0 / 107.9 | 7.51 (7.20-7.81) | 7.14 (7.05-7.19) | 42.8 / 22.0 / 35.2 % | 144 % | x8 | + +Means of 3 reps with the min-max in brackets. "CPU utility" is the Windows `Processor Information\% Processor Utility` counter (frequency-scaled, so above 100 %), from the 2 reps that had it. The GPU's NVML "utilisation" reads 87-100 % in every arm because the resident kernels poll, so it says nothing. Prefill does not depend on the share, as expected. + +B is 25-29 % slower than A in every pair. C is faster than A in turn 1 in every pair (7.20-7.81 against 6.46-7.01, +12 % on the means) and +5 % in turn 2, where the ranges overlap (7.05-7.19 against 6.58-7.19): +8 % on the means over both turns, a small gain that I would not call proven from 3 reps. The replies of this workload are the thinking model's reasoning (both turns end `incomplete` at 200 tokens with an empty visible text, so every `reply_sha256` is the hash of the empty string) and cannot show a greedy mismatch. The 2,066-token request can: its replies fall into the same six or so variants in every arm, the unset one included (they share the first 345-775 characters, then differ), because the adaptive tier and the GPU/CPU split change from run to run. No share arm produced a reply the unset arm did not also produce, and nothing matches a fixed baseline bit for bit; this is not a quality gate. + +**Recommendation.** Keep the preset's default (no share): a pageable bank pays more for its copies than the CPU pool does. The workstation preset's default is not changed here. `LELEKA_ORIGINAL_SERVE_PCIE_FRAC=0.6 LELEKA_PCIE_LOCKED_ONLY=on` is the one setting that is not worse, about +8 % decode in these 9 runs; it needs more reps, a 256k-class run and the quality gate before it becomes a profile line. What would move the bound: the CPU pool's compute is only about a third of a window (the rest waits on the GPU graph's own routing ids), and the locked set is 8 GiB. More pinned experts (a larger `resident_budget_gib`, which the 48 GB machine cannot spare) or a copy that does not stall the driving thread (staging the pageable pieces on helper threads, as the prefill ring does) are the next steps; neither is done. + +**Not verified:** why the driver faults (a driver defect is the only reading the tests leave; no vendor confirmation); the batch call on another driver or on Linux; C against A beyond 3 reps; a 256k session with a share; GPU against CPU expert math at the tensor level (the replies of one arm already differ between runs, so a greedy comparison cannot show it); `compute-sanitizer` on this laptop (not installed; nothing was installed).