Repository navigation
[AC] Set preserve_rng_state=True as default for activation checkpointing - #2380
Merged
Merged
Conversation
Set preserve_rng_state to True for all checkpoint_wrapper calls to ensure deterministic output compared to non-checkpointed passes. This changes the default in the ActivationCheckpoint config and updates the hardcoded values in the Flux model's activation checkpointing.
soulitzer
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
February 13, 2026 22:16
tianyu-l
approved these changes
Feb 15, 2026
| # pyrefly: ignore [missing-attribute] | ||
| for layer_id, block in model.double_blocks.named_children(): | ||
| block = ptd_checkpoint_wrapper(block, preserve_rng_state=False) | ||
| block = ptd_checkpoint_wrapper(block, preserve_rng_state=True) |
Contributor
There was a problem hiding this comment.
@wwwjn do you still remember why we didn't apply llm-like per-layer AC wrapping to flux? Over there we support SAC and some other features.
Contributor
There was a problem hiding this comment.
I don't remember any blocker prevent us using SAC on flux model, but I don't recall we tested SAC on FLUX. The only reason I could think of is apply_ac() function in LLM evolved a lot after FLUX is developed. Since FLUX has single_blocks and double_blocks, FLUX keeps a separate apply_ac function which is not changed yet. We should test SAC + new AC features on FLUX
TXacs
pushed a commit
to McmillanTAC/torchtitan
that referenced
this pull request
Apr 13, 2026
…ing (pytorch#2380) Fixes pytorch#1323 Set preserve_rng_state to True for all checkpoint_wrapper calls to ensure deterministic output compared to non-checkpointed passes. This changes the default in the ActivationCheckpoint config and updates the hardcoded values in the Flux model's activation checkpointing. Measured overhead is 30-50us on 8gpu node. For comparison, AC already has a 350us constant overhead. Overhead scales linearly when inputs are on more distinct devices, but that is rare . This shouldn't matter anyway because AC regions are fairly large (transformer blocks). It is possible the constant overhead will matter more when only several ops are being AC'd. ```python """Measure the overhead of preserve_rng_state in torch.utils.checkpoint.""" import torch import torch.nn as nn import torch.utils.checkpoint as checkpoint import time class Block(nn.Module): def __init__(self, dim=1024): super().__init__() self.linear1 = nn.Linear(dim, dim) self.linear2 = nn.Linear(dim, dim) def forward(self, x): return self.linear2(torch.relu(self.linear1(x))) def bench(block, x, preserve_rng_state, warmup=10, iters=100): for _ in range(warmup): out = checkpoint.checkpoint(block, x, preserve_rng_state=preserve_rng_state, use_reentrant=False) out.sum().backward() torch.cuda.synchronize() torch.cuda.synchronize() t0 = time.perf_counter() for _ in range(iters): out = checkpoint.checkpoint(block, x, preserve_rng_state=preserve_rng_state, use_reentrant=False) out.sum().backward() torch.cuda.synchronize() return (time.perf_counter() - t0) / iters * 1000 # ms per iter block = Block(dim=1024).cuda() x = torch.randn(64, 128, 1024, device="cuda", requires_grad=True) ms_false = bench(block, x, preserve_rng_state=False) ms_true = bench(block, x, preserve_rng_state=True) print(f"preserve_rng_state=False: {ms_false:.3f} ms/iter") print(f"preserve_rng_state=True: {ms_true:.3f} ms/iter") print(f"Overhead: {ms_true - ms_false:.3f} ms ({(ms_true / ms_false - 1) * 100:.2f}%)") ```
ACharacterInASimulation
pushed a commit
to ACharacterInASimulation/torchtitan
that referenced
this pull request
Apr 21, 2026
…ing (pytorch#2380) Fixes pytorch#1323 Set preserve_rng_state to True for all checkpoint_wrapper calls to ensure deterministic output compared to non-checkpointed passes. This changes the default in the ActivationCheckpoint config and updates the hardcoded values in the Flux model's activation checkpointing. Measured overhead is 30-50us on 8gpu node. For comparison, AC already has a 350us constant overhead. Overhead scales linearly when inputs are on more distinct devices, but that is rare . This shouldn't matter anyway because AC regions are fairly large (transformer blocks). It is possible the constant overhead will matter more when only several ops are being AC'd. ```python """Measure the overhead of preserve_rng_state in torch.utils.checkpoint.""" import torch import torch.nn as nn import torch.utils.checkpoint as checkpoint import time class Block(nn.Module): def __init__(self, dim=1024): super().__init__() self.linear1 = nn.Linear(dim, dim) self.linear2 = nn.Linear(dim, dim) def forward(self, x): return self.linear2(torch.relu(self.linear1(x))) def bench(block, x, preserve_rng_state, warmup=10, iters=100): for _ in range(warmup): out = checkpoint.checkpoint(block, x, preserve_rng_state=preserve_rng_state, use_reentrant=False) out.sum().backward() torch.cuda.synchronize() torch.cuda.synchronize() t0 = time.perf_counter() for _ in range(iters): out = checkpoint.checkpoint(block, x, preserve_rng_state=preserve_rng_state, use_reentrant=False) out.sum().backward() torch.cuda.synchronize() return (time.perf_counter() - t0) / iters * 1000 # ms per iter block = Block(dim=1024).cuda() x = torch.randn(64, 128, 1024, device="cuda", requires_grad=True) ms_false = bench(block, x, preserve_rng_state=False) ms_true = bench(block, x, preserve_rng_state=True) print(f"preserve_rng_state=False: {ms_false:.3f} ms/iter") print(f"preserve_rng_state=True: {ms_true:.3f} ms/iter") print(f"Overhead: {ms_true - ms_false:.3f} ms ({(ms_true / ms_false - 1) * 100:.2f}%)") ```
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.
Fixes #1323
Set preserve_rng_state to True for all checkpoint_wrapper calls to ensure deterministic output compared to non-checkpointed passes. This changes the default in the ActivationCheckpoint config and updates the hardcoded values in the Flux model's activation checkpointing.
Measured overhead is 30-50us on 8gpu node. For comparison, AC already has a 350us constant overhead. Overhead scales linearly when inputs are on more distinct devices, but that is rare . This shouldn't matter anyway because AC regions are fairly large (transformer blocks). It is possible the constant overhead will matter more when only several ops are being AC'd.