pytorch / audio

Data manipulation and transformation for audio signal processing, powered by PyTorch
https://pytorch.org/audio
BSD 2-Clause "Simplified" License
2.44k stars 640 forks source link

RuntimeError when loading large FLAC file #3316

Open notadoktor opened 1 year ago

notadoktor commented 1 year ago

🐛 Describe the bug

When using torchaudio.load on a 1.79 GB FLAC file, it throws RuntimeError: Trying to create tensor with negative dimension -225262592: [-225262592, 2].

import json
from pathlib import Path
import torchaudio

long_rec = Path("long_rec.flac")

print(long_rec.stat().st_size)
# 1921401859

print(json.dumps(vars(torchaudio.info(long_rec)), indent=2))
# {
#   "sample_rate": 96000,
#   "num_frames": 1922221056,
#   "num_channels": 2,
#   "bits_per_sample": 16,
#   "encoding": "FLAC"
# }

ta_data, sr = torchaudio.load(long_rec)
# ... ~45s processing

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[52], line 1
----> 1 ta_data, sr = torchaudio.load(long_rec)

File ~/mambaforge/envs/whisper/lib/python3.10/site-packages/torchaudio/backend/sox_io_backend.py:251, in load(filepath, frame_offset, num_frames, normalize, channels_first, format)
    241         return _fallback_load_fileobj(
    242             filepath,
    243             frame_offset,
   (...)
    248             buffer_size,
    249         )
    250     filepath = os.fspath(filepath)
--> 251 ret = torch.ops.torchaudio.sox_io_load_audio_file(
    252     filepath, frame_offset, num_frames, normalize, channels_first, format
    253 )
    254 if ret is not None:
    255     return ret

File ~/mambaforge/envs/whisper/lib/python3.10/site-packages/torch/_ops.py:502, in OpOverloadPacket.__call__(self, *args, **kwargs)
    497 def __call__(self, *args, **kwargs):
    498     # overloading __call__ to ensure torch.ops.foo.bar()
    499     # is still callable from JIT
    500     # We save the function ptr as the `op` attribute on
    501     # OpOverloadPacket to access it here.
--> 502     return self._op(*args, **kwargs or {})

RuntimeError: Trying to create tensor with negative dimension -225262592: [-225262592, 2]

However, I am still able to load load the file into a numpy array via librosa and then convert it to a tensor.

from pathlib import Path
import librosa
import torch

long_rec = Path("long_rec.flac")
data, sr = librosa.load(long_rec, mono=False, sr=None)
data.shape
# (2, 1922221056)

data_tensor = torch.tensor(data)
data_tensor.shape
# torch.Size([2, 1922221056])

Versions

Collecting environment information...
PyTorch version: 2.0.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.2 LTS (x86_64)
GCC version: (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
Clang version: Could not collect
CMake version: version 3.22.1
Libc version: glibc-2.35

Python version: 3.10.9 | packaged by conda-forge | (main, Feb  2 2023, 20:20:04) [GCC 11.3.0] (64-bit runtime)
Python platform: Linux-6.2.0-10005-tuxedo-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
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
Address sizes:                   39 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          8
On-line CPU(s) list:             0-7
Vendor ID:                       GenuineIntel
Model name:                      11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
CPU family:                      6
Model:                           140
Thread(s) per core:              2
Core(s) per socket:              4
Socket(s):                       1
Stepping:                        1
CPU max MHz:                     4700.0000
CPU min MHz:                     400.0000
BogoMIPS:                        5606.40
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 tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx 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 cat_l2 invpcid_single cdp_l2 ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves split_lock_detect dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid movdiri movdir64b fsrm avx512_vp2intersect md_clear ibt flush_l1d arch_capabilities
Virtualization:                  VT-x
L1d cache:                       192 KiB (4 instances)
L1i cache:                       128 KiB (4 instances)
L2 cache:                        5 MiB (4 instances)
L3 cache:                        12 MiB (1 instance)
NUMA node(s):                    1
NUMA node0 CPU(s):               0-7
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Not affected
Vulnerability Retbleed:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected

Versions of relevant libraries:
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.23.5
[pip3] torch==2.0.0
[pip3] torchaudio==2.0.0
[conda] blas                      1.0                         mkl    conda-forge
[conda] libblas                   3.9.0            16_linux64_mkl    conda-forge
[conda] libcblas                  3.9.0            16_linux64_mkl    conda-forge
[conda] liblapack                 3.9.0            16_linux64_mkl    conda-forge
[conda] mkl                       2022.2.1         h84fe81f_16997    conda-forge
[conda] numpy                     1.23.5          py310h53a5b5f_0    conda-forge
[conda] pytorch                   2.0.0              py3.10_cpu_0    pytorch
[conda] pytorch-mutex             1.0                         cpu    pytorch
[conda] torchaudio                2.0.0                 py310_cpu    pytorch
mthrok commented 1 year ago

@notadoktor Thanks for the report. Looks like a integer overflow.

Could you run the same script with export TORCH_SHOW_CPP_STACKTRACES=1?