File size: 1,258 Bytes
3da88e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Counterbalanced timings and macOS physical footprint, without another GPU runtime."""

from __future__ import annotations

import statistics
import time

import mlx.core as mx


def paired(control, candidate, repeats=6, warmup=2):
    for _ in range(warmup):
        mx.eval(control())
        mx.eval(candidate())
    samples = {"control": [], "candidate": []}
    order = []
    for i in range(repeats):
        names = ("control", "candidate") if i % 2 == 0 else ("candidate", "control")
        for name in names:
            fn = control if name == "control" else candidate
            mx.synchronize()
            start = time.perf_counter()
            value = fn()
            mx.eval(value)
            mx.synchronize()
            samples[name].append((time.perf_counter() - start) * 1000)
            order.append(name)
            del value
    result = {"order": order}
    for name, values in samples.items():
        result[name] = {
            "median_ms": statistics.median(values),
            "min_ms": min(values),
            "max_ms": max(values),
            "samples_ms": values,
        }
    result["reduction_pct"] = 100 * (
        1 - result["candidate"]["median_ms"] / result["control"]["median_ms"]
    )
    return result