Skip to content

PyTorch backend warns on every read-only constant: torch.as_tensor on non-writable array #2365

Description

@jessegrabowski

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtorchPyTorch backend

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions