-
Notifications
You must be signed in to change notification settings - Fork 11
ENH: pad
: add delegation
#72
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
Merged
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
d17fd2f
ENH: `pad`: add delegation
lucascolley 38690bb
fix vendor test
lucascolley d4d05b0
fixes
lucascolley ea09206
Merge branch 'main' into pad-delegate
lucascolley 9dcb9e5
finish merge
lucascolley 5db1a93
clean up
lucascolley 486ebef
use an enum
lucascolley 44ec95a
Merge branch 'main' into pad-delegate
lucascolley 303c7fc
Merge branch 'main' into pad-delegate
lucascolley 6f72daf
update lockfile
lucascolley a54357b
fix torch delegation
lucascolley 71edc05
typing
lucascolley fd6b9d8
Merge branch 'main' into pad-delegate
lucascolley 1e59fbd
update lockfile
lucascolley e0046c5
combine enums
lucascolley 2bd8205
green
lucascolley 1531841
update lockfile
lucascolley 64db422
tweak docstring
lucascolley ed7fd25
update docstring
lucascolley 93f2591
Update src/array_api_extra/_delegation.py
lucascolley 47e9b5b
fix CI
lucascolley 4c786a2
fix mypy
lucascolley 9daa33a
address review
lucascolley 3ac8e45
flush ci
lucascolley 82e5258
update index
lucascolley 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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
create_diagonal | ||
expand_dims | ||
kron | ||
pad | ||
setdiff1d | ||
sinc | ||
``` |
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,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 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 +1 @@ | ||
"""Modules housing private functions.""" | ||
"""Array-agnostic implementations for the public API.""" |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Modules housing private utility functions.""" |
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,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] | ||
lucascolley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 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
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 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
File renamed without changes.
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.
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.