pitch_shift consumes excessive GPU memory which is also not cleared

Open
#3,447 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
performance

Research direction

Start by reproducing the CUDA memory behavior with the reported pitch_shift examples, then inspect the functional.py paths shown in the traceback and the PitchShift transform entry point. Done means the examples no longer allocate excessive GPU memory and repeated calls do not retain the reported allocation.

Written by the indexing model from the issue text.

Description

🐛 Describe the bug

Both torchaudio.functional.pitch_shift and torchaudio.transforms.PitchShift occupy excessive amount of GPU memory, which is not cleared, while working fine on CPU.

ISSUE 1

The following piece of code

import torch
from torchaudio.functional import pitch_shift

waveform = torch.randn(1600, device=torch.device("cuda:0"))
output_tensor = pitch_shift(waveform, 16000, n_steps=1)

raises the below exception, in a machine with a GPU having 4GB memory,

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/dhanvanth/miniconda3/envs/z2/lib/python3.9/site-packages/torchaudio/functional/functional.py", line 1765, in pitch_shift
    waveform_shift = resample(waveform_stretch, int(sample_rate / rate), sample_rate)
  File "/home/dhanvanth/miniconda3/envs/z2/lib/python3.9/site-packages/torchaudio/functional/functional.py", line 1604, in resample
    kernel, width = _get_sinc_resample_kernel(
  File "/home/dhanvanth/miniconda3/envs/z2/lib/python3.9/site-packages/torchaudio/functional/functional.py", line 1522, in _get_sinc_resample_kernel
    kernels = torch.where(t == 0, torch.tensor(1.0).to(t), t.sin() / t)
torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 1.01 GiB (GPU 0; 3.95 GiB total capacity; 2.28 GiB already allocated; 340.44 MiB free; 3.04 GiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation.  See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

In a machine with a GPU having ~48GB memory, the memory consumption to run the same code is observed to be 6.28 GB, of which the last line alone occupies 5.6 GB

On another note, the below code shows the same memory usage pattern

import torch
from torchaudio.transforms import PitchShift

waveform = torch.randn(1600, device=torch.device("cuda:0"))
effect = PitchShift(
    sample_rate = 16000,
    n_steps = 1,
).to(waveform.device)
output_tensor = effect(waveform)
ISSUE 2

The occupied GPU memory is not cleared after applying the effect

ISSUE 3

This occupied memory scales for certain values of n_steps. The below code consumes 10.67 GB GPU memory,

import torch
from torchaudio.functional import pitch_shift

waveform = torch.randn(1600, device=torch.device("cuda:1"))
output_tensor = pitch_shift(waveform, 16000, n_steps=1)
output_tensor = pitch_shift(waveform, 16000, n_steps=2)
Versions
Versions

PyTorch version: 2.0.0+cu118
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.6 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: Could not collect
CMake version: version 3.26.3
Libc version: glibc-2.31

Python version: 3.9.16 (main, Mar 8 2023, 14:00:05) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.0-75-generic-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: Quadro P1000
Nvidia driver version: 470.182.03
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 39 bits physical, 48 bits virtual
CPU(s): 12
On-line CPU(s) list: 0-11
Thread(s) per core: 2
Core(s) per socket: 6
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 158
Model name: Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
Stepping: 10
CPU MHz: 2600.000
CPU max MHz: 4300.0000
CPU min MHz: 800.0000
BogoMIPS: 5199.98
L1d cache: 192 KiB
L1i cache: 192 KiB
L2 cache: 1.5 MiB
L3 cache: 9 MiB
NUMA node0 CPU(s): 0-11
Vulnerability Itlb multihit: KVM: Mitigation: VMX unsupported
Vulnerability L1tf: Mitigation; PTE Inversion
Vulnerability Mds: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown: Mitigation; PTI
Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed: Mitigation; IBRS
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; IBRS, IBPB conditional, STIBP conditional, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds: Mitigation; Microcode
Vulnerability Tsx async abort: Mitigation; TSX disabled
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp md_clear flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] mypy==0.961
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.23.5
[pip3] pytorch-lightning==2.0.0
[pip3] torch==2.0.0+cu118
[pip3] torch-poly-lr-decay==0.0.1
[pip3] torchaudio==2.0.0+cu118
[pip3] torchmetrics==0.11.4
[pip3] triton==2.0.0
[conda] cudatoolkit 11.3.1 h2bc3f7f_2
[conda] numpy 1.23.5 pypi_0 pypi
[conda] pytorch-lightning 2.0.0 pypi_0 pypi
[conda] torch 2.0.0+cu118 pypi_0 pypi
[conda] torch-poly-lr-decay 0.0.1 pypi_0 pypi
[conda] torchaudio 2.0.0+cu118 pypi_0 pypi
[conda] torchmetrics 0.11.4 pypi_0 pypi
[conda] triton 2.0.0 pypi_0 pypi

Dominant language
Python
Stars
2.9k
Forks
799
Avg merge
58m
Merged PRs (30d)
3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pytorch/audio

All issues in pytorch/audio

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.