Torch.nn.functional.pad return tensor

Hi all, does torch.nn.functional.pad return a view of the tensor or a new tensor?

Hi,
To be more precise, view (Doccumentation) itself returns a new tensor, but it can only be applied for contiguous tensors. So I’m assuming you wanted to know the memory format of the output tensor w.r.t the input tensor. Is that the same you wanted to know?

You can check this using the is_contiguous() operator in PyTorch. torch.nn.functional.pad returns a new tensor with the contiguous memory format. Please see:

In [1]: import torch

In [2]: from torch.nn import functional as F

In [3]: t4d = torch.empty(3, 3, 4, 2)

In [4]: p1d = (1, 1)

In [5]: out = F.pad(t4d, p1d, "constant", 0)

In [6]: out.is_contiguous()
Out[6]: True

Feel free to ping me if it’s not clear to you :slight_smile: Thanks!