pytorch / pytorch/rl

[BUG] MemmapTensor indexing does not behave like indexed torch.Tensor

Open
#342 0 comments 0 reactions 1 assignee View on GitHub

@vmoens is already working on this.

Since Aug 6, 2022.

  • #347 by @vmoens — closed without merging
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_numpy but 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.