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

[BugFix] Make TransformedEnv mirror allow_done_after_reset property of base env #1810

Merged
merged 6 commits into from
Jan 17, 2024
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
1 change: 1 addition & 0 deletions test/mocking_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def __init__(
super().__init__(
device=kwargs.pop("device", "cpu"),
dtype=torch.get_default_dtype(),
allow_done_after_reset=kwargs.pop("allow_done_after_reset", False),
)
self.set_seed(seed)
self.is_closed = False
Expand Down
15 changes: 15 additions & 0 deletions test/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7691,6 +7691,21 @@ def test_independent_reward_specs_from_shared_env(self):
assert base_env.reward_spec.space.minimum == -np.inf
assert base_env.reward_spec.space.maximum == np.inf

def test_allow_done_after_reset(self):
base_env = ContinuousActionVecMockEnv(allow_done_after_reset=True)
assert base_env._allow_done_after_reset
t1 = TransformedEnv(
base_env, transform=RewardClipping(clamp_min=0, clamp_max=4)
)
assert t1._allow_done_after_reset
with pytest.raises(
RuntimeError,
match="_allow_done_after_reset is a read-only property for TransformedEnvs",
):
t1._allow_done_after_reset = False
base_env._allow_done_after_reset = False
assert not t1._allow_done_after_reset


def test_nested_transformed_env():
base_env = ContinuousActionVecMockEnv()
Expand Down
14 changes: 13 additions & 1 deletion torchrl/envs/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ def __init__(
env = env.to(device)
else:
device = env.device
super().__init__(device=None, **kwargs)
super().__init__(device=None, allow_done_after_reset=None, **kwargs)

if isinstance(env, TransformedEnv):
self._set_env(env.base_env, device)
Expand Down Expand Up @@ -679,6 +679,18 @@ def run_type_checks(self, value):
"run_type_checks is a read-only property for TransformedEnvs"
)

@property
def _allow_done_after_reset(self) -> bool:
return self.base_env._allow_done_after_reset

@_allow_done_after_reset.setter
def _allow_done_after_reset(self, value):
if value is None:
return
raise RuntimeError(
"_allow_done_after_reset is a read-only property for TransformedEnvs"
)

@property
def _inplace_update(self):
return self.base_env._inplace_update
Expand Down