Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio audio analyze: screen the true peak in float32 and convolve only the candidate outputs (#16794)
Daniel Han418e88d97384
diff --git a/studio/backend/core/inference/audio_metrics.py b/studio/backend/core/inference/audio_metrics.pyindex 145b5246df1..136ee03e48a 100644--- a/studio/backend/core/inference/audio_metrics.py+++ b/studio/backend/core/inference/audio_metrics.py@@ -248,6 +248,35 @@ def _oversampler(factor: int = 4, taps_per_phase: int = 12) -> np.ndarray:return taps.reshape(taps_per_phase, factor).T+# Past this many candidate spans per phase the whole chunk is convolved instead.+_PEAK_SPANS_MAX = 128+++def _phase_peak(+ window: np.ndarray, taps: np.ndarray, peak: float, screen: Optional[np.ndarray], err: float+) -> float:+ """max(peak, max |np.convolve(window, taps, "valid")|), convolving only where ``screen`` (+-err) allows."""+ if screen is None:+ return max(peak, float(np.max(np.abs(np.convolve(window, taps, mode = "valid")))))+ top = float(screen.max())+ # |output j - screen[j]| <= err: below top - 2 err cannot be the max, below peak - err cannot raise it.+ floor = max(top - 2.0 * err, peak - err)+ if floor > top:+ return peak+ idx = np.flatnonzero(screen >= floor)+ length = len(taps)+ breaks = np.flatnonzero(np.diff(idx) > length) + 1+ if breaks.size >= _PEAK_SPANS_MAX:+ return _phase_peak(window, taps, peak, None, err)+ firsts = idx[np.concatenate(([0], breaks))].tolist()+ lasts = idx[np.concatenate((breaks - 1, [idx.size - 1]))].tolist()+ for first, last in zip(firsts, lasts):+ # Same memory, same dot product per output as the whole-chunk np.convolve.+ part = np.convolve(window[first : last + length], taps, mode = "valid")+ peak = max(peak, float(np.max(np.abs(part))))+ return peak++def true_peak(data: np.ndarray) -> float:"""The largest absolute sample after 4x oversampling (BS.1770 Annex 2), linear."""phases = _oversampler()@@ -255,6 +284,9 @@ def true_peak(data: np.ndarray) -> float:# np.convolve's "same" output i reads input i - before .. i + after.after = (length - 1) // 2before = length - 1 - after+ # np.convolve(window, taps, "valid")[j] is sum(taps[k] * window[j + length - 1 - k]).+ flipped = phases[:, ::-1].astype(np.float32)+ reach = float(np.abs(phases).sum(axis = 1).max())peak = 0.0for channel in range(data.shape[1]):x = data[:, channel]@@ -262,8 +294,22 @@ def true_peak(data: np.ndarray) -> float:stop = min(start + _CHUNK, len(x))peak = max(peak, float(np.max(np.abs(x[start:stop]))))window = _padded(x, start - before, stop + after)- for taps in phases:- peak = max(peak, float(np.max(np.abs(np.convolve(window, taps, mode = "valid")))))+ scale = float(np.max(np.abs(window)))+ screens, err = [None] * len(phases), 0.0+ if 2.0**-60 <= scale <= 2.0**60:+ # Screen and np.convolve each lie within ~14 * 2**-24 * reach * scale of the exact+ # sum in any summation order; 2**-19 leaves 2x margin.+ err = reach * scale * 2.0**-19+ samples = window.astype(np.float32, copy = False)+ cols = len(window) - length + 1+ # Plain ufuncs, not BLAS: independent of the caller's BLAS thread setting.+ screens = flipped[:, :1] * samples[:cols]+ term = np.empty_like(screens)+ for k in range(1, length):+ screens += np.multiply(flipped[:, k : k + 1], samples[k : k + cols], out = term)+ np.abs(screens, out = screens)+ for taps, screen in zip(phases, screens):+ peak = _phase_peak(window, taps, peak, screen, err)return peakdiff --git a/studio/backend/tests/test_audio_true_peak_screen.py b/studio/backend/tests/test_audio_true_peak_screen.pynew file mode 100644index 00000000000..7374d322b13--- /dev/null+++ b/studio/backend/tests/test_audio_true_peak_screen.py@@ -0,0 +1,105 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""The screened true peak returns the very float the whole-chunk convolution walk returns."""++from __future__ import annotations++import numpy as np+import pytest++from core.inference import audio_metrics as am+++def _walk(data: np.ndarray) -> float:+ """The true peak as every output of every chunk convolved in full (the walk before the screen)."""+ phases = am._oversampler()+ length = phases.shape[1]+ after = (length - 1) // 2+ before = length - 1 - after+ peak = 0.0+ for channel in range(data.shape[1]):+ x = data[:, channel]+ for start in range(0, len(x), am._CHUNK):+ stop = min(start + am._CHUNK, len(x))+ peak = max(peak, float(np.max(np.abs(x[start:stop]))))+ window = am._padded(x, start - before, stop + after)+ for taps in phases:+ peak = max(peak, float(np.max(np.abs(np.convolve(window, taps, mode = "valid")))))+ return peak+++def _signals(rng: np.random.Generator):+ rate = 48000+ for n in (1, 5, 11, 12, 13, 47, 1000, 4097, 30011):+ t = np.arange(n) / rate+ yield rng.uniform(-1, 1, (n, 1)).astype(np.float32)+ yield np.stack([0.3 * np.sin(2 * np.pi * 997 * t), 0.5 * np.cos(2 * np.pi * 1000 * t)], 1)+ tone = (0.5 * np.sin(2 * np.pi * 1000 * t)).astype(np.float32)[:, None]+ yield tone+ yield np.where(np.sin(2 * np.pi * 100 * t) >= 0, 1.0, -1.0).astype(np.float32)[:, None]+ yield np.zeros((n, 2), np.float32)+ yield np.clip(3.0 * tone, -1.0, 1.0)+ yield (rng.standard_normal((n, 1)) * 1e-30).astype(np.float32)+ yield (rng.standard_normal((n, 2)) * 1e20).astype(np.float32)+ yield np.full((n, 1), 1e-40, np.float32)+ for special in (np.nan, np.inf, -np.inf):+ spiked = rng.uniform(-0.5, 0.5, (n, 1)).astype(np.float32)+ spiked[rng.integers(n)] = special+ yield spiked+ pcm = np.round(rng.standard_normal((n, 2)) * 6000).clip(-32768, 32767) / 32768.0+ yield pcm.astype(np.float32)+ envelope = np.abs(np.sin(2 * np.pi * 3 * t))[:, None] ** 4+ yield (envelope * rng.standard_normal((n, 1)) * 0.2).astype(np.float32)++[email protected]("chunk", (7, 100, 4096, 1 << 18))+def test_screen_matches_the_full_walk(monkeypatch, chunk):+ monkeypatch.setattr(am, "_CHUNK", chunk)+ rng = np.random.default_rng(chunk)+ for data in _signals(rng):+ expected, got = _walk(data), am.true_peak(data)+ assert repr(got) == repr(expected), (data.shape, data.dtype, expected, got)+++def test_many_ties_fall_back_to_the_whole_chunk(monkeypatch):+ # A full-scale square wave ties at every edge, so the screen hands whole chunks to np.convolve.+ calls = []+ real = np.convolve++ def counting(+ a,+ v,+ mode = "full",+ ):+ calls.append(len(a))+ return real(a, v, mode = mode)++ monkeypatch.setattr(np, "convolve", counting)+ t = np.arange(1 << 18) / 48000+ square = np.where(np.sin(2 * np.pi * 1000 * t) >= 0, 1.0, -1.0).astype(np.float32)[:, None]+ assert repr(am.true_peak(square)) == repr(_walk(square))+ assert max(calls) == (1 << 18) + 11+++def test_a_long_take_convolves_only_near_its_peak(monkeypatch):+ calls = []+ real = np.convolve++ def counting(+ a,+ v,+ mode = "full",+ ):+ calls.append(len(a))+ return real(a, v, mode = mode)++ rng = np.random.default_rng(3)+ t = np.arange(48000 * 12) / 48000+ speech = (np.abs(np.sin(2 * np.pi * 2 * t)) ** 4 * rng.standard_normal(t.size) * 0.2).astype(+ np.float32+ )+ expected = _walk(speech[:, None])+ monkeypatch.setattr(np, "convolve", counting)+ assert repr(am.true_peak(speech[:, None])) == repr(expected)+ assert calls and max(calls) < 1000