Skip to content

[AC] Set preserve_rng_state=True as default for activation checkpointing - #2380

Merged
tianyu-l merged 1 commit into
mainfrom
jw3468/preserve-rng-state-true
Feb 16, 2026
Merged

tianyu-l merged 1 commit into
mainfrom
jw3468/preserve-rng-state-true

Conversation

@soulitzer

@soulitzer soulitzer commented Feb 13, 2026 •

Copy link
Copy Markdown
Contributor

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.

  """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}%)")

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.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Feb 13, 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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@tianyu-l
tianyu-l merged commit e347b1e into main Feb 16, 2026
29 checks passed
@tianyu-l
tianyu-l deleted the jw3468/preserve-rng-state-true branch February 16, 2026 00:57
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}%)")
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Why preserve_rng_state=False in activation checkpointing

3 participants