Skip to content

fix(cuda): make NVRTC JIT need only redistributable CUDA headers - #3540

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-3538-nvrtc-redistributable-headers
Nov 28, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-3538-nvrtc-redistributable-headers

Conversation

@inureyes

@inureyes inureyes commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Summary

The kernels the CUDA backend compiles at run time reached cooperative_groups.h, math_constants.h, cuda_runtime.h and mma.h, which the CUDA EULA's Attachment A does not list, so mlxcel could not ship the headers its own JIT needs. After this change the JIT reads only Attachment A headers. This unblocks the slim CUDA container image (#3484).

What changed

  • device/mlxcel_grid.cuh computes the grid thread rank from the launch builtins. The fused elementwise kernels, the custom-kernel default header and new overlays of gather, gather_axis, scatter, scatter_axis and slice_update use it. The cg alias is gone from both preludes.
  • The Mamba1 scan sums a warp with five __shfl_xor_sync steps (distances 16 to 1), the pairing cg::reduce uses; the reverse order changes bits in 138382 of 200000 test rows.
  • unary_ops.cuh carries ln(2) as 0x3F317218 instead of CUDART_LN2_F.
  • jit_module.cpp registers embedded stand-ins under the names cuda_runtime.h and mma.h, which the bundled CUTLASS includes. Two details differ from the issue, both measured on NVRTC 13.0: NVRTC predefines CUDART_VERSION, so the stand-in includes cuda_runtime_api.h only as a fallback, and mma.h also declares bmma_sync because one non-template CUTLASS function calls it.
  • Disk-cache module names are unchanged: modules compiled from the old sources compute the same results.
  • scripts/ci/check_nvrtc_headers.py: a static layer over every include written in a JIT source or in a string literal of the kernel and CUDA backend sources (every PR), and an NVRTC layer that compiles every JIT root plus instantiated qmm, scatter and slice-update kernels against only the allow-listed headers (both Linux CUDA release jobs, after packaging; arch lists untouched).
  • The Windows bundle stops copying the four headers. It keeps six non-listed ones until the check has run with NVRTC on Windows.
  • NOTICE is unchanged: nothing here is derived from another project, and what ships does not change. Listing CCCL and CUTLASS there is left to feat(docker): Add CUDA container image with mlxcel and mlxcel-server #3484.

Test plan

GB10, CUDA 13.0, main at 76599fca against this branch. Record: docs/benchmark_results/nvrtc-headers-gb10-2026-11-29.md.

  • Cold-cache strace: 11 toolkit headers opened, all allow-listed (21 to 26 on main), across gather, gather_axis, scatter_axis, fused elementwise, qmm_sm80, qmm_naive and the Mamba1 scan. The four headers are not even probed.
  • Text byte-identical to main: Qwen3 4B, Gemma 3 4B, Llama 3.1 8B, Falcon Mamba 7B, Jamba Reasoning 3B (greedy), and seeded sampling runs on Qwen3 and BitNet.
  • mamba1_scan_parity_tests passes (7), and 204 more tests over the other custom kernels' modules pass with every kernel compiled from the new sources; all 14 cuda_kernel call sites were compiled and launched.
  • PTX of qmm_sm80, qmm_naive and the Hopper-only qmm_sm90 is unchanged.
  • check_nvrtc_headers.py --require-nvrtc passes (38 roots); check_nvrtc_headers_test.sh (18 cases) proves it fails on a non-listed include, directly, through CUTLASS, and through six spellings found in review.
  • Cold start, medians of 7 interleaved runs: Qwen3 17.97 s against 18.28 s, Jamba 30.52 s against 31.31 s. Ranges overlap; load 1 to 45 (another job started mid-run).
  • Not validated: NVRTC other than 13.0 on Linux aarch64 (x86_64, Windows), a product run with only the allow-listed headers present (feat(docker): Add CUDA container image with mlxcel and mlxcel-server #3484), device output of scatter, slice_update and gather_gemm, and the release-job step itself (it runs at release time).

Closes #3538

@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority platform:linux Linux (CUDA / packaging) specific status:review Under review labels Nov 28, 2026
inureyes added a commit that referenced this pull request Nov 28, 2026
Review of #3540 showed the include scan could be bypassed and could pass without checking anything. None of the forms is used in the tree today, but the check exists to catch the next one.

- Literals: raw strings with any delimiter, ordinary string literals that spell an include, `#include_next`, and `.cu`/`.cuh` files are now scanned. A literal that also includes a Metal or ROCm header is no longer skipped whole; only those includes are.
- Coverage: program literals in the CUDA backend's own sources (the overlay on a pull request, upstream MLX's as well when a build tree is present) are scanned, and their include lines are compiled as NVRTC roots.
- No vacuous pass: a run in which NVRTC accepts none of the requested architectures fails, and the self-test skips its NVRTC cases only on the "cannot run" notice, not on any exit 2.
- The build tree is chosen by the mtime of its libmlx.a, not of a directory, and allow-list entries must stay inside the toolkit include directory.
- The allow-list quotes the EULA's own date, installation.md says the header-only deployment is compile-checked but not yet run, and the record lists gather_gemm as not instantiated.

On GB10 with NVRTC 13.0 the check compiles 38 roots for sm_80, sm_90a and sm_120a and passes, also with the release job's arguments; the self-test passes 18 cases.

Refs #3538
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 28, 2026
The kernels compiled at run time reached cooperative_groups.h, math_constants.h, cuda_runtime.h and mma.h, which the CUDA EULA's Attachment A does not list, so a release, runtime bundle or container image could not carry the headers its own JIT needs. This blocked the slim CUDA image (#3484).

- device/mlxcel_grid.cuh computes the grid thread rank from the launch builtins. The fused elementwise kernels, the custom-kernel default header and overlays of gather, gather_axis, scatter, scatter_axis and slice_update use it; the `cg` alias is gone from both preludes.
- The Mamba1 scan sums a warp with five __shfl_xor_sync steps (distances 16 to 1), the pairing cg::reduce uses.
- unary_ops.cuh carries ln(2) as 0x3F317218 instead of CUDART_LN2_F.
- jit_module.cpp registers embedded stand-ins under the names cuda_runtime.h and mma.h, which the bundled CUTLASS includes; NVRTC resolves embedded names before the include path. Disk-cache module names are unchanged, since modules compiled from the old sources compute the same results.
- scripts/ci/check_nvrtc_headers.py checks the includes written in every JIT source (all PRs) and compiles every JIT root with NVRTC against only the allow-listed headers (both Linux CUDA release jobs). The Windows bundle stops copying the four headers.

Validated on GB10 with CUDA 13.0: a cold-cache run opens 11 toolkit headers, all allow-listed (21 to 26 on main); text is byte-identical to main on five model families; mamba1_scan_parity_tests passes; qmm PTX is unchanged; cold start is not slower. Not validated: other NVRTC versions, Windows, and a run with only those headers present. Record: docs/benchmark_results/nvrtc-headers-gb10-2026-11-29.md.

Closes #3538
Review of #3540 showed the include scan could be bypassed and could pass without checking anything. None of the forms is used in the tree today, but the check exists to catch the next one.

- Literals: raw strings with any delimiter, ordinary string literals that spell an include, `#include_next`, and `.cu`/`.cuh` files are now scanned. A literal that also includes a Metal or ROCm header is no longer skipped whole; only those includes are.
- Coverage: program literals in the CUDA backend's own sources (the overlay on a pull request, upstream MLX's as well when a build tree is present) are scanned, and their include lines are compiled as NVRTC roots.
- No vacuous pass: a run in which NVRTC accepts none of the requested architectures fails, and the self-test skips its NVRTC cases only on the "cannot run" notice, not on any exit 2.
- The build tree is chosen by the mtime of its libmlx.a, not of a directory, and allow-list entries must stay inside the toolkit include directory.
- The allow-list quotes the EULA's own date, installation.md says the header-only deployment is compile-checked but not yet run, and the record lists gather_gemm as not instantiated.

On GB10 with NVRTC 13.0 the check compiles 38 roots for sm_80, sm_90a and sm_120a and passes, also with the release job's arguments; the self-test passes 18 cases.

Refs #3538
@inureyes
inureyes force-pushed the fix/issue-3538-nvrtc-redistributable-headers branch from bb00748 to a22b488 Compare November 28, 2026 21:51
@inureyes
inureyes marked this pull request as ready for review November 28, 2026 21:51
@inureyes
inureyes merged commit 15e6395 into main Nov 28, 2026
1 check passed
@inureyes
inureyes deleted the fix/issue-3538-nvrtc-redistributable-headers branch November 28, 2026 21:52
inureyes added a commit that referenced this pull request Nov 28, 2026
Since #3540 the NVRTC JIT reads only the CUDA headers in scripts/ci/nvrtc_header_allowlist.txt, so the runtime target can carry them. The image now copies exactly that list into /usr/local/cuda/include (docker/copy-cuda-headers.sh, no second list) and the build fails if any other file is there; docker/copy-cuda-headers_test.sh covers the guard in CI.

The CUDA and cuDNN libraries and headers now come from NVIDIA's CUDA apt repository, signature-checked against pinned key fingerprints and unpacked without installing, instead of being copied out of an nvidia/cuda image, whose container license allows a derived image only when it includes the whole container. Package versions are recorded in /opt/mlxcel/share/cuda-packages.txt. The debug target stays the whole cudnn-devel image.

amd64 now defaults to Ubuntu 26.04 with CUDA 13.5, matching the x86_64 release runner (its binaries need GLIBC_2.43); arm64 stays on Ubuntu 24.04 with CUDA 13.0. NOTICE records the CUDA and cuDNN distribution basis and the bundled CCCL and CUTLASS headers; README and docs/installation.md describe the image and its readiness behavior.

Refs #3484
inureyes added a commit that referenced this pull request Nov 28, 2026
Rewrite the GB10 record for a build of main at 15e6395 (includes #3508, #3509 and #3540). A cold-cache Qwen3-4B-4bit generation inside the runtime image under strace opened 11 CUDA toolkit headers, all in the allow-list, and succeeded; the server smoke (uid 65532, chat completion, CLI generate, docker stop drain, PTX cache reuse on a named volume, a root:root 0777 bind mount and a private-group 2775 bind mount) passed again. The record also states the license basis, the amd64 cross builds on Ubuntu 26.04 with CUDA 13.5, and what was not run.

Refs #3484
inureyes added a commit that referenced this pull request Nov 28, 2026
The pin's mlx/scheduler.h calls std::atomic<int>::notify_all(), a C++20 member, and every .hip file reaches it through mlx/array.h, so the -std=c++17 that the ROCm overlay passed to hipcc failed the Windows compile-only job. The .hip command now uses -std=c++20, matching the host sources (ROCm LOCAL_FIXES.md item 80).

The device/unary_ops.cuh overlay that #3540 added was copied at the old pin fc344c98; upstream added the SignBit functor between that pin and 5a48f9cc, so the overlay now carries it and is stamped at 5a48f9cc. It is the only one of the CUDA overlay files from #3540 that upstream changed between the pins.

check_nvrtc_headers.py (static and --require-nvrtc against the new-pin build tree), make verify-rocm-overlay, the CUDA compile and jit lib tests and both real-checkpoint DSpark gates pass on spark-102.
inureyes added a commit that referenced this pull request Nov 28, 2026
…3543)

## Summary

Adds `docker/Dockerfile.cuda`: a shell-less chiseled Ubuntu `runtime` image and a `debug` image (the whole `nvidia/cuda` `cudnn-devel` image plus mlxcel) for `linux/amd64` and `linux/arm64`, running as uid 65532 with `/models` and `/cache` volumes. The previous attempt stopped at the CUDA header license gate; #3540 removed the non-redistributable includes, so the runtime image now ships.

## What changed

- The runtime image copies exactly `scripts/ci/nvrtc_header_allowlist.txt` into `/usr/local/cuda/include` (`docker/copy-cuda-headers.sh`, no second list). The build fails if a listed header is missing or any other file is there. `docker/copy-cuda-headers_test.sh` covers the guard and runs in CI.
- CUDA and cuDNN files come from NVIDIA's CUDA apt repository. The signing key fingerprints are pinned, and the packages are unpacked without installing. Nothing is copied out of an `nvidia/cuda` image, because the NGC container license allows a derived image only when it includes the whole container (1.c, 4.c). Package versions go to `/opt/mlxcel/share/cuda-packages.txt`.
- Libraries come from a `readelf` closure plus a measured dlopen set, with a glibc/libstdc++ version guard.
- amd64 defaults to Ubuntu 26.04 with CUDA 13.5, because the x86_64 release runner builds with CUDA 13.5 and its binaries need `GLIBC_2.43`. arm64 stays on Ubuntu 24.04 with CUDA 13.0.
- `NOTICE` records the CUDA EULA and cuDNN distribution basis and adds the bundled CCCL (Apache-2.0 with LLVM exceptions) and CUTLASS/CuTe (BSD-3-Clause) headers. `README.md` and `docs/installation.md` describe the image.
- The `/health` wording is corrected, not the server: with `-m` the listener binds only after load and warmup, so probes are refused until the server returns 200. The image has no `HEALTHCHECK`.
- Security review: `HOME=/cache` means `-m owner/name` reads `/cache/.cache/huggingface/hub` before `/models` without an owner check, so a `/cache` other host uids can write lets them substitute a model. The docs now recommend a 65532-owned or private-group bind; a resolver fix is left for a follow-up.

## Test plan

GB10, local build of `main` at `15e63955` (#3508, #3509, #3540), arm64 runtime image, all GPU runs under `gpu-lock`.

- [x] Cold-cache `mlxcel generate` under strace inside the runtime image: exit 0, 0 to 12 PTX files. It opened 11 toolkit headers, all allow-listed, and no header outside the toolkit and bundled include roots.
- [x] Server smoke as 65532, named volume, `root:root 0777` bind with the trusted root, and `65532:65532 2775` bind with the trusted root unset. In every case the chat completion returned text, `docker stop` took under 1 s with exit 0, and the second start compiled nothing and left the cache identical. CLI `generate` worked and `--entrypoint sh` failed.
- [x] The literal `--platform linux/amd64,linux/arm64` build from v0.8.1, cache-only, for `runtime` and `debug`; amd64 runtime also from v0.9.0-beta.3 (glibc 2.43 rootfs).
- [x] The GPU runs used the image before the review follow-up `3ab35c1f` (copyright files, build-step hardening); that version was only rebuilt, not run.
- [x] `docker/copy-cuda-headers_test.sh` (5 cases, including a planted `math_constants.h` and a symlink).
- [ ] Not run: any amd64 container or x86_64 GPU (including NVRTC 13.5), release binaries at run time (no release has #3508/#3509/#3540 yet), either debug image at run time, kernels Qwen3 does not reach. Record: `docs/benchmark_results/docker-cuda-smoke-gb10-2026-11-29.md`.

Owner review requested: the license basis in `NOTICE` (CUDA EULA Attachment A and 1.1.2, cuDNN supplement, NGC container license 1.c/4.c/7) is my reading of those documents, not legal advice.

Closes #3484
inureyes added a commit that referenced this pull request Nov 28, 2026
## Summary

- New `publish-images` job in `release.yml`: after both Linux CUDA build jobs succeed it builds `docker/Dockerfile.cuda` (`runtime`, `debug`) for `linux/amd64,linux/arm64` from the release zips those jobs just uploaded and pushes one manifest list per target to `ghcr.io/lablup/mlxcel`, with BuildKit SBOM, `provenance: mode=max`, and an `actions/attest` provenance attestation pushed to the registry. Only this job gets `packages`, `id-token` and `attestations` write; every action in it is SHA-pinned. No QEMU: every `RUN` executes on the build platform.
- Versioned tags (`vX.Y.Z-cuda13`, `-debug`) are pushed first. Moving tags are added last with `imagetools create`, re-decided from a fresh release list at that point, so a failed debug build cannot leave `latest` moved alone and an older release finishing last cannot take it. The job condition uses `!cancelled()`, so a cancelled run does not publish.
- `scripts/ci/docker_image_tags.sh` holds the tag rule: moving tags only for a `release` event of a stable `vX.Y.Z` that is a full release and the newest of its line. Any hyphen suffix counts as a prerelease (not only `alpha|beta|rc`), because betas are promoted to full releases and the release flag cannot tell them apart. Dispatches, betas, backports and re-runs of older tags never move `latest`. Beyond the issue's tag contract, `vX.Y-cuda13` (and `-debug`) is added under the same rule.
- A tag without the Dockerfile or these scripts is skipped with a notice.
- `ci.yml`: `docker-build` (on `docker/**`, `.dockerignore`, the header allow-list or the disk script) builds both targets for both platforms from the newest release with all four CUDA zips, no login or push; `docker-image-tags` runs the tag tests.
- `scripts/ci/free_docker_disk.sh` removes preinstalled SDKs and moves Docker's data root to `/mnt` when that disk is larger; it refuses to run unless `RUNNER_ENVIRONMENT` is `github-hosted`.

## Validation

- `actionlint` 1.7.7 with CI's shellcheck floor passes; full-severity finding count unchanged from `main`. `check_cuda_arch_lists.py` passes.
- `docker_image_tags_test.sh`: 20 cases pass on macOS and Linux; a mutation of the event gate is caught. With the real release list, `v0.9.0` gets the moving tags and a beta gets none.
- spark1 (arm64) dry run of the job's build flags, pushing `v0.9.0-beta.3` into a loopback `registry:2`: both indexes list `linux/amd64`, `linux/arm64` and two attestation manifests, with SPDX SBOM and provenance for both platforms and the index annotations. `imagetools create` for `latest-cuda13`, `v0.9-cuda13` and `latest-cuda13-debug` kept the same index digests. Builder storage peaked at 28.9 GB (runtime) and 47.5 GB (debug).
- `docker-build` on this PR passed on a hosted amd64 runner in 12 minutes: both targets for both platforms from `v0.9.0-beta.3`, so the amd64-host arm64 cross build works. Root had 113 GB free after cleanup, so the `/mnt` move did not trigger.
- GPU smoke of the pulled arm64 image did not pass, for a reason outside this PR: `v0.9.0-beta.3` predates #3540, so the CLI fails on `cooperative_groups.h`. Every tag that has the Dockerfile contains #3540 (`15e63955` precedes `66c13292`).

## Not validated

- The real GHCR push, `GITHUB_TOKEN` package permissions, `gh attestation verify`, SBOM and attestation push on a hosted runner (`docker-build` neither pushes nor attests), and the `/mnt` relocation branch.
- A passing GPU smoke of a published image on either arch.
- Left open from #3484: base images are pinned by tag, not digest, and the chisel `.sha384` is fetched from the same release. `mode=max` provenance records the resolved base digests.

The first push creates the GHCR package as private; set it public once in the package settings.

Closes #3485
inureyes added a commit that referenced this pull request Nov 29, 2026
Advance the reproducible MLX pin from fc344c98 to 5a48f9cc, the head of upstream main on 2026-11-28 (87 commits, past v0.32.5). Review record: docs/mlxcelverse/mlx-pin-2026-11-28.md.

- The CPU quantized-matmul overlay (#2248) is retired: upstream now accumulates the fp qmm loops in float (ml-explore/mlx#4879), and cpu_qmm_accum_tests guard the upstream loops.
- 24 Metal and CUDA overlays are rebased onto the pin. SDPA threads upstream's window_size through the deterministic and bucketed plan paths. The CUDA grouped_gemm.h overlay includes <cstdint>, and device/unary_ops.cuh (added on main by #3540 at the old pin) carries upstream's new SignBit functor.
- The #637 sm_120/121 qmm tile cap is dropped for upstream's #4851 rule (large_tiles ? 128 : 64). A GB10 A/B on llama-3.1-8b-4bit and qwen2.5-7b-4bit showed no prefill gain from the cap at 2048 or 8192 tokens and byte-identical greedy output; the 512-token qwen gain (about 10%) is recorded in the pin doc. TILE_M_CAP_BLACKWELL is 64.
- ROCm overlay retargeted: the fork's fusion limits move to mlx/compile/fuse.cpp, the new allocator hooks are inert, GroupedMM and SignBit get NO_GPU stubs, and HIP sources compile as C++20 because the pin's scheduler.h uses std::atomic::notify_all (LOCAL_FIXES 77 to 80, after main's 76).
- The MiniCPM5 and Qwen3 DSpark exactness gates require MLXCEL_SDPA_DETERMINISTIC=1 on CUDA, where decode attention otherwise takes cuDNN stream-K and its engine choice depends on what ran earlier in the process.
- A clippy needless_range_loop in the router WebUI harness test from #3552 is fixed; main's clippy job had not run on that change.

Validation: on M3 Ultra, make verify-test (17374 passed). On GB10 (spark-102), make verify-test-cuda-qmm-determinism (5 passed) and make verify-test-cuda (17741 passed, 3 failed, none from this change: a DeepSeek-OCR CLIP parity failure identical on main, and two port or timing flakes that pass alone), check_nvrtc_headers.py with NVRTC against the new-pin build tree, make verify-rocm-overlay, and both real-checkpoint DSpark gates. CI passes except binary assets, which fails on main for the undeclared tests/fixtures/audio/speech.flac from #3533. No ROCm host was available: the HIP C++20 change is covered by the Windows compile-only job only.
inureyes added a commit that referenced this pull request Nov 30, 2026
Bilingual reports for the eight PRs of epic #3488: #3508 (graceful drain), #3509 (CUDA PTX cache trust), #3530 (ROCm hsaco cache trust), #3540 (NVRTC redistributable headers), #3543 (CUDA container image), #3549 (Helm chart), #3550 (GHCR image publishing) and #3554 (chart OCI publishing and docs).

Each report names what the PR record leaves unvalidated: a real GHCR push, a GPU pod on a cluster, the amd64 and debug images. The HuggingFace cache follow-up is #3570.
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

platform:linux Linux (CUDA / packaging) specific priority:high High priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(cuda): make NVRTC JIT need only redistributable CUDA headers

1 participant