### Install Development Dependencies Source: https://github.com/meta-pytorch/torchfix/blob/main/CONTRIBUTING.md Install the project with development dependencies, including linters and testing tools. ```shell pip install ".[dev]" ``` -------------------------------- ### Install TorchFix Source: https://context7.com/meta-pytorch/torchfix/llms.txt Install TorchFix from PyPI for the release version or from GitHub for the latest code. ```bash # Install release version from PyPI pip install torchfix # Install latest code from GitHub git clone https://github.com/meta-pytorch/torchfix cd torchfix pip install . ``` -------------------------------- ### Configure TorchFix with pre-commit Source: https://context7.com/meta-pytorch/torchfix/llms.txt Example configuration for integrating TorchFix with pre-commit. Can be used directly or via Flake8. ```yaml # .pre-commit-config.yaml repos: - repo: local hooks: - id: torchfix name: torchfix entry: torchfix language: system types: [python] args: ["--select=TOR0,TOR1,TOR2"] exclude: ^tests/fixtures/ # Or use TorchFix via Flake8 in pre-commit: - repo: local hooks: - id: flake8 name: flake8 entry: flake8 language: system types: [python] args: ["--isolated", "--select=TOR0,TOR1,TOR2"] ``` -------------------------------- ### Deprecated Symbols Configuration Source: https://context7.com/meta-pytorch/torchfix/llms.txt Example entries for the deprecated_symbols.yaml file, defining removed or deprecated PyTorch symbols and their replacements. ```yaml # torchfix/deprecated_symbols.yaml — example entries # Removed function (remove_pr is set) → triggers TOR001 - name: torch.solve deprecate_pr: https://github.com/pytorch/pytorch/pull/57741 remove_pr: https://github.com/pytorch/pytorch/pull/70986 reference: https://github.com/meta-pytorch/torchfix#torchsolve # Deprecated function with drop-in replacement → triggers TOR101, autofix renames call - name: torch.ger deprecate_pr: TBA remove_pr: # null → not yet removed → TOR101 replacement: torch.outer # autofix: torch.ger(a, b) → torch.outer(a, b) # functorch migration → replacement points to torch.func equivalent - name: functorch.vmap deprecate_pr: TBA remove_pr: replacement: torch.func.vmap # autofix: functorch.vmap(f) → torch.func.vmap(f) # Deprecated with no known replacement yet - name: torch.norm deprecate_pr: https://github.com/pytorch/pytorch/pull/57986 remove_pr: # no replacement field → violation reported but no autofix ``` -------------------------------- ### Use torch.arange instead of torch.range Source: https://github.com/meta-pytorch/torchfix/blob/main/README.md Use torch.arange for range generation as it is compatible with Python's builtin range and produces values in [start, end). ```python torch.range(start, end) ``` ```python torch.arange(start, end) ``` ```python torch.range(start, end, 1) ``` ```python torch.arange(start, end + 1, 1) ``` -------------------------------- ### Run Pre-commit Hooks Source: https://github.com/meta-pytorch/torchfix/blob/main/CONTRIBUTING.md Execute all pre-commit hooks to lint and format code across the entire project. ```shell pre-commit run --all-files ``` -------------------------------- ### Use torch.log1p for accurate log(1 + x) Source: https://context7.com/meta-pytorch/torchfix/llms.txt Suggests using `torch.log1p(x)` instead of `torch.log(1 + x)` for improved numerical accuracy, especially when `x` is small. This change requires manual intervention. ```python import torch x = torch.randn(100) # BEFORE (triggers TOR106) result = torch.log(1 + x) result = torch.log(1.0 + x) result = torch.log(x + 1) # Recommended replacement (no autofix, manual change required) result = torch.log1p(x) ``` -------------------------------- ### Run TorchFix CLI Source: https://context7.com/meta-pytorch/torchfix/llms.txt Use the torchfix CLI to lint files or directories. Add --fix to apply autofixes in place. ```bash # Lint all Python files in the current directory (default rules: TOR0, TOR1, TOR2) torchfix . # Lint a single file torchfix my_model.py # Show all violations including disabled-by-default rules (TOR3, TOR4, TOR9) torchfix --select=ALL . # Enable only specific rule categories torchfix --select=TOR0,TOR1 . # Enable a single specific rule torchfix --select=TOR102 . # Autofix violations in place (files are overwritten) torchfix --fix . # Run with 4 parallel worker processes torchfix -j 4 . # Show stderr output (useful for debugging "Failed to determine module name" warnings) torchfix --show-stderr . # Print the installed version torchfix --version # Output: 0.7.0 ``` -------------------------------- ### Replace torch.solve with torch.linalg.solve Source: https://github.com/meta-pytorch/torchfix/blob/main/README.md Use `torch.linalg.solve(A, B)` instead of `torch.solve(B, A).solution` for solving linear equations. ```python X = torch.solve(B, A).solution should be replaced with X = torch.linalg.solve(A, B) ``` -------------------------------- ### Migrate torch.qr to torch.linalg.qr Source: https://github.com/meta-pytorch/torchfix/blob/main/README.md Replace torch.qr with torch.linalg.qr. The 'some' parameter is replaced by 'mode'. ```python Q, R = torch.qr(A) ``` ```python Q, R = torch.linalg.qr(A) ``` ```python Q, R = torch.qr(A, some=False) ``` ```python Q, R = torch.linalg.qr(A, mode="complete") ``` -------------------------------- ### Manually Run Linters Source: https://github.com/meta-pytorch/torchfix/blob/main/CONTRIBUTING.md Individually run the black, flake8, and mypy linters on the project files. ```shell black . ``` ```shell flake8 ``` ```shell mypy . ``` -------------------------------- ### Migrate torch.cuda.amp.autocast to torch.amp.autocast Source: https://context7.com/meta-pytorch/torchfix/llms.txt Updates the deprecated `torch.cuda.amp.autocast` context manager to the recommended `torch.amp.autocast` API. ```python with torch.cuda.amp.autocast(): output = model(input) # Autofix → with torch.amp.autocast("cuda"): ``` -------------------------------- ### Use torch.special.expm1 for accurate exp(x) - 1 Source: https://context7.com/meta-pytorch/torchfix/llms.txt Recommends `torch.special.expm1(x)` over `torch.exp(x) - 1` for better numerical precision when calculating `exp(x) - 1`, particularly near zero. This is a manual change. ```python import torch x = torch.randn(100) # BEFORE (triggers TOR107) result = torch.exp(x) - 1 result = torch.exp(x) - 1.0 # Recommended replacement (no autofix, manual change required) result = torch.special.expm1(x) ``` -------------------------------- ### Safe torch.load usage Source: https://github.com/meta-pytorch/torchfix/blob/main/README.md To ensure safety when loading data, explicitly set weights_only=True. Set weights_only=False only if the data is trusted and full pickle functionality is required. ```python torch.load(data, weights_only=True) ``` -------------------------------- ### Secure torch.load with weights_only parameter Source: https://context7.com/meta-pytorch/torchfix/llms.txt Recommends using `weights_only=True` in `torch.load` to prevent arbitrary code execution via pickle deserialization. Loading without this parameter is unsafe. ```python import torch # BEFORE (triggers TOR102: `torch.load` without `weights_only` parameter is unsafe) model_state = torch.load("model.pt") model_state = torch.load("model.pt", map_location="cpu") # AFTER autofix (adds weights_only=True when no pickle_module is present) model_state = torch.load("model.pt", weights_only=True) model_state = torch.load("model.pt", map_location="cpu", weights_only=True) # If you explicitly need full pickle functionality (trusted data only): model_state = torch.load("model.pt", weights_only=False) ``` -------------------------------- ### Run TorchFix as Flake8 Plugin Source: https://context7.com/meta-pytorch/torchfix/llms.txt TorchFix integrates with Flake8. Run flake8 normally to see TorchFix rules alongside other violations. ```bash # Run all configured linters including TorchFix flake8 my_project/ # Run only TorchFix rules (isolated mode, no other plugins) flake8 --isolated --select=TOR0,TOR1,TOR2 my_project/ # Run only TorchFix and enable all rules flake8 --isolated --select=TOR my_project/ ``` -------------------------------- ### Migrate torch.cholesky to torch.linalg.cholesky Source: https://github.com/meta-pytorch/torchfix/blob/main/README.md Replace torch.cholesky with torch.linalg.cholesky. For upper triangular matrices, use .mH. ```python L = torch.cholesky(A) ``` ```python L = torch.linalg.cholesky(A) ``` ```python L = torch.cholesky(A, upper=True) ``` ```python L = torch.linalg.cholesky(A).mH ``` -------------------------------- ### Use torch.logsumexp for numerical stability Source: https://context7.com/meta-pytorch/torchfix/llms.txt Replaces the numerically unstable pattern `torch.log(torch.sum(torch.exp(x), dim=...))` with the stable `torch.logsumexp(x, dim=...)`. This is a manual change. ```python import torch x = torch.randn(4, 10) # BEFORE (triggers TOR108) — numerically unstable, can overflow/underflow result = torch.log(torch.sum(torch.exp(x), dim=1)) # Recommended replacement (no autofix, manual change required) result = torch.logsumexp(x, dim=1) ``` -------------------------------- ### Replace torch.symeig with torch.linalg.eigvalsh or torch.linalg.eigh Source: https://github.com/meta-pytorch/torchfix/blob/main/README.md Replace `torch.symeig` with `torch.linalg.eigvalsh` for eigenvalues or `torch.linalg.eigh` for eigenvalues and eigenvectors. Pay attention to the `UPLO` argument for specifying the triangular portion. ```python L, _ = torch.symeig(A, upper=upper) should be replaced with L = torch.linalg.eigvalsh(A, UPLO='U' if upper else 'L') ``` ```python L, V = torch.symeig(A, eigenvectors=True) should be replaced with L, V = torch.linalg.eigh(A, UPLO='U' if upper else 'L') ``` -------------------------------- ### Migrate torch.chain_matmul to torch.linalg.multi_dot Source: https://context7.com/meta-pytorch/torchfix/llms.txt Replaces the deprecated `torch.chain_matmul` function with `torch.linalg.multi_dot` for matrix multiplication chains. ```python result = torch.chain_matmul(a, b, c) # Autofix → result = torch.linalg.multi_dot([a, b, c]) ``` -------------------------------- ### Configure Flake8 for TorchFix Source: https://context7.com/meta-pytorch/torchfix/llms.txt Configure TorchFix rules and exclusions in your .flake8 file. ```ini # .flake8 [flake8] # Exclude test fixtures from linting exclude = ./tests/fixtures/ max-line-length = 88 # Extend default ignored codes if needed extend-ignore = E203 ``` -------------------------------- ### Use optimizer.zero_grad(set_to_none=True) Source: https://context7.com/meta-pytorch/torchfix/llms.txt Detects optimizer.zero_grad(set_to_none=False) calls. Using set_to_none=True is more memory-efficient. Note that True is the default since PyTorch 2.0. ```python import torch import torch.nn as nn model = nn.Linear(10, 10) optimizer = torch.optim.Adam(model.parameters()) # BEFORE (triggers TOR402: Detected gradient set to zero instead of None) optimizer.zero_grad(set_to_none=False) # Recommended fix (no autofix, manual change required) optimizer.zero_grad(set_to_none=True) # or simply (True is the default since PyTorch 2.0) optimizer.zero_grad() ``` -------------------------------- ### Migrate functorch.vmap to torch.func.vmap Source: https://context7.com/meta-pytorch/torchfix/llms.txt Replaces the deprecated `functorch.vmap` with the equivalent `torch.func.vmap` for automatic vectorization. ```python f_vmap = functorch.vmap(f) # Autofix → f_vmap = torch.func.vmap(f) ``` -------------------------------- ### Migrate TorchVision pretrained parameters to weights API Source: https://context7.com/meta-pytorch/torchfix/llms.txt Updates TorchVision model constructors to use the `weights` API instead of the deprecated `pretrained` and `pretrained_backbone` boolean parameters. ```python import torchvision.models as models # BEFORE (triggers TOR201: Parameter `pretrained` is deprecated) model = models.resnet50(pretrained=True) model = models.resnet50(pretrained=False) detector = models.detection.fasterrcnn_resnet50_fpn( pretrained=True, pretrained_backbone=True ) # AFTER autofix (example for resnet50) # model = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V1) # model = models.resnet50(weights=None) # detector = models.detection.fasterrcnn_resnet50_fpn( # weights=models.detection.FasterRCNN_ResNet50_FPN_Weights.DEFAULT, # weights_backbone=models.detection.FasterRCNN_ResNet50_FPN_Weights.DEFAULT # ) ``` -------------------------------- ### Migrate torch.chain_matmul to torch.linalg.multi_dot Source: https://github.com/meta-pytorch/torchfix/blob/main/README.md Replace torch.chain_matmul with torch.linalg.multi_dot for improved functionality. multi_dot accepts a list of tensors. ```python torch.chain_matmul(a, b, c) ``` ```python torch.linalg.multi_dot([a, b, c]) ``` -------------------------------- ### Instantiate DataLoader with num_workers > 0 Source: https://context7.com/meta-pytorch/torchfix/llms.txt Detects DataLoader instantiation with num_workers=0, which runs data loading synchronously. Recommended fix is to set num_workers to a positive integer. ```python from torch.utils.data import DataLoader, TensorDataset import torch dataset = TensorDataset(torch.randn(1000, 3, 224, 224)) # BEFORE (triggers TOR401: Detected DataLoader running with synchronized implementation) loader = DataLoader(dataset) loader = DataLoader(dataset, batch_size=32, num_workers=0) # Recommended fix (no autofix, manual change required) loader = DataLoader(dataset, batch_size=32, num_workers=4) ``` -------------------------------- ### Use public API for default_collate and default_convert Source: https://context7.com/meta-pytorch/torchfix/llms.txt Replaces imports of non-public PyTorch data utilities like `torch.utils.data._utils.collate.default_collate` with their public counterparts in `torch.utils.data.dataloader`. ```python # BEFORE (triggers TOR104: Use of non-public function) from torch.utils.data._utils.collate import default_collate batch = default_collate(samples) # AFTER autofix from torch.utils.data.dataloader import default_collate batch = default_collate(samples) ``` ```python # BEFORE (triggers TOR105: Import of non-public function) from torch.utils.data._utils.collate import default_convert # AFTER autofix from torch.utils.data.dataloader import default_convert ``` -------------------------------- ### TOR101: Use of Deprecated Function (torch.qr) Source: https://context7.com/meta-pytorch/torchfix/llms.txt Detects calls to the deprecated `torch.qr` function. The autofix replaces it with `torch.linalg.qr`. ```python import torch # TOR101: Use of deprecated function torch.qr Q, R = torch.qr(A) # Autofix → Q, R = torch.linalg.qr(A) ``` -------------------------------- ### TorchChecker Flake8 Plugin API Source: https://context7.com/meta-pytorch/torchfix/llms.txt Illustrates the internal API of TorchChecker, the Flake8 extension class. It parses source code, runs visitors, and yields violations. ```python # Internal usage — not typically called directly, but illustrates the API import libcst as cst from torchfix.torchfix import TorchChecker, GET_ALL_ERROR_CODES, expand_error_codes source = """ import torch X = torch.solve(B, A).solution model.require_grad = True data = torch.load("weights.pt") """ lines = source.splitlines(keepends=True) tree = cst.parse_module(source) # tree arg required by Flake8 protocol but unused checker = TorchChecker(tree=tree, lines=lines) for line, col, message, plugin in checker.run(): print(f"Line {line}, Col {col}: {message}") # Output: # Line 2, Col 1: TOR001 Use of removed function torch.solve: https://... # Line 3, Col 1: TOR002 Likely typo `require_grad` in assignment. Did you mean `requires_grad`? # Line 4, Col 1: TOR102 `torch.load` without `weights_only` parameter is unsafe. ... # Query all available error codes print(GET_ALL_ERROR_CODES()) # ['TOR001', 'TOR002', 'TOR003', 'TOR004', 'TOR101', 'TOR102', ...] # Expand a prefix to all matching codes print(expand_error_codes(("TOR1",))) # {'TOR101', 'TOR102', 'TOR103', ...} ``` -------------------------------- ### Run Autofixes in Parallel Source: https://context7.com/meta-pytorch/torchfix/llms.txt Executes code transformations in parallel. Set unified_diff to None to fix files in place. ```python command_instance = TorchCodemod(codemod.CodemodContext(), config) result = codemod.parallel_exec_transform_with_prettyprint( command_instance, files, jobs=4, unified_diff=None, # None = fix in place; integer = show diff with N context lines hide_progress=True, format_code=False, repo_root=None, ) print(f"Checked {result.successes + result.skips + result.failures} files.") print(f"Transformed {result.successes} files successfully.") if result.failures > 0: print(f"{result.failures} files failed.") ``` -------------------------------- ### TorchCodemod Standalone Autofix Engine Source: https://context7.com/meta-pytorch/torchfix/llms.txt Demonstrates the usage of TorchCodemod, the LibCST codemod class for the standalone torchfix CLI. It configures rules, gathers files, and applies fixes. ```python import libcst.codemod as codemod from torchfix.torchfix import TorchCodemod, TorchCodemodConfig, process_error_code_str # Configure which rules to apply (expand "TOR0,TOR1" to full code list) config = TorchCodemodConfig() config.select = list(process_error_code_str("TOR0,TOR1,TOR2")) # Gather all Python files under a path files = codemod.gather_files(["./my_project"]) ``` -------------------------------- ### TOR001: Use of Removed Function (torch.solve) Source: https://context7.com/meta-pytorch/torchfix/llms.txt Detects calls to `torch.solve`, which has been removed. The autofix replaces it with `torch.linalg.solve`. ```python # BEFORE (triggers TOR001: Use of removed function torch.solve) import torch A = torch.randn(3, 3) B = torch.randn(3, 2) X = torch.solve(B, A).solution # AFTER autofix X = torch.linalg.solve(A, B) ``` -------------------------------- ### TOR004: Import of Removed Function Source: https://context7.com/meta-pytorch/torchfix/llms.txt Detects `from torch import ...` style imports of removed symbols like `solve`. The autofix rewrites the import to the replacement module. ```python # BEFORE (triggers TOR004: Import of removed function torch.solve) from torch import solve result = solve(B, A) # AFTER autofix — import is rewritten to the replacement module from torch.linalg import solve result = solve(A, B) ``` -------------------------------- ### TOR101: Use of Deprecated Function (torch.cholesky) Source: https://context7.com/meta-pytorch/torchfix/llms.txt Detects calls to the deprecated `torch.cholesky` function. The autofix replaces it with `torch.linalg.cholesky`. ```python # TOR101: Use of deprecated function torch.cholesky L = torch.cholesky(A) # Autofix → L = torch.linalg.cholesky(A) ``` -------------------------------- ### Fix typo: require_grad vs requires_grad Source: https://github.com/meta-pytorch/torchfix/blob/main/README.md Corrects the common misspelling of `require_grad` to `requires_grad` to avoid potential performance issues. ```python Did you mean requires_grad? ``` -------------------------------- ### TOR001: Use of Removed Function (torch.symeig) Source: https://context7.com/meta-pytorch/torchfix/llms.txt Detects calls to `torch.symeig`, which has been removed. The autofix replaces it with `torch.linalg.eigh`. ```python # BEFORE (triggers TOR001: Use of removed function torch.symeig) L, V = torch.symeig(A, eigenvectors=True) # AFTER autofix L, V = torch.linalg.eigh(A, UPLO='L') ``` -------------------------------- ### Replace ToTensor with Compose Source: https://github.com/meta-pytorch/torchfix/blob/main/tests/fixtures/vision/checker/to_tensor.txt The `v2.ToTensor()` transform is deprecated. Use `v2.Compose` with `v2.ToImage` and `v2.ToDtype` for equivalent functionality. ```python torchvision.transforms.v2.Compose([torchvision.transforms.v2.ToImage(), torchvision.transforms.v2.ToDtype(torch.float32, scale=True)]) ``` -------------------------------- ### TOR101: Use of Deprecated Function (torch.range) Source: https://context7.com/meta-pytorch/torchfix/llms.txt Detects calls to the deprecated `torch.range` function. The autofix replaces it with `torch.arange` with adjusted end value. ```python # TOR101: Use of deprecated function torch.range t = torch.range(0, 10) # Autofix → t = torch.arange(0, 11) ``` -------------------------------- ### Pass use_reentrant explicitly to checkpoint Source: https://github.com/meta-pytorch/torchfix/blob/main/README.md Explicitly pass the `use_reentrant` argument to `torch.utils.checkpoint` as its default value is changing. ```python Please pass `use_reentrant` explicitly to `checkpoint` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.