Repository: Lightning-AI/torchmetrics

Issue:
Error numpy.ndarray size changed
## 🐛 Bug

Hello,

I Installed a clean Ubuntu 20.04, installed nvidia drivers 440 for RTX 3080. Then I followed the instructions to train a model with NVIDIA NeMo Tacotron2.

I got the next error. It seems that the latest release of metrics is not compatible with the latest release of numpy.

numpy fails with the next error:
valueerror: numpy.ndarray size changed, may indicate binary incompatibility. expected 96 from c header, got 88 from pyobject

I share the full stack trace:
```
pc@pc:~/Downloads$ sudo python3.8 tacotron2.py sample_rate=25050 train_dataset=/home/pc/Downloads/newcontent.json validation_datasets=/home/pc/Downloads/newcontent.json trainer.max_epochs=1 trainer.accelerator=null trainer.check_val_every_n_epoch=1
[sudo] password for pc:
Traceback (most recent call last):
File "tacotron2.py", line 24, in
import pytorch_lightning as pl
File "/usr/local/lib/python3.8/site-packages/pytorch_lightning/init.py", line 20, in
from pytorch_lightning.callbacks import Callback # noqa: E402
File "/usr/local/lib/python3.8/site-packages/pytorch_lightning/callbacks/init.py", line 14, in
from pytorch_lightning.callbacks.base import Callback
File "/usr/local/lib/python3.8/site-packages/pytorch_lightning/callbacks/base.py", line 26, in
from pytorch_lightning.utilities.types import STEP_OUTPUT
File "/usr/local/lib/python3.8/site-packages/pytorch_lightning/utilities/types.py", line 25, in
from torchmetrics import Metric
File "/usr/local/lib/python3.8/site-packages/torchmetrics/init.py", line 14, in
from torchmetrics import functional # noqa: E402
File "/usr/local/lib/python3.8/site-packages/torchmetrics/functional/init.py", line 14, in
from torchmetrics.functional.audio.pit import permutation_invariant_training, pit, pit_permutate
File "/usr/local/lib/python3.8/site-packages/torchmetrics/functional/audio/init.py", line 26, in
from torchmetrics.functional.audio.pesq import perceptual_evaluation_speech_quality # noqa: F401
File "/usr/local/lib/python3.8/site-packages/torchmetrics/functional/audio/pesq.py", line 20, in
import pesq as pesq_backend
File "/usr/local/lib/python3.8/site-packages/pesq/init.py", line 6, in
from .cypesq import cypesq, cypesq_retvals, cypesq_error_message as pesq_error_message
File "pesq/cypesq.pyx", line 1, in init cypesq
ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 96 from C header, got 88 from PyObject
```

### Environment

- PyTorch Version: 1.10.1
- OS: Ubuntu 20.10
- PyTorch installed with pip
- Python version: python3.8
- CUDA/cuDNN version: 11.3
- GPU models and configuration: RTX 3080
- Any other relevant information: MSI Creator P100X 637

Could you help me to solve this?

Thank you!


Discussion:
Hi! thanks for your contribution!, great first issue!
it is about `numpy`, we have a similar issue in our CI and mostly it is when the `pesq` is compiled with a different version of NumPy that is later used... I would recommend freezing NumPy version to 1.20
```bash
pip install -q "numpy==1.20.0"
pip install torchmetrics[audio] --force-reinstall
```
> it is about `numpy`, we have a similar issue in our CI and mostly it is when the `pesq` is compiled with a different version of NumPy that is later used... I would recommend freezing NumPy version to 1.20
> 
> ```shell
> pip install -q "numpy==1.20.0"
> pip install torchmetrics[audio] --force-reinstall
> ```

Hi Borda, thanks for responding. The latest version of Nvidia NeMo only works with Numpy 1.21 or above. Unfortunately, version 1.20.0 is not compatible. I already tried it...

So, the alternative would be to recompile pesq with the latest version of Numpy?
> So, the alternative would be to recompile pesq with the latest version of Numpy?

you can give it try, just be patient as these compilation issues are tricky...
pls, let us know if it worked for you... :rabbit: 
Ok, thank you. Absolutely, I am going to try it. 
You could also try reinstall pesq. after reinstalling, the pesq will recompile it on the currently installed numpy. Related disscussion in https://github.com/PyTorchLightning/metrics/pull/725#issuecomment-1006890970
```
pip uninstall pesq -y
pip install pesq
```
Since we cannot implement `pesq` ourself due to the original license, I guess that the best we can do here is to include better install/re-install instructions in the documentation.

Resolving patch:
diff --git a/torchmetrics/audio/pesq.py b/torchmetrics/audio/pesq.py
--- a/torchmetrics/audio/pesq.py
+++ b/torchmetrics/audio/pesq.py
@@ -29,7 +29,9 @@ class PerceptualEvaluationSpeechQuality(Metric):
     to perform the metric calculation.
 
     .. note:: using this metrics requires you to have ``pesq`` install. Either install as ``pip install
-        torchmetrics[audio]`` or ``pip install pesq``
+        torchmetrics[audio]`` or ``pip install pesq``. Note that ``pesq`` will compile with your currently
+        installed version of numpy, meaning that if you upgrade numpy at some point in the future you will
+        most likely have to reinstall ``pesq``.
 
     Forward accepts
 
diff --git a/torchmetrics/functional/audio/pesq.py b/torchmetrics/functional/audio/pesq.py
--- a/torchmetrics/functional/audio/pesq.py
+++ b/torchmetrics/functional/audio/pesq.py
@@ -36,7 +36,9 @@ def perceptual_evaluation_speech_quality(
     to perform the metric calculation.
 
     .. note:: using this metrics requires you to have ``pesq`` install. Either install as ``pip install
-        torchmetrics[audio]`` or ``pip install pesq``
+        torchmetrics[audio]`` or ``pip install pesq``. Note that ``pesq`` will compile with your currently
+        installed version of numpy, meaning that if you upgrade numpy at some point in the future you will
+        most likely have to reinstall ``pesq``.
 
     Args:
         preds:


Test patch: