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 TorchScopedLibraryVisitor #22

Merged
merged 1 commit into from
Feb 5, 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
3 changes: 3 additions & 0 deletions tests/fixtures/internal/checker/scoped_library.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import torch
from torch.library import Library, impl, fallthrough_kernel
my_lib1 = Library("aten", "IMPL")
1 change: 1 addition & 0 deletions tests/fixtures/internal/checker/scoped_library.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3:11 TOR901 Use `torch.library._scoped_library` instead of `torch.library.Library` in PyTorch tests files. See https://github.com/pytorch/pytorch/pull/118318 for details.
Copy link

Choose a reason for hiding this comment

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

Is there is a way to suppress the lint? We'll probably want to allow some usages of torch.library.Library (just to test that API).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's disabled by default. When we enable it for some files, usual flake8 noqa comments can be used to ignore some lines.

5 changes: 4 additions & 1 deletion torchfix/torchfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
_UpdateFunctorchImports,
)

from .visitors.internal import TorchScopedLibraryVisitor

from .visitors.performance import TorchSynchronizedDataLoaderVisitor
from .visitors.misc import (TorchRequireGradVisitor, TorchReentrantCheckpointVisitor)

Expand All @@ -24,11 +26,12 @@

DEPRECATED_CONFIG_PATH = Path(__file__).absolute().parent / "deprecated_symbols.yaml"

DISABLED_BY_DEFAULT = ["TOR3", "TOR4"]
DISABLED_BY_DEFAULT = ["TOR3", "TOR4", "TOR9"]

ALL_VISITOR_CLS = [
TorchDeprecatedSymbolsVisitor,
TorchRequireGradVisitor,
TorchScopedLibraryVisitor,
TorchSynchronizedDataLoaderVisitor,
TorchVisionDeprecatedPretrainedVisitor,
TorchVisionDeprecatedToTensorVisitor,
Expand Down
33 changes: 33 additions & 0 deletions torchfix/visitors/internal/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import libcst as cst
from ...common import TorchVisitor, LintViolation


class TorchScopedLibraryVisitor(TorchVisitor):
"""
Suggest `torch.library._scoped_library` for PyTorch tests.
"""

ERROR_CODE = "TOR901"
MESSAGE = (
"Use `torch.library._scoped_library` instead of `torch.library.Library` "
"in PyTorch tests files. See https://github.com/pytorch/pytorch/pull/118318 "
"for details."
)

def visit_Call(self, node):
qualified_name = self.get_qualified_name_for_call(node)
if qualified_name == "torch.library.Library":
position_metadata = self.get_metadata(
cst.metadata.WhitespaceInclusivePositionProvider, node
)

self.violations.append(
Copy link

Choose a reason for hiding this comment

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

what limits this rule to files under test/ ?

Copy link
Contributor Author

@kit1980 kit1980 Feb 5, 2024

Choose a reason for hiding this comment

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

The rule is disabled by default. We can enable it for test/ in the CI config in pytorch/pytorch.

LintViolation(
error_code=self.ERROR_CODE,
message=self.MESSAGE,
line=position_metadata.start.line,
column=position_metadata.start.column,
node=node,
replacement=None,
)
)
Loading