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] Fix zero-ing from specs in RewardSum #860

Merged
merged 2 commits into from
Jan 24, 2023
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
2 changes: 2 additions & 0 deletions test/test_rb.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,8 @@ def test_smoke_replay_buffer_transform(transform):

@pytest.mark.parametrize("transform", transforms)
def test_smoke_replay_buffer_transform_no_inkeys(transform):
if PinMemoryTransform is PinMemoryTransform and not torch.cuda.is_available():
raise pytest.skip("No CUDA device detected, skipping PinMemory")
rb = ReplayBuffer(collate_fn=lambda x: torch.stack(x, 0), transform=transform())

td = TensorDict({"observation": torch.randn(3, 3, 3, 16, 1)}, [])
Expand Down
15 changes: 7 additions & 8 deletions torchrl/envs/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2632,19 +2632,18 @@ def reset(self, tensordict: TensorDictBase) -> TensorDictBase:
)
for in_key, out_key in zip(self.in_keys, self.out_keys):
if out_key in tensordict.keys():
z = torch.zeros_like(tensordict[out_key])
_reset = _reset.view_as(z)
tensordict[out_key][_reset] = z[_reset]
value = tensordict[out_key]
dtype = value.dtype
tensordict[out_key] = value * (~_reset).to(dtype)
elif in_key == "reward":
# Since the episode reward is not in the tensordict, we need to allocate it
# with zeros entirely (regardless of the _reset mask)
z = self.parent.reward_spec.zero(self.parent.batch_size)
tensordict[out_key] = z
tensordict[out_key] = self.parent.reward_spec.zero()
else:
try:
tensordict[out_key] = self.parent.observation_spec[in_key].zero(
self.parent.batch_size
)
tensordict[out_key] = self.parent.observation_spec[
in_key
].zero()
except KeyError as err:
raise KeyError(
f"The key {in_key} was not found in the parent "
Expand Down