pyannote-audio icon indicating copy to clipboard operation
pyannote-audio copied to clipboard

UserWarning: std(): degrees of freedom is <= 0

Open ganczarek opened this issue 7 months ago • 2 comments

Tested versions

  • Reproducible in v3.3.2 with PyTorch 2.6.0

System information

Linux 6.14.2

Issue description

When using SpeakerDiarization pipeline, I noticed the following warning triggered by PyTorch 2.6.0:

  warnings.warn(
/home/.../venv/lib/python3.12/site-packages/pyannote/audio/models/blocks/pooling.py:104: UserWarning: std(): degrees of freedom is <= 0. Correction should be strictly less than the reduction factor (input numel divided by output numel). (Triggered internally at /pytorch/aten/src/ATen/native/ReduceOps.cpp:1831.)
  std = sequences.std(dim=-1, correction=1)

It's caused by this line:

std = sequences.std(dim=-1, correction=1)

The last dimension in sequences can be of size 1. For example, when I looked at it, a tensor shape was (1, 2560, 1). In such case unbiased estimator divides by 0 and result is full of NaN values.

A possible solution could be:

if sequences.size(dim=-1) > 1:
   std = sequences.std(dim=-1, correction=1)
else:
    std = torch.zeros_like(mean)

Above change gets rid of warning and I didn't notice any difference in the computed output.

I'm happy to create a pull request with this change unless the problem is deeper and the last dimension of sequences tensor should never be 1.

Minimal reproduction example (MRE)

N/A

ganczarek avatar Apr 20 '25 23:04 ganczarek

I'm also getting this warning when using the Speaker Diarization 3.1 pipeline.

leafybees avatar May 25 '25 20:05 leafybees

Thanks for raising this issue and for proposing a clean fix.

I encountered the same warning using PyTorch 2.7.0 and implemented a monkeypatch based on your suggested logic. It works well and suppresses the warning without affecting the output.

Here's a temporary workaround for anyone experiencing the issue:

import torch
from pyannote.audio.models.blocks.pooling import StatsPool

def patched_forward(self, sequences, weights=None):
    mean = sequences.mean(dim=-1)
    if sequences.size(-1) > 1:
        std = sequences.std(dim=-1, correction=1)
    else:
        std = torch.zeros_like(mean)
    return torch.cat([mean, std], dim=-1)

StatsPool.forward = patched_forward

I’m not planning to open a pull request myself, but I hope this helps others until a fix is merged upstream.

cypher-256 avatar Jun 13 '25 03:06 cypher-256

Is this Warning still not solved?

kenenbek avatar Nov 14 '25 13:11 kenenbek