File size: 3,679 Bytes
902d2bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: CC-BY-NC-4.0
"""The SOL score and the speedup, from per-workload times (rows from harness.parse_trace)."""
from __future__ import annotations

import json
import math

# The benchmark publishes two times per workload: t_b, an optimized PyTorch baseline, and t_sol,
# the speed-of-light bound on a B200. They are NVIDIA's data, so they are not in this package.
# Pass your own copy as anchors = {workload uuid: {"t_b_ms": ..., "t_sol_ms": ...}}. Without
# anchors the reward falls back to the harness's own reference time, or to 1 / latency.


def sol_score(t_k: float, t_b: float, t_sol: float) -> float | None:
    """S = (t_b - t_sol) / ((t_k - t_sol) + (t_b - t_sol)): 0.5 at the baseline, 1.0 at the bound."""
    gap = t_b - t_sol
    if gap <= 0:
        return 1.0 if t_k <= t_sol else 0.0
    den = (t_k - t_sol) + gap
    return gap / den if den > 0 else None


def geomean(values) -> float | None:
    """The geometric mean of the positive values, or None if there are none."""
    xs = [float(v) for v in values if v is not None and v > 0]
    if not xs:
        return None
    return math.exp(sum(math.log(x) for x in xs) / len(xs))


def load_anchors(path: str | None) -> dict | None:
    """Your copy of the published per-workload times (see the comment above), or None."""
    if not path:
        return None
    with open(path, encoding="utf-8") as f:
        return json.load(f)


def _passed(rows):
    return [r for r in rows if r.get("status") == "PASSED" and (r.get("latency_ms") or 0) > 0]


def problem_score(rows, anchors: dict | None) -> float | None:
    """The problem's SOL score: the arithmetic mean over its workloads; a failed workload is 0."""
    if not anchors or not rows:
        return None
    scores = []
    for r in rows:
        a = anchors.get(r.get("uuid") or "")
        if not a or a.get("t_b_ms") is None or a.get("t_sol_ms") is None:
            return None                     # every workload needs its two published times
        ok = r.get("status") == "PASSED" and (r.get("latency_ms") or 0) > 0
        s = sol_score(r["latency_ms"], a["t_b_ms"], a["t_sol_ms"]) if ok else 0.0
        scores.append(s or 0.0)
    return sum(scores) / len(scores)


def reward(rows, anchors: dict | None = None) -> tuple:
    """(value, basis): the geometric-mean speedup t_base / t_k over the workloads."""
    # basis "baseline": t_b from your anchors (the paper's reward); "reference": the reference
    # time the harness measures with benchmark_reference on; "latency": t_base = 1. For correct
    # kernels (every workload passed) the three rank candidates alike: a fixed t_base per
    # workload only scales the geometric mean (the measured reference time adds some noise).
    ok = _passed(rows)
    if not ok:
        return None, None
    if anchors and all((anchors.get(r.get("uuid") or "") or {}).get("t_b_ms") for r in ok):
        return geomean(anchors[r["uuid"]]["t_b_ms"] / r["latency_ms"] for r in ok), "baseline"
    if all(r.get("reference_latency_ms") for r in ok):
        return geomean(r["reference_latency_ms"] / r["latency_ms"] for r in ok), "reference"
    return geomean(1.0 / r["latency_ms"] for r in ok), "latency"


def flags(rows, anchors: dict | None = None) -> list:
    """["below_sol"] if a workload ran faster than its speed-of-light bound: not a real result."""
    if not anchors:
        return []
    for r in _passed(rows):
        t_sol = (anchors.get(r.get("uuid") or "") or {}).get("t_sol_ms")
        if t_sol is not None and r["latency_ms"] < t_sol:
            return ["below_sol"]
    return []