Repository navigation
feat(core): add an opt-in cuda-oxide route behind fused_add_rms_norm - #3451
Conversation
Issue #2833 asks whether the precompiled cuda-oxide fused residual-add + RMSNorm module from #2832 can run inside MLX lazy evaluation without replacing MLX's CUDA backend. It can: the kernel is now an MLX primitive (kernels/fused_norm_oxide.cpp) that loads the module through get_jit_module, allocates outputs with malloc_async, registers buffers with the stream's command encoder and enqueues with add_kernel_node_raw inside eval_gpu. A dedicated primitive is needed only because the entry interleaves (pointer, u64 length) pairs with the scalars, which CustomKernel cannot pack. No file under overlays/ changes. The route is off twice. MLXCEL_BUILD_CUDA_OXIDE_FUSED_NORM=1 (Linux CUDA only) compiles the adapter and embeds the committed sm_121 PTX after the build script checks its SHA-256 against the manifests. It is a build environment variable, not a Cargo feature or build dependency, because either would change the build script's OUT_DIR and rebuild MLX from source. At run time MLXCEL_CUDA_OXIDE_FUSED_NORM=1 validates the artifact against its manifests and ABI schemas, checks that device 0 is sm_121 and installs it. fused_add_rms_norm calls the adapter before select_kernel_port. Unsupported dtype pairs, more than 2^32-1 elements and a CPU default device take the existing kernel when the node is built; a failure during evaluation throws and is never retried. The JIT module name carries the payload digest, target, ABI version and compiler revision. Route and launch counters let tests prove which implementation ran. Validated on GB10 under gpu-lock with CUDA graph capture on and off; commands, results and gaps are in docs/mlxcelverse/cuda-oxide-mlx-adapter-evidence.md. No checkpoint was run and no throughput is claimed (#2834). Refs #2833
Follow-up to the cuda-oxide adapter for #2833. The parameter-role types and the argument-list check move from cuda_oxide_artifact.rs into cuda_oxide_abi.rs so both files stay under 500 lines; fused_norm.cpp and fused_norm_oxide.cpp lose comment lines for the same reason. No behavior changes. The evidence report now records two observations instead of inferring them: the CUDA graph dump of the captured-child-graph test (three matmul child graphs and two fused_add_rms_norm_f32 kernel nodes in one graph, with the dependency edges listed), and a run of the whole opted-in mlxcel-core lib binary in one process (2517 passed, 0 failed). It also stops claiming each test thread had its own stream, which was not inspected. Adds the changelog entry. Refs #2833
Implementation Review SummaryIntent
Findings AddressedNo CRITICAL or HIGH findings, so nothing was changed. Remaining Items
DeviationsBoth hold. A Cargo feature changes the build-script metadata hash and therefore Verification
Re-run on GB10 under |
Security and performance reviewNo CRITICAL or HIGH findings, so nothing was changed or pushed. Checked and clean:
LOW, left for follow-up:
|
Distinct streams: add distinct_gpu_streams_can_drive_the_route_at_once, which drives the oxide route from three threads on three streams made with new_stream_on_gpu, asserts the stream indices differ, compares every output with the existing fused kernel and checks 60 routes, 60 launches and 0 existing-kernel routes. Grid limit: the route decision now refuses more than 2^31-1 rows (one block per row, CUDA's grid.x cap) before enqueue and records the new Fallback::GridLimit; a lazy-shape test covers both sides of the limit. Disk cache: the oxide module no longer reads or writes MLX's PTX disk cache, so a cached entry can never stand in for the validated payload. This needs a trailing read_disk_cache parameter (default true, so no other caller changes) on JitModule and get_jit_module in the CUDA jit_module overlay, which makes the overlay-untouched claim in the docs false and is documented as such. A test plants a different payload under the module name and checks it is neither loaded nor rewritten. The same change stops an_evaluation_time_failure_is_an_error_not_a_retry from leaving a junk entry in the per-user cache, and the test now asserts that. Docs: CONTRIBUTING.md notes that CommandEncoder has an NDEBUG-conditional member, so the bridge's NDEBUG must agree with MLX's for fused_norm_oxide.cpp. The evidence report records the new results and lists the deferred follow-ups. Validation on GB10 (opted-in build, gpu-lock, --test-threads=1): cuda_oxide filter 31 passed with CUDA graphs on and with MLX_USE_CUDA_GRAPHS=0, the 130-case matrix line unchanged, fused_norm_parity_tests 10 passed, layer-route test passed with both switches. cargo fmt --check and clippy -D warnings on mlxcel-core --lib --tests --features cuda pass for the default and opted-in builds. Refs #2833
The overlay's `get_jit_module` carried two trailing bools: upstream's `use_disk_cache`, which this overlay has ignored since #2492, and the new `read_disk_cache`, which also turned off writes despite its name. The cuda-oxide adapter called it as `(..., false, false)`, which did not say which flag did what and let the two be swapped silently. The second parameter is now `cu::DiskCache disk_cache = DiskCache::Use` with `enum class DiskCache { Use, Bypass }`. `Bypass` neither reads nor writes the PTX disk cache for the module, the adapter passes it by name, and a bool can no longer be passed in its place. The header documents that upstream's flag is kept only for signature parity. Every existing caller still gets the default, so their behavior is unchanged. Comments, the mlxcelverse README and the adapter evidence report are updated to the new name. GB10, opted-in test-fast build, under gpu-lock with --test-threads=1: cuda_oxide 31 passed with CUDA graphs on and 31 with MLX_USE_CUDA_GRAPHS=0 (matrix 130/130 on the oxide route, bit-identical to the fused CUDA kernel), fused_norm_parity_tests 10 passed, layer-route test with both switches 1 passed (2 oxide routes, 2 launches). No mlxcel_oxide entry in the PTX disk cache afterwards. Refs #2833
…da-oxide-rmsnorm-adapter # Conflicts: # CHANGELOG.md
The Windows build job checks out with core.autocrlf on, which rewrote the committed PTX with CRLF so its SHA-256 no longer matched the manifests and three mlxcel-mlx-pin build_support tests failed. Mark the spike artifacts and ABI schemas -text so git never converts them. Refs #2833
Summary
The precompiled cuda-oxide fused residual-add + RMSNorm module from #2832 now runs inside MLX lazy evaluation as an optional implementation behind
fused_add_rms_norm. It is off at build time and at run time, GB10 only. The only change to MLX's CUDA overlay is one defaulted parameter onget_jit_module(see below), so the stop condition was not reached.kernels/fused_norm_oxide.cpp):get_jit_moduleloads the PTX,malloc_asyncallocates outputs, the stream's command encoder registers the buffers andadd_kernel_node_rawenqueues, all insideeval_gpu.fast::precompiled_cuda_kernelcould not be reused because the entry interleaves(pointer, u64 length)pairs with the scalars.fused_add_rms_normcalls the adapter beforeselect_kernel_port. No artifact, a dtype pair without an entry, more than 2^32-1 elements, more than 2^31-1 rows (CUDA grid limit) or a CPU default device build the existing kernel's node, with the reason recorded. A failure during evaluation throws and is not retried.MLXCEL_CUDA_OXIDE_FUSED_NORM=1(withMLXCEL_FUSED_ADD_RMSNORM=1) validates the embedded artifact against its manifests and ABI schemas, checks that device 0 is sm_121 and installs it. A refused artifact prints one line and the existing kernel keeps serving.MLXCEL_CUDA_OXIDE_FUSED_NORM_DIRloads another copy through the same validation.Overlay change
overlays/cuda/mlx/backend/cuda/jit_module.{cpp,h}gainenum class DiskCache { Use, Bypass }and a trailingDiskCache disk_cache = DiskCache::UseonJitModuleandcu::get_jit_module.Bypassneither reads nor writes the disk cache for the module. Only the adapter passesBypass, by name; every existing caller keeps its behavior. It is a typed enum rather than a secondboolso it cannot be confused with or swapped for upstream'suse_disk_cache, which the header now documents as kept only for signature parity. Upstream'suse_disk_cacheargument has been ignored since #2492, and caching a precompiled PTX saves no compile time, so bypassing was simpler and safer than comparing on hit. Editing the overlay rebuilds MLX once in each build profile. Documented in the file header andsrc/lib/mlxcelverse/README.md.Deviations from the brief
MLXCEL_BUILD_CUDA_OXIDE_FUSED_NORM=1, not a Cargo feature. A feature or a build dependency changes the build script'sOUT_DIRand rebuilds MLX from source on every toggle (the reason recorded inbuild_support/ptx_cache_test_hooks.rsandmlxcel-mlx-pin/Cargo.toml). Measured: 57 s to turn it on after a default build, 12 min 22 s cold. The build-time digest check therefore uses a SHA-256 written out inbuild_support/, unit-tested inmlxcel-mlx-pin.kernels/, not in the overlay. MLX installs its CUDA backend headers, so an opted-in bridge build reaches them with two extra include paths. The cost is one file that uses MLX CUDA internals directly, listed inCONTRIBUTING.mdfor pin bumps (including theCommandEncoder#ifndef NDEBUGlayout note).eval_gpu, asfast::cuda_kerneldoes for the existing port.Test plan
GB10, driver 580.178.04, every GPU run under
gpu-lockwith--test-threads=1. Commands and results are indocs/mlxcelverse/cuda-oxide-mlx-adapter-evidence.md.cuda_oxidein the opted-in lib binary: 31 passed with CUDA graphs on and 31 withMLX_USE_CUDA_GRAPHS=0. Matrix:cases=130 elements=2181390 failed=0 bitwise_identical_to_fused_cuda=130 oxide_routes=130 launches=130 native_routes=0.try_evalwith 0 launches and 0 reroutes; a lazy shape over the grid limit takes the existing kernel.layers::fused_add_rms_normwith the real environment switches: graph path, existing kernel, cuda-oxide (2 routes, 2 launches), also with graphs off and with the directory override.ff479507; later commits reran the narrow filters above).fused_norm_parity_tests10 passed,check-clean-build.shpasses.mlxcel-mlx-pinbuild-gate tests 8 passed; clippy-D warningsonmlxcel-corelib and tests (default and opted-in);cargo fmt --check; the argument-freescripts/ci/check_*.pygates.Not verified: no checkpoint was loaded, so which dtype pairs real models present is unmeasured; no non-CUDA or Windows build was compiled; workspace-wide clippy and tests were not run; no throughput measurement (#2834).
Known follow-ups, out of scope here: the route decision takes a shared lock per call;
last_fallback()is last-writer-wins across threads; the override directory is read without a size bound or regular-file check; the existing port'sstatic_cast<int>(x.size() / dim)infused_norm.cppwraps above 2^31-1 rows (predates this PR).Closes #2833