- Notifications
You must be signed in to change notification settings - Fork20
TorchFix - a linter for PyTorch-using code with autofix support
License
pytorch-labs/torchfix
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
TorchFix is a Python code static analysis tool - a linter with autofix capabilities -for users of PyTorch. It can be used to find and fix issues like usage of deprecatedPyTorch functions and non-public symbols, and to adopt PyTorch best practices in general.
TorchFix is built uponhttps://github.com/Instagram/LibCST - a library to manipulatePython concrete syntax trees. LibCST enables "codemods" (autofixes) in addition toreporting issues.
TorchFix can be used as a Flake8 plugin (linting only) or as a standaloneprogram (with autofix available for a subset of the lint violations).
Warning
Currently TorchFix is in abeta version stage, so there are still a lot of roughedges and many things can and will change.
To install the latest code from GitHub, clone/downloadhttps://github.com/pytorch-labs/torchfix and runpip install .
inside the directory.
To install a release version from PyPI, runpip install torchfix
.
After the installation, TorchFix will be available as a Flake8 plugin, so runningFlake8 normally will run the TorchFix linter.
To see only TorchFix warnings without the rest of the Flake8 linters, you can runflake8 --isolated --select=TOR0,TOR1,TOR2
TorchFix can also be run as a standalone program:torchfix .
Add--fix
parameter to try to autofix some of the issues (the files will be overwritten!)To see some additional debug info, add--show-stderr
parameter.
Caution
Please keep in mind that autofix is a best-effort mechanism. Given the dynamic nature of Python,and especially the beta version status of TorchFix, it's very difficult to havecertainty when making changes to code, even for the seemingly trivial fixes.
Warnings for issues with codes starting with TOR0, TOR1, and TOR2 are enabled by default.Warnings with other codes may be too noisy, so not enabled by default.To enable them, use standard flake8 configuration options for the plugin mode or usetorchfix --select=ALL .
for the standalone mode.
If you encounter a bug or some other problem with TorchFix, please file an issue onhttps://github.com/pytorch-labs/torchfix/issues.
New rule codes are assigned incrementally across the following categories:
- TOR0XX, TOR1XX: General-purpose
torch
functionality. - TOR2XX: Domain-specific rules, such as TorchVision.
- TOR4XX: Noisy rules that are disabled by default.
- TOR9XX: Internal rules specific for
pytorch/pytorch
repo, other users should not use these.
TOR0, TOR1 and TOR2 are enabled by default.
This function was deprecated since PyTorch version 1.9 and is now removed.
torch.solve
is deprecated in favor oftorch.linalg.solve
.torch.linalg.solve
has its arguments reversed and does not return the LU factorization.
To get the LU factorization seetorch.lu
, which can be used withtorch.lu_solve
ortorch.lu_unpack
.
X = torch.solve(B, A).solution
should be replaced withX = torch.linalg.solve(A, B)
.
This function was deprecated since PyTorch version 1.9 and is now removed.
torch.symeig
is deprecated in favor oftorch.linalg.eigh
.
The default behavior has changed from using the upper triangular portion of the matrix by default to using the lower triangular portion.
L,_=torch.symeig(A,upper=upper)
should be replaced with
L=torch.linalg.eigvalsh(A,UPLO='U'ifupperelse'L')
and
L,V=torch.symeig(A,eigenvectors=True)
should be replaced with
L,V=torch.linalg.eigh(A,UPLO='U'ifupperelse'L')
This is a common misspelling that can lead to silent performance issues.
The default value of theuse_reentrant
parameter intorch.utils.checkpoint
is being changedfromTrue
toFalse
. In the meantime, the value needs to be passed explicitly.
See thisforum postfor details.
SeeTOR001
.
This function is deprecated. Usetorch.nn.utils.parametrizations.weight_norm
which uses the modern parametrization API. The newweight_norm
is compatiblewithstate_dict
generated from oldweight_norm
.
Migration guide:
The magnitude (
weight_g
) and direction (weight_v
) are now expressedasparametrizations.weight.original0
andparametrizations.weight.original1
respectively.To remove the weight normalization reparametrization, use
torch.nn.utils.parametrize.remove_parametrizations
.The weight is no longer recomputed once at module forward; instead, it willbe recomputed on every access. To restore the old behavior, use
torch.nn.utils.parametrize.cached
before invoking the modulein question.
This function is deprecated. Use thetorch.nn.attention.sdpa_kernel
context manager instead.
Migration guide:Each boolean input parameter (defaulting to true unless specified) ofsdp_kernel
corresponds to aSDPBackened
. If the input parameter is true, the corresponding backend should be added to the input list ofsdpa_kernel
.
This function is deprecated in favor oftorch.linalg.multi_dot
.
Migration guide:multi_dot
accepts a list of two or more tensors whereaschain_matmul
accepted multiple tensors as input arguments. For migration, convert the multiple tensors in argument ofchain_matmul
into a list of two or more tensors formulti_dot
.
Example: Replacetorch.chain_matmul(a, b, c)
withtorch.linalg.multi_dot([a, b, c])
.
torch.cholesky()
is deprecated in favor oftorch.linalg.cholesky()
.
Migration guide:
L = torch.cholesky(A)
should be replaced withL = torch.linalg.cholesky(A)
.L = torch.cholesky(A, upper=True)
should be replaced withL = torch.linalg.cholesky(A).mH
torch.qr()
is deprecated in favor oftorch.linalg.qr()
.
Migration guide:
- The usage
Q, R = torch.qr(A)
should be replaced withQ, R = torch.linalg.qr(A)
. - The boolean parameter
some
oftorch.qr
is replaced with a string parametermode
intorch.linalg.qr
. The corresponding change in usage is fromQ, R = torch.qr(A, some=False)
toQ, R = torch.linalg.qr(A, mode="complete")
.
The functiontorch.range()
is deprecated as its usage is incompatible with Python's builtin range. Instead, usetorch.arange()
as it produces values in[start, end)
.
Migration guide:
torch.range(start, end)
produces values in the range of[start, end]
. Buttorch.arange(start, end)
produces values in[start, end)
. For step size of 1, migrate usage fromtorch.range(start, end, 1)
totorch.arange(start, end+1, 1)
.
Explicitly setweights_only
to False only if you trust the data you load and full pickle functionality is needed, otherwise setweights_only=True
.
SeeTOR101
.
TorchFix is BSD License licensed, as found in the LICENSE file.
About
TorchFix - a linter for PyTorch-using code with autofix support
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors15
Uh oh!
There was an error while loading.Please reload this page.