Skip to content

gpu: cover pageable snapshot copies and resumed token bits - #730

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:fix-snapshot-dma-contract
Oct 23, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:fix-snapshot-dma-contract

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

프롬프트 캐시 스냅샷 복사가 일반(pageable) 호스트 메모리에서도 바이트와 복원 토큰을 그대로 지키는지 GPU 회귀 시험으로 고정합니다.

Part of #565. Tracked in #699.

What and why

The synchronous prompt-cache snapshot path hands ordinary Vec storage to the 2-D device-to-host copy wrapper dtoh_2d. When that wrapper came in (f8f7c3c), its documentation required page-locked host memory, so these calls broke the stated contract. A plain Vec that the CPU has written is not page-locked. The CUDA documentation for cuMemcpy2DAsync does not forbid pageable host memory, and #706 removed the requirement.

This PR adds regression coverage for that path: pageable and pinned 2-D copies, exact recurrent bits in a saved and restored snapshot (signed zero included), and the next token read from a restored state. It changes no production code: no new public item, no unsafe block, allocation, sync or transfer.

The change:

  • hw_a_2d_copy_moves_the_bytes_of_the_planes_one_by_one (crates/gpu/src/tensor.rs) now runs its read and write loop twice, once on a plain Vec<u8> and once on a PinnedHostBuffer. Each round checks the bytes of every plane, the gap bytes the copy must leave alone, and the device offset.
  • hw_a_state_goes_and_comes_back (crates/gpu/src/checkpoint/saved.rs) saves a recurrent state with values [5.0, -0.0, 7.0, -2.0] and compares raw bits after the save and after the restore, so a -0.0 turned into +0.0 is caught. It also runs enqueue_argmax_finite_fault on the state before the save and after the restore, and compares the token and fault readback. It does not run a model forward after the restore.

Where the fix goes: both tests live in the existing tests modules, next to the code they check. The callers of dtoh_2d and htod_2d are read_into, put_from, save and load in saved.rs, the staging tests in checkpoint/staging.rs and the copy test in tensor.rs. The asynchronous path keeps borrowing pinned extents from staging, so a pageable fallback does not add a pin, a copy or a sync there.

Release notes: Under the hood: the GPU tests for snapshot copies now cover pageable host memory, recurrent bits and the resumed token.

Code shape

  • No second copy. The tests call the existing Gpu, SeqState, dtoh_2d and htod_2d owners. The expected bytes come from the plane values, not from a second copy of the transfer.
  • Model code holds only architecture facts. No model code changes.
  • Dead code is gone. The old pinned-only read and write block of the 2-D test moved into the loop over both host buffers.
  • No silent failure. The new asserts compare bytes and bits and name what they check.
  • Comments state what is true now. The SAFETY comments say what each host buffer holds.
  • No relaxed gate. No band, tolerance or lint level moved, and no test function was removed.
  • cargo fmt is clean on what I touched, and I ran no clippy (see "What I ran").

Duplication found or removed (path:line), or "none": none.

What I ran

Both machines ran the two changed tests through the gate wrapper, then cargo fmt. The tests are #[ignore] and need a CUDA device.

RUST_TEST_THREADS=1 BLOOMERY_BOX_LOCAL=1 bash tools/gate.sh --oxide -p bloomery-gpu --release --lib -- --ignored --nocapture checkpoint::saved::tests::hw_
RUST_TEST_THREADS=1 BLOOMERY_BOX_LOCAL=1 bash tools/gate.sh --oxide -p bloomery-gpu --release --lib -- --ignored --nocapture --exact tensor::tests::hw_a_2d_copy_moves_the_bytes_of_the_planes_one_by_one
cargo fmt --all -- --check

DGX Spark, one GB10:

| NVIDIA-SMI 595.84                 Driver Version: 595.84         CUDA Version: 13.3     |
|   0  NVIDIA GB10                    On  |   0000000F:01:00.0 Off |                  N/A |
  • The six checkpoint::saved::tests::hw_ tests: test result: ok. 6 passed; 0 failed.
  • The 2-D copy test: test result: ok. 1 passed; 0 failed.
  • cargo fmt --all -- --check passed, and the run ended with SNAPSHOT-CONTRACT PASS.

RTX 5090 machine:

| NVIDIA-SMI 595.99.02              Driver Version: 595.99.02      CUDA Version: 13.2     |
|   0  NVIDIA GeForce RTX 5090        Off |   00000000:01:00.0 Off |                  N/A |
  • The six checkpoint::saved::tests::hw_ tests: test result: ok. 6 passed; 0 failed.
  • The 2-D copy test: test result: ok. 1 passed; 0 failed.
  • cargo fmt --all -- --check passed, and the run ended with SNAPSHOT-CONTRACT PASS.

just gate-gpu-lib, not run: the same ignored tests it selects ran directly through tools/gate.sh as shown above.

FAIL-first: the production code is unchanged, so the new asserts also hold on the code in main, and none of them is a FAIL-first case. What I did observe is the old precondition itself. A temporary probe in dtoh_2d, right before the copy is submitted, asked cuMemHostGetFlags about a plain Vec that the CPU had already written. The call returned 1 where the CUDA success code is 0, so the memory is not page-locked, and the probe stopped on its assert:

assertion `left == right` failed: old pinned-only precondition at actual D2H boundary
  left: 1
 right: 0
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 385 filtered out; finished in 12.51s

That run was on a GB10, and its raw test exit code was 101. The old documented precondition did not hold for a plain Vec, and the passing runs above show the driver accepts pageable memory.

Not run: clippy, t0 and the rest of the check suite, which I leave to the PR chain, and any timing run, because the change adds no production code and I claim no speed change. I did not run a model forward after a restore.

What I ran into

Nothing here blocks this PR. The end of the saved-state copy event and the staging error path sit in the same saved.rs, in code this PR does not touch, and I did not change them.

Hardware (if the change is for a machine)

  • DGX Spark: NVIDIA GB10, driver 595.84, CUDA 13.3.
  • RTX 5090 machine: NVIDIA GeForce RTX 5090, driver 595.99.02, CUDA 13.2.

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

@github-actions github-actions Bot added gpu The card engine: crates/gpu*, crates/app needs-gpu Changes code a card must check, and carries no gate run on a card labels Oct 23, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: CLEAN: The test changes add byte and recurrent-bit checks without changing production behavior.

Non-blocking

⚠ Demonstrate the added assertions failing: saved.rs:985. The reported FAIL-first run fails a temporary precondition probe, not an added assertion. Attach failing output with recurrent signed zero normalized and with a copy row omitted. This establishes the regression signal while the test inputs are available.

Checked

Duplicates: 0 new function/type/constant definitions; the local token closure reuses the existing argmax launcher. Grep found no duplicated implementation; dryer reported 0 pairs.
Complexity: crapper flagged no changed functions; changes are confined to two existing tests.
Silent failure: 0 added default/error-suppression sites; new fallible calls use named expectations.
Tests (static mutation analysis): dropping the recurrent copy at saved.rs:471 or saved.rs:521 fails the saved/restored bit assertions. Reducing Height at tensor.rs:314 to rows - 1 fails the plane assertions. Signed-zero normalization also fails bit comparison. These mutants were not executed.
Gates: no pins moved or coverage removed; slice equality retains the removed length check. Both tests remain hw_-prefixed and ignored.
Escape catalog: no patterns matched. No production hot path changed.

Notes

CUDA gates were not run during this review. The PR reports GB10 and RTX 5090 runs, but omits both tested commit hashes; GPU evidence is incomplete. The token check applies argmax directly to recurrent values; it does not exercise a model forward, as the PR body states.

Card check: needs-gpu. This pull request changes crates/gpu/src/checkpoint/saved.rs, crates/gpu/src/tensor.rs and carries no gate run on a card (CONTRIBUTING's evidence list); a maintainer adds gpu-verified after one.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

@juliankang4
juliankang4 merged commit ba1eeb5 into midagedev:main Oct 23, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gpu The card engine: crates/gpu*, crates/app needs-gpu Changes code a card must check, and carries no gate run on a card

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant