Fix int32 overflow in conv padded input and pad shapes - #4258
Open
eyupcanakman wants to merge 2 commits into
Open
Fix int32 overflow in conv padded input and pad shapes#4258eyupcanakman wants to merge 2 commits into
eyupcanakman wants to merge 2 commits into
Conversation
zcbenz
approved these changes
Aug 15, 2026
zcbenz
force-pushed
the
fix/pad-conv-shape-overflow-3611
branch
from
August 16, 2026 04:42
bd04769 to
a9a7bc1
Compare
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.
Follow-up to #3611 and #3938. #3938 made
conv_out_shapeand the backward pass compute inint64_t, but it guards the output shape. The backends build a padded input buffer ofin + pad_lo + pad_hiin 32 bits, which can overflow even when the output stays in range, because a large stride shrinks the output while the padded input does not.mx.conv_general(mx.ones((1, 8, 1)), mx.ones((1, 3, 1)), stride=3, padding=([2**31-1], [2**31-1]))returns a valid(1, 1431655767, 1)output, then wraps the padded buffer and writes out of bounds on eval.mx.padcomputes the same 32-bit sum in its own shape.The CPU
explicit_gemmpaths (1D and ND), the Metal Winograd path, andpadnow compute these sums inint64_tand narrow throughsafe_cast, the same way #3938 did for the output shape. In-range convolutions and pads are unchanged.Inputs that used to wrap now raise
std::overflow_error, so Python raisesOverflowError.The Winograd change is hardening for the same class rather than a live fix. With stride one, a padding large enough to overflow the padded input also forces an output near
INT32_MAX, so the output buffer allocation raises first. It is guarded anyway because the tile round-up it does was itself 32-bit.Built CPU-only under ASan and the Metal backend with
-DMLX_BUILD_METAL=ON. The added tests crash or fail on the current code and pass with the fix. The full C++ suite passes 251/251.