Skip to content

[PyTorch] Preserve CUDA RNG state during checkpoint recompute - #3835

Open
losewayy wants to merge 2 commits into
NVIDIA:mainfrom
losewayy:fix/cudarngstates-tracker-checkpoint-dropout
Open

losewayy wants to merge 2 commits into
NVIDIA:mainfrom
losewayy:fix/cudarngstates-tracker-checkpoint-dropout

Conversation

@losewayy

@losewayy losewayy commented Nov 8, 2026

Copy link
Copy Markdown

Description

Checkpointing saves graph-safe generator handles before forward. Those handles share mutable CUDA RNG state, so forward advances the offsets that recompute later reads. Dropout can then produce a different mask and incorrect input gradients.

Save the seed and offset separately from the handles, rewind the original states in place for recompute, and restore the live values on exit. This preserves shared RNG streams and the state already registered with CUDA graphs. The captured dropout recompute mismatch reported in #3834 remains.

Fixes #3833

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

  • Share RNG save and restore between reentrant and non-reentrant checkpointing.
  • Cover default, tracked, and shared RNG streams, and replay a graph captured before an eager checkpoint.

On an RTX 5070 Ti Laptop GPU with PyTorch 2.14.1+cu130, tracked-dropout gradient mismatches fell from 2017 of 4096 elements to zero. The checkpoint test selection passed all 33 cases, including eight regression cases. An explicit generator registration probe also passed capture and replay on PyTorch 2.11.0+cu128. The graph regression exercises the registration API that PyTorch 2.14 reports as deprecated.

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Graph-safe generator handles share state with the active CUDA generator.
Saving only the handles lets forward advance the saved RNG position, so
dropout recomputation can produce a different mask and incorrect input
gradients.

Save seed and offset values separately and rewind the original states
only while recomputing. Restore the live values on exit so aliases and
CUDA graph registrations stay valid. During capture, keep using the
graph-safe handles. Captured dropout recomputation remains a separate
limitation.

Cover both checkpoint modes with default, tracked, and shared RNG
streams, and verify that an existing graph can replay after an eager
checkpoint.

Signed-off-by: losewayy <104015127+losewayy@users.noreply.github.com>
@github-actions github-actions Bot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Nov 8, 2026
@losewayy
losewayy marked this pull request as ready for review November 8, 2026 14:23
@losewayy
losewayy requested a review from ksivaman as a code owner November 8, 2026 14:23
@greptile-apps

greptile-apps Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge, with no new actionable issues found.

Summary

Shares RNG save and restore code between both checkpoint modes while preserving the original generator handles.

  • Checkpoint recompute now reuses the random draws from forward.

Reviews (2) · Last reviewed commit: "[PyTorch] Test CUDA RNG restoration afte..." · Reviewed by Greptile

Comment thread tests/pytorch/test_numerics.py Resolved
Draw from the default and tracked CUDA generators between forward and backward. Compare their states before and after backward in both checkpoint modes to catch any change to the live streams.

Signed-off-by: losewayy <104015127+losewayy@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution PRs from external contributor outside the core maintainers, representing community-driven work.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[PyTorch] Incorrect dropout gradients in checkpoint with graph-safe CudaRNGStatesTracker

1 participant