Migrate torch.cuda.amp.autocast to torch.amp - #593
Open
xyf5432 wants to merge 1 commit into
Open
Conversation
torch.cuda.amp.autocast is deprecated since torch 2.4 and scheduled for removal. Six imports in the core flagai/ package switch to a try/except import (torch.amp on torch >= 2.0, torch.cuda.amp below, which is not yet deprecated), keeping the documented torch >= 1.8 floor intact. Call sites gain the required device arg: Predictor, AltDiffusion/AltDiffusionM18, Unet, openaimodel, and the autograd function's **kwargs form in diffusionmodules/util.py (via a version-guarded helper). Fixes FlagAI-Open#592 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 #592
Summary
torch.cuda.amp.autocasthas been deprecated since torch 2.4 and is scheduled for removal. This PR migrates the seven uses in the coreflagai/package totorch.ampwithout raising the torch floor, since the repo documentsPyTorch >= 1.8.0(README "Requirements and Installation") and pins no torch version:torch.ampon torch >= 2.0,torch.cuda.ampbelow, where it is not yet deprecated):PredictorAPI)with autocast():→with _autocast():, where a version-guarded helper keeps both call signatures correct:autocast("cuda")? The two APIs take different call forms:torch.amp.autocast(>= 2.0) requires the device type as the first positional argument, whiletorch.cuda.amp.autocast(< 2.0) hasenabledas its first positional parameter — a bareautocast("cuda")would silently feed"cuda"into the old API'senabledslot. That happens to behave equivalently (truthy check), but it relies on unspecified behavior, so the helper dispatches to the exact signature each version expects.torch.cuda.amp.autocast(**ctx.gpu_autocast_kwargs)takes keyword args only):The vendored
examples/ca-lora/src/section-4.1/opendelta/copy is third-party embedded code and intentionally left untouched.Type of change
Validation
Verified on torch 2.11.0 with
warnings.simplefilter("error", FutureWarning):_autocast()/_autocast_ctx()on torch >= 2.0 dispatch totorch.amp.autocast("cuda", ...)— including theenabled=...kwargs form — and run warning-free.torch.ampmasked (simulating torch < 2.0), theImportErroris raised, the imports fall through totorch.cuda.amp.autocast, and both helpers dispatch totorch.cuda.amp.autocast(**kwargs)— the signature the old API actually takes (the old API emits no warning before torch 2.4).torch.cuda.amp.autocast()raisesFutureWarningunder the same filter — the fix is effective and the filter is sensitive.py_compilepasses on all six touched files; the coreflagai/package has zero baretorch.cuda.ampreferences (grep-verified).User impact
No behavior change on any supported torch version (the documented floor of 1.8 is preserved); the
FutureWarningemitted by anyPredictor-based inference and AltDiffusion inference/training on torch 2.4+ is eliminated, as is the breakage risk oncetorch.cuda.ampis removed.Notes for reviewers
with autocast():form (default enabled, float16), and_autocast()preserves exactly that on both torch paths — no semantics change.