Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/fixtures/misc/checker/rsqrt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import torch


a = torch.randn(5)
b = 1 / torch.sqrt(a)
b = 1.0 / torch.sqrt(a)
b = a / torch.sqrt(a)
# False negative
b = 1 / a.sqrt()
b = 1.0 / a.sqrt()
3 changes: 3 additions & 0 deletions tests/fixtures/misc/checker/rsqrt.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
5:5 TOR109 Consider faster `a*torch.rsqrt(b)` instead of `a/torch.sqrt(b)`.
6:5 TOR109 Consider faster `a*torch.rsqrt(b)` instead of `a/torch.sqrt(b)`.
7:5 TOR109 Consider faster `a*torch.rsqrt(b)` instead of `a/torch.sqrt(b)`.
1 change: 1 addition & 0 deletions tests/test_torchfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def pytest_generate_tests(metafunc):
"TOR106",
"TOR107",
"TOR108",
"TOR109",
},
),
(None, set(GET_ALL_ERROR_CODES()) - exclude_set),
Expand Down
2 changes: 2 additions & 0 deletions torchfix/torchfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
TorchScopedLibraryVisitor,
TorchSynchronizedDataLoaderVisitor,
TorchUnsafeLoadVisitor,
TorchRsqrtVisitor,
TorchVisionDeprecatedPretrainedVisitor,
TorchVisionDeprecatedToTensorVisitor,
TorchVisionSingletonImportVisitor,
Expand All @@ -35,6 +36,7 @@
TorchExpm1Visitor,
TorchLog1pVisitor,
TorchLogsumexpVisitor,
TorchRsqrtVisitor,
TorchNonPublicAliasVisitor,
TorchRequireGradVisitor,
TorchReentrantCheckpointVisitor,
Expand Down
2 changes: 2 additions & 0 deletions torchfix/visitors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
TorchLogsumexpVisitor,
TorchReentrantCheckpointVisitor,
TorchRequireGradVisitor,
TorchRsqrtVisitor,
)
from .nonpublic import TorchNonPublicAliasVisitor
from .performance import (
Expand All @@ -30,6 +31,7 @@
"TorchScopedLibraryVisitor",
"TorchSynchronizedDataLoaderVisitor",
"TorchUnsafeLoadVisitor",
"TorchRsqrtVisitor",
"TorchVisionDeprecatedPretrainedVisitor",
"TorchVisionDeprecatedToTensorVisitor",
"TorchVisionSingletonImportVisitor",
Expand Down
27 changes: 26 additions & 1 deletion torchfix/visitors/misc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ def visit_Call(self, node):
)
== "torch.exp"
):

# if `dim` is not provided or None for sum, skip:
# https://github.com/pytorch/pytorch/issues/144339
dim_arg = self.get_specific_arg(
Expand All @@ -201,3 +200,29 @@ def visit_Call(self, node):
message=self.ERRORS[0].message(),
replacement=None,
)


class TorchRsqrtVisitor(TorchVisitor):
"""
Suggest using `a*torch.rsqrt(b)` instead of `a/torch.sqrt(b)`.
"""

ERRORS = [
TorchError(
"TOR109",
("Consider faster `a*torch.rsqrt(b)` instead of `a/torch.sqrt(b)`."),
)
]

def visit_BinaryOperation(self, node):
if m.matches(
node,
m.BinaryOperation(operator=m.Divide(), right=m.Call()),
):
if self.get_qualified_name_for_call(node.right) == "torch.sqrt":
self.add_violation(
node,
error_code=self.ERRORS[0].error_code,
message=self.ERRORS[0].message(),
replacement=None,
)