-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `pt.pad` * Refactor linspace, logspace, and geomspace to match numpy implementation * Add `pt.flip` * Move `flip` to `tensor/subtensor.py`, add docstring * Move `slice_at_axis` to `tensor/subtensor` and expose it in `pytensor.tensor`
- Loading branch information
1 parent
f489cf4
commit 981688c
Showing
11 changed files
with
1,632 additions
and
40 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
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,53 @@ | ||
import jax.numpy as jnp | ||
import numpy as np | ||
|
||
from pytensor.link.jax.dispatch import jax_funcify | ||
from pytensor.tensor.pad import Pad | ||
|
||
|
||
@jax_funcify.register(Pad) | ||
def jax_funcify_pad(op, **kwargs): | ||
pad_mode = op.pad_mode | ||
reflect_type = op.reflect_type | ||
has_stat_length = op.has_stat_length | ||
|
||
if pad_mode == "constant": | ||
|
||
def constant_pad(x, pad_width, constant_values): | ||
return jnp.pad(x, pad_width, mode=pad_mode, constant_values=constant_values) | ||
|
||
return constant_pad | ||
|
||
elif pad_mode == "linear_ramp": | ||
|
||
def lr_pad(x, pad_width, end_values): | ||
# JAX does not allow a dynamic input if end_values is non-scalar | ||
if not isinstance(end_values, int | float): | ||
end_values = tuple(np.array(end_values)) | ||
return jnp.pad(x, pad_width, mode=pad_mode, end_values=end_values) | ||
|
||
return lr_pad | ||
|
||
elif pad_mode in ["maximum", "minimum", "mean"] and has_stat_length: | ||
|
||
def stat_pad(x, pad_width, stat_length): | ||
# JAX does not allow a dynamic input here, need to cast to tuple | ||
return jnp.pad( | ||
x, pad_width, mode=pad_mode, stat_length=tuple(np.array(stat_length)) | ||
) | ||
|
||
return stat_pad | ||
|
||
elif pad_mode in ["reflect", "symmetric"]: | ||
|
||
def loop_pad(x, pad_width): | ||
return jnp.pad(x, pad_width, mode=pad_mode, reflect_type=reflect_type) | ||
|
||
return loop_pad | ||
|
||
else: | ||
|
||
def pad(x, pad_width): | ||
return jnp.pad(x, pad_width, mode=pad_mode) | ||
|
||
return pad |
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.