Skip to content

[BugFix] Sample every window in RandomCropTensorDict - #4578

Open
jackiectl2 wants to merge 1 commit into
pytorch:mainfrom
jackiectl2:fix-random-crop-last-window
Open

jackiectl2 wants to merge 1 commit into
pytorch:mainfrom
jackiectl2:fix-random-crop-last-window

Conversation

@jackiectl2

Copy link
Copy Markdown

Description

RandomCropTensorDict samples the window start with torch.randint(max_idx_0, ...), which draws from [0, max_idx_0). The last valid window is never sampled, and when the sequence length equals sub_seq_len (max_idx_0 == 0) the call fails with RuntimeError: random_ expects 'from' to be less than 'to', although the length check accepts that case. The masked path has the same off-by-one (floor(rand * (L - sub_seq_len)) never reaches L - sub_seq_len).

This samples the start from [0, max_idx_0] (and [0, L - sub_seq_len] with a mask).

import torch
from tensordict import TensorDict
from torchrl.envs.transforms import RandomCropTensorDict

td = TensorDict({"a": torch.arange(4).expand(1000, 4)}, [1000, 4])
print(set(RandomCropTensorDict(2)(td)["a"][:, 0].tolist()))  # main: {0, 1}; expected {0, 1, 2}
RandomCropTensorDict(4)(td)  # main: RuntimeError

Motivation and Context

close #4577

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I have read the CONTRIBUTION guide (required)
  • I have updated the tests accordingly (required for a bug fix or a new feature).

Tests: added test_crop_full_length and test_crop_samples_every_window (with and without mask) to TestCroSeq; they fail on main and pass with the fix. pytest test/transforms/test_compose_and_env.py: 173 passed, 21 skipped (CPU). pre-commit passes on the changed files.

RandomCropTensorDict drew the window start with
torch.randint(max_idx_0), which samples from [0, max_idx_0) and never
picks the last valid start. When the sequence length equals sub_seq_len
(max_idx_0 == 0), randint raised "random_ expects 'from' to be less
than 'to'", although the length check accepts that case.

The masked path had the same off-by-one: floor(rand * (L - sub_seq_len))
never reaches L - sub_seq_len.

Sample the start from [0, max_idx_0] (and [0, L - sub_seq_len] with a
mask) so every window can be drawn and full-length crops work.

Signed-off-by: Tianlang (Jackie) Chen <305351710+jackiectl2@users.noreply.github.com>
@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/4578

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

❌ 1 New Failure

As of commit d6810d0 with merge base 264a3c2 (image):

NEW FAILURE - The following job has failed:

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] RandomCropTensorDict never samples the last window and fails when the length equals sub_seq_len

1 participant