Skip to content

Fix FusedAdam empty tensor handling#3212

Open
wujingyue wants to merge 3 commits into
NVIDIA:mainfrom
wujingyue:agent/fix-fusedadam-empty-param
Open

Fix FusedAdam empty tensor handling#3212
wujingyue wants to merge 3 commits into
NVIDIA:mainfrom
wujingyue:agent/fix-fusedadam-empty-param

Conversation

@wujingyue

@wujingyue wujingyue commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Filter zero-sized optimizer tensor slots through a shared helper before Adam/SGD call multi_tensor_apply
  • Add a native multi_tensor_apply check so zero-chunk tensors fail with a clear error if they reach the launcher
  • Add regression tests for an empty parameter at the end of Adam and SGD parameter groups

Root cause

multi_tensor_apply drives launch bookkeeping from the per-tensor chunk loop. A zero-sized tensor has zero chunks, so if an empty tensor appears at the end of a group, pending work for earlier tensors can remain unlaunched and FusedAdam silently skips the update.

The generic MultiTensorApply wrapper still preserves caller-visible slot semantics. Adam and SGD opt into filtering because empty tensor slots are no-op optimizer updates.

Fixes #3207

Validation

  • NVTE_FRAMEWORK=pytorch NVTE_CUDA_ARCHS=86 NVTE_WITH_NCCL_EP=0 NVTE_SKIP_SUBMODULE_CHECKS_DURING_BUILD=1 MAX_JOBS=4 NVTE_BUILD_THREADS_PER_JOB=1 python setup.py build_ext --inplace
  • python -m py_compile transformer_engine/pytorch/optimizers/fused_adam.py transformer_engine/pytorch/optimizers/fused_sgd.py transformer_engine/pytorch/optimizers/multi_tensor_apply.py
  • PYTHONPATH=/tmp/TransformerEngine pytest -q tests/pytorch/test_fused_optimizer.py::TestFusedAdam::test_empty_param_at_end_of_group tests/pytorch/test_fused_optimizer.py::TestFusedSGD::test_empty_param_at_end_of_group
  • All-empty sanity for te.optimizers.FusedAdam and te.optimizers.FusedSGD: both completed step() and torch.cuda.synchronize()
  • Issue reproducer: te 0.0009999871253967285, torch 0.0009999871253967285
  • git diff --check

@wujingyue
wujingyue requested a review from ksivaman as a code owner July 15, 2026 01:12
@github-actions github-actions Bot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Jul 15, 2026
@greptile-apps

greptile-apps Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes empty tensor handling in fused optimizer multi-tensor launches. The main changes are:

  • Empty optimizer slots are filtered before Adam and SGD launch work.
  • The native multi-tensor launcher now reports a clear error for unfiltered zero-sized tensors.
  • Adam and SGD tests cover an empty parameter at the end of a group.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
transformer_engine/pytorch/optimizers/multi_tensor_apply.py Adds aligned empty-slot filtering before dispatching to the native multi-tensor op.
transformer_engine/pytorch/optimizers/fused_sgd.py Routes SGD launch sets through the shared filtering path before native launch.
transformer_engine/common/multi_tensor/multi_tensor_apply.cuh Adds a native guard for zero-chunk tensors that reach the shared launcher.
tests/pytorch/test_fused_optimizer.py Adds Adam and SGD coverage for empty parameters at the end of a group.

Reviews (6): Last reviewed commit: "Move empty tensor filtering into MultiTe..." | Re-trigger Greptile

Comment thread transformer_engine/common/multi_tensor/multi_tensor_apply.cuh
@wujingyue
wujingyue force-pushed the agent/fix-fusedadam-empty-param branch from 2306b6e to 0b4b00b Compare July 15, 2026 01:20
Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
@wujingyue
wujingyue force-pushed the agent/fix-fusedadam-empty-param branch from 0b4b00b to 69337e7 Compare July 15, 2026 02:37
from torch.distributed._tensor import DTensor


def filter_empty_tensor_lists(tensor_lists):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should we call this from MultiTensorApply unconditionally? I'm not sure whether filtering is safe for every optimizer.

@vthumbe1503 vthumbe1503 Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes, agreed, I dont think there is any harm applying this filter directly in the MultiTensorApply class instead of putting it in each optimizer. Could we make that change @wujingyue ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Non blocking comment, given that we have just have optimizers exposed in TE

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

vthumbe1503
vthumbe1503 previously approved these changes Jul 20, 2026

@vthumbe1503 vthumbe1503 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Just one minor review comment

@vthumbe1503

Copy link
Copy Markdown
Collaborator

/te-ci pytorch

Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
@wujingyue
wujingyue force-pushed the agent/fix-fusedadam-empty-param branch from 7558e37 to a6baf76 Compare July 20, 2026 04:03
@vthumbe1503

Copy link
Copy Markdown
Collaborator

/te-ci

@wujingyue

Copy link
Copy Markdown
Contributor Author

FYI, CI has an unrelated build error:

  nccl_ep.cc:23:10: fatal error: nccl_device.h: No such file or directory
     23 | #include <nccl_device.h>
        |          ^~~~~~~~~~~~~~~
  compilation terminated.

@wujingyue
wujingyue requested a review from vthumbe1503 July 20, 2026 05:49
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] FusedAdam silently skips updates when a parameter group ends with an empty tensor

2 participants