From 5e45a6ad3d59937416cbf195717b1065a2f166b2 Mon Sep 17 00:00:00 2001 From: spencerwooo Date: Sat, 1 Feb 2025 21:31:46 +0800 Subject: [PATCH] Make mypy happy --- requirements.txt | 40 ++++++++++++++++++++++++++++++++++++- torchattack/att.py | 2 +- torchattack/decowa.py | 3 +++ torchattack/dr.py | 2 +- torchattack/pna_patchout.py | 2 +- torchattack/ssa.py | 3 +++ torchattack/tgr.py | 2 +- torchattack/vdc.py | 2 +- torchattack/vmifgsm.py | 3 +++ torchattack/vnifgsm.py | 3 +++ 10 files changed, 56 insertions(+), 6 deletions(-) diff --git a/requirements.txt b/requirements.txt index d8fb2cd..bf3bf9f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,9 @@ # This file was autogenerated by uv via the following command: # uv pip compile pyproject.toml -o requirements.txt filelock==3.16.1 - # via torch + # via + # torch + # triton fsspec==2024.10.0 # via torch jinja2==3.1.4 @@ -17,10 +19,44 @@ numpy==2.2.0 # torchattack (pyproject.toml) # scipy # torchvision +nvidia-cublas-cu12==12.4.5.8 + # via + # nvidia-cudnn-cu12 + # nvidia-cusolver-cu12 + # torch +nvidia-cuda-cupti-cu12==12.4.127 + # via torch +nvidia-cuda-nvrtc-cu12==12.4.127 + # via torch +nvidia-cuda-runtime-cu12==12.4.127 + # via torch +nvidia-cudnn-cu12==9.1.0.70 + # via torch +nvidia-cufft-cu12==11.2.1.3 + # via torch +nvidia-curand-cu12==10.3.5.147 + # via torch +nvidia-cusolver-cu12==11.6.1.9 + # via torch +nvidia-cusparse-cu12==12.3.1.170 + # via + # nvidia-cusolver-cu12 + # torch +nvidia-nccl-cu12==2.21.5 + # via torch +nvidia-nvjitlink-cu12==12.4.127 + # via + # nvidia-cusolver-cu12 + # nvidia-cusparse-cu12 + # torch +nvidia-nvtx-cu12==12.4.127 + # via torch pillow==11.0.0 # via torchvision scipy==1.14.1 # via torchattack (pyproject.toml) +setuptools==75.8.0 + # via torch sympy==1.13.1 # via torch torch==2.5.1 @@ -29,5 +65,7 @@ torch==2.5.1 # torchvision torchvision==0.20.1 # via torchattack (pyproject.toml) +triton==3.1.0 + # via torch typing-extensions==4.12.2 # via torch diff --git a/torchattack/att.py b/torchattack/att.py index fce37fd..f7ef63d 100644 --- a/torchattack/att.py +++ b/torchattack/att.py @@ -59,7 +59,7 @@ def __init__( elif hasattr(model, 'model_name'): # If model is initialized via `torchattack.AttackModel`, the model_name # is automatically attached to the model during instantiation. - self.hook_cfg = model.model_name + self.hook_cfg = model.model_name # type: ignore self.eps = eps self.steps = steps diff --git a/torchattack/decowa.py b/torchattack/decowa.py index 4faf678..be4aa6f 100644 --- a/torchattack/decowa.py +++ b/torchattack/decowa.py @@ -122,6 +122,9 @@ def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: # Compute gradient loss.backward() + if delta.grad is None: + continue + # Accumulate gradient g += delta.grad diff --git a/torchattack/dr.py b/torchattack/dr.py index 4a9acd5..7cb9224 100644 --- a/torchattack/dr.py +++ b/torchattack/dr.py @@ -57,7 +57,7 @@ def __init__( # If model is initialized via `torchattack.AttackModel`, infer its model_name # from automatically attached attribute during instantiation. if not model_name and hasattr(model, 'model_name'): - model_name = model.model_name + model_name = model.model_name # type: ignore self.eps = eps self.steps = steps diff --git a/torchattack/pna_patchout.py b/torchattack/pna_patchout.py index 6ec52b6..68ed08e 100644 --- a/torchattack/pna_patchout.py +++ b/torchattack/pna_patchout.py @@ -62,7 +62,7 @@ def __init__( elif hasattr(model, 'model_name'): # If model is initialized via `torchattack.AttackModel`, the model_name # is automatically attached to the model during instantiation. - self.hook_cfg = model.model_name + self.hook_cfg = model.model_name # type: ignore self.eps = eps self.steps = steps diff --git a/torchattack/ssa.py b/torchattack/ssa.py index 1f1b5d9..93f8dd0 100644 --- a/torchattack/ssa.py +++ b/torchattack/ssa.py @@ -92,6 +92,9 @@ def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: # Compute gradient loss.backward() + if delta.grad is None: + continue + # Accumulate gradient g += delta.grad diff --git a/torchattack/tgr.py b/torchattack/tgr.py index d34b998..ab50892 100644 --- a/torchattack/tgr.py +++ b/torchattack/tgr.py @@ -58,7 +58,7 @@ def __init__( elif hasattr(model, 'model_name'): # If model is initialized via `torchattack.AttackModel`, the model_name # is automatically attached to the model during instantiation. - self.hook_cfg = model.model_name + self.hook_cfg = model.model_name # type: ignore self.eps = eps self.steps = steps diff --git a/torchattack/vdc.py b/torchattack/vdc.py index de40c30..9c86139 100644 --- a/torchattack/vdc.py +++ b/torchattack/vdc.py @@ -60,7 +60,7 @@ def __init__( elif hasattr(model, 'model_name'): # If model is initialized via `torchattack.AttackModel`, the model_name # is automatically attached to the model during instantiation. - self.hook_cfg = model.model_name + self.hook_cfg = model.model_name # type: ignore self.eps = eps self.steps = steps diff --git a/torchattack/vmifgsm.py b/torchattack/vmifgsm.py index ca85518..3c93b90 100644 --- a/torchattack/vmifgsm.py +++ b/torchattack/vmifgsm.py @@ -117,6 +117,9 @@ def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: neighbor_loss.backward() + if neighbors.grad is None: + continue + gv_grad += neighbors.grad # Accumulate gradient variance into v diff --git a/torchattack/vnifgsm.py b/torchattack/vnifgsm.py index e7eeb13..0ad78f2 100644 --- a/torchattack/vnifgsm.py +++ b/torchattack/vnifgsm.py @@ -121,6 +121,9 @@ def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: neighbor_loss.backward() + if neighbors.grad is None: + continue + gv_grad += neighbors.grad # Accumulate gradient variance into v