Add TorchFix to the CI (#113403)

Enable flake8 plugin for https://github.com/pytorch/test-infra/tree/main/tools/torchfix - TorchFix 0.1.1.
Disable TorchFix codes that don't make sense for PyTorch itself.
Update deprecated TorchVision APIs to make TorchFix pass.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/113403
Approved by: https://github.com/Skylion007, https://github.com/malfet
This commit is contained in:
Sergii Dymchenko
2023-11-14 01:26:01 +00:00
committed by PyTorch MergeBot
parent e1c872e009
commit d94bfaff2e
8 changed files with 18 additions and 15 deletions

View File

@ -2,7 +2,7 @@
# NOTE: **Mirror any changes** to this file the [tool.ruff] config in pyproject.toml # NOTE: **Mirror any changes** to this file the [tool.ruff] config in pyproject.toml
# before we can fully move to use ruff # before we can fully move to use ruff
enable-extensions = G enable-extensions = G
select = B,C,E,F,G,P,SIM1,T4,W,B9 select = B,C,E,F,G,P,SIM1,T4,W,B9,TOR0,TOR1,TOR2
max-line-length = 120 max-line-length = 120
# C408 ignored because we like the dict keyword argument syntax # C408 ignored because we like the dict keyword argument syntax
# E501 is not flexible enough, we're using B950 instead # E501 is not flexible enough, we're using B950 instead
@ -23,6 +23,9 @@ ignore =
SIM105,SIM108,SIM110,SIM111,SIM113,SIM114,SIM115,SIM116,SIM117,SIM118,SIM119,SIM12, SIM105,SIM108,SIM110,SIM111,SIM113,SIM114,SIM115,SIM116,SIM117,SIM118,SIM119,SIM12,
# flake8-simplify code styles # flake8-simplify code styles
SIM102,SIM103,SIM106,SIM112, SIM102,SIM103,SIM106,SIM112,
# TorchFix codes that don't make sense for PyTorch itself:
# removed and deprecated PyTorch functions.
TOR001,TOR101,
per-file-ignores = per-file-ignores =
__init__.py: F401 __init__.py: F401
torch/utils/cpp_extension.py: B950 torch/utils/cpp_extension.py: B950

View File

@ -48,6 +48,7 @@ init_command = [
'mccabe==0.7.0', 'mccabe==0.7.0',
'pycodestyle==2.10.0', 'pycodestyle==2.10.0',
'pyflakes==3.0.1', 'pyflakes==3.0.1',
'torchfix==0.1.1',
] ]

View File

@ -1,23 +1,21 @@
import torch import torch
import torchvision from torchvision import models
print(torch.version.__version__) print(torch.version.__version__)
resnet18 = torchvision.models.resnet18(pretrained=True) resnet18 = models.resnet18(weights=models.ResNet18_Weights.IMAGENET1K_V1)
resnet18.eval() resnet18.eval()
resnet18_traced = torch.jit.trace(resnet18, torch.rand(1, 3, 224, 224)).save( resnet18_traced = torch.jit.trace(resnet18, torch.rand(1, 3, 224, 224)).save(
"app/src/main/assets/resnet18.pt" "app/src/main/assets/resnet18.pt"
) )
resnet50 = torchvision.models.resnet50(pretrained=True) resnet50 = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V1)
resnet50.eval() resnet50.eval()
torch.jit.trace(resnet50, torch.rand(1, 3, 224, 224)).save( torch.jit.trace(resnet50, torch.rand(1, 3, 224, 224)).save(
"app/src/main/assets/resnet50.pt" "app/src/main/assets/resnet50.pt"
) )
mobilenet2q = torchvision.models.quantization.mobilenet_v2( mobilenet2q = models.quantization.mobilenet_v2(pretrained=True, quantize=True)
pretrained=True, quantize=True
)
mobilenet2q.eval() mobilenet2q.eval()
torch.jit.trace(mobilenet2q, torch.rand(1, 3, 224, 224)).save( torch.jit.trace(mobilenet2q, torch.rand(1, 3, 224, 224)).save(
"app/src/main/assets/mobilenet2q.pt" "app/src/main/assets/mobilenet2q.pt"

View File

@ -5,11 +5,11 @@ build script to create a tailored build which only contains these used ops.
""" """
import torch import torch
import torchvision
import yaml import yaml
from torchvision import models
# Download and trace the model. # Download and trace the model.
model = torchvision.models.mobilenet_v2(pretrained=True) model = models.mobilenet_v2(weights=models.MobileNet_V2_Weights.IMAGENET1K_V1)
model.eval() model.eval()
example = torch.rand(1, 3, 224, 224) example = torch.rand(1, 3, 224, 224)
# TODO: create script model with `torch.jit.script` # TODO: create script model with `torch.jit.script`

View File

@ -1,7 +1,7 @@
import torch import torch
import torchvision
from torch.backends._coreml.preprocess import CompileSpec, CoreMLComputeUnit, TensorSpec from torch.backends._coreml.preprocess import CompileSpec, CoreMLComputeUnit, TensorSpec
from torchvision import models
def mobilenetv2_spec(): def mobilenetv2_spec():
@ -24,7 +24,7 @@ def mobilenetv2_spec():
def main(): def main():
model = torchvision.models.mobilenet_v2(pretrained=True) model = models.mobilenet_v2(weights=models.MobileNet_V2_Weights.IMAGENET1K_V1)
model.eval() model.eval()
example = torch.rand(1, 3, 224, 224) example = torch.rand(1, 3, 224, 224)
model = torch.jit.trace(model, example) model = torch.jit.trace(model, example)

View File

@ -1,8 +1,8 @@
import torch import torch
import torchvision
from torch.utils.mobile_optimizer import optimize_for_mobile from torch.utils.mobile_optimizer import optimize_for_mobile
from torchvision import models
model = torchvision.models.mobilenet_v2(pretrained=True) model = models.mobilenet_v2(weights=models.MobileNet_V2_Weights.IMAGENET1K_V1)
model.eval() model.eval()
example = torch.rand(1, 3, 224, 224) example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example) traced_script_module = torch.jit.trace(model, example)

View File

@ -1,8 +1,8 @@
import torch import torch
import torchvision
import yaml import yaml
from torchvision import models
model = torchvision.models.mobilenet_v2(pretrained=True) model = models.mobilenet_v2(weights=models.MobileNet_V2_Weights.IMAGENET1K_V1)
model.eval() model.eval()
example = torch.rand(1, 3, 224, 224) example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example) traced_script_module = torch.jit.trace(model, example)

View File

@ -8,3 +8,4 @@ flake8-pyi==20.5.0
mccabe==0.6.1 mccabe==0.6.1
pycodestyle==2.6.0 pycodestyle==2.6.0
pyflakes==2.2.0 pyflakes==2.2.0
torchfix==0.1.1