Skip to content

Commit

Permalink
[Feature] spec.is_empty(recurse)
Browse files Browse the repository at this point in the history
ghstack-source-id: faa3b1df5133c77462d6dd013d3854d684cc7e94
Pull Request resolved: #2596
  • Loading branch information
vmoens committed Nov 24, 2024
1 parent 152bc81 commit 097d8ad
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions torchrl/data/tensor_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4278,8 +4278,22 @@ def shape(self, value: torch.Size):
)
self._shape = _size(value)

def is_empty(self):
"""Whether the composite spec contains specs or not."""
def is_empty(self, recurse: bool = False):
"""Whether the composite spec contains specs or not.
Args:
recurse (bool): whether to recursively assess if the spec is empty.
If ``True``, will return ``True`` if there are no leaves. If ``False``
(default) will return whether there is any spec defined at the root level.
"""
if recurse:
for spec in self._specs.values():
if spec is None:
continue
if isinstance(spec, Composite) and spec.is_empty(recurse=True):
continue
return False
return len(self._specs) == 0

@property
Expand Down

1 comment on commit 097d8ad

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'CPU Benchmark Results'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 097d8ad Previous: 2e82cab Ratio
benchmarks/test_replaybuffer_benchmark.py::test_rb_iterate[TensorDictReplayBuffer-LazyMemmapStorage-RandomSampler-10000] 561.1355713542818 iter/sec (stddev: 0.03454725840617003) 1894.2115977753372 iter/sec (stddev: 0.000036834716955760704) 3.38

This comment was automatically generated by workflow using github-action-benchmark.

CC: @vmoens

Please sign in to comment.