Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] use from_file instead of mmap+from_buffer for readonly files #808

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion tensordict/_td.py
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,13 @@ def _unbind(self, dim: int):
is_shared = self._is_shared
is_memmap = self._is_memmap

def empty():
def empty(
batch_size=batch_size,
names=names,
device=device,
is_shared=is_shared,
is_memmap=is_memmap,
):
result = TensorDict(
{}, batch_size=batch_size, names=names, _run_checks=False, device=device
)
Expand Down
2 changes: 1 addition & 1 deletion tensordict/_torch_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from torch.utils._pytree import tree_flatten

def tree_leaves(pytree):
"""torch 2.0 compatible version of tree_leaves."""
"""Torch 2.0 compatible version of tree_leaves."""
return tree_flatten(pytree)[0]


Expand Down
29 changes: 9 additions & 20 deletions tensordict/memmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,18 +737,12 @@ def from_filename(cls, filename, dtype, shape, index=None):
"nested tensors. Please upgrade to a more recent "
"version."
)
if writable:
tensor = torch.from_file(
str(filename),
shared=True,
dtype=dtype,
size=shape.prod(-1).sum().int(),
)
else:
with open(str(filename), "rb") as f:
mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
tensor = torch.frombuffer(mm, dtype=dtype)
# mm.close()
tensor = torch.from_file(
str(filename),
shared=writable,
dtype=dtype,
size=shape.prod(-1).sum().int(),
)
tensor = torch._nested_view_from_buffer(
tensor,
shape,
Expand All @@ -757,14 +751,9 @@ def from_filename(cls, filename, dtype, shape, index=None):
else:
shape = torch.Size(shape)
# whether the file already existed
if writable:
tensor = torch.from_file(
str(filename), shared=True, dtype=dtype, size=shape.numel()
)
else:
with open(str(filename), "rb") as f:
mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
tensor = torch.frombuffer(mm, dtype=dtype)
tensor = torch.from_file(
str(filename), shared=writable, dtype=dtype, size=shape.numel()
)
tensor = tensor.view(shape)

if index is not None:
Expand Down
17 changes: 8 additions & 9 deletions test/test_memmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,15 +782,14 @@ def test_read_only_nested(self, tmpdir):
assert mmap1.filename == mmap.filename
assert mmap1.filename == data.filename
assert mmap1.filename == data.untyped_storage().filename
with pytest.raises(AssertionError):
assert mmap1.untyped_storage().filename == data.untyped_storage().filename

os.chmod(str(file_path), 0o444)
data.fill_(0)
os.chmod(str(file_path), 0o444)

assert (mmap1[0].view(-1) == 0).all()
assert (mmap1[1].view(-1) == 0).all()
assert mmap1.untyped_storage().filename == data.untyped_storage().filename

# os.chmod(str(file_path), 0o444)
# data.fill_(0)
# os.chmod(str(file_path), 0o444)
#
# assert (mmap1[0].view(-1) == 0).all()
# assert (mmap1[1].view(-1) == 0).all()


if __name__ == "__main__":
Expand Down
Loading