Skip to content
Open
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
f335cc7
custom tests for selective activation checkpointing for layernorm mlp
jaimec00 Oct 27, 2025
e349f46
add selective layernorm mlp to te.pytorch
jaimec00 Oct 27, 2025
aa18e74
update test and fix SLNMLP bug
jaimec00 Oct 27, 2025
8f50f4a
implement slnmlp
jaimec00 Oct 28, 2025
f6f034b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2025
00841c2
fix tests pointed out by greptile app bot, still pass
jaimec00 Oct 28, 2025
955f068
minor formatting change in tests/pytorch/selective_layernorm_mlp/dist…
jaimec00 Oct 28, 2025
5e47706
remove duplicate import in test/pytorch/selective_layernorm_mlp/test_…
jaimec00 Oct 28, 2025
9a69a6c
clean up tests, remove unused imports
jaimec00 Oct 28, 2025
ea8270d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2025
f896579
remove unused paths in test_deffered_init
jaimec00 Oct 28, 2025
9ee2df8
fix issue with zero_centered_gamma in test_numerics reference impleme…
jaimec00 Oct 28, 2025
05d3908
clean up tests
jaimec00 Oct 28, 2025
435fe9c
make comparison.py more extensive, cleaner output
jaimec00 Oct 28, 2025
903f37e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2025
0a31a70
fix small typo in tests/pytorch/selective_layernorm_mlp/compare.py
jaimec00 Oct 28, 2025
418dce6
fix typo by grepbot in compare.py
jaimec00 Oct 28, 2025
31cdd9d
make selectiuve activation checkpointing optional in slnmlp via check…
jaimec00 Oct 28, 2025
fae6052
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2025
a6a927e
add comments to clarify logic
jaimec00 Oct 29, 2025
16b816b
add checkpoint param to pytests, change compare.py to compare checkpp…
jaimec00 Oct 29, 2025
f623124
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 29, 2025
ff6f58f
refactor tests to call modified LayerNormMLP
jaimec00 Oct 29, 2025
8cbdb91
refactor to implement selective activation checkpointing directly int…
jaimec00 Oct 29, 2025
c46ad4c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 29, 2025
b068c5f
fix skip explanation for cuda_graphs.py
jaimec00 Oct 29, 2025
f0670ed
make _recompute deal with lists instead of tuples
jaimec00 Oct 29, 2025
5a34186
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 29, 2025
e12fa7c
fix MOST cuda graph failures by initializing identical quantizers dur…
jaimec00 Oct 30, 2025
9b29e49
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2025
cc52db5
fix cuda graphs issue, all tests pass now
jaimec00 Oct 31, 2025
e94ef33
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 31, 2025
ebd2329
fix small logic bugs, clean up
jaimec00 Nov 1, 2025
212fadb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 1, 2025
402e5f9
integrate tests into main testing scripts
jaimec00 Nov 5, 2025
483bbf6
incorporate rng state tracking in checkpointing
jaimec00 Nov 5, 2025
643a3c8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 5, 2025
0d0255f
clean up tests
jaimec00 Nov 5, 2025
d86bc00
fix return type mismatches
jaimec00 Nov 5, 2025
9aaa1b9
merge main into features/SLNMLP
jaimec00 Nov 12, 2025
07ff0c1
remove checkpoint test from test_recipe, add sperate test in test_num…
jaimec00 Nov 12, 2025
8dec0fc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 12, 2025
78b3437
minor typo fix
jaimec00 Nov 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
incorporate rng state tracking in checkpointing
Signed-off-by: Jaime Cardenas <[email protected]>
  • Loading branch information
jaimec00 committed Nov 5, 2025
commit 483bbf69f4a0675eb2b0d38ff066ebfb4a3147fe
67 changes: 39 additions & 28 deletions transformer_engine/pytorch/module/layernorm_mlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
use_reentrant_activation_recompute,
in_fp8_activation_recompute_phase,
_fsdp_scatter_tensors,
_get_cuda_rng_state,
_set_cuda_rng_state,
)
from ..constants import dist_group_type
from ..jit import no_torch_dynamo
Expand Down Expand Up @@ -232,6 +234,9 @@ def _forward(
FP8GlobalStateManager.copy_forward_fp8_meta_tensors_for_recompute(
module.fp8_meta
) # to restore quantizers during recomputation
# save the rng states
ctx.cpu_rng_state = torch.get_rng_state()
ctx.cuda_rng_state = _get_cuda_rng_state()

# whether to save activations regularly, or save inputs for recomputation in bwd
save_for_checkpoint = checkpoint and is_grad_enabled and not recompute_for_bwd
Expand Down Expand Up @@ -449,13 +454,13 @@ def _forward(
# FP8 cast to workspace buffer
update_workspace = (
is_first_microbatch is None or is_first_microbatch
) and not is_recomputation
)
fc1_weight_quantizer.set_usage(
rowwise=True, columnwise=is_grad_enabled
) # and (is_recomputation or not checkpoint))
)
fc2_weight_quantizer.set_usage(
rowwise=True, columnwise=is_grad_enabled
) # and (is_recomputation or not checkpoint))
)
fc1_weight_final = module.get_weight_workspace(
tensor=fc1_weight,
quantizer=fc1_weight_quantizer,
Expand Down Expand Up @@ -976,6 +981,15 @@ def _recompute(ctx):
FP8GlobalStateManager.get_old_fp8_meta_tensors_for_recompute(
ctx.other_args["module"].fp8_meta
) # set old quantizer state

# get current rng state
final_cpu_rng_state = torch.get_rng_state()
final_cuda_rng_state = _get_cuda_rng_state()

# set rng state for fwd
torch.set_rng_state(ctx.cpu_rng_state)
_set_cuda_rng_state(ctx.cuda_rng_state)

out = _LayerNormMLP._forward( # recompute
ctx, *tensors, *ctx.other_args.values(), recompute_for_bwd=True
)
Expand All @@ -988,6 +1002,9 @@ def _recompute(ctx):
FP8GlobalStateManager.restore_fp8_meta_tensors(
ctx.other_args["module"].fp8_meta
) # restore quantizers
# set rng state for fwd
torch.set_rng_state(final_cpu_rng_state)
_set_cuda_rng_state(final_cuda_rng_state)

return out

Expand All @@ -1000,25 +1017,26 @@ def backward(
) -> Tuple[Union[torch.Tensor, None], ...]:
# pylint: disable=missing-function-docstring

( # pylint: disable=unbalanced-tuple-unpacking
ctx,
inputmat,
ln_weight,
ln_out,
fc1_weight,
origin_fc1_weight,
fc1_bias,
fc1_out,
fc1_out_without_bias,
act_out,
fc2_weight,
origin_fc2_weight,
fc2_bias,
mu,
rsigma,
) = _LayerNormMLP._recompute(ctx)
with torch.cuda.nvtx.range("_LayerNormMLP_backward"):

( # pylint: disable=unbalanced-tuple-unpacking
ctx,
inputmat,
ln_weight,
ln_out,
fc1_weight,
origin_fc1_weight,
fc1_bias,
fc1_out,
fc1_out_without_bias,
act_out,
fc2_weight,
origin_fc2_weight,
fc2_bias,
mu,
rsigma,
) = _LayerNormMLP._recompute(ctx)

# Since main_grad can be modified inplace, it should not be a part of saved_tensors
fc1_weight_main_grad = (
ctx.fc1_main_grad_func()
Expand Down Expand Up @@ -1681,19 +1699,12 @@ def fc1_wgrad_gemm(
# )
return (
dgrad.view(ctx.inp_shape) if ctx.requires_dgrad else None,
# inputmat.view(ctx.inp_shape) if ctx.requires_dgrad else None,
dgamma,
# ln_weight,
dbeta,
# ln_weight,
fc1_wgrad,
# origin_fc1_weight,
fc1_bias_grad if fc1_bias is not None else None,
# fc1_bias,
fc2_wgrad, # pylint: disable=possibly-used-before-assignment
# origin_fc2_weight,
fc2_bias_grad,
# fc2_bias,
None, # eps
None, # is_first_microbatch
None, # fp8
Expand Down Expand Up @@ -1886,7 +1897,7 @@ def __init__(
ub_bulk_wgrad: bool = False,
delay_wgrad_compute: bool = False,
symmetric_ar_type: Optional[str] = None,
checkpoint: Optional[bool] = False,
checkpoint: bool = False,
) -> None:
super().__init__()

Expand Down