[CUDA] Fix custom kernel cache collision for same name, different source - #4273
Open
HaoXuAI wants to merge 1 commit into
Open
[CUDA] Fix custom kernel cache collision for same name, different source#4273HaoXuAI wants to merge 1 commit into
HaoXuAI wants to merge 1 commit into
Conversation
get_jit_module keys the module cache on the kernel name alone, so a second kernel sharing a name silently runs the first one's compiled code. Include a hash of the source in the module name, as ml-explore#3833 did for Metal.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4275.
get_jit_modulekeys the module cache on the kernel name alone (jit_module.cpp:471-485), so a second custom kernel sharing a name silently runs the first one's compiled code — no error, wrong results.This is the CUDA counterpart of #3832, fixed for Metal in #3833. It is worse here: on Metal the stale-source invalidation worked across
evalboundaries and only failed within a single batch, whereasget_jit_modulehas no invalidation at all, so the collision persists for the lifetime of the process.The fix hashes the source into the module name, the same way #3833 did. The kernel name passed to
get_kernel()is unchanged, so lookup inside the module still works.It also covers a second path with the same root cause:
read_cached_ptx()runs unconditionally (jit_module.cpp:407) even whenuse_disk_cacheis false, so a stale on-disk PTX under the same name could be picked up too.Testing
test_cuda_kernel_same_name_different_sourcemirrors the Metal regression test from #3833. Verified on an L40S (sm_89) with CUDA 12.6 — two kernels sharing an entry name but differing in body, with a separatemx.evalbetween them:x * 2)[0, 2, 4]correctx + 100)[0, 2, 4]— kernel A's code[100, 101, 102]correctThe rest of
test_fast.pyis unaffected: 24 passed / 4 skipped on CUDA, 27 passed / 1 skipped on Metal.I ran into this through
mx.fast.precompiled_cuda_kernelwith Triton output, where it is easy to hit by accident since Triton names kernels after the Python function.