Add reflect and symmetric padding modes to mx.pad#3608
Open
katlun-lgtm wants to merge 1 commit into
Open
Conversation
Implements numpy.pad-compatible "reflect" and "symmetric" modes for mx.pad, matching numpy semantics for arbitrary pad sizes (the reflection repeats when the pad width exceeds the axis length). - mlx/ops.cpp: reflect_pad helper builds a per-axis triangle-wave index map and gathers with take; one take per padded axis. reflect uses period 2(n-1) and skips the edge; symmetric uses period 2n and repeats the edge. n==1 maps to 0. - python/src/ops.cpp: extend the pad mode Literal and docstring. - python/tests/test_ops.py: test_pad_reflect_symmetric covers in-bounds, multi-reflect, asymmetric per-axis, zero-width sides, and degenerate axes (n==1, n==2), checked against numpy.pad. - tests/ops_tests.cpp: reflect/symmetric CHECK cases incl. multi-reflect.
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.
Summary
Adds
numpy.pad-compatible"reflect"and"symmetric"modes tomx.pad. These join the existing"constant"and"edge"modes.Both modes match
numpy.padsemantics for arbitrary pad sizes — when the pad width exceeds the axis length the reflection repeats, exactly as NumPy does. (Earlier attempts at these modes were limited topad < dim; this implementation removes that restriction.)reflect— mirror padding that does not repeat the edge value (period2(n-1)).symmetric— mirror padding that does repeat the edge value (period2n).Implementation
reflect_pad(inmlx/ops.cpp) builds a per-axis index map with a triangle-wave reflection function and gathers withtake— onetakeper padded axis. A degenerate axis of length 1 maps every coordinate to 0. No new primitive or kernel is introduced; it composes existing ops, so it works on every backend and is differentiable for free.Files changed
mlx/ops.cpp—reflect_padhelper +reflect/symmetricdispatch branches inpad.python/src/ops.cpp— extend themodeLiteraland docstring.python/tests/test_ops.py—test_pad_reflect_symmetric.tests/ops_tests.cpp— reflect/symmetricCHECKcases incl. multi-reflect.Testing
All run locally on an M3 Max:
test_pad_reflect_symmetric— 13 shape/pad-width cases × 2 modes compared element-for-element againstnumpy.pad(in-bounds, multi-reflect where pad ≫ axis, asymmetric per-axis, zero-width sides, and degenerate axesn==1,n==2). Exact match.tests/ops_tests.cpp"test pad" — 9 assertions pass.251 cases / 251 passed,3442 assertions / 0 failed.