pytorch-labs / torchfix Goto Github PK
View Code? Open in Web Editor NEWTorchFix - a linter for PyTorch-using code with autofix support
License: Other
TorchFix - a linter for PyTorch-using code with autofix support
License: Other
As described in pytorch/test-infra#4559 (comment)
It will be just a linter, not a codemod, as it's not possible to determine the desired value statically.
Inside a model definition, the torch.nn.Module
objects inside a Python list do not get their parameters registered. Hence such parameters do not get trained by the optimizer, even though they are in the call graph formed by forward(). This should be flagged by torchfix -- currently no warning is given for this issue.
Example:
class FeedForward(torch.nn.Module):
def __init__(self, n_features, n_classes, n_hidden, width):
super().__init__()
# Ideally, torchfix should issue a warning on below code
# The parameters of the hidden layers do not get registered if they are in a list, and are not optimized!
self.hidden_layers = [torch.nn.Linear(n_features if i ==0 else width, width, bias=True) for i in range(n_hidden)]
# Correct version of the above code -- use ModuleList([]) instead of python list []
self.hidden_layers = torch.nn.ModuleList([torch.nn.Linear(n_features if i ==0 else width, width, bias=True) for i in range(n_hidden)])
# Dummy call to torch.solve() to throw a torchfix warning (to demonstrate that torchfix is working correctly)
torch.solve()
Torchfix output:
$ torchfix --select=ALL ./supervised/nn/feed_forward_nn.py
supervised/nn/feed_forward_nn.py:20:9: TOR001 Use of removed function torch.solve: https://github.com/pytorch-labs/torchfix#torchsolve
Finished checking 1 files.
Today, use_reentrant defaults to True, but TOR003 sets use_reentrant=False which may subtly differ in behavior in certain cases. We should either make TOR003 set use_reentrant=True, or not run it by default.
cc @kit1980
There is a bit of code duplication (although not too much) in #7 and https://github.com/pytorch-labs/torchfix/blob/main/torchfix/visitors/security/__init__.py#L19
Add a simple rule to replace import torchvision.models as models
with from torchvision import models
.
The former is pretty common in the codebases for historical reasons.
See pytorch/pytorch#125050 for details.
This will require passing file path as an input to the rules.
In general it doesn't make too much sense, but for PyTorch-internal rule like TOR901 where we know the path in the CI this will allow to remove long enumerations of files to ignore from .flake8
Add a doc (probably just part of README) listing all available error types with codes and explanations.
Then add a test that the doc has the info for the error code if and only if there is a visitor with this error code.
Currently TorchFix tries to follow latest PyTorch main for things like deprecated APIs.
This is not ideal for people who want to use a specific release, for example.
So every rule should be annotated for which PyTorch (and also TorchVision and other libraries where applicable) version it applies, and there should be a configuration parameter for TorchFix to pass the version.
TorchFix should understand statically types of the objects. This feature will enable more rules and more precise targeting for the existing rules. The implementation will require adoption of Pyre and LibCST’s TypeInferenceProvider
. The feature will likely be optional as running Pyre may be a barrier for some users.
As an example, currently TorchFix will understand that this qr
is a deprecated function here:
import torch
torch.tensor([[1.0], [2.0]])
torch.qr(a)
But not when using method notation - a.qr()
- because TorchFix doesn't understand statically that a
is a PyTorch tensor, maybe it's some unrelated object that just happens to have a method named qr
.
torch.utils._pytree._register_pytree_node is deprecated. Please use torch.utils._pytree.register_pytree_node instead.
This can probably be just a config change at https://github.com/pytorch-labs/torchfix/blob/main/torchfix/deprecated_symbols.yaml
Maybe in future if we have several pytree-related rules, they can their own category and error codes.
See this comment for an example #44 (comment)
Use https://pre-commit.com/ hooks to run black/flake8.
TorchVisitor
has self.needed_imports
to add new imports after code transformations: https://github.com/pytorch-labs/torchfix/blob/main/torchfix/common.py#L47
But there is no functionality to remove imports that are no longer needed.
It's not critical, as unnecessary imports can be caught by existing Python linters, but it would be nicer to have them cleaned up in TorchFix.
/home/runner/work/_tool/Python/3.11.9/x64/lib/python3.11/site-packages/torch/backends/cuda/init.py:342: FutureWarning: torch.backends.cuda.sdp_kernel() is deprecated. In the future, this context manager will be removed. Please see, torch.nn.attention.sdpa_kernel() for the new context manager, with updated signature.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.