Skip to content

[Feature] Collector final_obs: store true boundary next-obs for shifted-GAE#3758

Open
vmoens wants to merge 3 commits into
gh/vmoens/274/basefrom
gh/vmoens/274/head
Open

[Feature] Collector final_obs: store true boundary next-obs for shifted-GAE#3758
vmoens wants to merge 3 commits into
gh/vmoens/274/basefrom
gh/vmoens/274/head

Conversation

@vmoens
Copy link
Copy Markdown
Collaborator

@vmoens vmoens commented May 15, 2026

Stack from ghstack (oldest at bottom):

When compact_obs=True is paired with shifted-GAE, the last step of
each rollout window has no ("next", obs) to read; GAE was falling
back to V(s_{T-1}) via _fill_missing_next_inputs and biasing
the advantage by 1/T at every window boundary that is not a real done.

Add a final_obs flag (requires compact_obs=True) on the single
and multi-process collectors. When on, the collector maintains a
per-env side buffer that mirrors the most recent ("next", k) in
place; the rollout returned to the consumer carries each leaf under
("final", k) wrapped in :class:tensordict.UnbatchedTensor so the
[*envs, T] batch shape is preserved (the buffer has no time dim).

On the consumer side, ValueEstimatorBase gets two new static
methods:

  • _apply_final_obs_to_next_done finds the synthetic last-step
    positions 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_obs deletes ("final", ...) from the
    consumed tensordict. UnbatchedTensor leaves are incompatible
    with contiguous-storage replay buffers (LazyTensorStorage,
    LazyMemmapStorage); dropping after consumption keeps the
    collector -> GAE -> ReplayBuffer.extend() pipeline clean.

All five estimators (TD0 / TD1 / TDLambda / GAE / VTrace) invoke
_maybe_drop_final_obs at the end of forward.

Also documents the memmap-vs-on-device tradeoff on
LazyMemmapStorage — the example below the stack picks memmap to
keep large rollout buffers off-device.

Tests cover: final_obs=True without compact_obs raises;
boundary obs match a compact_obs=False reference (non-done envs);
UnbatchedTensor survives indexing along time; bootstrap parity
between compact+final and a non-compact reference for TD0 / TD1 /
TDLambda / GAE; and ("final", ...) is dropped from the returned
tensordict after consumption.

[ghstack-poisoned]
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented May 15, 2026

🔗 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 SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 18 New Failures, 1 Unrelated Failure, 20 Unclassified Failures

As of commit 843af6f with merge base 0a01ee8 (image):

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.

Comment thread torchrl/collectors/_single.py Outdated
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``.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

[ghstack-poisoned]
[ghstack-poisoned]
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. Collectors Feature New feature Integrations/torch_geometric Integrations Objectives ReplayBuffers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant