forked from data-apis/array-api-extra
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
112def5
commit f90cb78
Showing
13 changed files
with
110 additions
and
33 deletions.
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,59 @@ | ||
"""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 | ||
|
||
value = constant_values | ||
|
||
# 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=value) | ||
|
||
if is_numpy_namespace(x) or is_jax_namespace(xp) or is_cupy_namespace(xp): | ||
return xp.pad(x, pad_width, mode, constant_values=value) | ||
|
||
return _funcs.pad(x, pad_width, mode, constant_values=value, 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] | ||
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