-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rules for deprecated AMP APIs (#87)
Add codemods for `torch.cuda.amp.autocast` and `torch.cpu.amp.autocast`, and checkers for `torch.cuda.amp.custom_fwd` and `torch.cuda.amp.custom_bwd`.
- Loading branch information
Showing
7 changed files
with
91 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import torch | ||
|
||
torch.cuda.amp.autocast() | ||
torch.cuda.amp.custom_fwd() | ||
torch.cuda.amp.custom_bwd() | ||
|
||
dtype = torch.float32 | ||
maybe_autocast = torch.cpu.amp.autocast() | ||
maybe_autocast = torch.cpu.amp.autocast(dtype=torch.bfloat16) | ||
maybe_autocast = torch.cpu.amp.autocast(dtype=dtype) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
3:1 TOR101 Use of deprecated function torch.cuda.amp.autocast | ||
4:1 TOR101 Use of deprecated function torch.cuda.amp.custom_fwd | ||
5:1 TOR101 Use of deprecated function torch.cuda.amp.custom_bwd | ||
8:18 TOR101 Use of deprecated function torch.cpu.amp.autocast | ||
9:18 TOR101 Use of deprecated function torch.cpu.amp.autocast | ||
10:18 TOR101 Use of deprecated function torch.cpu.amp.autocast |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import torch | ||
|
||
dtype = torch.float32 | ||
|
||
maybe_autocast = torch.cuda.amp.autocast() | ||
maybe_autocast = torch.cuda.amp.autocast(dtype=torch.bfloat16) | ||
maybe_autocast = torch.cuda.amp.autocast(dtype=dtype) | ||
|
||
maybe_autocast = torch.cpu.amp.autocast() | ||
maybe_autocast = torch.cpu.amp.autocast(dtype=torch.bfloat16) | ||
maybe_autocast = torch.cpu.amp.autocast(dtype=dtype) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import torch | ||
|
||
dtype = torch.float32 | ||
|
||
maybe_autocast = torch.amp.autocast("cuda") | ||
maybe_autocast = torch.amp.autocast("cuda", dtype=torch.bfloat16) | ||
maybe_autocast = torch.amp.autocast("cuda", dtype=dtype) | ||
|
||
maybe_autocast = torch.amp.autocast("cpu") | ||
maybe_autocast = torch.amp.autocast("cpu", dtype=torch.bfloat16) | ||
maybe_autocast = torch.amp.autocast("cpu", dtype=dtype) |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import libcst as cst | ||
|
||
from ...common import get_module_name | ||
|
||
|
||
def call_replacement_cpu_amp_autocast(node: cst.Call) -> cst.CSTNode: | ||
return _call_replacement_amp(node, "cpu") | ||
|
||
|
||
def call_replacement_cuda_amp_autocast(node: cst.Call) -> cst.CSTNode: | ||
return _call_replacement_amp(node, "cuda") | ||
|
||
|
||
def _call_replacement_amp(node: cst.Call, device: str) -> cst.CSTNode: | ||
""" | ||
Replace `torch.cuda.amp.autocast()` with `torch.amp.autocast("cuda")` and | ||
Replace `torch.cpu.amp.autocast()` with `torch.amp.autocast("cpu")`. | ||
""" | ||
device_arg = cst.ensure_type(cst.parse_expression(f'f("{device}")'), cst.Call).args[ | ||
0 | ||
] | ||
|
||
module_name = get_module_name(node, "torch") | ||
replacement = cst.parse_expression(f"{module_name}.amp.autocast(args)") | ||
replacement = replacement.with_changes(args=(device_arg, *node.args)) | ||
return replacement |