Repository navigation
Description
Verdict: the build is not the nondeterministic layer, and there is nothing to file
upstream against the codegen backend. The register allocation a GEMM kernel gets is
not decided at build time at all — it is decided by the driver's PTX JIT at
cuModuleLoad, and the driver is the one compiler in the chain this tree does not
pin. What made #119's split fragile was separate and fixable: its entry points
declared no .maxntid, so the driver derived the launchable block width from
whatever allocation it chose, and a wide enough block turns any movement in that
allocation into DriverError(701) instead of a slower kernel. #123 pins it.
Where the register count is actually decided
gpu/gemm emits a pure-PTX artifact. cuda_host::ltoir::load_kernel_module selects
FileArtifact::Ptx and calls ctx.load_module_from_file(gemm.ptx) → cuModuleLoad
on PTX text. No ptxas runs during cargo oxide build; the allocator that produces
the number cuFuncGetAttribute(NUM_REGS) reports lives inside libcuda, and Modal
supplies that at run time.
So the chain has three compilers and the tree pins two of them:
| stage | produces | pinned by | reproducible? |
|---|---|---|---|
rustc + cuda-oxide backend + llc-21 |
gemm.ptx |
rust-toolchain.toml, CUDA_OXIDE_REF, the image |
yes, measured below |
| ptxas (image, CUDA 13.0.48) | only ever a cross-check here | the image | yes, measured below |
the driver's JIT (libcuda) |
the SASS and the registers | nothing | unmeasurable across a driver roll |
What was ruled out
New harness on this branch: modal run modal_app.py::rebuilds --kernel <k> --rounds N
builds one commit N times, each round in a fresh copy of the tree at the same path,
and reports the PTX digest, the entry emission order, per-function body digests and
ptxas -v's allocation.
1. Codegen is byte-reproducible. main @ 89f086e, 6 rounds, one image:
gemm.ptx 149dc7a27dbc x6 1 distinct
entry order identical 1 distinct
gemm_tcgen05_bf16_optimized 132/0 x6
gemm_tcgen05_f32_optimized 168/0 x6
gemm_tcgen05_f32_accumulate 146/0 x6
All 12 entry points in the crate are stable, registers and frame alike.
2. The commit that actually flapped does not flap. 2c6b786 — the warpgroup-split
commit of #119, THREADS = 320, the exact tree where 140/0 and 182/2176 were seen —
built 8 times:
gemm_tcgen05_bf16_optimized 86/0 x8
gemm_tcgen05_f32_optimized 96/0 x8
gemm_tcgen05_f32_accumulate 140/0 x8 <- never 182, never a frame
3. The driver's JIT agrees with the image's ptxas. modal run modal_app.py::jit
prints the PTX digest, the driver version, ptxas -v's numbers and
cuFuncGetAttribute's side by side. Three fresh B200 containers on the split commit,
all driver 580.95.05, all gemm.ptx sha256 6eb869d7d5eb: build-time ptxas 86/96/140
and the JIT 86/96/140, three times out of three.
One caveat, and it is a real finding of its own. The first pass of the harness
built each round in /tmp/build<N>, and got 8 distinct PTX digests out of 8.
That is cargo's -C metadata disambiguator, which is derived from the build directory
and rides inside every mangled generic symbol — and it also permutes the order entries
are emitted in. It changed the PTX bytes and changed nothing about the allocation
(all 8 rounds still 86/96/140). Worth knowing because it makes a naive sha256 gemm.ptx comparison across two working copies useless.
The mechanism that turns any allocation movement into a 701
This is the part that matters, and it reproduces cleanly.
An entry point with no .maxntid does not tell the JIT what block it will be
launched in. The JIT allocates freely against the architecture's 255 registers, and
then reports CU_FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK as a value derived from the
allocation it chose. Measured on the split commit, unpinned:
| kernel | registers | derived maxntid |
|---|---|---|
gemm_tcgen05_bf16_optimized |
86 | 640 |
gemm_tcgen05_f32_optimized |
96 | 640 |
gemm_tcgen05_f32_accumulate |
140 | 384 |
Those three points fit largest multiple of 128 ≤ 65536 / registers. Run the fit
forward:
- 182 registers → 256. The split launches 320 threads. 256 < 320, so
max_active_blocks_per_multiprocessoris 0 and the launch is refused with
701— precisely ferro-kittens #197-#210 surveyed: the warpgroup split is measured here and rejected, and the instrument that rejected it ships #119's report. - The same fit puts the boundary at ≤ 170 registers for a 320-thread block, which
is ferro-kittens #197-#210 surveyed: the warpgroup split is measured here and rejected, and the instrument that rejected it ships #119's "an SM will not place one CTA past 168" to within the sample it had. - At 192 threads, 182 registers derives 256 ≥ 192 and launches fine — which is
ferro-kittens #197-#210 surveyed: the warpgroup split is measured here and rejected, and the instrument that rejected it ships #119's other observation, and why this is silent onmaintoday.
So the landmine is not "ptxas is flaky." It is: the shipped kernels let a compiler
nobody pins choose a number, and then let that number decide whether the launch is
legal. A driver roll on Modal is enough; so is any future widening of the block.
Scope
Measured with rebuilds over three GPU crates, same image, fixed build path:
| crate | PTX artifacts | entry points | PTX bytes | allocation | #[launch_bounds] in source |
|---|---|---|---|---|---|
gpu/gemm |
2 | 12 | 1 distinct / 6 rounds | stable, all 12 | 0 of 9 before #123, 3 after |
gpu/ops |
2 | 81 | 1 distinct / 3 rounds | stable, all 81 | 0 of 81 |
gpu/flash-attn |
4 | 103 | 1 distinct / 3 rounds | stable, all 103 | 6 of 22 (tcgen05.rs) |
(Entry-point counts are per artifact and a kernel repeats across a crate's bins;
source-kernel counts are the #[launch_bounds] column.)
Nothing flaps — not one entry point in 196. The exposure is not flapping, it is the
missing declaration: a kernel is exposed exactly when it launches a block wider than
the maxntid its allocation happens to derive. gpu/ops (81 kernels) and
gpu/tensor-gpu (31, unmeasured) declare none. They are mostly narrow elementwise
blocks where the derived value sits far above the launch, but that is an accident of
their register counts and not a property anything checks. gpu/flash-attn's
tcgen05.rs already pins its six for exactly this reason, documented at
tcgen05.rs:249 — its lib.rs twelve do not.
Follow-ups, deliberately not in #123 (other agents are in those trees this week):
gpu/ops+gpu/tensor-gpu: pin, or at least assert, every kernel whose block
exceeds 128 threads.gpu/flash-attn:lib.rs's 12 entry points declare nothing.
Upstream
No nondeterminism bug to file against cuda-oxide. The backend at 20a5616 produced
identical PTX in 6 of 6 and identical per-function bodies; the earlier "8 distinct" is
the build path, which is cargo's behaviour and not the backend's.
Two things are worth raising upstream, neither urgent:
- PTX is not path-reproducible. The
-C metadatadisambiguator reaches the device
artifact, so the same commit built in two directories yields different PTX bytes
and a different entry emission order. Harmless to codegen quality, hostile to any
artifact-diffing workflow. - The PTX artifact route leaves the driver as an unpinned compiler.
#[cuda_module]
modules that emit PTX are JIT-compiled by whateverlibcudais present, so register
counts and occupancy are a property of the host, not the build. cargo-oxide already
has the machinery (cuda-artifact-finalizer,nvjitlink-sys) to materialize a cubin
ahead of time; an opt-in for the PTX route would make these numbers a build output.
A cheaper half-measure: have#[cuda_module]emit.maxntidfrom a declared
launch_contractblock even when#[launch_bounds]is absent, so the geometry is
never left implicit.
Mitigation
#123: #[launch_bounds(192, 1)] on the three shipped tcgen05 GEMMs. .maxntid now
reads exactly 192 instead of a derived 640/640/384, one resident CTA becomes a
constraint ptxas must satisfy rather than an outcome, and an allocation that cannot
reach it spills — which the budget gate already fails on — instead of producing a
kernel the driver refuses.
Activity
Merge pull request #123 from feitreim/diag/gemm-regalloc-pins