File size: 3,237 Bytes
5f0f535
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""Run a speaker-diarization model over the packed diarization conditions (Hub Job side)."""

from __future__ import annotations

from collections.abc import Callable

from diarization_config import (
    TASK_DIARIZATION,
    is_full_condition_set,
    metric_column,
    resolve_condition_keys,
)
from evaluation.runtime import (
    apply_cpu_thread_settings_once,
    disable_broken_torchcodec,
    resolve_eval_devices,
)


def run_diarization_evaluation(
    model_id: str,
    family_id: str = "auto",
    progress_cb: Callable[[int, int, str], None] | None = None,
    custom_script: str = "",
    condition_keys: list[str] | None = None,
) -> dict:
    """Score ``model_id`` on each diarization condition.

    Returns ``task``, ``model_id``, ``eval_family``, ``num_params``, one
    ``<metric>_<condition>`` field per metric (see ``diarization_config.METRICS``), and the
    same timing fields as ASR runs (``eval_rtf`` etc.).
    """
    import torch

    from backends.diarization import build_diarizer
    from benchmark.diarization import evaluate_condition_timed, load_packed_condition

    apply_cpu_thread_settings_once()
    disable_broken_torchcodec()
    device_str, _device_int = resolve_eval_devices()
    keys = resolve_condition_keys(condition_keys)

    totals: dict[str, int] = {}
    for ck in keys:
        try:
            totals[ck] = len(load_packed_condition(ck))
        except Exception:
            totals[ck] = 0
    grand_total = sum(totals.values())
    if progress_cb is not None:
        progress_cb(0, grand_total, "")

    diarize, cleanup, family_label = build_diarizer(
        model_id, family_id, device_str, custom_script=custom_script
    )
    try:
        results: dict = {
            "task": TASK_DIARIZATION,
            "model_id": model_id,
            "eval_family": family_label,
            "num_params": int(getattr(diarize, "_num_params", 0) or 0),
        }
        total_audio_s = 0.0
        total_infer_s = 0.0
        total_samples = 0
        done = 0
        for ck in keys:
            base = done

            def _cb(cond: str, i: int, _n: int, _base: int = base) -> None:
                if progress_cb is not None:
                    progress_cb(_base + i, grand_total, cond)

            metrics, n, audio_s, infer_s = evaluate_condition_timed(diarize, ck, progress_cb=_cb)
            for metric, value in metrics.items():
                results[metric_column(metric, ck)] = value
            total_audio_s += audio_s
            total_infer_s += infer_s
            total_samples = max(total_samples, n)
            done += n

        results["eval_conditions"] = list(keys)
        results["eval_full_run"] = is_full_condition_set(condition_keys)
        results["num_samples"] = total_samples
        results["eval_audio_seconds"] = round(total_audio_s, 3)
        results["eval_wall_time_s"] = round(total_infer_s, 3)
        results["eval_rtf"] = round(total_audio_s / total_infer_s, 4) if total_infer_s > 1e-6 else ""
        if progress_cb is not None:
            progress_cb(done, grand_total, "")
        return results
    finally:
        cleanup()
        if device_str == "cuda" and torch.cuda.is_available():
            torch.cuda.empty_cache()