Fix MLX Reshape: resolve the shape at funcify time - #2395
Open
guillaume-osmo wants to merge 1 commit into
Open
Conversation
`mlx_funcify_Reshape` forwarded the shape input straight to `mx.reshape`, but the linker typifies every input to `mx.array` while `mx.reshape` only accepts a Python sequence of ints, so every reshape raised `TypeError`. Reading the array back at runtime is not an option either: the linker enables `mx.compile` by default and MLX forbids evaluating a traced array, so the shape has to be resolved when the dispatch is built. Prefer the statically inferred output shape (which already resolves any `-1`, and covers shapes read off another input such as `x.reshape(y.shape)`), then fall back to a constant shape input. A genuinely data-dependent shape now raises `NotImplementedError` with an explanation instead of an eval error from deep inside MLX. This turns four already-red tests in `tests/link/mlx/test_shape.py` green and adds regression tests under the full "MLX" mode.
This was referenced Aug 26, 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.
Closes #2386.
Motivation
mlx_funcify_Reshapeforwarded the shape input straight tomx.reshape, but the linker typifies every input tomx.arrayandmx.reshapeonly accepts a Python sequence of ints, so every reshape raisedTypeError.Reading the array back at runtime is not an option either: the linker enables
mx.compileby default and MLX forbids evaluating a traced array, so the shape has to be resolved when the dispatch is built.Implementation
Resolve the target shape at funcify time, preferring the statically inferred output shape:
node.outputs[0].type.shape, when fully known. This already resolves any-1, and it also covers shapes read off another input (x.reshape(y.shape)), which are notConstants but which PyTensor's shape inference does resolve.Constantshape input.NotImplementedErrorwith an explanation instead of a bare eval error from inside MLX. This is the case the existingtest_mlx_Reshape_shape_graph_inputxfail already describes.Mirrors the approach in
pytensor/link/jax/dispatch/shape.py.Tests
Four tests in
tests/link/mlx/test_shape.pywere already red onmainand are green now:test_mlx_Reshape_constanttest_mlx_Reshape_various_shapestest_mlx_Reshape_negative_onetest_mlx_Reshape_concrete_shapeTwo added, both under the full
"MLX"mode (where the typification actually happens):test_mlx_Reshape_full_mlx_mode— constant,-1, and 1-d/2-d/3-d targetstest_mlx_Reshape_shape_from_other_input—x.reshape(y.shape)Full
tests/link/mlx/suite: no regressions.