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
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci

Signed-off-by: Jaime Cardenas <[email protected]>
  • Loading branch information
pre-commit-ci[bot] authored and jaimec00 committed Oct 29, 2025
commit fae60521e99fa221d426e3fa0dfad4b58e78e15f
61 changes: 38 additions & 23 deletions transformer_engine/pytorch/module/selective_layernorm_mlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ def _forward(
ln_bias = cast_if_needed(ln_bias, activation_dtype)

tp_world_size = get_distributed_world_size(tp_group)
backwards_needs_fc1_input = (
fc1_weight.requires_grad and (is_recomputation or (is_grad_enabled and not checkpoint))
backwards_needs_fc1_input = fc1_weight.requires_grad and (
is_recomputation or (is_grad_enabled and not checkpoint)
)
device = inp.device

Expand Down Expand Up @@ -552,9 +552,11 @@ def _forward(
# so we must be in the fwd
# is_grad_enabled can be true or false
# if false, can safely delete
# if true, we can only delete if checkpoint is true, since we will recompute anyways,
# if true, we can only delete if checkpoint is true, since we will recompute anyways,
# otherwise, checkpoint is false, so cant delete
if checkpoint or not is_grad_enabled: # we can safely get rid of these if this is the case
if (
checkpoint or not is_grad_enabled
): # we can safely get rid of these if this is the case
clear_tensor_data(fc1_out)

if not fp8 and fp8_calibration:
Expand Down Expand Up @@ -593,7 +595,9 @@ def _forward(
# ------------------------------------------------------

# Deallocate tensors if no longer needed, again, can safely deallocate
if checkpoint or not is_grad_enabled: # we can safely get rid of these if this is the case
if (
checkpoint or not is_grad_enabled
): # we can safely get rid of these if this is the case
clear_tensor_data(act_out, fc1_out_without_bias, fc1_out)

# Prepare output tensor
Expand Down Expand Up @@ -633,9 +637,9 @@ def _forward(
clear_tensor_data(act_out)
act_out = None

if not checkpoint: # regular path, no selective activation checkpointing
if not checkpoint: # regular path, no selective activation checkpointing

if cpu_offloading: # cpu offloading only relevant when have activations to offload
if cpu_offloading: # cpu offloading only relevant when have activations to offload
mark_activation_offload(
inputmat, mu, rsigma, ln_out, fc1_out, fc1_out_without_bias, act_out
)
Expand All @@ -645,18 +649,27 @@ def _forward(
# shards/unshards the base weights so we don't do it ourselves
ctx.fsdp_group = fsdp_group

ctx.fsdp_shapes = _fsdp_scatter_tensors( # again, ony relevant if we have activations to save
fsdp_group,
mu,
rsigma,
ln_out,
fc1_out_without_bias if bias_gelu_fusion else fc1_out,
act_out,
fc1_weight_final if fp8 and not isinstance(fc1_weight, Float8Tensor) else None,
fc2_weight_final if fp8 and not isinstance(fc2_weight, Float8Tensor) else None,
ctx.fsdp_shapes = (
_fsdp_scatter_tensors( # again, ony relevant if we have activations to save
fsdp_group,
mu,
rsigma,
ln_out,
fc1_out_without_bias if bias_gelu_fusion else fc1_out,
act_out,
(
fc1_weight_final
if fp8 and not isinstance(fc1_weight, Float8Tensor)
else None
),
(
fc2_weight_final
if fp8 and not isinstance(fc2_weight, Float8Tensor)
else None
),
)
)


tensors_to_save, tensor_objects = prepare_for_saving(
inputmat,
ln_weight,
Expand Down Expand Up @@ -750,7 +763,7 @@ def _forward(
FP8GlobalStateManager.IS_FIRST_FP8_MODULE = _first_fp8_module

ctx.wgrad_store = wgrad_store
if is_recomputation: # return the recomputed tensors
if is_recomputation: # return the recomputed tensors
return (
ctx,
inputmat,
Expand All @@ -776,7 +789,7 @@ def _forward(
return fc2_out, ln_out_return.view(shape)
return fc2_out, ln_out_return.view(inp_shape)
return fc2_out

@staticmethod
def forward(
ctx,
Expand Down Expand Up @@ -904,9 +917,11 @@ def _recompute(ctx):
# by the `restore_from_saved` method to construct back the actual tensors.
ctx.tensor_objects = None

if ctx.checkpoint: # do recomputation from the original args
return _SelectiveLayerNormMLP._forward(ctx, *tensors, *ctx.other_args, recompute_for_bwd=True)
else: # load from saved (return ctx is just because the other branch does too)
if ctx.checkpoint: # do recomputation from the original args
return _SelectiveLayerNormMLP._forward(
ctx, *tensors, *ctx.other_args, recompute_for_bwd=True
)
else: # load from saved (return ctx is just because the other branch does too)
return [ctx] + tensors

@staticmethod
Expand All @@ -916,7 +931,7 @@ def backward(
# pylint: disable=missing-function-docstring

with torch.cuda.nvtx.range("_LayerNormMLP_backward"):

( # pylint: disable=unbalanced-tuple-unpacking
ctx,
inputmat,
Expand Down