[Performance] Compile the DreamerV3 learner losses and make the two-hot decode reduction-safe - #4290
Open
theap06 wants to merge 4 commits into
Open
[Performance] Compile the DreamerV3 learner losses and make the two-hot decode reduction-safe#4290theap06 wants to merge 4 commits into
theap06 wants to merge 4 commits into
Conversation
…ot decode reduction-safe
Add optimization.compile_learner to the DreamerV3 example. "losses" compiles
the encoder, decoder, reward, continuation, actor and value networks together
with the value and replay-value losses, which is numerically identical to
eager; "all" also compiles the actor loss with its imagination rollout. The
--fast reproduction now enables "losses". On one GB10 the captured learner
update goes from 27.8 ms to 23.1 ms ("losses") and 17.2 ms ("all"), and the
training script from 33.5 to 43.4 and 52.0 updates per second.
Two library changes were needed for the compiled regions to be correct:
- two_hot_decode pairs mirrored bins through their probability difference
before summing. The previous mirrored-pair sum was only exact for uniform
probabilities in a fixed reduction order, and inductor's fused reduction
broke that cancellation on GPU: zero logits decoded to a reward of 0.154
instead of 0 at initialization, which shifted the first imagined targets.
- RSSMRolloutV3._scan casts the carry returned by each step to the incoming
carry dtypes. Under bf16 autocast the body returned bf16 state and belief
while the initial carry was fp32, and the higher-order scan rejected the
mismatch whenever an outer torch.compile inlined the compiled scan.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MbCsKrmHN55DtBCgJc2iHb
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4290
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 09d4248 with merge base bedcc83 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
…or policy eager The reproduction-modes test pins the exact overrides that --fast forwards to benchmark.py; add the new compile_learner=losses entry so the tests-cpu jobs pass again. Compile the learner after the real-world actor is built so the behavior policy keeps the eager networks it was measured with, move the scan carry note into the _scan docstring, and widen the decode tolerance for the GPU runners. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MbCsKrmHN55DtBCgJc2iHb
The MPS inductor backend cannot compile the random draws that the compiled actor loss traces; `losses` works there, `all` does not. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MbCsKrmHN55DtBCgJc2iHb
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MbCsKrmHN55DtBCgJc2iHb
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
optimization.compile_learnerto the DreamerV3 example:lossescompiles the encoder, decoder, reward, continuation, actor and value networks together with the value and replay-value losses (numerically identical to eager);alladditionally compiles the actor loss with its imagination rolloutcompile_learner=lossesin the--fastreproductiontwo_hot_decodeexact for uniform probabilities under any reduction order by pairing mirrored bins through their probability differencetorch.compileunder bf16 autocastWhy
After #4216 the fast path spends 95% of training wall time inside the captured learner update, and that update launches about 9,700 kernels with a 3.9 us mean. The compiled RSSM scan is already fused; the loss math around it and the imagination rollout still run as eager kernel streams. Compiling those regions removes most of the launches.
Measured on one NVIDIA GB10 (PyTorch 2.13.0, CUDA 13.0), DMC Walker preset, batch 16 x 64, BF16 compute, same protocol as
benchmarks/ad_hoc/bench_dreamer_v3_learner.py:compile_learner=lossescompile_learner=allUnmodified
train.py, 20,000 Walker steps on the same GPU: 33.5 updates/s for the fast path, 43.4 withlosses(+30%), 52.0 withall(+55%).Two bugs surfaced when compiling these regions:
two_hot_decoderelied on a fixed summation order of mirrored products so that uniform probabilities cancel to exactly zero over the +-4.85e8 symexp support. Inductor's fused reduction reorders it and zero logits decoded to 0.154 instead of 0, which shifted the imagined rewards and lambda targets at initialization. The new form(p_upper - p_lower) * b_upper + p_lower * (b_upper + b_lower)is exact under any order and general for any support.torch.compileinlines the compiled scan, which is what compiling the model loss does.The actor network object is shared between the actor loss and the real-world policy, so with
compile_learnerset the collector and evaluation policies run the compiled actor MLP for their own batch shapes (dynamic=False, one graph per shape); the policy's encoder copy stays eager. This is the configuration the numbers above were measured with.allmoves the imagination's random draws inside the compiled region, like thescanbackend, so it is not on by default; its actor loss after 60 updates differs from eager by float32 rounding at the tail bins (about -0.05 vs -0.0002). A full Walker learning curve withallis still owed before it becomes part of--fast.