Skip to content

fix(ops): fix convolve padRight for even kernels in same mode - #467

Merged
davidkoski merged 2 commits into
ml-explore:mainfrom
shubhransh-gupta:fix/convolve-same-even-kernel-padding
Sep 14, 2026
Merged

davidkoski merged 2 commits into
ml-explore:mainfrom
shubhransh-gupta:fix/convolve-same-even-kernel-padding

Conversation

@shubhransh-gupta

Copy link
Copy Markdown

Proposed Changes

Resolves #466.

Fixes right-side padding calculation in convolve(_:_:mode:stream:) (Source/MLX/Ops.swift) for .same mode when the kernel length is even.

Root Cause

Previously, when the weight size was even, padRight was computed as max(0, padLeft / 2 - 1) instead of max(0, padLeft - 1). For example, with padLeft = 2 (kernel size 4), padRight incorrectly evaluated to 0 instead of 1, causing the input to be under-padded and the output length for .same mode to be truncated by 1 element.

This change aligns the calculation with the MLX C++ / Python implementation (int pad_r = std::max(0, pad_l - 1)).

Tests

Added testConvolve() in Tests/MLXTests/OpsTests.swift testing .full, .valid, and .same mode outputs with both even and odd kernel sizes.

Checklist

  • I have read the CONTRIBUTING document
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

@simeetnayan81 simeetnayan81 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@shubhransh-gupta

Copy link
Copy Markdown
Author

Hi @davidkoski — Friendly ping on this PR when you get a chance. It fixes convolution right-padding calculation for even kernel sizes under 'same' mode. Tests pass cleanly. Thanks!

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I realized I just found the same issue in #478

Just waiting on CI.

@davidkoski
davidkoski merged commit 555692a into ml-explore:main Sep 14, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] convolve(..., mode: .same) wrong right pad for even kernels

3 participants