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

[BugFix] Better tensor allocation in memmap_like #543

Merged
merged 2 commits into from
Oct 11, 2023
Merged
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
30 changes: 21 additions & 9 deletions tensordict/tensordict.py
Original file line number Diff line number Diff line change
Expand Up @@ -2546,22 +2546,34 @@ def memmap_like(self, prefix: str | None = None) -> T:
if prefix is not None:
# ensure subdirectory exists
os.makedirs(prefix / key, exist_ok=True)
tensordict[key] = value.memmap_like(
prefix=prefix / key,
tensordict._set_str(
key,
value.memmap_like(
prefix=prefix / key,
),
inplace=False,
validated=True,
)
torch.save(
{"batch_size": value.batch_size, "device": value.device},
prefix / key / "meta.pt",
)
else:
tensordict[key] = value.memmap_like()
tensordict._set_str(
key, value.memmap_like(), inplace=False, validated=True
)
continue
else:
tensordict[key] = MemmapTensor.empty_like(
value,
filename=str(prefix / f"{key}.memmap")
if prefix is not None
else None,
tensordict._set_str(
key,
MemmapTensor.empty_like(
value,
filename=str(prefix / f"{key}.memmap")
if prefix is not None
else None,
),
inplace=False,
validated=True,
)
if prefix is not None:
torch.save(
Expand Down Expand Up @@ -4806,7 +4818,7 @@ def to(tensor):

apply_kwargs = {}
if device is not None or dtype is not None:
apply_kwargs["device"] = device
apply_kwargs["device"] = device if device is not None else self.device
apply_kwargs["batch_size"] = batch_size
result = result._fast_apply(to, **apply_kwargs)
elif batch_size is not None:
Expand Down