Skip to content

Commit

Permalink
[Feature] Make default rollout tensordict contiguous (pytorch#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmoens authored Aug 6, 2022
1 parent ffe906c commit 2f57154
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions torchrl/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ def rollout(
auto_reset: bool = True,
auto_cast_to_device: bool = False,
break_when_any_done: bool = True,
return_contiguous: bool = True,
tensordict: Optional[TensorDictBase] = None,
) -> TensorDictBase:
"""Executes a rollout in the environment.
Expand All @@ -435,6 +436,7 @@ def rollout(
auto_cast_to_device (bool, optional): if True, the device of the tensordict is automatically cast to the
policy device before the policy is used. Default is `False`.
break_when_any_done (bool): breaks if any of the done state is True. Default is True.
return_contiguous (bool): if False, a LazyStackedTensorDict will be returned. Default is True.
tensordict (TensorDict, optional): if auto_reset is False, an initial
tensordict must be provided.
Expand Down Expand Up @@ -485,6 +487,8 @@ def policy(td):
raise Exception("reset env before calling rollout!")

out_td = torch.stack(tensordicts, len(self.batch_size))
if return_contiguous:
return out_td.contiguous()
return out_td

def _select_observation_keys(self, tensordict: TensorDictBase) -> Iterator[str]:
Expand Down

0 comments on commit 2f57154

Please sign in to comment.