[BUG] MemmapTensor indexing does not behave like indexed torch.Tensor
Open
bug
- Dominant language
- Python
- Stars
- 3.6k
- Forks
- 484
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 207
Description
Describe the bug
When indexing a MemmapTensor, we would expect that the indexed tensor shares the memory of the parent tensor whenever this happens with torch.Tensor.
To Reproduce
from torchrl.data import MemmapTensor
import torch
x = torch.ones(3)
y = MemmapTensor(x)
x0 = x[0]
x0.copy_(torch.zeros([]))
y0 = y[0]
y0.copy_(torch.zeros([]))
print(x, y.clone())
results in
tensor([0., 1., 1.]) tensor([1., 1., 1.])
However this works:
x[None].fill_(0.0)
y[None].fill_(0.0)
print(x, y.clone())
tensor([0., 0., 0.]) tensor([0., 0., 0.])
Reason and Possible fixes
- We could use
torch.from_numpybut this won't work on cuda. - We can create another (sub-)MemmapTensor that shares the same file pointer but has a stored index. The problem then becomes: what happens if the parent MemmapTensor gets out of scope? The file will likely be deleted, and the sub-MemmapTensor will be orphan. Also, working with the sub-MemmapTensor will come with some overhead as every time we access its data we must index the array on disk.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.