-
Couldn't load subscription status.
- Fork 146
Add more MLX dispatches #1684
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
Merged
+178
−7
Merged
Add more MLX dispatches #1684
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import mlx.core as mx | ||
|
|
||
| from pytensor.link.mlx.dispatch.basic import mlx_funcify | ||
| from pytensor.tensor.extra_ops import CumOp, Repeat | ||
|
|
||
|
|
||
| @mlx_funcify.register(CumOp) | ||
| def mlx_funcify_CumOp(op, **kwargs): | ||
| axis = op.axis | ||
| mode = op.mode | ||
|
|
||
| def cumop(x, axis=axis, mode=mode): | ||
| match mode: | ||
| case "add": | ||
| return mx.cumsum(x, axis=axis) | ||
| case "mul": | ||
| return mx.cumprod(x, axis=axis) | ||
| case _: | ||
| raise NotImplementedError(f"CumOp mode {mode} not implemented in MLX") | ||
|
|
||
| return cumop | ||
|
|
||
|
|
||
| @mlx_funcify.register(Repeat) | ||
| def jax_funcify_Repeat(op, **kwargs): | ||
| axis = op.axis | ||
|
|
||
| def repeat(x, repeats, axis=axis): | ||
| if not isinstance(repeats, int): | ||
| raise NotImplementedError( | ||
| "MLX repeat does not support sequence-valued repeat argument." | ||
| ) | ||
| return mx.repeat(x, repeats, axis=axis) | ||
|
|
||
| return repeat | ||
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 |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import warnings | ||
|
|
||
| import mlx.core as mx | ||
|
|
||
| from pytensor.link.mlx.dispatch.basic import mlx_funcify | ||
| from pytensor.tensor.sort import ArgSortOp, SortOp | ||
|
|
||
|
|
||
| @mlx_funcify.register(SortOp) | ||
| def mlx_funcify_Sort(op, **kwargs): | ||
| kind = op.kind | ||
| if kind != "quicksort": | ||
| warnings.warn( | ||
| message=f"MLX sort does not support the kind argument (got kind={kind}). The argument will be " | ||
| f"ignored.", | ||
| category=UserWarning, | ||
| ) | ||
|
|
||
| def sort(x, axis): | ||
| return mx.sort(x, axis=axis) | ||
|
|
||
| return sort | ||
|
|
||
|
|
||
| @mlx_funcify.register(ArgSortOp) | ||
| def mlx_funcify_ArgSort(op, **kwargs): | ||
| kind = op.kind | ||
| if kind != "quicksort": | ||
| warnings.warn( | ||
| message=f"MLX argsort does not support the kind argument (got kind={kind}). The argument will be " | ||
| f"ignored.", | ||
| category=UserWarning, | ||
| ) | ||
|
|
||
| def argsort(x, axis): | ||
| return mx.argsort(x, axis=axis) | ||
|
|
||
| return argsort |
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
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
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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from pytensor.configdefaults import config | ||
| from pytensor.tensor import extra_ops as pt_extra_ops | ||
| from pytensor.tensor.type import matrix | ||
| from tests.link.mlx.test_basic import compare_mlx_and_py | ||
|
|
||
|
|
||
| mx = pytest.importorskip("mlx.core") | ||
|
|
||
|
|
||
| def test_extra_ops(): | ||
| a = matrix("a") | ||
| a_test = np.arange(6, dtype=config.floatX).reshape((3, 2)) | ||
|
|
||
| out = pt_extra_ops.cumsum(a, axis=0) | ||
| compare_mlx_and_py([a], [out], [a_test]) | ||
|
|
||
| out = pt_extra_ops.cumprod(a, axis=1) | ||
| compare_mlx_and_py([a], [out], [a_test]) | ||
|
|
||
| out = pt_extra_ops.repeat(a, 3, axis=1) | ||
| compare_mlx_and_py([a], [out], [a_test]) |
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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from pytensor.tensor.sort import argsort, sort | ||
| from pytensor.tensor.type import matrix | ||
| from tests.link.mlx.test_basic import compare_mlx_and_py | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("axis", [None, -1]) | ||
| @pytest.mark.parametrize("func", (sort, argsort)) | ||
| def test_sort(func, axis): | ||
| x = matrix("x", shape=(2, 2), dtype="float64") | ||
| out = func(x, axis=axis) | ||
| arr = np.array([[1.0, 4.0], [5.0, 2.0]]) | ||
| compare_mlx_and_py([x], [out], [arr]) | ||
|
|
||
|
|
||
| def test_sort_invalid_kind_warning(): | ||
| x = matrix("x", shape=(2, 2), dtype="float64") | ||
| z = sort(x, axis=-1, kind="mergesort") | ||
| with pytest.warns(UserWarning, match="MLX sort does not support the kind argument"): | ||
| z.eval({x: np.array([[3.0, 1.0], [2.0, 4.0]])}, mode="MLX") |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is known at dispatch time, raise then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
repeatsis a symbolic input. We only know axis as dispatch time.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a weird mlx-specific limitation. There might be a work-around, but I don't want to do it in this PR. Just getting some common cases covered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you know whether repeats is 0d or 1d at dispatch time? actually isn't the op always 1d now and we use alloc for 0d?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How? It's a symbolic input
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh by checking
op.inputs[1]:fivehead:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
by checking node.inputs. Node is the second argument to all dispatches