[Doc] Memory-efficient RL training tutorial + cross-refs#3745
Open
vmoens wants to merge 5 commits into
Open
Conversation
New tutorial under tutorials/sphinx-tutorials/memory_efficient_rl.py that ties together the three recent memory-efficiency PRs: - compact_obs flag on the collector (pytorch#3742) - NextStateReconstructor RB transform (pytorch#3743) - NaN-safe value-estimator forward (pytorch#3744) The tutorial walks through: - Where the observation memory goes and why TorchRL keeps both obs and ("next", obs) by default (bootstrap targets, MultiStep n-step fallback) - Knob 1: SyncDataCollector(compact_obs=True) — halves the obs footprint at the producer side - Knob 2: NextStateReconstructor — rebuilds ("next", obs) at sampling time, NaN at trajectory ends - Knob 2.5: ValueEstimatorBase._sanitize_next_obs_nan keeps GAE/TD targets numerically defined - When NOT to take this path: MultiStepTransform, truncated transitions where the V(obs[t]) ≈ V(real_next_obs) approximation is unacceptable - Knob 3: LazyMemmapStorage for buffers larger than VRAM - Knob 4: SliceSampler + scan/Triton recurrent backends for padding-free sequence training - End-to-end pipeline snippet The tutorial runs end-to-end on CPU (CartPole-v1, 200 frames) and reports concrete byte-level savings from `td.bytes()`. Cross-references added to: - SyncDataCollector / MultiSyncCollector / MultiAsyncCollector (`compact_obs` docstring) — pointers to NextStateReconstructor, the value-estimator sanitizer, MultiStep incompatibility note, and the new tutorial. - NextStateReconstructor — `.. seealso::` block to compact_obs, the sanitizer, MultiStep incompatibility, and the tutorial. - ValueEstimatorBase._sanitize_next_obs_nan — `.. seealso::` to compact_obs, NextStateReconstructor, and the tutorial. docs/source/index.rst — register the new tutorial under "Basics". Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/3745
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: ❌ 1 New FailureAs of commit 01502ba with merge base 6e46542 ( NEW FAILURE - The following job has failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Add a new section between Knob 2 and Knob 2.5 that describes the lossy-delta variant of memory-efficient observation storage shipped in pytorch#3777: - env-side: write `(next_obs - obs).to(delta_dtype)` under `("next", "delta", obs)` and drop the full-precision next obs. - RB-side: the same transform reconstructs `("next", obs)` as `obs + delta` at sample time. The new knob trades a smaller memory saving (~25% vs ~50%) for boundary-preserving reconstruction: no NaN at trajectory ends, so losses that bootstrap on truncated transitions get the real next obs instead of the `V(obs[t])` fallback used by the value-estimator sanitizer. MultiStep is still incompatible. Cross-references: - "When not to rehydrate" now points at NextObservationDelta as the alternative for truncated-bootstrap-heavy losses. - Conclusion bullets include the delta knob alongside the compact + reconstructor pair. The runnable code path is unchanged; the new section uses a `.. code-block:: python` (non-executed) snippet, so the tutorial does not depend on pytorch#3777 being merged first.
…NextObservationDelta The conflict in torchrl/collectors/_single.py was between two extensions of the compact_obs docstring -- HEAD added the tutorial / NaN-sanitizer / MultiStep cross-references, main added the new shifted='compact' GAE pairing. Resolved by keeping both. Now that NextObservationDelta (pytorch#3777) is in main, point at it from the three places that already cross-reference the memory-efficient knobs: - torchrl/collectors/_single.py compact_obs docstring -- 'lossy-precision alternative that *does* preserve boundary transitions'. - torchrl/collectors/_multi_base.py compact_obs docstring -- same line. - torchrl/envs/transforms/rb_transforms.py NextStateReconstructor seealso -- mention the delta variant for the NaN-at-boundary case. - torchrl/objectives/value/advantages.py _sanitize_next_obs_nan seealso -- mention the delta variant as an alternative that avoids NaN. No code changes; docs only.
…ether'
Now that NextObservationDelta is in main, promote the Knob 2b
.. code-block:: python snippet to a runnable section. The section:
- builds a TransformedEnv(CartPole, NextObservationDelta())
- runs a 200-step CartPole rollout
- prints the bytes for default vs compact_obs vs delta side by side
- confirms ('next', 'delta', 'observation') is float16 and
('next', 'observation') is absent from the rollout
- attaches the same class to a ReplayBuffer (with explicit in_keys for
the RB side, since there is no env parent for auto-inference)
- samples and verifies ('next', 'observation') is reconstructed
finite at every position -- including trajectory boundaries, which
is exactly the case where the compact-obs path produces NaN.
Also extend 'Putting it together' with a parallel Recipe B that uses
NextObservationDelta on both sides, alongside the existing
compact_obs + NextStateReconstructor recipe.
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.
Summary
Ties together the three recently-merged memory-efficiency PRs into a
single story:
compact_obscollector flag ([Performance] Add compact_obs flag to DataCollector #3742)NextStateReconstructorRB transform ([Feature] NextStateReconstructor RB transform #3743)Two parts:
1. Runnable Sphinx-gallery tutorial at
tutorials/sphinx-tutorials/memory_efficient_rl.py. Sections:td.bytes()numbers)("next", obs)is kept by default — bootstrap target attrajectory ends,
MultiStepTransformn-step fallbackSyncDataCollector(compact_obs=True)NextStateReconstructorwith the traj_id + done contract(
_sanitize_next_obs_nan), GAE finite everywhereMultiStepTransformincompatibility,the
V(obs[t]) ≈ V(real_next_obs)approximation at truncated steps,and how
shifted=TrueinteractsLazyMemmapStoragefor buffers ≥ VRAMSliceSampler+ the new"scan"/"triton"recurrent backends for padding-free sequence training
Runs end-to-end on CPU (CartPole-v1, 200 frames; <2s wall) and reports
the byte-level savings concretely from
td.bytes().2. Docstring cross-references so a reader landing on any of the
three new APIs finds the other two:
Collector(compact_obs=…)(and the multi-process collectors):pointers to
NextStateReconstructor, the value-estimatorsanitizer, the
MultiStepTransformincompatibility note, and thenew tutorial.
NextStateReconstructor:.. seealso::block coveringcompact_obs, the sanitizer,MultiStepTransform, and thetutorial.
ValueEstimatorBase._sanitize_next_obs_nan:.. seealso::block to
compact_obs,NextStateReconstructor, and thetutorial.
docs/source/index.rstregisters the new tutorial under "Basics".Test plan
reported, NaN at slice boundaries confirmed to coincide with
trajectory boundaries, GAE advantage finite everywhere, memmap
roundtrip works).
(verified by reading the rendered class docstrings via
Collector.__doc__etc.).🤖 Generated with Claude Code