Replace deprecated torch.cuda.amp.GradScaler with torch.amp in capture.py - #1946
Replace deprecated torch.cuda.amp.GradScaler with torch.amp in capture.py#1946xyf5432 wants to merge 7 commits into
Conversation
torch.cuda.amp.GradScaler is deprecated since torch 2.3 and scheduled for
removal; torch.amp.GradScaler('cuda') is the API-compatible replacement
(available since torch 2.3, well below the torch>=2.10 floor). This
removes the last torch.cuda.amp reference in the core physicsnemo
library (examples/tests still use it, tracked in NVIDIA#1945).
Fixes NVIDIA#1945
Co-Authored-By: Claude <noreply@anthropic.com>
CODEOWNERS review mapCurrent for commit ⏳ @ktangsali — 1 file(s)
⏳ @NickGeneva — 1 file(s)
Comment |
Greptile SummaryMigrates static-capture gradient scaling from the deprecated
Important Files Changed
Reviews (1): Last reviewed commit: "Use torch.amp.GradScaler in capture.py" | Re-trigger Greptile |
Completes the torch.cuda.amp deprecation cleanup tracked in NVIDIA#1945: mechanical replacement across 13 example scripts, 2 Hydra configs (_target_ class paths) and test/common/optimization.py. The remaining torch.cuda.amp string is a doc URL comment in examples/minimal/ShardTensorExamples/.../measure_perf.py, not code. Co-Authored-By: Claude <noreply@anthropic.com>
|
/ok to test 5a5458e |
|
/ok to test de9a8f4 |
|
/ok to test 0aee19a |
There was a problem hiding this comment.
🟡 Changes recommended
Several scripts now import torch.amp.autocast but still call autocast(...) without providing/binding device_type, which will error at runtime.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR migrates remaining uses of deprecated torch.cuda.amp APIs to torch.amp across the core library, examples, Hydra configs, and tests to eliminate FutureWarnings and avoid future breakage when torch.cuda.amp is removed.
Changes:
- Update core AMP scaler creation/typing in
physicsnemo/utils/capture.pytotorch.amp.GradScaler. - Replace
torch.cuda.ampimports/usages in multiple example training scripts and one benchmark utility. - Update Hydra
_target_references and adjust test AMP scaler construction.
File summaries
| File | Description |
|---|---|
physicsnemo/utils/capture.py |
Switch core gradient-scaler annotation/instantiation to torch.amp.GradScaler. |
test/common/optimization.py |
Update test scaler construction away from deprecated torch.cuda.amp.GradScaler. |
examples/weather/graphcast/train_graphcast.py |
Update GradScaler import to torch.amp. |
examples/weather/graphcast/train_base.py |
Update autocast import to torch.amp. |
examples/reservoir_simulation/xmgn/src/train.py |
Update GradScaler import to torch.amp. |
examples/healthcare/bloodflow_1d_mgn/train.py |
Update GradScaler import to torch.amp. |
examples/healthcare/bloodflow_1d_mgn/inference.py |
Update GradScaler import to torch.amp. |
examples/cfd/transient_conjugate_heat_transfer_tank_fill/train.py |
Update GradScaler/autocast imports to torch.amp. |
examples/cfd/external_aerodynamics/xaeronet/volume/train.py |
Update GradScaler import to torch.amp. |
examples/cfd/external_aerodynamics/moe/train.py |
Update GradScaler/autocast imports to torch.amp. |
examples/cfd/external_aerodynamics/figconvnet/train.py |
Update autocast partial to use torch.amp.autocast with CUDA device type. |
examples/cfd/external_aerodynamics/figconvnet/configs/base.yaml |
Update Hydra target to torch.amp.GradScaler. |
examples/cfd/external_aerodynamics/domino/src/conf/config.yaml |
Update Hydra target to torch.amp.GradScaler. |
examples/cfd/external_aerodynamics/domino/src/benchmark_dataloader.py |
Update GradScaler/autocast imports to torch.amp. |
examples/cfd/external_aerodynamics/domino_nim_finetuning/src/train.py |
Update GradScaler/autocast imports to torch.amp. |
examples/cfd/darcy_transolver/train_transolver_darcy_fix.py |
Update GradScaler type annotations/docstrings to torch.amp.GradScaler. |
Review details
- Files reviewed: 16/16 changed files
- Comments generated: 5
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| from hydra.utils import to_absolute_path | ||
| from omegaconf import DictConfig, OmegaConf | ||
| import torch.distributed as dist | ||
| from torch.cuda.amp import GradScaler, autocast | ||
| from torch.amp import GradScaler, autocast | ||
| from torch.nn.parallel import DistributedDataParallel | ||
| from torch.utils.data import DataLoader |
| from torch.utils.data import DataLoader, DistributedSampler | ||
| import numpy as np | ||
| from tqdm import tqdm | ||
| from torch.cuda.amp import GradScaler, autocast | ||
| from torch.amp import GradScaler, autocast | ||
|
|
| from omegaconf import DictConfig, OmegaConf | ||
| from torch.cuda.amp import GradScaler, autocast | ||
| from torch.amp import GradScaler, autocast | ||
| from torch.nn.parallel import DistributedDataParallel | ||
| from torch.utils.data.distributed import DistributedSampler | ||
| from torch.utils.tensorboard import SummaryWriter |
| import torch | ||
| from torch.profiler import profile, record_function, ProfilerActivity | ||
| from torch.cuda.amp import autocast | ||
| from torch.amp import autocast | ||
|
|
||
| import sys |
| amp_device = "cpu" | ||
| amp_dtype = torch.bfloat16 | ||
| scaler = torch.cuda.amp.GradScaler(enabled=False) # Always false on CPU | ||
| scaler = torch.amp.GradScaler("cuda", enabled=False) # Always false on CPU |
… consistency
torch.amp.autocast requires device_type as first positional argument;
torch.cuda.amp.autocast implied it. The migration left 6 call sites in
example scripts without it, which raises TypeError at runtime.
- domino_nim_finetuning/src/train.py:620,685
- external_aerodynamics/moe/train.py:189
- transient_conjugate_heat_transfer_tank_fill/train.py:82,150
- weather/graphcast/train_base.py:31
Also align test/common/optimization.py:308 GradScaler device with the
CPU branch ("cpu", consistent with the rest of the file).
Verified on torch 2.11.0+cpu: all six forms run clean; AST scan shows no
autocast call without device_type.
Head branch was pushed to by a user without write access
|
Fixed in 543b097:
Verified on torch 2.11.0+cpu: all six |
Fixes #1945
Summary
torch.cuda.amphas been deprecated since torch 2.3 (GradScaler) / 2.4 (autocast) and is scheduled for removal. This PR migrates every remaining use in the repository — the core library, 13 example scripts, 2 Hydra configs, and the test suite — to thetorch.ampAPI:examples/):from torch.cuda.amp import GradScaler/autocast→from torch.amp import ...(bare call sites unchanged), thepartial(torch.cuda.amp.autocast, ...)infigconvnet/train.py→partial(torch.amp.autocast, "cuda", ...), and type annotations/docstrings indarcy_transolver/train_transolver_darcy_fix.py._target_: torch.cuda.amp.GradScaler→_target_: torch.amp.GradScaler(instantiatepassesenabled=...; device defaults to"cuda").test/common/optimization.py(2 call sites).torch.amp.GradScaler("cuda", ...)/torch.amp.autocast("cuda", ...)are the official replacements (available since torch 2.3 / 2.0, well below thetorch>=2.10.0floor inpyproject.toml) and share the same methods and state-dict/checkpoint format, so behavior is unchanged. The only remainingtorch.cuda.ampstring in the repo is a doc URL comment inexamples/minimal/ShardTensorExamples/5_vit_training_loop/utils/measure_perf.py, not code.Type of change
Validation
Verified on torch 2.11.0 with
warnings.simplefilter("error", FutureWarning):torch.amp.GradScaler("cuda", enabled=...)andtorch.amp.autocast("cuda", ...)construct warning-free (AMP is disabled when CUDA is unavailable, by design).torch.cuda.amp.GradScaler(enabled=True)raisesFutureWarningunder the same filter — the fix is effective and the filter is sensitive.py_compilepasses on all 15 touched files; the repo now has zero code references totorch.cuda.amp(grep-verified).ruff checkshows no new errors vs. baseline; all touched files passruff format --check(the one remaining unformatted file,train_graphcast.py, was already unformatted at HEAD).User impact
No behavior change; the
FutureWarningemitted at import ofphysicsnemo.utils.captureand on every AMP-enabled example/test run on torch 2.4+ is eliminated, as is theAttributeError/Hydra instantiation failure risk oncetorch.cuda.ampis removed.Notes for reviewers