[Metal] Fix 32-bit integer overflow in conv3d unfold kernel#3143
Merged
angeloskath merged 2 commits intoml-explore:mainfrom Feb 19, 2026
Merged
[Metal] Fix 32-bit integer overflow in conv3d unfold kernel#3143angeloskath merged 2 commits intoml-explore:mainfrom
angeloskath merged 2 commits intoml-explore:mainfrom
Conversation
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.
Proposed changes
Fixes #3138$M \times K > 2^{32}$ ) were causing silent data corruption due to 32-bit integer overflow in the naive_unfold_Nd kernel index calculation.
Bug: Large 3D convolutions (
The Fix: Explicitly cast gid's to size_t before multiplying by filter_size to ensure 64-bit arithmetic for the output offset.
Verification: Verified locally (macOS 26.2, MLX 0.30.7, Apple M2) with a script very similar to the one described in the issue. Except using float16, and T=2500, H=64, W=64, C=16, to be just on the edge of 2^32, and my system's memory limit for a single buffer (some 9.5GB).
Result before change:
M*K= 4,423,680,000 > 2^32 max_diff=152.884216 corr=0.956153 [FAIL]Result after change:
M*K= 4,423,680,000 > 2^32 max_diff=0.031334 corr=1.000000 [OK]Didn't add a new test for this because it would need to allocate at least 2^32 * sizeof(type) to reproduce.
Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes