Skip to content

Commit

Permalink
Merge pull request #2329 from Trusted-AI/dependabot/pip/torch-2.1.1
Browse files Browse the repository at this point in the history
Bump torch from 1.13.1 to 2.1.1
  • Loading branch information
beat-buesser authored Dec 13, 2023
2 parents ab389e7 + a5a8fcc commit ffd3a49
Show file tree
Hide file tree
Showing 27 changed files with 95 additions and 185 deletions.
45 changes: 0 additions & 45 deletions .github/actions/deepspeech-v2/Dockerfile

This file was deleted.

7 changes: 0 additions & 7 deletions .github/actions/deepspeech-v2/action.yml

This file was deleted.

10 changes: 0 additions & 10 deletions .github/actions/deepspeech-v2/run.sh

This file was deleted.

29 changes: 11 additions & 18 deletions .github/actions/deepspeech-v3/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Get base from a pytorch image
FROM pytorch/pytorch:1.6.0-cuda10.1-cudnn7-runtime
pod# Get base from a pytorch image
FROM pytorch/pytorch:2.1.1-cuda12.1-cudnn8-runtime

# Set to install things in non-interactive mode
ENV DEBIAN_FRONTEND noninteractive
Expand All @@ -17,26 +17,19 @@ RUN apt-get update \
curl \
libsndfile-dev \
libsndfile1 \
vim \
curl \
&& apt-get clean all \
&& rm -r /var/lib/apt/lists/*

RUN /opt/conda/bin/conda install --yes \
astropy \
matplotlib \
pandas \
scikit-learn \
scikit-image

# Install necessary libraries for deepspeech v3
RUN pip install torch
RUN pip install tensorflow
RUN pip install torchaudio==0.6.0
RUN pip install --no-build-isolation fairscale
RUN pip install --ignore-installed PyYAML torch==2.1.1 tensorflow==2.14.1 torchaudio==2.1.1 pytorch-lightning==2.1.2 scikit-learn==1.3.2
RUN pip install --no-build-isolation fairscale==0.4.13

RUN git clone https://github.com/SeanNaren/deepspeech.pytorch.git
RUN cd deepspeech.pytorch && pip install -r requirements.txt
RUN cd deepspeech.pytorch && pip install -e .
RUN cd deepspeech.pytorch && sed -i '/^sklearn/d' requirements.txt && pip install -r requirements.txt && pip install -e .

RUN pip install numba==0.56.4 pytest-cov==4.1.0 pydub==0.25.1
RUN pip list

RUN pip install numba==0.50.0
RUN pip install pytest-cov
RUN pip install pydub==0.25.1
RUN mkdir -p /root/.art/data && cd /root/.art/data && curl -LJO "https://github.com/SeanNaren/deepspeech.pytorch/releases/download/V3.0/librispeech_pretrained_v3.ckpt"
37 changes: 0 additions & 37 deletions .github/workflows/ci-deepspeech-v2.yml

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/ci-deepspeech-v3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ on:

jobs:
test_deepspeech_v3_torch_1_10:
name: PyTorchDeepSpeech v3 / PyTorch 1.10
name: PyTorchDeepSpeech v3 / PyTorch 2.1.1
runs-on: ubuntu-latest
container: adversarialrobustnesstoolbox/art_testing_envs:deepspeech_v3_torch_1_10
container: adversarialrobustnesstoolbox/art_testing_envs:deepspeech_v3_torch_2_1_1
steps:
- name: Checkout Repo
uses: actions/checkout@v3
Expand Down
18 changes: 6 additions & 12 deletions .github/workflows/ci-pytorch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,18 @@ jobs:
fail-fast: false
matrix:
include:
- name: PyTorch 1.12.1 (Python 3.9)
framework: pytorch
python: 3.9
torch: 1.12.1+cpu
torchvision: 0.13.1+cpu
torchaudio: 0.12.1
- name: PyTorch 1.13.1 (Python 3.9)
framework: pytorch
python: 3.9
torch: 1.13.1+cpu
torchvision: 0.14.1+cpu
torchaudio: 0.13.1
- name: PyTorch 1.13.1 (Python 3.10)
framework: pytorch
python: '3.10'
torch: 1.13.1+cpu
torchvision: 0.14.1+cpu
torchaudio: 0.13.1
- name: PyTorch 2.1.1 (Python 3.10)
framework: pytorch
python: '3.10'
torch: 2.1.1
torchvision: 0.16.1+cpu
torchaudio: 2.1.1

name: ${{ matrix.name }}
steps:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci-style-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ jobs:
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: 3.8
python-version: '3.10'
- name: Pre-install
run: |
sudo apt-get update
sudo apt-get -y -q install ffmpeg libavcodec-extra
- name: Install Dependencies
run: |
python -m pip install --upgrade pip setuptools wheel
pip install -q pylint==2.12.2 mypy==0.931 pycodestyle==2.8.0 black==21.12b0
pip install -q pylint==2.12.2 mypy==1.7.1 pycodestyle==2.8.0 black==21.12b0
pip install -q -r <(sed '/^numpy/d;/^pluggy/d;/^tensorflow/d;/^keras/d' requirements_test.txt)
pip install numpy==1.22.4
pip install pluggy==0.13.1
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/ci-tensorflow-v1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
sudo apt-get update
sudo apt-get -y -q install ffmpeg libavcodec-extra
python -m pip install --upgrade pip setuptools wheel
pip install -q -r <(sed '/^pandas/d;/^scipy/d;/^matplotlib/d;/^xgboost/d;/^tensorflow/d;/^keras/d;/^jax/d' requirements_test.txt)
pip install -q -r <(sed '/^pandas/d;/^scipy/d;/^matplotlib/d;/^xgboost/d;/^tensorflow/d;/^keras/d;/^jax/d;/^torch/d' requirements_test.txt)
pip install pandas==1.3.5
pip install scipy==1.7.2
pip install matplotlib==3.5.3
Expand All @@ -57,6 +57,9 @@ jobs:
pip install tensorflow==${{ matrix.tensorflow }}
pip install keras==${{ matrix.keras }}
pip install numpy==1.20
pip install torch==1.13.1
pip install torchaudio==0.13.1
pip install torchvision==0.14.1+cpu
pip list
- name: Run Tests
run: ./run_tests.sh ${{ matrix.framework }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,11 @@ def generate( # type: ignore
return self.patch, self._get_circular_patch_mask()

def apply_patch(
self, x: np.ndarray, scale: float, patch_external: np.ndarray = None, mask: Optional[np.ndarray] = None
self,
x: np.ndarray,
scale: float,
patch_external: Optional[np.ndarray] = None,
mask: Optional[np.ndarray] = None,
) -> np.ndarray:
"""
A function to apply the learned adversarial patch to images or videos.
Expand Down
2 changes: 1 addition & 1 deletion art/attacks/evasion/dpatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def _augment_images_with_patch(
random_location: bool,
channels_first: bool,
mask: Optional[np.ndarray] = None,
transforms: List[Dict[str, int]] = None,
transforms: Optional[List[Dict[str, int]]] = None,
) -> Tuple[np.ndarray, List[Dict[str, int]]]:
"""
Augment images with patch.
Expand Down
19 changes: 11 additions & 8 deletions art/attacks/evasion/imperceptible_asr/imperceptible_asr.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,14 +540,17 @@ def _approximate_power_spectral_density_torch(

# compute short-time Fourier transform (STFT)
# pylint: disable=W0212
stft_matrix = torch.stft(
perturbation,
n_fft=self._window_size,
hop_length=self._hop_size,
win_length=self._window_size,
center=False,
window=torch.hann_window(self._window_size).to(self.estimator._device),
).to(self.estimator._device)
stft_matrix = torch.view_as_real(
torch.stft(
perturbation,
n_fft=self._window_size,
hop_length=self._hop_size,
win_length=self._window_size,
center=False,
window=torch.hann_window(self._window_size).to(self.estimator._device),
return_complex=True,
).to(self.estimator._device)
)

# compute power spectral density (PSD)
# note: fixes implementation of Qin et al. by also considering the square root of gain_factor
Expand Down
24 changes: 15 additions & 9 deletions art/attacks/evasion/imperceptible_asr/imperceptible_asr_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,10 @@ class only supports targeted attack.
loss.backward()

# Get sign of the gradients
self.global_optimal_delta.grad = torch.sign(self.global_optimal_delta.grad)
if self.global_optimal_delta.grad is not None:
self.global_optimal_delta.grad = torch.sign(self.global_optimal_delta.grad)
else:
raise ValueError("Received None instead of gradient tensor.")

# Do optimization
self.optimizer_1.step()
Expand Down Expand Up @@ -747,14 +750,17 @@ def _psd_transform(self, delta: "torch.Tensor", original_max_psd: np.ndarray) ->
window_fn = torch.hann_window # type: ignore

# Return STFT of delta
delta_stft = torch.stft(
delta,
n_fft=self.n_fft,
hop_length=self.hop_length,
win_length=self.win_length,
center=False,
window=window_fn(self.win_length).to(self.estimator.device),
).to(self.estimator.device)
delta_stft = torch.view_as_real(
torch.stft(
delta,
n_fft=self.n_fft,
hop_length=self.hop_length,
win_length=self.win_length,
center=False,
window=window_fn(self.win_length).to(self.estimator.device),
return_complex=True,
).to(self.estimator.device)
)

# Take abs of complex STFT results
transformed_delta = torch.sqrt(torch.sum(torch.square(delta_stft), -1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ def _get_loss_gradients(self, x: "torch.Tensor", y: "torch.Tensor", perturbation
# Compute gradients
loss.backward()
grads = eps.grad
grads_batch.append(grads[0, ...])
if grads is not None:
grads_batch.append(grads[0, ...])
else:
raise ValueError("Received None instead of gradient tensor.")

grads_batch_tensor = torch.stack(grads_batch)

Expand Down
8 changes: 4 additions & 4 deletions art/attacks/inference/membership_inference/shadow_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ def _hill_climbing_synthesis(
max_iterations: int = 40,
max_rejections: int = 3,
min_features_randomized: int = 1,
random_record_fn: Callable[[], np.ndarray] = None,
randomize_features_fn: Callable[[np.ndarray, int], np.ndarray] = None,
random_record_fn: Optional[Callable[[], np.ndarray]] = None,
randomize_features_fn: Optional[Callable[[np.ndarray, int], np.ndarray]] = None,
) -> np.ndarray:
"""
This method implements the hill climbing algorithm from R. Shokri et al. (2017)
Expand Down Expand Up @@ -247,8 +247,8 @@ def generate_synthetic_shadow_dataset(
member_ratio: float = 0.5,
min_confidence: float = 0.4,
max_retries: int = 6,
random_record_fn: Callable[[], np.ndarray] = None,
randomize_features_fn: Callable[[np.ndarray, int], np.ndarray] = None,
random_record_fn: Optional[Callable[[], np.ndarray]] = None,
randomize_features_fn: Optional[Callable[[np.ndarray, int], np.ndarray]] = None,
) -> Tuple[Tuple[np.ndarray, np.ndarray, np.ndarray], Tuple[np.ndarray, np.ndarray, np.ndarray]]:
"""
Generates a shadow dataset (member and nonmember samples and their corresponding model predictions) by training
Expand Down
6 changes: 4 additions & 2 deletions art/attacks/poisoning/perturbations/audio_perturbations.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
because loading the audio trigger from disk (librosa.load()) is very slow
and should be done only once.
"""
import numpy as np
from typing import Optional

import librosa
import numpy as np


class CacheTrigger:
Expand Down Expand Up @@ -89,7 +91,7 @@ def __init__(
self,
sampling_rate: int = 16000,
backdoor_path: str = "../../../utils/data/backdoors/cough_trigger.wav",
duration: float = None,
duration: Optional[float] = None,
**kwargs,
):
"""
Expand Down
2 changes: 1 addition & 1 deletion art/attacks/poisoning/sleeper_agent_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def _select_poison_indices(
classifier.model.trainable = model_trainable
else:
raise NotImplementedError("SleeperAgentAttack is currently implemented only for PyTorch and TensorFlowV2.")
indices = sorted(range(len(grad_norms)), key=lambda k: grad_norms[k])
indices = sorted(range(len(grad_norms)), key=lambda k: grad_norms[k]) # type: ignore
indices = indices[-num_poison:]
return np.array(indices) # this will get only indices for target class

Expand Down
4 changes: 2 additions & 2 deletions art/defences/trainer/adversarial_trainer_awp_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def fit(
validation_data: Optional[Tuple[np.ndarray, np.ndarray]] = None,
batch_size: int = 128,
nb_epochs: int = 20,
scheduler: "torch.optim.lr_scheduler._LRScheduler" = None,
scheduler: Optional["torch.optim.lr_scheduler._LRScheduler"] = None,
**kwargs,
): # pylint: disable=W0221
"""
Expand Down Expand Up @@ -198,7 +198,7 @@ def fit_generator(
generator: DataGenerator,
validation_data: Optional[Tuple[np.ndarray, np.ndarray]] = None,
nb_epochs: int = 20,
scheduler: "torch.optim.lr_scheduler._LRScheduler" = None,
scheduler: Optional["torch.optim.lr_scheduler._LRScheduler"] = None,
**kwargs,
): # pylint: disable=W0221
"""
Expand Down
Loading

0 comments on commit ffd3a49

Please sign in to comment.