From 879ba1b6256fad3c49ff03f0c8a287652721e89c Mon Sep 17 00:00:00 2001 From: Aditya Date: Wed, 24 Nov 2021 20:24:42 -0500 Subject: [PATCH 01/18] loop efficiency --- augly/audio/functional.py | 13 ++++++++----- requirements.txt | 1 + 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/augly/audio/functional.py b/augly/audio/functional.py index 656d5da4a..4ec0f6d3d 100644 --- a/augly/audio/functional.py +++ b/augly/audio/functional.py @@ -7,6 +7,7 @@ import augly.audio.utils as audutils import numpy as np +import cupy as cp import torch from augly.utils import DEFAULT_SAMPLE_RATE from augly.utils.libsndfile import install_libsndfile @@ -618,16 +619,16 @@ def invert_channels( def loop( - audio: Union[str, np.ndarray], + audio: Union[str, cp.ndarray], sample_rate: int = DEFAULT_SAMPLE_RATE, n: int = 1, output_path: Optional[str] = None, metadata: Optional[List[Dict[str, Any]]] = None, -) -> Tuple[np.ndarray, int]: +) -> Tuple[cp.ndarray, int]: """ Loops the audio 'n' times - @param audio: the path to the audio or a variable of type np.ndarray that + @param audio: the path to the audio or a variable of type cp.ndarray that will be augmented @param sample_rate: the audio sample rate of the inputted audio @@ -635,7 +636,7 @@ def loop( @param n: the number of times the video will be looped @param output_path: the path in which the resulting audio will be stored. If None, - the resulting np.ndarray will still be returned + the resulting cp.ndarray will still be returned @param metadata: if set to be a list, metadata about the function execution including its name, the source & dest duration, sample rates, etc. will be @@ -645,10 +646,12 @@ def loop( """ assert isinstance(n, int) and n >= 0, "Expected 'n' to be a nonnegative integer" audio, sample_rate = audutils.validate_and_load_audio(audio, sample_rate) + audio = cp.array(audio) aug_audio = audio for _ in range(n): - aug_audio = np.append(aug_audio, audio, axis=(0 if audio.ndim == 1 else 1)) + aug_audio = cp.append(aug_audio, audio, axis=(0 if audio.ndim == 1 + else 1)) audutils.get_metadata( metadata=metadata, diff --git a/requirements.txt b/requirements.txt index 4ac2a974f..7a52b9433 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ numpy>=1.19.5 Pillow>=8.2.0 python-magic>=0.4.22 regex>=2021.4.4 +cupy>=9.6.0 \ No newline at end of file From f477122e7fc6449612ae97a31e55d404b82d97b8 Mon Sep 17 00:00:00 2001 From: Aditya Date: Wed, 24 Nov 2021 20:30:18 -0500 Subject: [PATCH 02/18] black formats --- augly/audio/functional.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/augly/audio/functional.py b/augly/audio/functional.py index 4ec0f6d3d..25d161109 100644 --- a/augly/audio/functional.py +++ b/augly/audio/functional.py @@ -650,8 +650,7 @@ def loop( aug_audio = audio for _ in range(n): - aug_audio = cp.append(aug_audio, audio, axis=(0 if audio.ndim == 1 - else 1)) + aug_audio = cp.append(aug_audio, audio, axis=(0 if audio.ndim == 1 else 1)) audutils.get_metadata( metadata=metadata, From 4c304d31f68e7e316c86dd3f86bbf7efc8d68489 Mon Sep 17 00:00:00 2001 From: Aditya Date: Thu, 25 Nov 2021 13:01:41 -0500 Subject: [PATCH 03/18] preliminary fixes to requested changes --- augly/audio/functional.py | 16 +++++++++++----- requirements.txt | 1 - setup.py | 5 ++++- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/augly/audio/functional.py b/augly/audio/functional.py index 25d161109..dfe9ccae5 100644 --- a/augly/audio/functional.py +++ b/augly/audio/functional.py @@ -4,14 +4,18 @@ import math from copy import deepcopy from typing import Any, Callable, Dict, List, Optional, Tuple, Union +import importlib import augly.audio.utils as audutils import numpy as np -import cupy as cp import torch from augly.utils import DEFAULT_SAMPLE_RATE from augly.utils.libsndfile import install_libsndfile +cupy_spec = importlib.util.find_spec("cupy") +cupy_found = cupy_spec is not None +if cupy_found: + import cupy as cp install_libsndfile() import librosa @@ -619,12 +623,12 @@ def invert_channels( def loop( - audio: Union[str, cp.ndarray], + audio: Union[str, np.ndarray], sample_rate: int = DEFAULT_SAMPLE_RATE, n: int = 1, output_path: Optional[str] = None, metadata: Optional[List[Dict[str, Any]]] = None, -) -> Tuple[cp.ndarray, int]: +) -> Tuple[np.ndarray, int]: """ Loops the audio 'n' times @@ -646,11 +650,13 @@ def loop( """ assert isinstance(n, int) and n >= 0, "Expected 'n' to be a nonnegative integer" audio, sample_rate = audutils.validate_and_load_audio(audio, sample_rate) - audio = cp.array(audio) aug_audio = audio for _ in range(n): - aug_audio = cp.append(aug_audio, audio, axis=(0 if audio.ndim == 1 else 1)) + aug_audio = (cp if cupy_found else np).append(aug_audio, audio, + axis=(0 + if audio.ndim == 1 + else 1)) audutils.get_metadata( metadata=metadata, diff --git a/requirements.txt b/requirements.txt index 7a52b9433..4ac2a974f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,3 @@ numpy>=1.19.5 Pillow>=8.2.0 python-magic>=0.4.22 regex>=2021.4.4 -cupy>=9.6.0 \ No newline at end of file diff --git a/setup.py b/setup.py index 8c5f881b8..8c4c8ed80 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,10 @@ extra_requirements = { "av": [ r for r in Path("av_requirements.txt").read_text().splitlines() if "@" not in r - ] + ], + "gpu": [ + "cupy" + ], } with open("README.md", encoding="utf8") as f: From 039dd5e91f606d4166bd62323475bf46bfb216c0 Mon Sep 17 00:00:00 2001 From: Aditya Date: Thu, 25 Nov 2021 13:02:59 -0500 Subject: [PATCH 04/18] black formats --- augly/audio/functional.py | 7 +++---- setup.py | 4 +--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/augly/audio/functional.py b/augly/audio/functional.py index dfe9ccae5..24c38fb2f 100644 --- a/augly/audio/functional.py +++ b/augly/audio/functional.py @@ -653,10 +653,9 @@ def loop( aug_audio = audio for _ in range(n): - aug_audio = (cp if cupy_found else np).append(aug_audio, audio, - axis=(0 - if audio.ndim == 1 - else 1)) + aug_audio = (cp if cupy_found else np).append( + aug_audio, audio, axis=(0 if audio.ndim == 1 else 1) + ) audutils.get_metadata( metadata=metadata, diff --git a/setup.py b/setup.py index 8c4c8ed80..371eb341b 100644 --- a/setup.py +++ b/setup.py @@ -14,9 +14,7 @@ "av": [ r for r in Path("av_requirements.txt").read_text().splitlines() if "@" not in r ], - "gpu": [ - "cupy" - ], + "gpu": ["cupy"], } with open("README.md", encoding="utf8") as f: From c20b6eb753993b59846a28d077f44409aadeff2c Mon Sep 17 00:00:00 2001 From: Aditya Date: Thu, 25 Nov 2021 13:07:44 -0500 Subject: [PATCH 05/18] making sure cupy works when imported --- augly/audio/functional.py | 1 + 1 file changed, 1 insertion(+) diff --git a/augly/audio/functional.py b/augly/audio/functional.py index 24c38fb2f..29312fbf5 100644 --- a/augly/audio/functional.py +++ b/augly/audio/functional.py @@ -650,6 +650,7 @@ def loop( """ assert isinstance(n, int) and n >= 0, "Expected 'n' to be a nonnegative integer" audio, sample_rate = audutils.validate_and_load_audio(audio, sample_rate) + if cupy_found: audio = cp.array(audio) aug_audio = audio for _ in range(n): From e3bb3d254c88f9115779130651b10abceea53ef0 Mon Sep 17 00:00:00 2001 From: Aditya Date: Thu, 25 Nov 2021 13:08:14 -0500 Subject: [PATCH 06/18] black format --- augly/audio/functional.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/augly/audio/functional.py b/augly/audio/functional.py index 29312fbf5..6fdd3e156 100644 --- a/augly/audio/functional.py +++ b/augly/audio/functional.py @@ -650,7 +650,8 @@ def loop( """ assert isinstance(n, int) and n >= 0, "Expected 'n' to be a nonnegative integer" audio, sample_rate = audutils.validate_and_load_audio(audio, sample_rate) - if cupy_found: audio = cp.array(audio) + if cupy_found: + audio = cp.array(audio) aug_audio = audio for _ in range(n): From a32acc55a40fec2b32e5880d29e0431b8debaf33 Mon Sep 17 00:00:00 2001 From: Aditya Date: Thu, 25 Nov 2021 13:42:20 -0500 Subject: [PATCH 07/18] edit docstring --- augly/audio/functional.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/augly/audio/functional.py b/augly/audio/functional.py index 6fdd3e156..4920c24d1 100644 --- a/augly/audio/functional.py +++ b/augly/audio/functional.py @@ -632,7 +632,7 @@ def loop( """ Loops the audio 'n' times - @param audio: the path to the audio or a variable of type cp.ndarray that + @param audio: the path to the audio or a variable of type np.ndarray that will be augmented @param sample_rate: the audio sample rate of the inputted audio @@ -640,7 +640,7 @@ def loop( @param n: the number of times the video will be looped @param output_path: the path in which the resulting audio will be stored. If None, - the resulting cp.ndarray will still be returned + the resulting np.ndarray will still be returned @param metadata: if set to be a list, metadata about the function execution including its name, the source & dest duration, sample rates, etc. will be From 0c91111bb66728a87d2f7e45a094b91675c93d5b Mon Sep 17 00:00:00 2001 From: Aditya Date: Mon, 29 Nov 2021 13:08:00 -0500 Subject: [PATCH 08/18] gpu support to readme --- augly/audio/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/augly/audio/README.md b/augly/audio/README.md index 502cd9b9d..1b7b5154f 100644 --- a/augly/audio/README.md +++ b/augly/audio/README.md @@ -9,6 +9,14 @@ pip install augly[av] This ensures that not only the base dependencies, but also the heavier dependencies required for audio & video processing, are installed. + +If you have [CUDA](https://developer.nvidia.com/cuda-gpus), you can also +install AugLy with GPU-accelerated audio augmentation support using the +following command: +```bash +pip install augly[av,gpu] +``` + ## Augmentations [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/facebookresearch/AugLy/blob/main/examples/AugLy_audio.ipynb) From 6cd8f4dd56576cbc2dc3a687a2dcb730fa301cbd Mon Sep 17 00:00:00 2001 From: Aditya Date: Tue, 30 Nov 2021 18:02:37 -0500 Subject: [PATCH 09/18] testing new workflow --- .github/workflows/test_cupy.yml | 26 ++++++++++++++++++++++++++ augly/audio/README.md | 16 +++++++++++++--- setup.py | 3 +-- 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/test_cupy.yml diff --git a/.github/workflows/test_cupy.yml b/.github/workflows/test_cupy.yml new file mode 100644 index 000000000..85a81a50a --- /dev/null +++ b/.github/workflows/test_cupy.yml @@ -0,0 +1,26 @@ +name: test_cupy +on: + pull_request: + branches: main + push: + branches: main +jobs: + test_python: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + with: + python-version: '3.9' + - uses: Jimver/cuda-toolkit@v0.2.4 + id: cuda-toolkit + with: + cuda: '11.2.2' + + - run: + echo "Installed cuda version is: ${{steps.cuda-toolkit.outputs.cuda}}" + + - run: + echo "Cuda install location: ${{steps.cuda-toolkit.outputs.CUDA_PATH}}" + + - run: nvcc ---version \ No newline at end of file diff --git a/augly/audio/README.md b/augly/audio/README.md index 1b7b5154f..3ab3cde6a 100644 --- a/augly/audio/README.md +++ b/augly/audio/README.md @@ -11,12 +11,22 @@ This ensures that not only the base dependencies, but also the heavier dependenc If you have [CUDA](https://developer.nvidia.com/cuda-gpus), you can also -install AugLy with GPU-accelerated audio augmentation support using the -following command: +install AugLy with builtin GPU-accelerated audio augmentation support. + +Check if your system has CUDA by running: ```bash -pip install augly[av,gpu] +nvcc --version ``` +Based on the compiler version, you can manually install `cupy`: +```bash +pip install cupy-cuda +``` +Note: Remove any periods from the version number. + +AugLy will now automatically detect if `cupy` is available and use it for +GPU-accelerated audio augmentation. + ## Augmentations [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/facebookresearch/AugLy/blob/main/examples/AugLy_audio.ipynb) diff --git a/setup.py b/setup.py index 371eb341b..8c5f881b8 100644 --- a/setup.py +++ b/setup.py @@ -13,8 +13,7 @@ extra_requirements = { "av": [ r for r in Path("av_requirements.txt").read_text().splitlines() if "@" not in r - ], - "gpu": ["cupy"], + ] } with open("README.md", encoding="utf8") as f: From babed4f82f79c6bd741520be2695b8b3cdfac20d Mon Sep 17 00:00:00 2001 From: Aditya Date: Tue, 30 Nov 2021 18:05:36 -0500 Subject: [PATCH 10/18] minor edit --- .github/workflows/test_cupy.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test_cupy.yml b/.github/workflows/test_cupy.yml index 85a81a50a..78602a4d0 100644 --- a/.github/workflows/test_cupy.yml +++ b/.github/workflows/test_cupy.yml @@ -1,9 +1,13 @@ name: test_cupy on: pull_request: - branches: main + branches: + - main + - cupying push: - branches: main + branches: + - main + - cupying jobs: test_python: runs-on: ubuntu-latest From 2f93c40352051256c688bfbfd327f1081b2db698 Mon Sep 17 00:00:00 2001 From: Aditya Date: Tue, 30 Nov 2021 18:08:03 -0500 Subject: [PATCH 11/18] oops --- .github/workflows/test_cupy.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test_cupy.yml b/.github/workflows/test_cupy.yml index 78602a4d0..48a5593f9 100644 --- a/.github/workflows/test_cupy.yml +++ b/.github/workflows/test_cupy.yml @@ -22,9 +22,9 @@ jobs: cuda: '11.2.2' - run: - echo "Installed cuda version is: ${{steps.cuda-toolkit.outputs.cuda}}" + echo "Installed cuda version is ${{steps.cuda-toolkit.outputs.cuda}}" - run: - echo "Cuda install location: ${{steps.cuda-toolkit.outputs.CUDA_PATH}}" + echo "Cuda install location ${{steps.cuda-toolkit.outputs.CUDA_PATH}}" - run: nvcc ---version \ No newline at end of file From fc78ae3a3635ba031fa73cce4914c354c5b3540b Mon Sep 17 00:00:00 2001 From: Aditya Date: Tue, 30 Nov 2021 18:12:58 -0500 Subject: [PATCH 12/18] testing -V --- .github/workflows/test_cupy.yml | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test_cupy.yml b/.github/workflows/test_cupy.yml index 48a5593f9..38c451452 100644 --- a/.github/workflows/test_cupy.yml +++ b/.github/workflows/test_cupy.yml @@ -20,11 +20,15 @@ jobs: id: cuda-toolkit with: cuda: '11.2.2' - - run: echo "Installed cuda version is ${{steps.cuda-toolkit.outputs.cuda}}" - - run: - echo "Cuda install location ${{steps.cuda-toolkit.outputs.CUDA_PATH}}" - - - run: nvcc ---version \ No newline at end of file + echo "Cuda install location is ${{steps.cuda-toolkit.outputs + .CUDA_PATH}}" + - run: nvcc -V + - run: sudo apt-get update + - run: sudo apt-get install --fix-missing ffmpeg python3-soundfile + - run: pip install pyre-check pytest torchvision + - run: pip install -e .[av] + - run: pyre --source-directory "." --noninteractive check || true + - run: pytest --durations=10 . \ No newline at end of file From ff4e0d3338ad10cf0a4ac18ccf31b101f765e5e9 Mon Sep 17 00:00:00 2001 From: Aditya Date: Tue, 30 Nov 2021 18:53:30 -0500 Subject: [PATCH 13/18] final edits to wkflow --- .github/workflows/test_cupy.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/test_cupy.yml b/.github/workflows/test_cupy.yml index 38c451452..9af1579af 100644 --- a/.github/workflows/test_cupy.yml +++ b/.github/workflows/test_cupy.yml @@ -3,11 +3,9 @@ on: pull_request: branches: - main - - cupying push: branches: - main - - cupying jobs: test_python: runs-on: ubuntu-latest @@ -25,7 +23,7 @@ jobs: - run: echo "Cuda install location is ${{steps.cuda-toolkit.outputs .CUDA_PATH}}" - - run: nvcc -V + - run: nvcc --version - run: sudo apt-get update - run: sudo apt-get install --fix-missing ffmpeg python3-soundfile - run: pip install pyre-check pytest torchvision From a19a443861f914e2fb55a18a9c7544bf0ae25ffd Mon Sep 17 00:00:00 2001 From: Aditya Date: Tue, 30 Nov 2021 19:20:28 -0500 Subject: [PATCH 14/18] naming --- .github/workflows/test_cupy.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test_cupy.yml b/.github/workflows/test_cupy.yml index 9af1579af..121ce2099 100644 --- a/.github/workflows/test_cupy.yml +++ b/.github/workflows/test_cupy.yml @@ -7,7 +7,7 @@ on: branches: - main jobs: - test_python: + test_cupy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 @@ -29,4 +29,4 @@ jobs: - run: pip install pyre-check pytest torchvision - run: pip install -e .[av] - run: pyre --source-directory "." --noninteractive check || true - - run: pytest --durations=10 . \ No newline at end of file + - run: pytest --durations=10 . From 5e4be8c1cb4a997346505afeb28b81e323077c15 Mon Sep 17 00:00:00 2001 From: Aditya Prasad Date: Wed, 1 Dec 2021 13:16:39 -0500 Subject: [PATCH 15/18] Update .github/workflows/test_cupy.yml Co-authored-by: Zoe Papakipos --- .github/workflows/test_cupy.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test_cupy.yml b/.github/workflows/test_cupy.yml index 121ce2099..e0ed5e354 100644 --- a/.github/workflows/test_cupy.yml +++ b/.github/workflows/test_cupy.yml @@ -25,7 +25,8 @@ jobs: .CUDA_PATH}}" - run: nvcc --version - run: sudo apt-get update - - run: sudo apt-get install --fix-missing ffmpeg python3-soundfile + - run: sudo apt-get install --fix-missing python3-soundfile + - run: pip install pyre-check pytest torchvision - run: pip install -e .[av] - run: pyre --source-directory "." --noninteractive check || true From 5b4f1dd0207548f658b45c9cc779e03a867a17d2 Mon Sep 17 00:00:00 2001 From: Aditya Prasad Date: Wed, 1 Dec 2021 13:16:52 -0500 Subject: [PATCH 16/18] Update .github/workflows/test_cupy.yml Co-authored-by: Zoe Papakipos --- .github/workflows/test_cupy.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test_cupy.yml b/.github/workflows/test_cupy.yml index e0ed5e354..c9e8fc5d7 100644 --- a/.github/workflows/test_cupy.yml +++ b/.github/workflows/test_cupy.yml @@ -27,7 +27,8 @@ jobs: - run: sudo apt-get update - run: sudo apt-get install --fix-missing python3-soundfile - - run: pip install pyre-check pytest torchvision + - run: pip install pyre-check pytest cupy-cuda112 + - run: pip install -e .[av] - run: pyre --source-directory "." --noninteractive check || true - run: pytest --durations=10 . From 3c5693f358719dda32ce9b266a7017e5d190ff3e Mon Sep 17 00:00:00 2001 From: Aditya Prasad Date: Wed, 1 Dec 2021 13:17:02 -0500 Subject: [PATCH 17/18] Update .github/workflows/test_cupy.yml Co-authored-by: Zoe Papakipos --- .github/workflows/test_cupy.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test_cupy.yml b/.github/workflows/test_cupy.yml index c9e8fc5d7..db95b918a 100644 --- a/.github/workflows/test_cupy.yml +++ b/.github/workflows/test_cupy.yml @@ -31,4 +31,5 @@ jobs: - run: pip install -e .[av] - run: pyre --source-directory "." --noninteractive check || true - - run: pytest --durations=10 . + - run: pytest --durations=10 augly/tests/audio_tests/ + From a8b812f316d094a3b41158ce07df7d9769a21eb9 Mon Sep 17 00:00:00 2001 From: Aditya Prasad Date: Wed, 1 Dec 2021 13:25:23 -0500 Subject: [PATCH 18/18] pip freeze for debugging --- .github/workflows/test_cupy.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test_cupy.yml b/.github/workflows/test_cupy.yml index db95b918a..8c73775ab 100644 --- a/.github/workflows/test_cupy.yml +++ b/.github/workflows/test_cupy.yml @@ -30,6 +30,7 @@ jobs: - run: pip install pyre-check pytest cupy-cuda112 - run: pip install -e .[av] + - run: pip freeze - run: pyre --source-directory "." --noninteractive check || true - run: pytest --durations=10 augly/tests/audio_tests/