---
title: metric
---

<div class="python-reference">
<a id="fastvideo.eval.metrics.audio.kl_divergence.metric-classes"></a>
<a id="fastvideo.eval.metrics.audio.kl_divergence.metric-functions"></a>
<a id="fastvideo.eval.metrics.audio.kl_divergence.metric"></a>
<p>PaSST KL divergence <code>KL(gt || pred)</code> on AudioSet-527 logits.</p>
<p>Ports <code>av_bench.metrics.kl.compute_kl</code> 1:1. The primary <code>score</code> is
the softmax variant (reported as "MKL" / "KL_PaSST" by V2A papers);
the sigmoid variant is exposed in <code>details["kl_sigmoid"]</code>.</p>
<h2 id="fastvideo.eval.metrics.audio.kl_divergence.metric.BaseMetric"><code>fastvideo.eval.metrics.audio.kl_divergence.metric.BaseMetric</code></h2>
<pre class="doc-signature"><code>BaseMetric()</code></pre>
<p>Abstract base class for all eval metrics.</p>
<p>Two execution shapes:</p>
<ul>
<li>
<p><strong>Per-sample</strong> (<code>is_set_metric=False</code>, default) — implement
  :meth:<code>compute</code>. The Evaluator calls it once per input sample and
  returns one :class:<code>MetricResult</code> per sample.</p>
</li>
<li>
<p><strong>Set-vs-set</strong> (<code>is_set_metric=True</code>) — implement
  :meth:<code>accumulate</code> (called once per sample to buffer features)
  and :meth:<code>finalize</code> (called once after all samples to compute
  the corpus-level result). Use :meth:<code>reset</code> to clear buffers and
  :meth:<code>merge_from</code> to fold multi-GPU per-worker state together.</p>
</li>
</ul>
<p>Optionally override :meth:<code>setup</code> to eagerly load models. Metrics
that chunk along the time dim for memory hardcode their own chunk
size in <code>__init__</code> (see <code>optical_flow</code> for the canonical
example). Eval always processes one video per
:meth:<code>Evaluator.evaluate</code> call; <code>compute</code> / <code>accumulate</code>
receive a single sample, not a batch.</p>

<h3 id="fastvideo.eval.metrics.audio.kl_divergence.metric.BaseMetric.to"><code>fastvideo.eval.metrics.audio.kl_divergence.metric.BaseMetric.to</code></h3>
<pre class="doc-signature"><code>to(device: str | torch.device) -&gt; BaseMetric</code></pre>
<p>Move metric (and its internal models) to <em>device</em>.</p>
<h3 id="fastvideo.eval.metrics.audio.kl_divergence.metric.BaseMetric.setup"><code>fastvideo.eval.metrics.audio.kl_divergence.metric.BaseMetric.setup</code></h3>
<pre class="doc-signature"><code>setup() -&gt; None</code></pre>
<p>Eagerly load models. Called once by :class:<code>EvalWorker</code>.</p>
<p>Default is a no-op; metrics with no eager state (pixel math,
closed-form ops) inherit this. Override only if your metric
needs to load weights.</p>
<h3 id="fastvideo.eval.metrics.audio.kl_divergence.metric.BaseMetric.compute"><code>fastvideo.eval.metrics.audio.kl_divergence.metric.BaseMetric.compute</code></h3>
<pre class="doc-signature"><code>compute(sample: dict) -&gt; MetricResult</code></pre>
<p>Per-sample metrics: compute the score for one sample.</p>
<p><code>sample["video"]</code> is <code>(T, C, H, W)</code> float in <code>[0, 1]</code>.
<code>sample["reference"]</code> (if used) has the same shape. Return
<code>self._skip(sample, reason)</code> for missing inputs.</p>
<h3 id="fastvideo.eval.metrics.audio.kl_divergence.metric.BaseMetric.reset"><code>fastvideo.eval.metrics.audio.kl_divergence.metric.BaseMetric.reset</code></h3>
<pre class="doc-signature"><code>reset() -&gt; None</code></pre>
<p>Clear accumulator state at the start of each evaluate() call.</p>
<h3 id="fastvideo.eval.metrics.audio.kl_divergence.metric.BaseMetric.accumulate"><code>fastvideo.eval.metrics.audio.kl_divergence.metric.BaseMetric.accumulate</code></h3>
<pre class="doc-signature"><code>accumulate(sample: dict) -&gt; None</code></pre>
<p>Buffer per-sample features for a corpus-level metric.</p>
<h3 id="fastvideo.eval.metrics.audio.kl_divergence.metric.BaseMetric.finalize"><code>fastvideo.eval.metrics.audio.kl_divergence.metric.BaseMetric.finalize</code></h3>
<pre class="doc-signature"><code>finalize() -&gt; MetricResult</code></pre>
<p>Compute the corpus-level result from buffered state.</p>
<h3 id="fastvideo.eval.metrics.audio.kl_divergence.metric.BaseMetric.merge_from"><code>fastvideo.eval.metrics.audio.kl_divergence.metric.BaseMetric.merge_from</code></h3>
<pre class="doc-signature"><code>merge_from(other: BaseMetric) -&gt; None</code></pre>
<p>Multi-GPU: fold another worker's accumulator state into this one.</p>
<h2 id="fastvideo.eval.metrics.audio.kl_divergence.metric.register"><code>fastvideo.eval.metrics.audio.kl_divergence.metric.register</code></h2>
<pre class="doc-signature"><code>register(name: str)</code></pre>
<p>Decorator to register a metric class.</p>
<p>Usage::</p>
<pre><code>@register("ssim")
class SSIMMetric(BaseMetric):
    ...
</code></pre>
<h2 id="fastvideo.eval.metrics.audio.kl_divergence.metric.MetricResult"><code>fastvideo.eval.metrics.audio.kl_divergence.metric.MetricResult</code></h2>
<pre class="doc-signature"><code>MetricResult(name: str, score: float | None, details: dict&#91;str, Any&#93; = dict())</code></pre>
<p>Standard result container returned by all metrics.</p>
<p><code>score</code> is <code>None</code> when the metric was skipped (e.g. missing
required input).  Check <code>details["skipped"]</code> for the reason.</p>

<h2 id="fastvideo.eval.metrics.audio.kl_divergence.metric.KLDivergenceMetric"><code>fastvideo.eval.metrics.audio.kl_divergence.metric.KLDivergenceMetric</code></h2>
<pre class="doc-signature"><code>KLDivergenceMetric()</code></pre>
<p>PaSST KL divergence <code>KL(gt || pred)</code> on AudioSet-527 logits.</p>
<p>Per-sample. Requires <code>sample["audio"]</code> (generated) and
<code>sample["reference_audio"]</code> (ground truth).</p>

<details><summary>Source</summary><pre><code>@register(&quot;audio.kl_divergence&quot;)
class KLDivergenceMetric(BaseMetric):
    &quot;&quot;&quot;PaSST KL divergence ``KL(gt || pred)`` on AudioSet-527 logits.

    Per-sample. Requires ``sample&#91;&quot;audio&quot;&#93;`` (generated) and
    ``sample&#91;&quot;reference_audio&quot;&#93;`` (ground truth).
    &quot;&quot;&quot;

    name = &quot;audio.kl_divergence&quot;
    requires_reference = True
    higher_is_better = False
    needs_gpu = True
    is_set_metric = False
    dependencies = &#91;&quot;hear21passt&quot;, &quot;librosa&quot;, &quot;pyloudnorm&quot;&#93;

    def __init__(self) -&gt; None:
        super().__init__()
        self._model: Any = None

    def to(self, device):
        super().to(device)
        if self._model is not None:
            self._model = self._model.to(self.device)
        return self

    def setup(self) -&gt; None:
        if self._model is not None:
            return
        from hear21passt.base import get_basic_model
        model = get_basic_model(mode=&quot;logits&quot;)
        model.eval()
        model = model.to(self.device)
        self._model = model

    @torch.no_grad()
    def compute(self, sample: dict) -&gt; MetricResult:
        if self._model is None:
            self.setup()
        gen_path = sample.get(&quot;audio&quot;)
        ref_path = sample.get(&quot;reference_audio&quot;)
        if gen_path is None or ref_path is None:
            missing = &#91;k for k, v in ((&quot;audio&quot;, gen_path), (&quot;reference_audio&quot;, ref_path)) if v is None&#93;
            return self._skip(sample, f&quot;missing &#123;&#x27;, &#x27;.join(repr(k) for k in missing)&#125;&quot;)

        gt_logits = _collect_logits(self._model, ref_path, self.device)
        pred_logits = _collect_logits(self._model, gen_path, self.device)
        if not torch.isfinite(gt_logits).all() or not torch.isfinite(pred_logits).all():
            which = &#91;&#93;
            if not torch.isfinite(gt_logits).all():
                which.append(&quot;reference_audio&quot;)
            if not torch.isfinite(pred_logits).all():
                which.append(&quot;audio&quot;)
            return self._skip(
                sample,
                f&quot;non-finite PaSST logits on &#123;&#x27;+&#x27;.join(which)&#125; (silent or corrupt audio?)&quot;,
            )
        kl_sm = _kl_softmax(pred_logits, gt_logits)
        kl_sg = _kl_sigmoid(pred_logits, gt_logits)
        return MetricResult(
            name=self.name,
            score=kl_sm,
            details=&#123;
                &quot;kl_softmax&quot;: kl_sm,
                &quot;kl_sigmoid&quot;: kl_sg
            &#125;,
        )</code></pre></details>
<h3 id="fastvideo.eval.metrics.audio.kl_divergence.metric.KLDivergenceMetric.to"><code>fastvideo.eval.metrics.audio.kl_divergence.metric.KLDivergenceMetric.to</code></h3>
<pre class="doc-signature"><code>to(device)</code></pre>

<details><summary>Source</summary><pre><code>def to(self, device):
    super().to(device)
    if self._model is not None:
        self._model = self._model.to(self.device)
    return self</code></pre></details>
<h3 id="fastvideo.eval.metrics.audio.kl_divergence.metric.KLDivergenceMetric.setup"><code>fastvideo.eval.metrics.audio.kl_divergence.metric.KLDivergenceMetric.setup</code></h3>
<pre class="doc-signature"><code>setup() -&gt; None</code></pre>

<details><summary>Source</summary><pre><code>def setup(self) -&gt; None:
    if self._model is not None:
        return
    from hear21passt.base import get_basic_model
    model = get_basic_model(mode=&quot;logits&quot;)
    model.eval()
    model = model.to(self.device)
    self._model = model</code></pre></details>
<h3 id="fastvideo.eval.metrics.audio.kl_divergence.metric.KLDivergenceMetric.compute"><code>fastvideo.eval.metrics.audio.kl_divergence.metric.KLDivergenceMetric.compute</code></h3>
<pre class="doc-signature"><code>compute(sample: dict) -&gt; MetricResult</code></pre>

<details><summary>Source</summary><pre><code>@torch.no_grad()
def compute(self, sample: dict) -&gt; MetricResult:
    if self._model is None:
        self.setup()
    gen_path = sample.get(&quot;audio&quot;)
    ref_path = sample.get(&quot;reference_audio&quot;)
    if gen_path is None or ref_path is None:
        missing = &#91;k for k, v in ((&quot;audio&quot;, gen_path), (&quot;reference_audio&quot;, ref_path)) if v is None&#93;
        return self._skip(sample, f&quot;missing &#123;&#x27;, &#x27;.join(repr(k) for k in missing)&#125;&quot;)

    gt_logits = _collect_logits(self._model, ref_path, self.device)
    pred_logits = _collect_logits(self._model, gen_path, self.device)
    if not torch.isfinite(gt_logits).all() or not torch.isfinite(pred_logits).all():
        which = &#91;&#93;
        if not torch.isfinite(gt_logits).all():
            which.append(&quot;reference_audio&quot;)
        if not torch.isfinite(pred_logits).all():
            which.append(&quot;audio&quot;)
        return self._skip(
            sample,
            f&quot;non-finite PaSST logits on &#123;&#x27;+&#x27;.join(which)&#125; (silent or corrupt audio?)&quot;,
        )
    kl_sm = _kl_softmax(pred_logits, gt_logits)
    kl_sg = _kl_sigmoid(pred_logits, gt_logits)
    return MetricResult(
        name=self.name,
        score=kl_sm,
        details=&#123;
            &quot;kl_softmax&quot;: kl_sm,
            &quot;kl_sigmoid&quot;: kl_sg
        &#125;,
    )</code></pre></details>
</div>
