Skip to content

update(ci): make the kernel dtype-key checker's scope fail-closed - #2055

Merged
inureyes merged 3 commits into
mainfrom
update/issue-1875-dtype-key-scope
Sep 29, 2026
Merged

inureyes merged 3 commits into
mainfrom
update/issue-1875-dtype-key-scope

Conversation

@inureyes

Copy link
Copy Markdown
Member

Closes #1875

check_kernel_dtype_keys.py scoped itself by globbing *.cpp in two directories and skipping files without cuda_kernel(, so a launch moved into a header, a helper or a HIP-only file shrank the scope while the check still printed OK. That had already happened once: the #1804 ROCm fault probe in mlx_cxx_bridge.cpp calls fast::hip_kernel and was never in scope.

What changes

  • Scan set. Every C/C++/CUDA/HIP/Objective-C++ source and header that git tracks or would track (git ls-files --cached --others --exclude-standard), so gitignored references/ checkouts with MLX's own launchers stay out. Non-git trees are walked with only top-level build and hidden directories skipped.
  • Scope token. A file is in scope when it calls cuda_kernel( or hip_kernel( outside a comment. Raw strings and digit separators are lexed correctly, and the vendored CustomKernelFunction hip_kernel( definitions do not count.
  • HIP finding. ROCm's custom_kernel.cpp builds "custom_kernel_" + name + template_arguments_hash(template_args), and rocm::get_jit_module memoises in a process-global map keyed only by device index and that name. The fix(paged): paged_v2 sparse f16 cache decode disagrees completely with its reference #1053 rule therefore applies to HIP unchanged.
  • Fail-closed scope. Output reads 9 in scope ... of 188 source files scanned. The in-scope set is pinned in EXPECTED_IN_SCOPE, and the check fails on any of these: a pinned file that was deleted, a pinned file that stopped launching (each with its own message), an unpinned new launcher, or an empty scope.
  • Companion test. check_kernel_dtype_keys_test.sh mutates throwaway copies across 16 cases and runs from make verify-kernel-dtype-keys (so make verify and make verify-rocm run it) and from a new step in the kernel dtype keys CI job. All 11 negative cases fail when run against the previous checker.
  • Docs. Updated docs/code-guidelines.md, including the two remaining limits: inline template_args and launches without a direct call. In CONTRIBUTING.md, the make verify prerequisite list was stale; it now includes verify-kernel-port-dispatch.

Changes during review

A review pass found that the test used GNU-only sed -i (it would have broken make verify on macOS), that the checker did not scan .hip files, and that it pruned directories by name at any depth. It also found that ignored references/ checkouts failed the local run. Commit e3d0150 fixes all four.

Verification

On the gfx1151 host: make verify-kernel-dtype-keys verify-kernel-port-dispatch verify-versions verify-llama-compat verify-fmt passes. cargo test --features rocm --test dead_doc_pointers passes (2 tests). The checker also passes on the primary checkout (188 files scanned, 9 in scope).

Not verified: Metal and CUDA. Neither is available on this host. The change is CI-script and docs only and touches no Metal or CUDA code path. The test script was made BSD-sed-safe but was not run on macOS.

The checker globbed `*.cpp` in two directories and skipped any file without `cuda_kernel(`, so a launch moved into a header, a shared helper or a HIP-only file left the check printing OK over a shrunk scope. It already had: the #1804 ROCm fault probe in `mlx_cxx_bridge.cpp` calls `fast::hip_kernel` and was never checked.

The checker now scans every C/C++/CUDA source and header in the repository, counts a file as a launcher when it calls `cuda_kernel(` or `hip_kernel(` outside a comment (the vendored entry-point definitions excluded), reports the in-scope count next to the scanned count, and pins the in-scope set in `EXPECTED_IN_SCOPE`. A pinned file that disappears or stops launching fails with a message saying which, as do an unpinned new launcher and an empty scope. HIP is covered because ROCm's `custom_kernel.cpp` builds the same `name + template_arguments_hash` name and `rocm::get_jit_module` memoises by device index and that name only.

`check_kernel_dtype_keys_test.sh` mutates throwaway copies (launch into a header, into a helper, commented out, file deleted, HIP-only launchers, empty tree, plus positive controls) and runs from `make verify-kernel-dtype-keys` and the CI job. Against the previous checker 10 of its 13 cases fail.

Closes #1875
Review of the first commit found that `make verify` would break on macOS (the companion test used GNU `sed -i`), that a gitignored `references/mlx` checkout made local runs fail on MLX's own launchers, that `.hip` translation units were not scanned, and that pruning `build`/`site` at any depth could hide a real launcher.

In a git work tree the checker now takes its file list from `git ls-files --cached --others --exclude-standard`; other trees (the test's copies) are walked with only top-level build and hidden directories skipped. `.hip` is scanned, and a digit separator such as `1'000` no longer opens a character literal that could hide a launch. The test mutates files through a Python helper that fails when its pattern matches nothing, and gains cases for a `.hip` file in a nested `build/` directory and for ignored versus untracked launchers in a git tree. The docs and docstring now state the two remaining limits (inline `template_args`, launches without a direct call).

Refs #1875
@inureyes inureyes added type:enhancement New features, capabilities, or significant additions priority:low Low priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers status:done Completed labels Sep 29, 2026
@inureyes
inureyes merged commit 81f13ec into main Sep 29, 2026
1 check passed
@inureyes
inureyes deleted the update/issue-1875-dtype-key-scope branch September 29, 2026 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers priority:low Low priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

chore(ci): kernel dtype-key checker cannot see launches that move into headers

1 participant