Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TorchLog1pVisitor #77

Merged
merged 2 commits into from
Sep 17, 2024
Merged
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
9 changes: 9 additions & 0 deletions tests/fixtures/misc/checker/log1p.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import torch
a = torch.randn(5)
b = torch.log(1 + a)
c = torch.log(a + 1)
b = torch.log(1.0 + a)
c = torch.log(a + 1.0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we already add Array API examples here? We don't have the capability of automatically detecting them at the moment, but it's good to be explicit about the known false negatives, especially when we can catch them later

import torch

a = torch.randn(5)

print((a + 1).log())
print(a.log1p())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a false negative.


# False negative: can not detect currently
x = (a + 1).log()
4 changes: 4 additions & 0 deletions tests/fixtures/misc/checker/log1p.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3:5 TOR106 Use `torch.log1p(x)` instead of `torch.log(1 + x)`. It is more accurate for small values of `x`.
4:5 TOR106 Use `torch.log1p(x)` instead of `torch.log(1 + x)`. It is more accurate for small values of `x`.
5:5 TOR106 Use `torch.log1p(x)` instead of `torch.log(1 + x)`. It is more accurate for small values of `x`.
6:5 TOR106 Use `torch.log1p(x)` instead of `torch.log(1 + x)`. It is more accurate for small values of `x`.
5 changes: 4 additions & 1 deletion tests/test_torchfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ def pytest_generate_tests(metafunc):
("ALL,TOR102", GET_ALL_ERROR_CODES()),
("TOR102", {"TOR102"}),
("TOR102,TOR101", {"TOR102", "TOR101"}),
("TOR1,TOR102", {"TOR102", "TOR101", "TOR103", "TOR104", "TOR105"}),
(
"TOR1,TOR102",
{"TOR102", "TOR101", "TOR103", "TOR104", "TOR105", "TOR106"},
),
(None, set(GET_ALL_ERROR_CODES()) - exclude_set),
]
metafunc.parametrize("case,expected", cases, ids=[case for case, _ in cases])
Expand Down
8 changes: 5 additions & 3 deletions torchfix/torchfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .visitors import (
TorchDeprecatedSymbolsVisitor,
TorchLog1pVisitor,
TorchNonPublicAliasVisitor,
TorchReentrantCheckpointVisitor,
TorchRequireGradVisitor,
Expand All @@ -28,15 +29,16 @@

ALL_VISITOR_CLS = [
TorchDeprecatedSymbolsVisitor,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any ideas how to avoid this ugly duplication of all visitors?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can automatically pick them up with a registry pattern. Already got something in progress for this.

TorchLog1pVisitor,
TorchNonPublicAliasVisitor,
TorchRequireGradVisitor,
TorchReentrantCheckpointVisitor,
TorchScopedLibraryVisitor,
TorchSynchronizedDataLoaderVisitor,
TorchUnsafeLoadVisitor,
TorchVisionDeprecatedPretrainedVisitor,
TorchVisionDeprecatedToTensorVisitor,
TorchVisionSingletonImportVisitor,
TorchUnsafeLoadVisitor,
TorchReentrantCheckpointVisitor,
TorchNonPublicAliasVisitor,
]


Expand Down
13 changes: 9 additions & 4 deletions torchfix/visitors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from .deprecated_symbols import TorchDeprecatedSymbolsVisitor
from .internal import TorchScopedLibraryVisitor
from .misc import TorchReentrantCheckpointVisitor, TorchRequireGradVisitor
from .misc import (
TorchReentrantCheckpointVisitor,
TorchRequireGradVisitor,
TorchLog1pVisitor,
)
from .nonpublic import TorchNonPublicAliasVisitor
from .performance import TorchSynchronizedDataLoaderVisitor
from .security import TorchUnsafeLoadVisitor
Expand All @@ -12,13 +16,14 @@

__all__ = [
"TorchDeprecatedSymbolsVisitor",
"TorchLog1pVisitor",
"TorchNonPublicAliasVisitor",
"TorchReentrantCheckpointVisitor",
"TorchRequireGradVisitor",
"TorchScopedLibraryVisitor",
"TorchSynchronizedDataLoaderVisitor",
"TorchUnsafeLoadVisitor",
"TorchVisionDeprecatedPretrainedVisitor",
"TorchVisionDeprecatedToTensorVisitor",
"TorchVisionSingletonImportVisitor",
"TorchUnsafeLoadVisitor",
"TorchReentrantCheckpointVisitor",
"TorchNonPublicAliasVisitor",
]
44 changes: 44 additions & 0 deletions torchfix/visitors/misc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,47 @@ def visit_Call(self, node):
message=self.ERRORS[0].message(),
replacement=replacement,
)


class TorchLog1pVisitor(TorchVisitor):
"""
Suggest using `torch.log1p(x)` instead of `torch.log(1 + x)`.
"""

ERRORS = [
TorchError(
"TOR106",
(
"Use `torch.log1p(x)` instead of `torch.log(1 + x)`. "
"It is more accurate for small values of `x`."
),
)
]

def visit_Call(self, node):
if self.get_qualified_name_for_call(node) == "torch.log":

if m.matches(
node,
m.Call(
args=[
m.Arg(
value=m.BinaryOperation(
left=m.Integer(value="1") | m.Float(value="1.0"),
operator=m.Add(),
)
| m.BinaryOperation(
operator=m.Add(),
right=m.Integer(value="1") | m.Float(value="1.0"),
),
),
],
),
):

self.add_violation(
node,
error_code=self.ERRORS[0].error_code,
message=self.ERRORS[0].message(),
replacement=None,
)