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

[Performance] Faster __init__ #576

Merged
merged 6 commits into from
Nov 24, 2023
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
30 changes: 13 additions & 17 deletions tensordict/_td.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,33 +196,29 @@ def __init__(
_is_shared: bool | None = False,
_is_memmap: bool | None = False,
) -> None:
self._lock_id = set()
self._locked_tensordicts = []

self._is_shared = _is_shared
self._is_memmap = _is_memmap
if device is not None and isinstance(device, (int, str)):
device = torch.device(device)
self._device = device

self._tensordict = _tensordict = _StringOnlyDict()
if not _run_checks:
_tensordict: dict = _StringOnlyDict()
self._batch_size = batch_size
for key, value in source.items():
if isinstance(value, dict):
value = TensorDict(
value,
batch_size=self._batch_size,
device=self._device,
_run_checks=_run_checks,
_is_shared=_is_shared,
_is_memmap=_is_memmap,
)
_tensordict[key] = value
self._tensordict = _tensordict
if source: # faster than calling items
for key, value in source.items():
if isinstance(value, dict):
value = TensorDict(
value,
batch_size=self._batch_size,
device=self._device,
_run_checks=_run_checks,
_is_shared=_is_shared,
_is_memmap=_is_memmap,
)
_tensordict[key] = value
self._td_dim_names = names
else:
self._tensordict = _StringOnlyDict()
if not isinstance(source, (TensorDictBase, dict)):
raise ValueError(
"A TensorDict source is expected to be a TensorDictBase "
Expand Down
22 changes: 22 additions & 0 deletions tensordict/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3758,6 +3758,28 @@ def _propagate_lock(self, lock_ids=None):
else:
self._locked_tensordicts += _locked_tensordicts

@property
def _lock_id(self):
_lock_id = self.__dict__.get("__lock_id", None)
if _lock_id is None:
_lock_id = self.__dict__["__lock_id"] = set()
return _lock_id

@_lock_id.setter
def _lock_id(self, value):
self.__dict__["__lock_id"] = value

@property
def _locked_tensordicts(self):
_locked_tensordicts = self.__dict__.get("__locked_tensordicts", None)
if _locked_tensordicts is None:
_locked_tensordicts = self.__dict__["__locked_tensordicts"] = []
return _locked_tensordicts

@_locked_tensordicts.setter
def _locked_tensordicts(self, value):
self.__dict__["__locked_tensordicts"] = value

@as_decorator("is_locked")
def lock_(self) -> T:
if self.is_locked:
Expand Down
8 changes: 4 additions & 4 deletions test/test_tensordict.py
Original file line number Diff line number Diff line change
Expand Up @@ -6123,10 +6123,10 @@ def test_empty_tensordict_list_stack(self):
a0 = td0["a"]
b0 = td0["a", "b"]
c0 = td0["a", "b", "c"]
assert not hasattr(td, "_locked_tensordicts")
assert not hasattr(a, "_locked_tensordicts")
assert not hasattr(b, "_locked_tensordicts")
assert not hasattr(c, "_locked_tensordicts")
assert not td._locked_tensordicts
assert not a._locked_tensordicts
assert not b._locked_tensordicts
assert not c._locked_tensordicts
assert len(a0._locked_tensordicts)
assert len(b0._locked_tensordicts)
td.unlock_()
Expand Down
Loading