Skip to content

[BugFix] Draw only long-enough trajectories in SliceSamplerWithoutReplacement with strict_length - #4584

Open
Kayvan-Zahiri wants to merge 1 commit into
pytorch:mainfrom
Kayvan-Zahiri:fix-slice-wor-strict-length
Open

Kayvan-Zahiri wants to merge 1 commit into
pytorch:mainfrom
Kayvan-Zahiri:fix-slice-wor-strict-length

Conversation

@Kayvan-Zahiri

Copy link
Copy Markdown

Description

With strict_length=True, SliceSamplerWithoutReplacement permutes over every trajectory in the storage, short ones included, and _sample_slices drops the short ones only after they've been drawn. Two things go wrong:

  • A batch that drew a short trajectory comes back short.
  • When every trajectory in a draw is short, sample() raises RuntimeError: None of the provided indices pointed to a trajectory of sufficient length in the middle of an epoch.

With trajectory lengths [7, 2, 9, 1, 5, 12, 4, 3], slice_len=3 and batch_size=6, on main:

flat:        of 400 sample() calls, 17 raised RuntimeError; 166 of the 383 returned batches were short
batch_time:  of 400 sample() calls, 19 raised RuntimeError; 162 of the 381 returned batches were short

The strict_length docstring says short trajectories are filtered out, and the with-replacement SliceSampler already filters before it draws. Now the without-replacement sampler permutes only over trajectories with length >= seq_length and maps the drawn positions back to trajectory indices, so an epoch covers exactly the eligible trajectories. If none is long enough, it falls back to the old draw, so the existing "Did not find a single trajectory with sufficient length" error is unchanged. With strict_length=False, nothing changes.

Tests

test_slice_sampler_without_replacement_strict_skips_short is parametrized over flat and batch_time. It checks that 30 draws are all full batches, that no short trajectory is drawn, and that each epoch covers every eligible trajectory. It fails on main and passes here. The 22 without-replacement tests in test/rb/test_samplers.py pass. Across the whole file, the failure set is the same 118 on main and on this branch: all are SumSegmentTreeFp32 is not available, because the C++ extension isn't built in my environment. ufmt (black 22.3.0, usort 1.0.3) and flake8 with the repo config are clean.

I found this with a property check over SliceSampler configurations. No open issue or PR covers it. It is independent of #4583, which fixes the ends=/trajectories= boundaries. AI assistance (Claude Code) was used for the investigation and the patch.

@pytorch-bot

pytorch-bot Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4584

Note: Links to docs will display an error until the docs builds have been completed.

⚠️ 16 Awaiting Approval

As of commit 5b6e976 with merge base e7a05a7 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 16, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. ReplayBuffers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant