Skip to content

Commit 097d8ad

Browse files
author
Vincent Moens
committed
[Feature] spec.is_empty(recurse)
ghstack-source-id: faa3b1d Pull Request resolved: #2596
1 parent 152bc81 commit 097d8ad

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

torchrl/data/tensor_specs.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4278,8 +4278,22 @@ def shape(self, value: torch.Size):
42784278
)
42794279
self._shape = _size(value)
42804280

4281-
def is_empty(self):
4282-
"""Whether the composite spec contains specs or not."""
4281+
def is_empty(self, recurse: bool = False):
4282+
"""Whether the composite spec contains specs or not.
4283+
4284+
Args:
4285+
recurse (bool): whether to recursively assess if the spec is empty.
4286+
If ``True``, will return ``True`` if there are no leaves. If ``False``
4287+
(default) will return whether there is any spec defined at the root level.
4288+
4289+
"""
4290+
if recurse:
4291+
for spec in self._specs.values():
4292+
if spec is None:
4293+
continue
4294+
if isinstance(spec, Composite) and spec.is_empty(recurse=True):
4295+
continue
4296+
return False
42834297
return len(self._specs) == 0
42844298

42854299
@property

0 commit comments

Comments
 (0)