TorchFix

repository·main·Indexed 41 days ago

https://github.com/meta-pytorch/torchfix

A static analysis tool and linter for PyTorch code that identifies deprecated functions, removed symbols, and common typos. It provides autofix capabilities to modernize codebases and integrates as a Flake8 plugin or runs as a standalone CLI program.

Tokens
1.4K
Snippets
8
Records
9
Agent score
50%

What's inside TorchFix

  1. Understand TorchFix rule categories

    main

    TorchFix uses a prefix-based system for rule assignment. Understanding these prefixes helps you manage noise and know which rules are applicable to your environment:

    • TOR0XX, TOR1XX: General-purpose torch functionality (enabled by default).
    • TOR2XX: Domain-specific rules, such as TorchVision (enabled by default).
    • TOR4XX: Noisy rules that are disabled by default.
    • TOR9XX: Internal rules specific to the pytorch/pytorch repository; these should not be used by external developers.
  2. Use TorchFix as a Flake8 plugin

    main

    Once installed, TorchFix integrates directly with Flake8. Running flake8 will automatically include TorchFix linting checks.

    To run only TorchFix checks without other Flake8 linters, use the --isolated and --select flags:

    flake8 --isolated --select=TOR0,TOR1,TOR2

    Note: Rules starting with TOR0, TOR1, and TOR2 are enabled by default. Other rules (like TOR4XX) are disabled by default to reduce noise.

    flake8 --isolated --select=TOR0,TOR1,TOR2
  3. Use TorchFix as a standalone program

    main

    You can run TorchFix independently of Flake8 using the torchfix command.

    Basic usage:

    torchfix .

    Enable autofix: To attempt to automatically fix issues (note: this will overwrite your files), use the --fix flag:

    torchfix . --fix

    Enable all rules: To enable noisy rules that are normally disabled by default, use the --select=ALL flag:

    torchfix --select=ALL .

    Debug mode: To see additional debug information, use the --show-stderr flag:

    torchfix . --show-stderr
    torchfix . --fix
  4. Install TorchFix

    main

    You can install TorchFix from PyPI for a stable release or directly from GitHub for the latest development version.

    To install the latest version from GitHub:

    git clone https://github.com/meta-pytorch/torchfix
    cd torchfix
    pip install .

    To install a release version from PyPI:

    pip install torchfix
    pip install torchfix
  5. Migrate from deprecated PyTorch functions (TOR101)

    main

    TorchFix identifies several deprecated functions. Common migrations include:

    torch.nn.utils.weight_norm

    • Use torch.nn.utils.parametrizations.weight_norm instead.
    • To remove reparametrization, use torch.nn.utils.parametrize.remove_parametrizations.

    torch.backends.cuda.sdp_kernel

    • Use the torch.nn.attention.sdpa_kernel context manager.

    torch.chain_matmul

    • Use torch.linalg.multi_dot. Note that multi_dot accepts a list of tensors.
    • Example: torch.linalg.multi_dot([a, b, c]) instead of torch.chain_matmul(a, b, c).

    torch.cholesky

    • Use torch.linalg.cholesky(A).
    • For upper triangular: torch.linalg.cholesky(A).mH.

    torch.qr

    • Use torch.linalg.qr(A).
    • For some=False, use mode="complete" in torch.linalg.qr(A, mode="complete").

    torch.range

    • Use torch.arange(start, end + 1, 1) to match the inclusive behavior of the old torch.range(start, end, 1).
    import torch
    
    # Example migration for chain_matmul
    # Instead of: torch.chain_matmul(a, b, c)
    torch.linalg.multi_dot([a, b, c])
  6. Secure torch.load usage (TOR102)

    main

    Using torch.load without the weights_only parameter is considered unsafe because it can execute arbitrary code via pickle.

    To fix this, explicitly set the parameter:

    • Use weights_only=True for standard weight loading (recommended).
    • Use weights_only=False only if you explicitly trust the data source and require full pickle functionality.
    import torch
    
    # Unsafe
    # data = torch.load("model.pth")
    
    # Safe
    data = torch.load("model.pth", weights_only=True)
  7. Migrate from removed PyTorch functions (TOR001)

    main

    Several functions have been removed in favor of the torch.linalg module. Use the following patterns to migrate:

    torch.solve

    • Old: X = torch.solve(B, A).solution
    • New: X = torch.linalg.solve(A, B) (Note: arguments are reversed).

    torch.symeig

    • For eigenvalues: Replace L, _ = torch.symeig(A, upper=upper) with L = torch.linalg.eigvalsh(A, UPLO='U' if upper else 'L').
    • For eigenvectors: Replace L, V = torch.symeig(A, eigenvectors=True) with L, V = torch.linalg.eigh(A, UPLO='U' if upper else 'L').
    import torch
    
    # Example migration for solve
    # Instead of: X = torch.solve(B, A).solution
    X = torch.linalg.solve(A, B)
  8. Run TorchFix via CLI

    main

    TorchFix can be run from the command line to lint or automatically fix PyTorch-related code violations in files or directories. By default, it checks for violations and prints a diff of suggested changes. To apply the fixes directly to your files, use the --fix flag.

    # Check files and see suggested diffs
    torchfix path/to/your/code
    
    # Automatically apply fixes to the files
    torchfix path/to/your/code --fix
  9. TorchFix CLI Reference

    main

    The following command-line arguments are available for the torchfix executable:

    • path: (Positional) One or more paths to check/fix. Can be a directory, a file, or multiple of either.
    • --fix: Enable autofixing of fixable violations.
    • -j, --jobs: Number of jobs to use when processing files. Defaults to the number of CPU cores.
    • --select: A comma-separated list of rules to enable, or ALL to enable all rules. Defaults to all rules except those in the DISABLED_BY_DEFAULT list.
    • --version: Show the current TorchFix version.
    • --show-stderr: Show stderr output (used to silence certain LibCST warnings by default).
    torchfix [PATH] [FLAGS]