-
Notifications
You must be signed in to change notification settings - Fork 33
ENH: Adding MLX #1008
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
Draft
prady0t
wants to merge
4
commits into
data-apis:main
Choose a base branch
from
prady0t:mlx-support
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.
Draft
ENH: Adding MLX #1008
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -2,11 +2,27 @@ | |
| # Allow packages that vendor both `array-api-extra` and | ||
| # `array-api-compat` to override the import location | ||
|
|
||
| # pylint: disable=duplicate-code | ||
| from functools import cache | ||
| from types import ModuleType | ||
| from typing import TYPE_CHECKING, ClassVar | ||
|
|
||
| if TYPE_CHECKING: # pragma: no cover | ||
| from typing_extensions import override | ||
| else: | ||
|
|
||
| def override(func): | ||
| return func | ||
|
|
||
|
|
||
| # pylint: disable=duplicate-code,redefined-outer-name | ||
| try: | ||
| from ..._array_api_compat_vendor import ( | ||
| array_namespace, | ||
| device, | ||
| array_namespace as _array_namespace, | ||
| ) | ||
| from ...._array_api_compat_vendor import ( | ||
| device as _device, | ||
| ) | ||
| from ...._array_api_compat_vendor import ( | ||
| is_array_api_obj, | ||
| is_array_api_strict_namespace, | ||
| is_cupy_array, | ||
|
|
@@ -24,12 +40,18 @@ | |
| is_torch_namespace, | ||
| is_writeable_array, | ||
| size, | ||
| to_device, | ||
| ) | ||
| from ...._array_api_compat_vendor import ( | ||
| to_device as _to_device, | ||
| ) | ||
| except ImportError: | ||
| from array_api_compat import ( | ||
| array_namespace, | ||
| device, | ||
| array_namespace as _array_namespace, | ||
| ) | ||
| from array_api_compat import ( | ||
| device as _device, | ||
| ) | ||
| from array_api_compat import ( | ||
| is_array_api_obj, | ||
| is_array_api_strict_namespace, | ||
| is_cupy_array, | ||
|
|
@@ -47,8 +69,131 @@ | |
| is_torch_namespace, | ||
| is_writeable_array, | ||
| size, | ||
| to_device, | ||
| ) | ||
| from array_api_compat import ( | ||
| to_device as _to_device, | ||
| ) | ||
|
|
||
|
|
||
| class _MLXNamespaceInfo: | ||
| def __init__(self, info: object) -> None: | ||
| self._info = info | ||
|
|
||
| def __getattr__(self, name: str) -> object: | ||
| return getattr(self._info, name) | ||
|
|
||
| def default_dtypes(self, *, device: object = None) -> object: | ||
| _ = device | ||
| return self._info.default_dtypes() # type: ignore[attr-defined] | ||
|
Comment on lines
+78
to
+87
Contributor
Author
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. Most of the changes in these files should be in |
||
|
|
||
|
|
||
| class _MLXNamespace(ModuleType): | ||
| _device_functions: ClassVar[set[str]] = { | ||
| "arange", | ||
| "asarray", | ||
| "empty", | ||
| "eye", | ||
| "full", | ||
| "linspace", | ||
| "ones", | ||
| "zeros", | ||
| } | ||
|
|
||
| def __init__(self, namespace: ModuleType) -> None: | ||
| super().__init__(namespace.__name__) | ||
| self._namespace = namespace | ||
|
|
||
| @override | ||
| def __getattr__(self, name: str) -> object: | ||
| if name == "__array_api_version__": | ||
| return "2024.12" | ||
| if name == "bool": | ||
| return self._namespace.bool_ | ||
| if name == "__array_namespace_info__": | ||
| info = getattr(self._namespace, name) | ||
| return lambda: _MLXNamespaceInfo(info()) | ||
| if name == "signbit": | ||
| return lambda x: x < 0 | ||
| function = getattr(self._namespace, name) | ||
| if name not in {"argsort", "astype", "result_type", "sort"} and ( | ||
| name not in self._device_functions | ||
| ): | ||
| return function | ||
|
|
||
| def compatible_call( | ||
| *args: object, | ||
| device: object = None, | ||
| copy: bool | None = None, | ||
| **kwargs: object, | ||
| ) -> object: | ||
| if name == "full" and "fill_value" in kwargs: | ||
| args = (*args, kwargs.pop("fill_value")) | ||
| if name == "asarray" and copy is not None: | ||
| kwargs["copy"] = copy | ||
| if name == "asarray" and "dtype" not in kwargs: | ||
| input_array = args[0] if args else kwargs.get("a") | ||
| input_dtype = getattr(input_array, "dtype", None) | ||
| dtype_name = getattr(input_dtype, "name", None) | ||
| if dtype_name is not None and hasattr(self._namespace, dtype_name): | ||
| kwargs["dtype"] = getattr(self._namespace, dtype_name) | ||
| if name == "astype": | ||
| _ = copy | ||
| return function(*args, **kwargs) | ||
| if name in {"argsort", "sort"}: | ||
| kwargs.pop("stable", None) | ||
| if name == "result_type": | ||
| import mlx.core as mx | ||
|
|
||
| args = tuple( | ||
| mx.asarray(value) | ||
|
Check failure on line 148 in src/array_api_extra/_lib/_compat.py
|
||
| if isinstance(value, int | float | complex | bool) | ||
| else value | ||
| for value in args | ||
| ) | ||
| if device is None: | ||
| return function(*args, **kwargs) | ||
| import mlx.core as mx | ||
|
|
||
| with mx.stream(device): | ||
| return function(*args, **kwargs) | ||
|
|
||
| return compatible_call | ||
|
|
||
|
|
||
| @cache | ||
| def _wrap_mlx_namespace(namespace: ModuleType) -> ModuleType: | ||
| return _MLXNamespace(namespace) | ||
|
|
||
|
|
||
| def array_namespace( | ||
| *xs: object, api_version: str | None = None, use_compat: bool | None = None | ||
| ) -> ModuleType: | ||
| namespace = _array_namespace(*xs, api_version=api_version, use_compat=use_compat) | ||
| if namespace.__name__ == "mlx.core": | ||
| return _wrap_mlx_namespace(namespace) | ||
| return namespace | ||
|
|
||
|
|
||
| def device(x: object, /) -> object: | ||
| if type(x).__module__.startswith("mlx."): | ||
| import mlx.core as mx | ||
|
|
||
| return mx.default_device() | ||
| return _device(x) | ||
|
|
||
|
|
||
| def to_device(x: object, device: object, /, *, stream: object = None) -> object: | ||
| if type(x).__module__.startswith("mlx."): | ||
| import mlx.core as mx | ||
|
|
||
| if device == "cpu": | ||
| device = mx.cpu | ||
| elif device == "gpu": | ||
| device = mx.gpu | ||
| with mx.stream(device): | ||
| return mx.array(x) | ||
| return _to_device(x, device, stream=stream) | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "array_namespace", | ||
|
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.
0.32.3 is not yet available on conda-forge. See