Repository navigation
[BugFix] Fix IncrementalTokenizer with unequal token lengths - #4722
KhushiKeswani wants to merge 2 commits into
Conversation
🔗 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.
|
|
| 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]).
|
| 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]).
coder-jayp
left a comment
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| batch_size=(2,), | ||
| ) | ||
|
|
||
| out = env.transform._step(td, next_td) |
There was a problem hiding this comment.
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
Description
IncrementalTokenizer._step()raising aRuntimeErrorwhen accessingtokens.fullfrom aLazyStackedTensorDictcontaining sequences of different lengths.get(..., as_list=True)forLazyStackedTensorDictto retrieve individual token sequences without attempting to stack tensors with incompatible shapes.TensorDictinstances.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 aRuntimeError.This change retrieves the sequences individually for lazy stacks while preserving the existing behavior for regular TensorDicts.
Fixes #4686.
Types of changes
Checklist