Replace deprecated torch.cuda.amp API with torch.amp equivalents - #2025
Open
xyf5432 wants to merge 1 commit into
Open
Replace deprecated torch.cuda.amp API with torch.amp equivalents#2025xyf5432 wants to merge 1 commit into
xyf5432 wants to merge 1 commit into
Conversation
torch.cuda.amp autocast/custom_fwd/custom_bwd are deprecated since torch 2.4 and GradScaler since 2.3, all scheduled for removal. Migrate the remaining uses (autocast_mode._cast, custom_fwd/custom_bwd, GradScaler, grad_scaler.OptState, autocast context) to torch.amp, which has provided the same names since torch 2.3. Also enforce the torch>=2.6.0 floor declared in requirements.txt inside setup.py, replacing the dormant 0.4 check. Co-Authored-By: Claude <noreply@anthropic.com>
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.
Fixes #2024
Summary
torch.cuda.amphas been deprecated since torch 2.3 (GradScaler) / 2.4 (autocast,custom_fwd,custom_bwd) and is scheduled for removal. This PR migrates the remaining usages totorch.amp, which has provided the same names since torch 2.3, and enforces the existingtorch>=2.6.0floor fromrequirements.txtinsidesetup.py.Changes:
apex/_autocast_utils.py—torch.cuda.amp.autocast_mode._cast(args, dtype)→torch.amp.autocast_mode._cast(args, "cuda", dtype). Thetorch.ampvariant takes an explicitdevice_typesince torch 2.5; the old two-arg form only forwards and warns "Please usetorch.amp.autocast_mode._cast(value, 'cuda', dtype)instead".apex/contrib/cudnn_gbn/batch_norm.py—from torch.amp import custom_bwd, custom_fwd; decorators now pass the required keyword-onlydevice_type="cuda"(same style as the existingapex/contrib/conv_bias_relu/conv_bias_relu.py).apex/contrib/optimizers/distributed_fused_adam.py— module-level aliasesGradScaler = torch.amp.GradScalerandGradScalerOptState = torch.amp.grad_scaler.OptState(verified to be the identical objects behind the deprecated names), used by thegrad_scalertype annotations, docstrings, and the per-methodOptStateassignments that are now removed.apex/contrib/test/optimizers/test_distributed_fused_lamb.py—from torch.amp import GradScaler.tests/L0/run_mlp/test_mlp.py—torch.amp.autocast("cuda", enabled=...).setup.py— compile-time check now rejectstorch < 2.6.0(viapackaging.parse, previously a dormant 0.4 check) andinstall_requiresgainstorch>=2.6.0, matchingrequirements.txt.Type of change
Validation
Verified on torch 2.11.0 (CPU build):
torch.amp.grad_scaler.OptState is torch.cuda.amp.grad_scaler.OptState→True, so theiscomparisons inunscale_grads/stepare unaffected._castpath is warning-free underwarnings.simplefilter("error", FutureWarning), while the old two-arg_castraises under the same filter — the migration is effective and the filter is sensitive._castproduce identical outputs for identical inputs.custom_fwd(device_type="cuda")/custom_bwd(device_type="cuda")forward + backward run cleanly.torch.ampobjects; theGradScaler | Noneannotations evaluate.setup.pyversion check passes 2.11.0 and rejects 1.13.1 (control group raisesRuntimeError).ruff format --checkwith the pinned 0.16.1: all touched files unchanged.ruff check: identical error set before and after (27 → 27, only line-number shifts from the added module-level aliases) — no new lint issues.py_compilepasses on all touched files.User impact
No API or behavior changes for users; the
FutureWarnings emitted throughtorch.cuda.amp(and the risk of breaking on its removal) are eliminated.Notes for reviewers
torch.amphas provided the same names since torch 2.3, andrequirements.txtalready declarestorch>=2.6.0, so the migration introduces no minimum-version regression._cast(value, device_type, dtype)signature exists since torch 2.5; 2.3/2.4 are outside the declared floor.