Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tests/contract/test_plain_baseline_decode_tps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""plain_baseline decode_tps: -n 1 must not divide by zero (#63)."""
import importlib.util
from pathlib import Path

import pytest

_SPEC = importlib.util.spec_from_file_location(
"plain_baseline",
Path(__file__).resolve().parents[2] / "tools" / "bench" / "plain_baseline.py",
)
_mod = importlib.util.module_from_spec(_SPEC)
# Load only the helper: the module imports mlx/etc at main() time, not at import.
# Executing the file still pulls `from spec_bench import PROMPTS` — stub if needed.
import sys
from types import ModuleType

if "spec_bench" not in sys.modules:
stub = ModuleType("spec_bench")
stub.PROMPTS = {}
sys.modules["spec_bench"] = stub

_SPEC.loader.exec_module(_mod)
decode_tps = _mod.decode_tps


@pytest.mark.parametrize(
"decode_tokens,decode_wall_ms,expected",
[
(0, 0.0, 0.0), # -n 1: prefill-only, both counters stay zero
(0, 12.5, 0.0), # no decode tokens
(8, 0.0, 0.0), # zero wall (should not raise)
(100, 1000.0, 100.0), # 100 tokens in 1 s → 100 tok/s
(50, 500.0, 100.0),
],
)
def test_decode_tps(decode_tokens, decode_wall_ms, expected):
assert decode_tps(decode_tokens, decode_wall_ms) == pytest.approx(expected)
12 changes: 11 additions & 1 deletion tools/bench/plain_baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
from spec_bench import PROMPTS # noqa: E402



def decode_tps(decode_tokens: int, decode_wall_ms: float) -> float:
"""Wall tok/s over the decode phase. Prefill-only runs (-n 1) skip decode, so both
counters stay at 0; return 0.0 instead of dividing by zero.
"""
if decode_tokens <= 0 or decode_wall_ms <= 0:
return 0.0
return decode_tokens / (decode_wall_ms / 1e3)


def mlx_generate(model, tokenizer, ids, n):
"""(tokens, wall tok/s) of one greedy mlx-lm generation over the prompt ids (the stream's last response carries the
generation rate over the whole decode)."""
Expand Down Expand Up @@ -105,7 +115,7 @@ def main() -> int:
for who in order:
if who == "ours":
gen = sess.generate(ids, a.max_new_tokens)
tps = gen.decode_tokens / (gen.decode_wall_ms / 1e3)
tps = decode_tps(gen.decode_tokens, gen.decode_wall_ms)
rec = {"tps": tps, "gpu_ms_per_token": gen.ms_per_token, "wall_ms_per_token": gen.decode_wall_ms / max(1, gen.decode_tokens),
"host_pct": 100 * gen.host_busy_ms / max(gen.decode_wall_ms, 1e-9)}
toks["ours"] = gen.tokens
Expand Down