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 slice sampler end computation at the cursor place #2225

Merged
merged 4 commits into from
Jun 12, 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
37 changes: 28 additions & 9 deletions torchrl/data/replay_buffers/samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,9 @@ def __repr__(self):
)

@classmethod
def _find_start_stop_traj(cls, *, trajectory=None, end=None, at_capacity: bool):
def _find_start_stop_traj(
cls, *, trajectory=None, end=None, at_capacity: bool, cursor=None
):
if trajectory is not None:
# slower
# _, stop_idx = torch.unique_consecutive(trajectory, return_counts=True)
Expand Down Expand Up @@ -954,12 +956,28 @@ def _find_start_stop_traj(cls, *, trajectory=None, end=None, at_capacity: bool):
dim=0,
value=1,
)
elif not end.any(0).all():
# we must have at least one end by traj to delimitate trajectories
else:
# we must have at least one end by traj to individuate trajectories
# so if no end can be found we set it manually
mask = ~end.any(0, True)
mask = torch.cat([torch.zeros_like(end[:-1]), mask])
end = torch.masked_fill(mask, end, 1)
if cursor is not None:
if isinstance(cursor, torch.Tensor):
cursor = cursor[-1].item()
elif isinstance(cursor, range):
cursor = cursor[-1]
if not _is_int(cursor):
raise RuntimeError(
"cursor should be an integer or a 1d tensor or a range."
)
end = torch.index_fill(
end,
index=torch.tensor(cursor, device=end.device, dtype=torch.long),
dim=0,
value=1,
)
if not end.any(0).all():
mask = ~end.any(0, True)
mask = torch.cat([torch.zeros_like(end[:-1]), mask])
end = torch.masked_fill(mask, end, 1)
ndim = end.ndim
if ndim == 0:
raise RuntimeError(
Expand Down Expand Up @@ -994,7 +1012,7 @@ def _end_to_start_stop(end, length):
# In this case we have only one start and stop has already been set
pass
lengths = stop_idx[:, 0] - start_idx[:, 0] + 1
lengths[lengths < 0] = lengths[lengths < 0] + length
lengths[lengths <= 0] = lengths[lengths <= 0] + length
return start_idx, stop_idx, lengths

def _start_to_end(self, st: torch.Tensor, length: int):
Expand Down Expand Up @@ -1072,7 +1090,9 @@ def _get_stop_and_length(self, storage, fallback=True):
"Could not get a tensordict out of the storage, which is required for SliceSampler to compute the trajectories."
)
vals = self._find_start_stop_traj(
end=done.squeeze()[: len(storage)], at_capacity=storage._is_full
end=done.squeeze()[: len(storage)],
at_capacity=storage._is_full,
cursor=getattr(storage, "_last_cursor", None),
)
if self.cache_values:
self._cache["stop-and-length"] = vals
Expand Down Expand Up @@ -1270,7 +1290,6 @@ def _get_index(
],
1,
)

index = self._tensor_slices_from_startend(seq_length, starts, storage_length)
if self.truncated_key is not None:
truncated_key = self.truncated_key
Expand Down
3 changes: 2 additions & 1 deletion torchrl/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2609,9 +2609,10 @@ def rollout(
for key in self.done_keys:
if _ends_with(key, "truncated"):
val = out_td.get(("next", key))
done = out_td.get(("next", _replace_last(key, "done")))
val[(slice(None),) * (out_td.ndim - 1) + (-1,)] = True
out_td.set(("next", key), val)
out_td.set(("next", _replace_last(key, "done")), val)
out_td.set(("next", _replace_last(key, "done")), val | done)
found_truncated = True
if not found_truncated:
raise RuntimeError(
Expand Down
2 changes: 1 addition & 1 deletion torchrl/envs/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3048,7 +3048,7 @@ def unfold_done(done, N):
reset_unfold_list = [torch.zeros_like(reset_unfold_slice)]
for r in reversed(reset_unfold.unbind(-1)):
reset_unfold_list.append(r | reset_unfold_list[-1])
reset_unfold_slice = reset_unfold_list[-1]
# reset_unfold_slice = reset_unfold_list[-1]
reset_unfold = torch.stack(list(reversed(reset_unfold_list))[1:], -1)
reset = reset[prefix + (slice(self.N - 1, None),)]
reset[prefix + (0,)] = 1
Expand Down
Loading