Skip to content

Commit 3ec3be7

Browse files
author
Vincent Moens
committed
[Feature] Allow update to increment the number of tds in a lazy stack
ghstack-source-id: 5b670a8 Pull Request resolved: #1220
1 parent 8bdbf01 commit 3ec3be7

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

tensordict/_lazy.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,13 +2727,16 @@ def update(
27272727
isinstance(input_dict_or_td, LazyStackedTensorDict)
27282728
and input_dict_or_td.stack_dim == self.stack_dim
27292729
):
2730-
if len(input_dict_or_td.tensordicts) != len(self.tensordicts):
2730+
tds = list(self.tensordicts)
2731+
if len(input_dict_or_td.tensordicts) > len(self.tensordicts):
2732+
tds.extend(
2733+
[td.copy() for td in input_dict_or_td.tensordicts[len(tds) :]]
2734+
)
2735+
elif len(input_dict_or_td.tensordicts) != len(self.tensordicts):
27312736
raise ValueError(
27322737
"cannot update stacked tensordicts with different shapes."
27332738
)
2734-
for td_dest, td_source in _zip_strict(
2735-
self.tensordicts, input_dict_or_td.tensordicts
2736-
):
2739+
for td_dest, td_source in _zip_strict(tds, input_dict_or_td.tensordicts):
27372740
td_dest.update(
27382741
td_source,
27392742
clone=clone,

tensordict/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6556,6 +6556,11 @@ def update(
65566556

65576557
.. seealso:: :meth:`~tensordict.is_leaf_nontensor` and :meth:`~tensordict.default_is_leaf`.
65586558

6559+
.. note:: When updating a :class:`~tensordict.LazyStackedTensorDict` with N elements with another
6560+
:class:`~tensordict.LazyStackedTensorDict` with M elements, with M > N, along the stack dimension,
6561+
the ``update`` method will append copies of the extra tensordicts to the dest (self) lazy stack.
6562+
This allows users to rely on ``update`` to increment lazy stacks progressively.
6563+
65596564
Returns:
65606565
self
65616566

0 commit comments

Comments
 (0)