fix(contrib): call torch.cuda.is_available() in FP16_Optimizer guard - #2021
Open
andrewwhitecdw wants to merge 1 commit into
Open
fix(contrib): call torch.cuda.is_available() in FP16_Optimizer guard#2021andrewwhitecdw wants to merge 1 commit into
andrewwhitecdw wants to merge 1 commit into
Conversation
crcrpar
reviewed
Jul 31, 2026
crcrpar
left a comment
Collaborator
There was a problem hiding this comment.
looks good but I'm afraid I don't think the test is necessary
andrewwhitecdw
force-pushed
the
sweep/bugfix-cuda-availability-check
branch
2 times, most recently
from
July 31, 2026 14:45
72d5d57 to
acb771b
Compare
Remove the unnecessary regression test and apply pre-commit auto-fixes.
andrewwhitecdw
force-pushed
the
sweep/bugfix-cuda-availability-check
branch
from
July 31, 2026 14:58
c5c6b72 to
40c0f0e
Compare
Author
|
Closing per adversarial audit: branch contains off-scope or not-a-bug changes. |
Author
|
script closed on accident |
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.
Summary
apex.contrib.optimizers.FP16_Optimizer.__init__is supposed to fail fast with a clearSystemError("Cannot use fp16 without CUDA.")on machines without CUDA. The guard never fires, so on a CPU-only machine (or a build without the CUDA extensions) construction proceeds and blows up later with a confusingRuntimeError: FP16_Optimizer requires cuda extensionsfrom deep in the initializer.Root cause
torch.cuda.is_available(without parentheses) is the bound method object, which is always truthy, sonot torch.cuda.is_availableis alwaysFalseand theraiseis dead code.Fix
Call the function:
One-line change; behavior on CUDA machines is unchanged (the guard still does not fire there).
Testing
On a CPU-only machine with an ephemeral env (
uv run --with torch), usingtorch.optim.SGDas the wrapped optimizer:FP16_Optimizer(opt, static_loss_scale=128.0)raisesSystemError: Cannot use fp16 without CUDA.immediately — the intended behavior.RuntimeError: FP16_Optimizer requires cuda extensionsfrom line 64, confirming the guard was dead code.tests/(L0/L1) require a GPU and the compiledamp_Cextensions, so they could not be run locally; this change does not touch any CUDA code path, but please rely on CI for the full suite.Why existing tests missed it
The optimizer tests run exclusively on CUDA machines, where the guard is a no-op either way; nothing exercises the CPU-only error path.