[Feature] Collector final_obs: store true boundary next-obs for shifted-GAE#3758
Open
vmoens wants to merge 3 commits into
Open
[Feature] Collector final_obs: store true boundary next-obs for shifted-GAE#3758vmoens wants to merge 3 commits into
vmoens wants to merge 3 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/3758
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ❌ 18 New Failures, 1 Unrelated Failure, 20 Unclassified FailuresAs of commit 843af6f with merge base 0a01ee8 ( NEW FAILURES - The following jobs have failed:
UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:
FLAKY - The following job failed but was likely due to flakiness present on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This was referenced May 15, 2026
vmoens
commented
May 15, 2026
Comment on lines
+293
to
+314
| final_obs (bool, optional): if ``True`` (and ``compact_obs=True``), the | ||
| collector additionally stores the true next-observation reached | ||
| after the last step of the rollout under a top-level ``("final", k)`` | ||
| sub-tensordict for each observation/state key ``k`` that was | ||
| compacted away. The value is wrapped in | ||
| :class:`tensordict.UnbatchedTensor` (one obs per env, no time | ||
| dimension) so the rollout's batch shape ``[*envs, T]`` is preserved. | ||
|
|
||
| This closes the bootstrap-correctness gap when running with short | ||
| rollout windows: under ``compact_obs=True``, the ``("next", obs)`` | ||
| of the very last step of each window is dropped, and shifted-GAE | ||
| falls back to bootstrapping ``V(s_T) ≈ V(s_{T-1})`` for that step | ||
| (a 1/T fraction of corruption). With ``final_obs=True``, GAE reads | ||
| the true ``s_T`` from ``("final", obs)`` instead. | ||
|
|
||
| The pipeline assumption is: | ||
| ``collector -> GAE(shifted=True) -> ReplayBuffer.extend()``. | ||
| :class:`~torchrl.objectives.value.advantages.GAE` consumes and | ||
| drops ``("final", ...)`` from the returned tensordict, so the | ||
| downstream replay buffer never sees an | ||
| :class:`~tensordict.UnbatchedTensor` (which would otherwise be | ||
| incompatible with a contiguous storage). Defaults to ``False``. |
Collaborator
Author
There was a problem hiding this comment.
mark this as experimental (can go at any time).
You mention GAE but it could be any value estimator.
This feature is for correctness but in practice using the last root observation as trailing obs works well in practice.
This was referenced May 15, 2026
This was referenced May 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack from ghstack (oldest at bottom):
When
compact_obs=Trueis paired with shifted-GAE, the last step ofeach rollout window has no
("next", obs)to read; GAE was fallingback to
V(s_{T-1})via_fill_missing_next_inputsand biasingthe advantage by 1/T at every window boundary that is not a real done.
Add a
final_obsflag (requirescompact_obs=True) on the singleand multi-process collectors. When on, the collector maintains a
per-env side buffer that mirrors the most recent
("next", k)inplace; the rollout returned to the consumer carries each leaf under
("final", k)wrapped in :class:tensordict.UnbatchedTensorso the[*envs, T]batch shape is preserved (the buffer has no time dim).On the consumer side,
ValueEstimatorBasegets two new staticmethods:
_apply_final_obs_to_next_donefinds the synthetic last-steppositions inside the interleaved bootstrap batch and substitutes
("final", k)values for the bootstrap input. No-op when("final", ...)is absent — non-final-obs callers are unaffected._maybe_drop_final_obsdeletes("final", ...)from theconsumed tensordict.
UnbatchedTensorleaves are incompatiblewith contiguous-storage replay buffers (
LazyTensorStorage,LazyMemmapStorage); dropping after consumption keeps thecollector -> GAE ->
ReplayBuffer.extend()pipeline clean.All five estimators (TD0 / TD1 / TDLambda / GAE / VTrace) invoke
_maybe_drop_final_obsat the end offorward.Also documents the memmap-vs-on-device tradeoff on
LazyMemmapStorage— the example below the stack picks memmap tokeep large rollout buffers off-device.
Tests cover:
final_obs=Truewithoutcompact_obsraises;boundary obs match a
compact_obs=Falsereference (non-done envs);UnbatchedTensorsurvives indexing along time; bootstrap paritybetween compact+final and a non-compact reference for TD0 / TD1 /
TDLambda / GAE; and
("final", ...)is dropped from the returnedtensordict after consumption.