-
Notifications
You must be signed in to change notification settings - Fork 4
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
ENH: pad
: add delegation
#72
Draft
lucascolley
wants to merge
3
commits into
data-apis:main
Choose a base branch
from
lucascolley:pad-delegate
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
Changes from all commits
Commits
Show all changes
3 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 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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
create_diagonal | ||
expand_dims | ||
kron | ||
pad | ||
setdiff1d | ||
sinc | ||
``` |
This file contains 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 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,61 @@ | ||
"""Delegators to existing implementations for Public API Functions.""" | ||
|
||
from ._lib import _funcs | ||
from ._lib._utils._compat import ( | ||
array_namespace, | ||
is_cupy_namespace, | ||
is_jax_namespace, | ||
is_numpy_namespace, | ||
is_torch_namespace, | ||
) | ||
from ._lib._utils._typing import Array, ModuleType | ||
|
||
|
||
def pad( | ||
x: Array, | ||
pad_width: int, | ||
mode: str = "constant", | ||
*, | ||
constant_values: bool | int | float | complex = 0, | ||
xp: ModuleType | None = None, | ||
) -> Array: | ||
""" | ||
Pad the input array. | ||
|
||
Parameters | ||
---------- | ||
x : array | ||
Input array. | ||
pad_width : int | ||
Pad the input array with this many elements from each side. | ||
mode : str, optional | ||
Only "constant" mode is currently supported, which pads with | ||
the value passed to `constant_values`. | ||
constant_values : python scalar, optional | ||
Use this value to pad the input. Default is zero. | ||
xp : array_namespace, optional | ||
The standard-compatible namespace for `x`. Default: infer. | ||
|
||
Returns | ||
------- | ||
array | ||
The input array, | ||
padded with ``pad_width`` elements equal to ``constant_values``. | ||
""" | ||
xp = array_namespace(x) if xp is None else xp | ||
|
||
if mode != "constant": | ||
msg = "Only `'constant'` mode is currently supported" | ||
raise NotImplementedError(msg) | ||
|
||
# https://github.com/pytorch/pytorch/blob/cf76c05b4dc629ac989d1fb8e789d4fac04a095a/torch/_numpy/_funcs_impl.py#L2045-L2056 | ||
if is_torch_namespace(xp): | ||
pad_width = xp.asarray(pad_width) | ||
pad_width = xp.broadcast_to(pad_width, (x.ndim, 2)) | ||
pad_width = xp.flip(pad_width, axis=(0,)).flatten() | ||
return xp.nn.functional.pad(x, (pad_width,), value=constant_values) | ||
|
||
if is_numpy_namespace(xp) or is_jax_namespace(xp) or is_cupy_namespace(xp): | ||
return xp.pad(x, pad_width, mode, constant_values=constant_values) | ||
|
||
return _funcs.pad(x, pad_width, constant_values=constant_values, xp=xp) |
This file contains 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 +1 @@ | ||
"""Modules housing private functions.""" | ||
"""Array-agnostic implementations for the public API.""" |
This file was deleted.
Oops, something went wrong.
This file contains 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 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 @@ | ||
"""Modules housing private utility functions.""" |
This file contains 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,31 @@ | ||
"""Acquire helpers from array-api-compat.""" | ||
# Allow packages that vendor both `array-api-extra` and | ||
# `array-api-compat` to override the import location | ||
|
||
try: | ||
from ...._array_api_compat_vendor import ( # pyright: ignore[reportMissingImports] | ||
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. Four dots, srsly |
||
array_namespace, # pyright: ignore[reportUnknownVariableType] | ||
device, # pyright: ignore[reportUnknownVariableType] | ||
is_cupy_namespace, # pyright: ignore[reportUnknownVariableType] | ||
is_jax_namespace, # pyright: ignore[reportUnknownVariableType] | ||
is_numpy_namespace, # pyright: ignore[reportUnknownVariableType] | ||
is_torch_namespace, # pyright: ignore[reportUnknownVariableType] | ||
) | ||
except ImportError: | ||
from array_api_compat import ( # pyright: ignore[reportMissingTypeStubs] | ||
array_namespace, # pyright: ignore[reportUnknownVariableType] | ||
device, | ||
is_cupy_namespace, # pyright: ignore[reportUnknownVariableType] | ||
is_jax_namespace, # pyright: ignore[reportUnknownVariableType] | ||
is_numpy_namespace, # pyright: ignore[reportUnknownVariableType] | ||
is_torch_namespace, # pyright: ignore[reportUnknownVariableType] | ||
) | ||
|
||
__all__ = [ | ||
"array_namespace", | ||
"device", | ||
"is_cupy_namespace", | ||
"is_jax_namespace", | ||
"is_numpy_namespace", | ||
"is_torch_namespace", | ||
] |
This file contains 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
2 changes: 1 addition & 1 deletion
2
src/array_api_extra/_lib/_utils.py → src/array_api_extra/_lib/_utils/_helpers.py
This file contains 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
File renamed without changes.
This file contains 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 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 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.
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.
Nit, optional: importing stdlib things from _utils three levels deep makes it look like there's something tricky on top of stdlib imports. I'd just import them as needed from stdlib.
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.
True for
ModuleType
, but sinceArray
is "custom", I'd rather leave it like this I think. Then whenever we can start using things fromarray-api-typing
, only that file needs to change.EDIT: thinking that through, maybe we should have something like
ArrayNamespace
which can alias toModuleType
for now. But let's see how array-api-typing goes.