Repository navigation
Fix conv input gradient shape with uneven or large padding - #4662
Merged
Merged
Conversation
zcbenz
force-pushed
the
fix/conv-vjp-padding
branch
from
October 9, 2026 10:17
6d15473 to
95c98c6
Compare
This was referenced Oct 13, 2026
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.
I used AI to help me diagnose and find this bug. All work coding and testing was done by me.
The input gradient of
conv_generalcomes back with the wrong shape when the padding is uneven or larger thankernel - 1. In most cases you get the wrong shape sometimes it raises a confusing[conv] Spatial dimensions of input after padding cannot be smaller than weight...error from inside the backward pass. Mostly it's silent, but not always. I ran into this bug when trying out the keras-mlx backend.With the same input and kernel 3, padding (1, 1) is fine, but (2, 0) and (3, 3) both give (1, 4, 2), and (4, 0) gives (1, 0, 2).
There were two bugs in
Convolution::vjp. The input-gradpadding_hiwas computed frompadding_hi_when it should usepadding_lo_. The two are equal for symmetric padding, so it never showed up. And when the padding is larger thankernel - 1, the backward padding goes negative and was passed straight intoconv_general. My proposed fix is to usepadding_lo_and clamp the backward padding to 0, letting the existing negative-padding crop handle the rest.The new
test_padding_input_gradfails on main and passes with the fix. It compares the grad against an explicitmx.pad+ unpadded conv for 9 cases (uneven, large, strided, 1-D and 2-D and 3-D).Built and tested on Linux CPU, CUDA and m4 chip (CPU).