Repository navigation
[PyTorch] Reject non-contiguous tensors in FusedAdam and FusedSGD - #4029
Draft
tanish-malekar wants to merge 1 commit into
Draft
tanish-malekar wants to merge 1 commit into
tanish-malekar wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The multi-tensor kernels receive only
data_ptr()and sizes (makeTransformerEngineTensor(at::Tensor)) and walk every tensor list linearly.FusedAdam/FusedSGDnever 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
ValueErrorfor such tensors, before launching any kernel for the param group. A bareis_contiguous()check would also reject cases that work today, so tensors that are dense and share one memory layout (e.g.channels_lastparam, grad and state) are still accepted. Achannels_lastparam 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), frozenW_k/W_velements modified:torch.optim.AdamFusedAdamValueErrorFusedSGDValueErrortest_fused_optimizer.pytest_multi_tensor.pyThe skips include the FP8/MXFP8 tests, which need a newer GPU; they were not run. As a smoke check on the A6000, two
FusedAdamsteps with FP8exp_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()withDTensors, and the otherL0suites.blackis clean on the changed Python files, andpylinton 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
Changes
check_multi_tensor_layoutsinoptimizers/multi_tensor_apply.pyFusedAdam.stepandFusedSGD.steptests/pytorch/test_fused_optimizer.pyChecklist: