File size: 7,203 Bytes
a84640a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python3
"""Mobile performance profiler for the Hermes model (pre-conversion).

Measures the PyTorch reference model's throughput and memory at batch size 1
(the mobile serving target) so you can compare presets and sequence lengths
*before* spending time on LiteRT conversion. Numbers here are an upper bound on
device behaviour β€” the INT4 ``.litertlm`` graph will differ β€” but the relative
ordering between presets/lengths is a useful proxy.

Reported per sequence length:

* **Prefill tokens/sec** β€” throughput of the single forward pass over the prompt.
* **Decode tokens/sec** β€” throughput of incremental single-token generation
  (KV-cache reuse), the metric users feel during streaming.
* **Time-to-first-token (ms)** β€” prefill latency for the prompt.
* **Peak RSS (MB)** β€” process resident memory via ``psutil``.
* **Peak VRAM (MB)** β€” ``torch.cuda.max_memory_allocated`` when on GPU.

Example::

    python scripts/benchmark.py --preset hermes-270m \
        --seq-lens 64 128 256 512 1024 --runs 5

Prints a markdown table and writes ``benchmark_results.json``.
"""

from __future__ import annotations

import argparse
import json
import os
import sys
import time
from typing import Dict, List, Optional

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import torch  # noqa: E402

from hermes.config import HermesConfig, get_config  # noqa: E402
from hermes.inference import HermesInference  # noqa: E402
from hermes.model import build_model  # noqa: E402

try:
    import psutil  # noqa: E402

    _HAS_PSUTIL = True
except ImportError:  # pragma: no cover - psutil is a declared dependency
    _HAS_PSUTIL = False


def _peak_rss_mb() -> Optional[float]:
    """Current process resident set size in MB, or None if psutil is absent."""
    if not _HAS_PSUTIL:
        return None
    return psutil.Process(os.getpid()).memory_info().rss / (1024 * 1024)


def _reset_vram(device: torch.device) -> None:
    if device.type == "cuda":
        torch.cuda.reset_peak_memory_stats(device)
        torch.cuda.synchronize(device)


def _peak_vram_mb(device: torch.device) -> Optional[float]:
    if device.type == "cuda":
        return torch.cuda.max_memory_allocated(device) / (1024 * 1024)
    return None


@torch.no_grad()
def benchmark_seq_len(
    engine: HermesInference,
    seq_len: int,
    runs: int,
    decode_tokens: int,
    device: torch.device,
) -> Dict[str, float]:
    """Time prefill + decode for one sequence length, averaged over ``runs``."""
    model = engine.model
    vocab = engine.config.vocab_size
    _reset_vram(device)

    prefill_times: List[float] = []
    decode_times: List[float] = []

    for _ in range(runs):
        prompt_ids = torch.randint(0, vocab, (1, seq_len), device=device)

        # --- Prefill: single forward over the whole prompt. ---
        caches = [None] * len(model.layers)
        if device.type == "cuda":
            torch.cuda.synchronize(device)
        t0 = time.perf_counter()
        logits, caches = engine._forward_with_cache(prompt_ids, caches, start_pos=0)
        if device.type == "cuda":
            torch.cuda.synchronize(device)
        prefill_times.append(time.perf_counter() - t0)

        # --- Decode: incremental single-token steps reusing the KV cache. ---
        pos = seq_len
        next_id = logits.argmax(dim=-1, keepdim=True)
        if device.type == "cuda":
            torch.cuda.synchronize(device)
        t0 = time.perf_counter()
        for _ in range(decode_tokens):
            step_logits, caches = engine._forward_with_cache(next_id, caches, start_pos=pos)
            next_id = step_logits.argmax(dim=-1, keepdim=True)
            pos += 1
            if pos >= engine.config.max_seq_len:
                break
        if device.type == "cuda":
            torch.cuda.synchronize(device)
        decode_times.append(time.perf_counter() - t0)

    avg_prefill = sum(prefill_times) / len(prefill_times)
    avg_decode = sum(decode_times) / len(decode_times)
    decoded = min(decode_tokens, max(1, engine.config.max_seq_len - seq_len))

    return {
        "seq_len": seq_len,
        "ttft_ms": avg_prefill * 1000.0,
        "prefill_tok_per_s": seq_len / avg_prefill if avg_prefill > 0 else 0.0,
        "decode_tok_per_s": decoded / avg_decode if avg_decode > 0 else 0.0,
        "peak_rss_mb": _peak_rss_mb() or 0.0,
        "peak_vram_mb": _peak_vram_mb(device) or 0.0,
    }


class _NullTokenizer:
    """Placeholder tokenizer β€” benchmark only needs the model, not real text."""

    def encode(self, text: str) -> List[int]:
        return [1]

    def decode(self, ids: List[int]) -> str:
        return ""


def render_table(rows: List[Dict[str, float]]) -> str:
    """Format benchmark rows as a markdown table."""
    header = (
        "| seq_len | TTFT (ms) | prefill tok/s | decode tok/s | "
        "peak RSS (MB) | peak VRAM (MB) |"
    )
    sep = "|---|---|---|---|---|---|"
    lines = [header, sep]
    for r in rows:
        lines.append(
            f"| {int(r['seq_len'])} | {r['ttft_ms']:.1f} | "
            f"{r['prefill_tok_per_s']:.1f} | {r['decode_tok_per_s']:.1f} | "
            f"{r['peak_rss_mb']:.1f} | {r['peak_vram_mb']:.1f} |"
        )
    return "\n".join(lines)


def run(args: argparse.Namespace) -> int:
    device = torch.device(args.device)
    config: HermesConfig = get_config(args.preset)
    model = build_model(config)
    engine = HermesInference(model, _NullTokenizer(), device=device, preset_name=args.preset)
    print(engine)

    rows: List[Dict[str, float]] = []
    for seq_len in args.seq_lens:
        if seq_len >= config.max_seq_len:
            print(f"Skipping seq_len={seq_len} (>= max_seq_len={config.max_seq_len}).")
            continue
        print(f"Benchmarking seq_len={seq_len} ...")
        rows.append(
            benchmark_seq_len(engine, seq_len, args.runs, args.decode_tokens, device)
        )

    table = render_table(rows)
    print("\n" + table + "\n")

    results = {
        "preset": args.preset,
        "device": args.device,
        "runs": args.runs,
        "decode_tokens": args.decode_tokens,
        "param_count": sum(p.numel() for p in model.parameters()),
        "psutil_available": _HAS_PSUTIL,
        "rows": rows,
    }
    with open(args.output, "w", encoding="utf-8") as f:
        json.dump(results, f, indent=2)
    print(f"Saved {args.output}")
    return 0


def parse_args(argv=None) -> argparse.Namespace:
    p = argparse.ArgumentParser(description="Benchmark Hermes model speed/memory.")
    p.add_argument("--preset", default="hermes-270m", choices=["hermes-1b", "hermes-500m", "hermes-270m"])
    p.add_argument("--seq-lens", type=int, nargs="+", default=[64, 128, 256, 512, 1024])
    p.add_argument("--runs", type=int, default=5, help="Repeats per sequence length.")
    p.add_argument("--decode-tokens", type=int, default=32, help="Tokens to time during decode.")
    p.add_argument("--device", default="cpu", help="Torch device (cpu, cuda, cuda:0, ...).")
    p.add_argument("--output", default="benchmark_results.json")
    return p.parse_args(argv)


if __name__ == "__main__":
    sys.exit(run(parse_args()))