Repository navigation
Fix kernel cache collision in Compiled constructor - #3427
Merged
angeloskath merged 1 commit intoApr 21, 2026
Merged
Conversation
The constructor moves `inputs` and `constant_ids` into members but the
loop building `kernel_lib_` continued to iterate the moved-from parameters.
After the move those parameters are empty, so the kernel name omits the
per-input dtype suffix and a kernel compiled for one input type can be
incorrectly reused for another. With ops fused around the cast (e.g.
chained `astype` feeding `matmul` or `einsum`), this surfaces as
byte-level corruption of the output.
Reproducer:
```python
import mlx.core as mx
def f(x_u16, y_i8):
a = x_u16.astype(mx.int32)
b = y_i8.astype(mx.int32)
af = a.astype(mx.float32)
bf = b.astype(mx.float32)
return mx.matmul(bf.reshape(2, 1), af.reshape(1, 2))
x = mx.array([10, 20], dtype=mx.uint16)
y = mx.array([1, 2], dtype=mx.int8)
print(f(x, y)) # [[10, 20], [20, 40]]
print(mx.compile(f)(x, y)) # [[5130, 10260], [0, 0]] <-- wrong
```
`5130 = 0x140A` is the little-endian byte pattern of the two `uint16`
inputs packed into one `int32`, confirming the kernel was specialized
for the wrong dtype.
Switching the loop to use the `inputs_` / `constant_ids_` members
restores the dtype suffix and the bug goes away.
Fixes ml-explore#3338.
zcbenz
approved these changes
Apr 19, 2026
zcbenz
left a comment
Member
There was a problem hiding this comment.
Great found, thanks for fixing this!
1 task done
rockiestar-com
pushed a commit
to RockieStar-Inc/mlx
that referenced
this pull request
Aug 25, 2026
(cherry picked from commit c594e6e)
This was referenced Sep 25, 2026
eejd
pushed a commit
to eejd/mlx-swift
that referenced
this pull request
Oct 18, 2026
Apply ml-explore/mlx@c594e6ec38 (ml-explore/mlx#3427) to the vendored MLX. Compiled::Compiled built the kernel name from the moved-from inputs, so graphs that differ only in input dtype got the same name and shared one Metal library. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
The constructor moves
inputsandconstant_idsinto members but the loop buildingkernel_lib_continued to iterate the moved-from parameters. After the move those parameters are empty, so the kernel name omits the per-input dtype suffix and a kernel compiled for one input type can be incorrectly reused for another. With ops fused around the cast (e.g. chainedastypefeedingmatmuloreinsum), this surfaces as byte-level corruption of the output.Reproducer:
5130 = 0x140Ais the little-endian byte pattern of the twouint16inputs packed into oneint32, confirming the kernel was specialized for the wrong dtype.Switching the loop to use the
inputs_/constant_ids_members restores the dtype suffix and the bug goes away.Verified locally against the issue reproducer, exhaustive dtype matrix, and fresh-process isolation tests (44/44 + 9/9 pass). I can add an inline regression test if you guys would like a specific format / location.
Fixes #3338.
Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes