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

[Feature,WIP] unique_traj arg in slice sampler #1962

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
30 changes: 27 additions & 3 deletions torchrl/data/replay_buffers/samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,11 @@ class SliceSampler(Sampler):
Be mindful that this can result in effective `batch_size` shorter
than the one asked for! Trajectories can be split using
:func:`~torchrl.collectors.split_trajectories`. Defaults to ``True``.
unique_traj (bool, optional): if ``True``, at most one slice of every
stored trajectory will be found in the samples.
If the number of slices required is lower than the number of
trajectories, an error will be thrown.
Defaults to ``False``.

.. note:: To recover the trajectory splits in the storage,
:class:`~torchrl.data.replay_buffers.samplers.SliceSampler` will first
Expand Down Expand Up @@ -707,6 +712,7 @@ def __init__(
cache_values: bool = False,
truncated_key: NestedKey | None = ("next", "truncated"),
strict_length: bool = True,
unique_traj: bool=False,
):
self.num_slices = num_slices
self.slice_len = slice_len
Expand Down Expand Up @@ -758,6 +764,7 @@ def __init__(
"Either num_slices or slice_len must be not None, and not both. "
f"Got num_slices={num_slices} and slice_len={slice_len}."
)
self.unique_traj = unique_traj

@staticmethod
def _find_start_stop_traj(*, trajectory=None, end=None):
Expand Down Expand Up @@ -904,9 +911,12 @@ def _sample_slices(
self, lengths, start_idx, stop_idx, seq_length, num_slices, traj_idx=None
) -> Tuple[Tuple[torch.Tensor, ...], Dict[str, Any]]:
if traj_idx is None:
traj_idx = torch.randint(
lengths.shape[0], (num_slices,), device=lengths.device
)
if self.unique_traj:
traj_idx = torch.multinomial(torch.full((lengths.shape[0],), 1/lengths.shape[0], device=lengths.device), num_slices, replacement=False)
else:
traj_idx = torch.randint(
lengths.shape[0], (num_slices,), device=lengths.device
)
else:
num_slices = traj_idx.shape[0]

Expand Down Expand Up @@ -1054,6 +1064,11 @@ class SliceSamplerWithoutReplacement(SliceSampler, SamplerWithoutReplacement):
:func:`~torchrl.collectors.split_trajectories`. Defaults to ``True``.
shuffle (bool, optional): if ``False``, the order of the trajectories
is not shuffled. Defaults to ``True``.
unique_traj (bool, optional): if ``True``, at most one slice of every
stored trajectory will be found in the samples.
If the number of slices required is lower than the number of
trajectories, an error will be thrown.
Defaults to ``False``.

.. note:: To recover the trajectory splits in the storage,
:class:`~torchrl.data.replay_buffers.samplers.SliceSamplerWithoutReplacement` will first
Expand Down Expand Up @@ -1128,6 +1143,7 @@ def __init__(
truncated_key: NestedKey | None = ("next", "truncated"),
strict_length: bool = True,
shuffle: bool = True,
unique_traj: bool = False,
):
SliceSampler.__init__(
self,
Expand All @@ -1140,6 +1156,7 @@ def __init__(
strict_length=strict_length,
ends=ends,
trajectories=trajectories,
unique_traj=unique_traj,
)
SamplerWithoutReplacement.__init__(self, drop_last=drop_last, shuffle=shuffle)

Expand Down Expand Up @@ -1232,6 +1249,11 @@ class PrioritizedSliceSampler(SliceSampler, PrioritizedSampler):
Be mindful that this can result in effective `batch_size` shorter
than the one asked for! Trajectories can be split using
:func:`~torchrl.collectors.split_trajectories`. Defaults to ``True``.
unique_traj (bool, optional): if ``True``, at most one slice of every
stored trajectory will be found in the samples.
If the number of slices required is lower than the number of
trajectories, an error will be thrown.
Defaults to ``False``.

Examples:
>>> import torch
Expand Down Expand Up @@ -1288,6 +1310,7 @@ def __init__(
cache_values: bool = False,
truncated_key: NestedKey | None = ("next", "truncated"),
strict_length: bool = True,
unique_traj: bool = False,
):
SliceSampler.__init__(
self,
Expand All @@ -1300,6 +1323,7 @@ def __init__(
strict_length=strict_length,
ends=ends,
trajectories=trajectories,
unique_traj=unique_traj,
)
PrioritizedSampler.__init__(
self,
Expand Down
Loading