Rewrites can produce constants whose data is read-only, and pytorch_typify_tensor hands them straight to torch.as_tensor, which shares memory and warns that writes are undefined behavior. Anything running filterwarnings = error fails on it.
import numpy as np
import pytensor
import pytensor.tensor as pt
x = pt.vector("x")
fn = pytensor.function([x], pt.grad((x**2).sum(), x), mode="PYTORCH")
fn(np.ones(3)) # UserWarning: The given NumPy array is not writable
The read-only array comes from TensorConstant.unique_value, so the forward pass is fine and only a rewrite in the gradient trips it.
Potential fix (requires testing):
def pytorch_typify_tensor(data, dtype=None, **kwargs):
if isinstance(data, np.ndarray) and not data.flags.writeable:
data = data.copy()
return torch.as_tensor(data, dtype=dtype)
Rewrites can produce constants whose data is read-only, and
pytorch_typify_tensorhands them straight totorch.as_tensor, which shares memory and warns that writes are undefined behavior. Anything runningfilterwarnings = errorfails on it.The read-only array comes from
TensorConstant.unique_value, so the forward pass is fine and only a rewrite in the gradient trips it.Potential fix (requires testing):