I was surprised that tensor.as_strided() doesn’t correct for the offset when the tensor is not at the base of the underlying storage:
import torch
matrix = torch.arange(20).view(4,5)
print(matrix)
row = matrix[2]
print(row)
patchOnRow = row.as_strided( (2,), (1,), 0)
print(patchOnRow)
patchOnMatrix = matrix.as_strided( (2,), (1,), 0)
print(patchOnMatrix)
tensor([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
tensor([10, 11, 12, 13, 14])
tensor([0, 1])
tensor([0, 1])
The documentation says “the offset in the underlying storage”, which is indeed what it does, but I can’t think of a time when you would not want it to be from the offset of the tensor you called it with.
Perhaps including a note in the docs that you should add tensor.storage_offset() if there is any chance you are operating on a tensor that isn’t at the start of the storage.