-
Notifications
You must be signed in to change notification settings - Fork 202
Support dynamic shape-derived bounds in MLX ARange #2260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cetagostini
wants to merge
5
commits into
pymc-devs:main
Choose a base branch
from
cetagostini:mlx-arange-dynamic-shape
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+97
−17
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9f4da8d
Support dynamic shape-derived bounds in MLX ARange
cetagostini cbbf903
Detect data-dependent ARange bounds statically on MLX
cetagostini d932488
Merge remote-tracking branch 'upstream/main' into mlx-arange-dynamic-…
cetagostini 61e0f7f
Cover root-input ARange bound rejection on MLX
cetagostini 83b06b9
Merge remote-tracking branch 'upstream/main' into mlx-arange-dynamic-…
cetagostini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| import mlx.core as mx | ||
| import numpy as np | ||
|
|
||
| from pytensor.graph.basic import Constant | ||
| from pytensor.graph.traversal import walk | ||
| from pytensor.link.mlx.dispatch.basic import convert_dtype_to_mlx, mlx_funcify | ||
| from pytensor.tensor import get_vector_length | ||
| from pytensor.tensor.basic import ( | ||
|
|
@@ -17,6 +19,7 @@ | |
| get_scalar_constant_value, | ||
| ) | ||
| from pytensor.tensor.exceptions import NotScalarConstantError | ||
| from pytensor.tensor.shape import Shape, Shape_i | ||
|
|
||
|
|
||
| MLX_DYNAMIC_SHAPE_ERROR = ( | ||
|
|
@@ -178,30 +181,57 @@ def alloc(x, *shape): | |
| return alloc | ||
|
|
||
|
|
||
| ARANGE_CONCRETE_VALUE_ERROR = ( | ||
| "MLX's arange requires all arguments (start, stop, step) to be concrete " | ||
| "Python int/float values, not symbolic variables. Unlike NumPy and JAX, " | ||
| "MLX does not accept array inputs for arange at all." | ||
| "\n\nAn example of a valid graph:" | ||
| "\n>>> import pytensor.tensor as pt" | ||
| "\n>>> pt.arange(1, 10, 2)" | ||
| ARANGE_DATA_DEPENDENT_ERROR = ( | ||
| "MLX cannot build arange with a data-dependent length: the bounds depend on " | ||
| "runtime array values, so the output shape is unknown at compile time. " | ||
| "Constant and shape-derived bounds (e.g. pt.arange(x.shape[0])) are supported." | ||
| ) | ||
|
|
||
|
|
||
| @mlx_funcify.register(ARange) | ||
| def mlx_funcify_ARange(op, node, **kwargs): | ||
| # MLX's arange only accepts Python int/float, not arrays, | ||
| # so all arguments must be known at graph-construction time. | ||
| def _arange_bound_is_static(var): | ||
| # A bound is concrete under mx.compile when its value derives only from input | ||
| # shapes and constants. Shape ops are barriers: only the shape, not the | ||
| # underlying data, is needed (more general than the JAX dispatch, which only | ||
| # recognizes a bare Shape_i). | ||
| def expand(v): | ||
| owner = v.owner | ||
| if owner is None or isinstance(owner.op, Shape | Shape_i): | ||
| return None | ||
| return owner.inputs | ||
|
|
||
| return all( | ||
| v.owner is not None or isinstance(v, Constant) for v in walk([var], expand) | ||
| ) | ||
|
|
||
|
|
||
| def _arange_static_bound(arg): | ||
| try: | ||
| start, stop, step = [ | ||
| get_scalar_constant_value(arg).item() for arg in node.inputs | ||
| ] | ||
| return get_scalar_constant_value(arg).item() | ||
| except NotScalarConstantError: | ||
| raise NotImplementedError(ARANGE_CONCRETE_VALUE_ERROR) | ||
| return None | ||
|
|
||
|
|
||
| def _arange_runtime_bound(value): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inline this |
||
| return value.item() if hasattr(value, "item") else value | ||
|
|
||
|
|
||
| @mlx_funcify.register(ARange) | ||
| def mlx_funcify_ARange(op, node, **kwargs): | ||
| # mx.arange only accepts Python int/float. Bake constant bounds and resolve | ||
| # shape-derived ones at runtime (concrete under mx.compile even when the | ||
| # static shape is unknown); reject genuinely data-dependent bounds up front. | ||
| if not all(_arange_bound_is_static(arg) for arg in node.inputs): | ||
| raise NotImplementedError(ARANGE_DATA_DEPENDENT_ERROR) | ||
|
|
||
| dtype = convert_dtype_to_mlx(op.dtype) | ||
| static_args = [_arange_static_bound(arg) for arg in node.inputs] | ||
|
|
||
| def arange(*_args): | ||
| return mx.arange(start, stop, step, dtype=dtype) | ||
| def arange(*args): | ||
| resolved = [ | ||
| static if static is not None else _arange_runtime_bound(runtime) | ||
| for static, runtime in zip(static_args, args, strict=True) | ||
| ] | ||
| return mx.arange(*resolved, dtype=dtype) | ||
|
|
||
| return arange | ||
|
|
||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -164,3 +164,53 @@ def test_arange(): | |
| out = arange(1, 10, 2) | ||
|
|
||
| compare_mlx_and_py([], [out], []) | ||
|
|
||
|
|
||
| def test_arange_dynamic_shape(): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you want to test steps? |
||
| # Shape-derived bounds are concrete under mx.compile even when the static | ||
| # shape is unknown, so a genuinely dynamic length must work (regression: this | ||
| # used to raise NotImplementedError because of an over-aggressive constant | ||
| # check). Exercises every position (start/stop/step) being shape-derived, an | ||
| # offset, and an empty result. | ||
| x = pt.vector("x") | ||
| y = pt.vector("y") | ||
| outs = [ | ||
| arange(x.shape[0]), # dynamic stop | ||
| arange(x.shape[0] + 2), # shape-derived expression | ||
| arange(x.shape[0], y.shape[0]), # dynamic start and stop | ||
| arange(0, y.shape[0], x.shape[0]), # dynamic step | ||
| arange(y.shape[0], x.shape[0]), # start > stop -> empty | ||
| ] | ||
| compare_mlx_and_py( | ||
| [x, y], | ||
| outs, | ||
| [np.zeros(3, dtype="float32"), np.zeros(7, dtype="float32")], | ||
| ) | ||
|
|
||
|
|
||
| def test_arange_dynamic_advanced_index(): | ||
| # The motivating case: a vectorized gather lowers to advanced indexing that | ||
| # internally builds arange(idx.shape[0]) with a runtime-dynamic length. | ||
| logp = pt.matrix("logp") | ||
| targets = pt.lvector("targets") | ||
| out = logp[arange(targets.shape[0]), targets] | ||
| compare_mlx_and_py( | ||
| [logp, targets], | ||
| [out], | ||
| [np.arange(12, dtype="float32").reshape(3, 4), np.array([0, 2, 3])], | ||
| ) | ||
|
|
||
|
|
||
| def test_arange_data_dependent_raises(): | ||
| # A genuinely data-dependent length has a runtime-only output shape, which MLX | ||
| # cannot compile. This must fail loudly (at compile time) rather than silently. | ||
| x = pt.vector("x") | ||
| out = arange(pt.sum(x > 0).astype("int64")) | ||
| with pytest.raises(NotImplementedError, match="data-dependent length"): | ||
| pytensor.function([x], out, mode=compile_mode) | ||
|
|
||
|
|
||
| def test_arange_root_input_raises(): | ||
| end = pt.lscalar("end") | ||
| with pytest.raises(NotImplementedError, match="data-dependent length"): | ||
| pytensor.function([end], arange(end), mode=compile_mode) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.