Conversation
torch.cuda.amp.autocast is deprecated since torch 2.4 and scheduled for
removal. Each of the six call sites now goes through a small version-guarded
helper -- torch.amp.autocast("cuda", ...) on torch >= 2.0, torch.cuda.amp
below, which is not yet deprecated -- so the deprecated import is dropped
while the documented torch >= 1.8 floor holds. Call sites gain the required
device arg: Predictor, AltDiffusion/AltDiffusionM18, Unet, openaimodel and
the autograd function's **kwargs form in diffusionmodules/util.py. Unet and
openaimodel bind torch as th, so their helpers reference th.
Fixes FlagAI-Open#592
Signed-off-by: 谢翊凡 <xyf5432@users.noreply.github.com>
Co-Authored-By: Claude Code <noreply@anthropic.com>
1 task
There was a problem hiding this comment.
🟡 Changes recommended
Fix the undefined torch references in the U-Net and OpenAI model autocast helpers.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Migrates core AMP usage to torch.amp while retaining compatibility with older PyTorch versions.
Changes:
- Adds version-compatible autocast helpers and updates inference/training call sites.
- Preserves the
torch.cuda.ampfallback for PyTorch <2.0. - Two helpers currently reference undefined
torchdespite importing it asth, causing criticalNameErrors.
File summaries
| File | Reviewed change |
|---|---|
flagai/model/predictor/predictor.py |
Updates Predictor autocast usage. |
flagai/model/mm/Unets/Unet.py |
Adds autocast helper; helper references undefined torch. |
flagai/model/mm/modules/diffusionmodules/util.py |
Updates checkpoint backward autocast handling. |
flagai/model/mm/modules/diffusionmodules/openaimodel.py |
Adds autocast helper; helper references undefined torch. |
flagai/model/mm/AltDiffusionM18.py |
Updates AltDiffusion M18 autocast usage. |
flagai/model/mm/AltDiffusion.py |
Updates AltDiffusion autocast usage. |
Review details
- Files reviewed: 6/6 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
xyf5432
force-pushed
the
fix/amp-deprecation
branch
from
September 14, 2026 13:37
f056cb4 to
7b7c1e6
Compare
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:from torch.cuda.amp import autocastlines across 5 files —AltDiffusionM18.pycarries it twice): removed. Nothing is imported fromtorch.ampat import time any more; each of these modules defines the version-guarded helper below instead.PredictorAPI)with autocast():→with _autocast():, where a version-guarded helper keeps both call signatures correct:Unets/Unet.pyandmodules/diffusionmodules/openaimodel.pyimport torch asth, so their copies of this helper referenceth(see the review note at the end).Why not a plain
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.13.0+cpu, executing each helper in a namespace that contains only the names its own module binds at top level, so a helper referencing an unbound name fails the check instead of passing silently:
torch.amp.autocast("cuda", ...)on torch >= 2.0 — in the bare and theenabled=...kwargs form — with noFutureWarningunderwarnings.simplefilter("error", FutureWarning).torch.ampremoved from the module (simulating torch < 2.0), 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 onlytorch.cuda.ampreferences left in the coreflagai/package are the guarded fallback branches (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.This replaces #593, which was closed when its branch was force-pushed to a commit with unrelated history while adding the DCO
Signed-off-byline. The code change is identical apart from the review fix noted above (the twoth-bound helpers); the commit now carries the sign-off.🤖 Generated with Claude Code