Skip to main content

TorchFix - a linter for PyTorch-using code with autofix support

Project description

TorchFix - a linter for PyTorch-using code with autofix support

PyPI

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 deprecated PyTorch functions and non-public symbols, and to adopt PyTorch best practices in general.

TorchFix is built upon https://github.com/Instagram/LibCST - a library to manipulate Python concrete syntax trees. LibCST enables "codemods" (autofixes) in addition to reporting issues.

TorchFix can be used as a Flake8 plugin (linting only) or as a standalone program (with autofix available for a subset of the lint violations).

[!WARNING] Currently TorchFix is in a beta version stage, so there are still a lot of rough edges and many things can and will change.

Installation

To install the latest code from GitHub, clone/download https://github.com/pytorch-labs/torchfix and run pip install . inside the directory.

To install a release version from PyPI, run pip install torchfix.

Usage

After the installation, TorchFix will be available as a Flake8 plugin, so running Flake8 normally will run the TorchFix linter.

To see only TorchFix warnings without the rest of the Flake8 linters, you can run flake8 --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 have certainty 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 use torchfix --select=ALL . for the standalone mode.

Reporting problems

If you encounter a bug or some other problem with TorchFix, please file an issue on https://github.com/pytorch-labs/torchfix/issues.

Rule Code Assignment Policy

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.

Rules

TOR001 Use of removed function

torch.solve

This function was deprecated since PyTorch version 1.9 and is now removed.

torch.solve is deprecated in favor of torch.linalg.solve. torch.linalg.solve has its arguments reversed and does not return the LU factorization.

To get the LU factorization see torch.lu, which can be used with torch.lu_solve or torch.lu_unpack.

X = torch.solve(B, A).solution should be replaced with X = torch.linalg.solve(A, B).

torch.symeig

This function was deprecated since PyTorch version 1.9 and is now removed.

torch.symeig is deprecated in favor of torch.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' if upper else 'L')

and

L, V = torch.symeig(A, eigenvectors=True)

should be replaced with

L, V = torch.linalg.eigh(A, UPLO='U' if upper else 'L')

TOR002 Likely typo require_grad in assignment. Did you mean requires_grad?

This is a common misspelling that can lead to silent performance issues.

TOR003 Please pass use_reentrant explicitly to checkpoint

The default value of the use_reentrant parameter in torch.utils.checkpoint is being changed from True to False. In the meantime, the value needs to be passed explicitly.

See this forum post for details.

TOR004 Import of removed function

See TOR001.

TOR101 Use of deprecated function

torch.nn.utils.weight_norm

This function is deprecated. Use torch.nn.utils.parametrizations.weight_norm which uses the modern parametrization API. The new weight_norm is compatible with state_dict generated from old weight_norm.

Migration guide:

  • The magnitude (weight_g) and direction (weight_v) are now expressed as parametrizations.weight.original0 and parametrizations.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 will be recomputed on every access. To restore the old behavior, use torch.nn.utils.parametrize.cached before invoking the module in question.

torch.backends.cuda.sdp_kernel

This function is deprecated. Use the torch.nn.attention.sdpa_kernel context manager instead.

Migration guide: Each boolean input parameter (defaulting to true unless specified) of sdp_kernel corresponds to a SDPBackened. If the input parameter is true, the corresponding backend should be added to the input list of sdpa_kernel.

torch.chain_matmul

This function is deprecated in favor of torch.linalg.multi_dot.

Migration guide: multi_dot accepts a list of two or more tensors whereas chain_matmul accepted multiple tensors as input arguments. For migration, convert the multiple tensors in argument of chain_matmul into a list of two or more tensors for multi_dot.

Example: Replace torch.chain_matmul(a, b, c) with torch.linalg.multi_dot([a, b, c]).

torch.cholesky

torch.cholesky() is deprecated in favor of torch.linalg.cholesky().

Migration guide:

  • L = torch.cholesky(A) should be replaced with L = torch.linalg.cholesky(A).
  • L = torch.cholesky(A, upper=True) should be replaced with L = torch.linalg.cholesky(A).mH

torch.qr

torch.qr() is deprecated in favor of torch.linalg.qr().

Migration guide:

  • The usage Q, R = torch.qr(A) should be replaced with Q, R = torch.linalg.qr(A).
  • The boolean parameter some of torch.qr is replaced with a string parameter mode in torch.linalg.qr. The corresponding change in usage is from Q, R = torch.qr(A, some=False) to Q, R = torch.linalg.qr(A, mode="complete").

torch.range

The function torch.range() is deprecated as its usage is incompatible with Python's builtin range. Instead, use torch.arange() as it produces values in [start, end).

Migration guide:

  • torch.range(start, end) produces values in the range of [start, end]. But torch.arange(start, end) produces values in [start, end). For step size of 1, migrate usage from torch.range(start, end, 1) to torch.arange(start, end+1, 1).

TOR102 torch.load without weights_only parameter is unsafe.

Explicitly set weights_only to False only if you trust the data you load and full pickle functionality is needed, otherwise set weights_only=True.

TOR103 Import of deprecated function

See TOR101.

License

TorchFix is BSD License licensed, as found in the LICENSE file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchfix-0.7.0.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

TorchFix-0.7.0-py3-none-any.whl (28.9 kB view details)

Uploaded Python 3

File details

Details for the file torchfix-0.7.0.tar.gz.

File metadata

  • Download URL: torchfix-0.7.0.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for torchfix-0.7.0.tar.gz
Algorithm Hash digest
SHA256 3b56176fcdd629739767b3d640ab0d5999b6abb8ca2a46f54ee3ca5da6b20dba
MD5 321d770e6483cbe46d99f9fe81ff3df7
BLAKE2b-256 fb5089bc6b4e161cd2e3ccf134bca1c1e7b63a4cb63eafed23ff5cc9575735f1

See more details on using hashes here.

File details

Details for the file TorchFix-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: TorchFix-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 28.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for TorchFix-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4d0069952a8322149cfbe7689e27d347a4bf1b2fa4c3bc4023121adcbc484ace
MD5 923683635064ce4a94586cea78c9ca86
BLAKE2b-256 0c4b70b83eae74b9c5907ceeb5264c281c389953b3247820156a5066b6397697

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page