Fix RuntimeError: clamp action std to prevent negative values#8
Open
diaskabdualiev wants to merge 1 commit intounitreerobotics:mainfrom
Open
Fix RuntimeError: clamp action std to prevent negative values#8diaskabdualiev wants to merge 1 commit intounitreerobotics:mainfrom
diaskabdualiev wants to merge 1 commit intounitreerobotics:mainfrom
Conversation
7907599 to
8a27541
Compare
Three issues caused training to fail on `Mjlab-Velocity-Rough-Unitree-Go2` with multiple GPUs: 1. **Ratio overflow**: `exp(log_prob_new - log_prob_old)` overflows to Inf when the policy changes significantly between collection and update, especially with unstable early training (robot falling immediately). Fix: clamp log-ratio to [-20, 20] before exp(). 2. **NaN gradient propagation across GPUs**: `all_reduce(SUM)` in `reduce_parameters()` causes one GPU with NaN gradients to poison all other GPUs. Fix: `nan_to_num()` on gradients before all_reduce. 3. **NaN loss corrupts parameters**: when loss becomes NaN, `backward()` produces NaN gradients and `optimizer.step()` writes NaN into all parameters, making recovery impossible. Fix: skip optimizer step when loss is not finite. 4. **Negative/NaN std**: `self.std` parameter (noise_std_type="scalar") can become negative or NaN after optimizer step, crashing `torch.distributions.Normal`. Fix: clamp + nan_to_num in `update_distribution()` for both ActorCritic and ActorCriticRecurrent. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
8a27541 to
d3f4158
Compare
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
RuntimeError: normal expects all elements of std >= 0.0inactor_critic.py:146during PPO trainingnoise_std_type="scalar",self.stdis a rawnn.Parameterwithout a lower bound. The optimizer can push it negative, crashingtorch.distributions.Normalall_reduceinreduce_parameters()amplifies conflicting gradient directions across GPUsReproduction
Train
Mjlab-Velocity-Rough-Unitree-Go2with multiple GPUs:Crashes at iteration 1 with:
Fix
Add
torch.clamp(std, min=1e-6)before creating theNormaldistribution to guarantee std remains positive.Test plan
Mjlab-Velocity-Rough-Unitree-Go2with multi-GPU setup — no crashMjlab-Velocity-Flat-Unitree-Go2— verify no regression🤖 Generated with Claude Code