Skip to content

[BugFix] Fix IncrementalTokenizer with unequal token lengths - #4722

Open
KhushiKeswani wants to merge 2 commits into
pytorch:mainfrom
KhushiKeswani:fix/incremental-tokenizer-unequal-lengths
Open

KhushiKeswani wants to merge 2 commits into
pytorch:mainfrom
KhushiKeswani:fix/incremental-tokenizer-unequal-lengths

Conversation

@KhushiKeswani

Copy link
Copy Markdown

Description

  • Fix IncrementalTokenizer._step() raising a RuntimeError when accessing tokens.full from a LazyStackedTensorDict containing sequences of different lengths.
  • Use get(..., as_list=True) for LazyStackedTensorDict to retrieve individual token sequences without attempting to stack tensors with incompatible shapes.
  • Preserve the existing retrieval behavior for regular TensorDict instances.
  • Add regression tests covering both cases:
    • Unequal token lengths when history is absent.
    • Unequal token lengths when existing history is extended.

Motivation and Context

When processing a 1-D batch with variable-length token sequences, LazyStackedTensorDict.get() attempts to stack the individual tensors. Since their lengths differ, this raises a RuntimeError.

This change retrieves the sequences individually for lazy stacks while preserving the existing behavior for regular TensorDicts.

Fixes #4686.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)
  • Example (update in the folder of examples)

Checklist

@pytorch-bot

pytorch-bot Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⚠️ 16 Awaiting Approval

As of commit ebc8606 with merge base a711ef9 (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 Nov 11, 2026
@github-actions github-actions Bot added the llm/ LLM-related PR, triggers LLM CI tests label Nov 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ PR Title Label Error

PR title must start with a label prefix in brackets (e.g., [BugFix]).

Current title: Fix IncrementalTokenizer with unequal token lengths

Supported Prefixes (case-sensitive)

Your PR title must start with exactly one of these prefixes:

Prefix Label Applied Example
[Algorithm] new algo [Algorithm] Add new RL objective
[BE] BE [BE] Improve error messages
[Benchmark] or [Benchmarks] Benchmarks [Benchmark] Add collector benchmark
[BugFix] BugFix [BugFix] Fix memory leak in collector
[Example] or [Examples] Examples [Example] Add training script
[Feature] Feature [Feature] Add new optimizer
[Doc] or [Docs] Documentation [Doc] Update installation guide
[Refactor] Refactoring [Refactor] Clean up module imports
[CI] CI [CI] Fix workflow permissions
[Test] or [Tests] Tests [Tests] Add unit tests for buffer
[Trainer] or [Trainers] Trainers [Trainer] Add trainer config
[Environment] or [Environments] Environments [Environments] Add Gymnasium support
[Data] Data [Data] Fix replay buffer sampling
[LLM] llm/ [LLM] Add reward model integration
[Minor] small change [Minor] Fix typo in error message
[Performance] or [Perf] Performance [Performance] Optimize tensor ops
[BC-Breaking] bc breaking [BC-Breaking] Remove deprecated API
[Deprecation] Deprecation [Deprecation] Mark old function
[Algorithm] or [Algorithms] new algo [Algorithm] Add new objective
[Quality] Quality [Quality] Fix typos and add codespell
[Versioning] versioning [Versioning] Bump release version
[WIP] WIP [WIP] Draft implementation

Note: Common variations like singular/plural are supported (e.g., [Doc] or [Docs]).

@KhushiKeswani KhushiKeswani changed the title Fix IncrementalTokenizer with unequal token lengths [BUGFIX] Fix IncrementalTokenizer with unequal token lengths Nov 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ PR Title Label Error

Unknown or invalid prefix [BUGFIX].

Current title: [BUGFIX] Fix IncrementalTokenizer with unequal token lengths

Supported Prefixes (case-sensitive)

Your PR title must start with exactly one of these prefixes:

Prefix Label Applied Example
[Algorithm] new algo [Algorithm] Add new RL objective
[BE] BE [BE] Improve error messages
[Benchmark] or [Benchmarks] Benchmarks [Benchmark] Add collector benchmark
[BugFix] BugFix [BugFix] Fix memory leak in collector
[Example] or [Examples] Examples [Example] Add training script
[Feature] Feature [Feature] Add new optimizer
[Doc] or [Docs] Documentation [Doc] Update installation guide
[Refactor] Refactoring [Refactor] Clean up module imports
[CI] CI [CI] Fix workflow permissions
[Test] or [Tests] Tests [Tests] Add unit tests for buffer
[Trainer] or [Trainers] Trainers [Trainer] Add trainer config
[Environment] or [Environments] Environments [Environments] Add Gymnasium support
[Data] Data [Data] Fix replay buffer sampling
[LLM] llm/ [LLM] Add reward model integration
[Minor] small change [Minor] Fix typo in error message
[Performance] or [Perf] Performance [Performance] Optimize tensor ops
[BC-Breaking] bc breaking [BC-Breaking] Remove deprecated API
[Deprecation] Deprecation [Deprecation] Mark old function
[Algorithm] or [Algorithms] new algo [Algorithm] Add new objective
[Quality] Quality [Quality] Fix typos and add codespell
[Versioning] versioning [Versioning] Bump release version
[WIP] WIP [WIP] Draft implementation

Note: Common variations like singular/plural are supported (e.g., [Doc] or [Docs]).

@KhushiKeswani KhushiKeswani changed the title [BUGFIX] Fix IncrementalTokenizer with unequal token lengths [BugFix] Fix IncrementalTokenizer with unequal token lengths Nov 11, 2026

@coder-jayp coder-jayp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution

Left inline comments—the rollout from #4686 still raises RuntimeError during env.step() because only the child "tokens" is lazy-stacked while the root is a standard TensorDict.

tokens_full_key = (*tokens_key[:-1], "full")
existing_tokens_full = tensordict.get(tokens_full_key, None)

if isinstance(tensordict, LazyStackedTensorDict):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ChatEnv, the root action_td is a normal TensorDict and only the child "tokens" is lazy-stacked. The isinstance check evaluates to False, falls back to else:, and env.step(action_td) still crashes with RuntimeError.

Using as_list=True directly in tensordict.get() handles this without needing the isinstance check.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With as_list=True, existing_tokens_full becomes a Python list. Setting it directly stores it as NonTensorData (which is why .data was needed in the test assertion). It should be converted back into a nested tensor with torch.nested.as_nested_tensor before being set on next_tensordict.

Comment thread test/llm/test_llm_transforms.py Outdated
batch_size=(2,),
)

out = env.transform._step(td, next_td)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this test be updated to call the public env.step(action_td) directly on the output of env.reset()? Calling private _step() with a manual lazy_stack bypasses how ChatEnv actually packages tensordicts in real rollouts

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. llm/ LLM-related PR, triggers LLM CI tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] IncrementalTokenizer._step raises for a 1-D batch whose tokens.full lengths differ

2 participants