Skip to content

[PyTorch] Reject non-contiguous tensors in FusedAdam and FusedSGD - #4029

Draft
tanish-malekar wants to merge 1 commit into
NVIDIA:mainfrom
tanish-malekar:fused-optimizer-noncontiguous-check
Draft

tanish-malekar wants to merge 1 commit into
NVIDIA:mainfrom
tanish-malekar:fused-optimizer-noncontiguous-check

Conversation

@tanish-malekar

@tanish-malekar tanish-malekar commented Dec 4, 2026 •

Copy link
Copy Markdown

Description

The multi-tensor kernels receive only data_ptr() and sizes (makeTransformerEngineTensor(at::Tensor)) and walk every tensor list linearly. FusedAdam / FusedSGD never check the layout, so optimizing a view such as a column slice of a fused QKV matrix silently overwrites the neighboring storage.

Both optimizers now raise a ValueError for such tensors, before launching any kernel for the param group. A bare is_contiguous() check would also reject cases that work today, so tensors that are dense and share one memory layout (e.g. channels_last param, grad and state) are still accepted. A channels_last param with a contiguous grad or state is now rejected, since linear traversal pairs the wrong elements there.

Direct calls to the low-level multi_tensor_* functions are unchanged.

Fixes #4027

Testing

RTX A6000, driver 570.124.06, PyTorch 2.11.0+cu128, CUDA 12.8 toolkit, TE built for sm86. "Before" is d9c216fd, "after" is this branch.

Reproducer from the issue (modified only to catch and print the ValueError), frozen W_k/W_v elements modified:

before after
torch.optim.Adam 0 0
FusedAdam 160 0, raises ValueError
FusedSGD 160 0, raises ValueError
before after
test_fused_optimizer.py 67 passed, 12 skipped 81 passed (14 new), 12 skipped
test_multi_tensor.py 1968 passed, 822 skipped 1968 passed, 822 skipped

The skips include the FP8/MXFP8 tests, which need a newer GPU; they were not run. As a smoke check on the A6000, two FusedAdam steps with FP8 exp_avg/exp_avg_sq, and two with a delayed-scaling FP8 parameter, ran with the new check and stayed finite (values not compared against a reference).

Also not run: a distributed step() with DTensors, and the other L0 suites. black is clean on the changed Python files, and pylint on the three optimizer files.

The check itself takes about 1.2 ms per step per 1000 contiguous parameters with four tensor lists, and 4.7 ms when they are all channels_last (CPU timing on an EPYC 7413).

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

  • Add check_multi_tensor_layouts in optimizers/multi_tensor_apply.py
  • Check gradients, parameters, optimizer states and master weights (FP16/BF16, FP8 and FP32 lists) in FusedAdam.step and FusedSGD.step
  • Document the requirement in both class docstrings
  • Add tests to tests/pytorch/test_fused_optimizer.py

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes (the two files above)

The multi-tensor kernels access every tensor linearly from its data
pointer and ignore strides. Optimizing a view such as a column slice of
a fused QKV matrix silently overwrote the neighboring storage.

Check gradients, parameters, optimizer states and master weights before
launching the kernels of a param group, and raise a ValueError. Tensors
that are dense with an identical memory layout, such as channels_last,
remain supported.

Signed-off-by: Tanish Malekar <60835372+tanish-malekar@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug][PyTorch] FusedAdam / FusedSGD silently overwrites adjacent tensor storage on non-contiguous parameter views

1 participant