File size: 9,938 Bytes
5181156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
951f760
 
5181156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4fce92
5181156
951f760
 
 
5181156
 
 
 
951f760
 
 
5181156
 
 
 
951f760
 
 
5181156
 
 
 
d4fce92
951f760
 
5181156
 
 
 
 
d4fce92
 
fe99817
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4fce92
 
 
566c9ef
5181156
 
 
 
 
 
566c9ef
 
5181156
 
 
566c9ef
5181156
 
 
 
 
 
 
 
 
 
566c9ef
5181156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4fce92
 
 
 
 
 
 
 
 
 
 
 
 
566c9ef
 
d4fce92
5181156
 
 
 
 
 
 
 
 
 
 
d4fce92
 
 
 
 
669b183
d4fce92
669b183
 
d4fce92
669b183
 
 
 
 
d4fce92
 
 
 
 
 
 
566c9ef
d4fce92
 
 
 
 
 
 
 
566c9ef
 
 
 
 
 
d4fce92
5181156
 
 
 
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""Hyena stack benchmark — measure TPS under the four knob combinations.



Produces the table requested in Task 4:

  | Config                     | TPS  | BPB@500 | VRAM |

  |----------------------------|------|---------|------|

  | B=8,  no flash, no cache   | ...  | ...     | ...  |  <-- baseline

  | B=16, no flash, no cache   | ...

  | B=16, no flash, cache on   | ...

  | B=16, flash on, cache on   | ...  | ...     | ...  |  <-- best



Run ONE config by invoking with command-line args, then collate externally.

Each invocation runs train.py for the specified wall-clock time with the

given env overrides, tails run.log, and emits a single summary line.



Invocation:

    cd /home/mikeb/work/feather



    # On the RTX 3060 (local validation only — these numbers will NOT hit

    # the 200k tps production floor):

    .venv/bin/python scripts/benchmark_hyena_stack.py --config baseline --time 300

    .venv/bin/python scripts/benchmark_hyena_stack.py --config b16     --time 300

    .venv/bin/python scripts/benchmark_hyena_stack.py --config cache   --time 300

    # "kernel" config requires flashfftconv built — see kernels/cuda/flashfftconv/README.md

    .venv/bin/python scripts/benchmark_hyena_stack.py --config kernel  --time 300



    # On A100/A10G (production cloud hardware), use time=900 (15 min) for

    # stable steady-state numbers.



After each run the script prints:

    BENCHMARK config=<name> tps_steady=<avg> bpb_at_500=<val> vram_peak=<MiB>



Collate those lines into the matrix table manually, then pick the winner

for the 6-hour production run (HYDRA_TIME_BUDGET=21600).

"""

from __future__ import annotations

import argparse
import os
import re
import subprocess
import sys
from pathlib import Path

REPO = Path(__file__).resolve().parents[1]


CONFIGS = {
    # Baseline: B=8, no flash, no train-cache. Current reference point.
    "baseline": {
        "HYDRA_BATCH_SIZE": "8",
        "HYDRA_HYENA_LAYERS": "3,7",
        "HYDRA_HYENA_FLASH_FFT": "0",
        "HYDRA_HYENA_TRAIN_CACHE": "0",
        "HYDRA_HYENA_FILTER_CACHE": "0",
    },
    "b16": {
        "HYDRA_BATCH_SIZE": "16",
        "HYDRA_HYENA_LAYERS": "3,7",
        "HYDRA_HYENA_FLASH_FFT": "0",
        "HYDRA_HYENA_TRAIN_CACHE": "0",
        "HYDRA_HYENA_FILTER_CACHE": "0",
    },
    "cache": {
        "HYDRA_BATCH_SIZE": "16",
        "HYDRA_HYENA_LAYERS": "3,7",
        "HYDRA_HYENA_FLASH_FFT": "0",
        "HYDRA_HYENA_TRAIN_CACHE": "1",
        "HYDRA_HYENA_FILTER_CACHE": "1",
    },
    "kernel": {
        "HYDRA_BATCH_SIZE": "16",
        "HYDRA_HYENA_LAYERS": "3,7",
        "HYDRA_HYENA_FLASH_FFT": "1",
        "HYDRA_HYENA_TRAIN_CACHE": "1",
        "HYDRA_HYENA_FILTER_CACHE": "1",
        # Task 4 note: also bump HYDRA_HTM_SUBSAMPLE to 128 (from 64) in the
        # best config to get more aggressive reclamation.
        "HYDRA_HTM_SUBSAMPLE": "128",
    },
    # A10 diagnostic profiles. These are intentionally explicit so that a
    # benchmark result says exactly which expensive subsystem was active.
    "a10_full": {
        "HYDRA_BATCH_SIZE": "8",
        "HYDRA_HYENA_LAYERS": "3,7",
        "HYDRA_HYENA_FLASH_FFT": "0",
        "HYDRA_HYENA_TRAIN_CACHE": "0",
        "HYDRA_HYENA_FILTER_CACHE": "0",
        "HYDRA_ABLATE_HTM": "0",
        "HYDRA_ABLATE_MIDSTACK": "0",
        "HYDRA_ABLATE_SDR_PROJECT": "0",
        "HYDRA_DISABLE_TRITON_SDR_PROJECT": "1",
        "HYDRA_FORCE_HTM_CPU": "0",
    },
    "a10_no_htm": {
        "HYDRA_BATCH_SIZE": "8",
        "HYDRA_HYENA_LAYERS": "3,7",
        "HYDRA_HYENA_FLASH_FFT": "0",
        "HYDRA_HYENA_TRAIN_CACHE": "0",
        "HYDRA_HYENA_FILTER_CACHE": "0",
        "HYDRA_ABLATE_HTM": "1",
        "HYDRA_ABLATE_MIDSTACK": "0",
        "HYDRA_ABLATE_SDR_PROJECT": "0",
        "HYDRA_DISABLE_TRITON_SDR_PROJECT": "1",
        "HYDRA_FORCE_HTM_CPU": "0",
    },
    "a10_no_htm_no_sdr": {
        "HYDRA_BATCH_SIZE": "8",
        "HYDRA_HYENA_LAYERS": "3,7",
        "HYDRA_HYENA_FLASH_FFT": "0",
        "HYDRA_HYENA_TRAIN_CACHE": "0",
        "HYDRA_HYENA_FILTER_CACHE": "0",
        "HYDRA_ABLATE_HTM": "1",
        "HYDRA_ABLATE_MIDSTACK": "0",
        "HYDRA_ABLATE_SDR_PROJECT": "1",
        "HYDRA_DISABLE_TRITON_SDR_PROJECT": "1",
        "HYDRA_FORCE_HTM_CPU": "0",
    },
    "a10_no_htm_no_midstack": {
        "HYDRA_BATCH_SIZE": "8",
        "HYDRA_HYENA_LAYERS": "3,7",
        "HYDRA_HYENA_FLASH_FFT": "0",
        "HYDRA_HYENA_TRAIN_CACHE": "0",
        "HYDRA_HYENA_FILTER_CACHE": "0",
        "HYDRA_ABLATE_HTM": "1",
        "HYDRA_ABLATE_MIDSTACK": "1",
        "HYDRA_ABLATE_SDR_PROJECT": "1",
        "HYDRA_DISABLE_TRITON_SDR_PROJECT": "1",
        "HYDRA_FORCE_HTM_CPU": "0",
    },
    "a10_all_hyena_fast": {
        "HYDRA_BATCH_SIZE": "4",
        "HYDRA_TOTAL_BATCH": "4096",
        "HYDRA_HYENA_LAYERS": "0,1,2,3",
        "HYDRA_HYENA_FLASH_FFT": "0",
        "HYDRA_HYENA_TRAIN_CACHE": "0",
        "HYDRA_HYENA_FILTER_CACHE": "0",
        "HYDRA_ABLATE_HTM": "1",
        "HYDRA_ABLATE_MIDSTACK": "1",
        "HYDRA_ABLATE_SDR_PROJECT": "1",
        "HYDRA_DISABLE_TRITON_SDR_PROJECT": "1",
        "HYDRA_FORCE_HTM_CPU": "0",
    },
}


def build_env(cfg_overrides: dict[str, str]) -> dict[str, str]:
    """Compose a full env dict from the inherited env + config overrides."""
    env = os.environ.copy()
    # Ensure the Hyena layer selection is always present (defaults to off).
    env.setdefault("HYDRA_HYENA_LAYERS", "")
    for k, v in cfg_overrides.items():
        env[k] = v
    if "HYDRA_BENCH_HYENA_LAYERS" in env:
        env["HYDRA_HYENA_LAYERS"] = env["HYDRA_BENCH_HYENA_LAYERS"]
    return env


def parse_step_line(line: str) -> dict[str, float] | None:
    """Parse a single step=... line into a dict of metrics, or None."""
    if not line.startswith("step="):
        return None
    parts = re.findall(r"(\w+)=([0-9.eE+\-]+)", line)
    try:
        return {k: float(v) for k, v in parts}
    except ValueError:
        return None


def summarize(log_path: Path, warmup_steps: int = 50) -> dict[str, float]:
    """Tail log_path, compute steady-state TPS / BPB@500 / VRAM peak.



    Skips the first `warmup_steps` to discard CUDA graph capture / autotune

    spikes; takes the median of the rest.

    """
    tps_vals = []
    bpbs = []
    vram_peak = 0.0
    bpb_at_500 = None
    with log_path.open() as f:
        for line in f:
            d = parse_step_line(line.strip())
            if d is None:
                continue
            step = int(d.get("step", -1))
            if step < warmup_steps:
                continue
            tps = d.get("tps")
            if tps is not None:
                tps_vals.append(tps)
            bpb = d.get("bpb")
            if bpb is not None:
                bpbs.append(bpb)
                if step == 500 and bpb_at_500 is None:
                    bpb_at_500 = bpb
            vram = d.get("vram")
            if vram is not None and vram > vram_peak:
                vram_peak = vram

    if not tps_vals:
        return {"tps_steady": 0.0, "bpb_at_500": 0.0, "vram_peak": 0.0, "steps": 0}

    tps_sorted = sorted(tps_vals)
    tps_steady = tps_sorted[len(tps_sorted) // 2]  # median

    return {
        "tps_steady": tps_steady,
        "bpb_at_500": bpb_at_500 or (bpbs[-1] if bpbs else 0.0),
        "vram_peak": vram_peak,
        "steps": len(tps_vals) + warmup_steps,
    }


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--config", required=True, choices=list(CONFIGS))
    ap.add_argument("--time", type=int, default=300, help="training seconds")
    ap.add_argument("--log", default=None, help="output log path (default: run_bench_<cfg>.log)")
    ap.add_argument("--min-tps", type=float, default=0.0, help="fail if steady-state TPS is below this threshold")
    ap.add_argument("--warmup-steps", type=int, default=50, help="steps to ignore before computing steady-state TPS")
    args = ap.parse_args()

    cfg = CONFIGS[args.config]
    log_path = Path(args.log or (REPO / f"run_bench_{args.config}.log"))

    env = build_env(cfg)
    env["HYDRA_TIME_BUDGET"] = str(args.time)

    # Make the config visible up-front so failed runs are debuggable.
    print(f"BENCH start config={args.config} time={args.time}s log={log_path}", flush=True)
    print(f"  overrides: {cfg}", flush=True)

    with log_path.open("w") as logf:
        proc = subprocess.Popen(
            ["python", "-u", str(REPO / "train.py")],
            env=env,
            cwd=str(REPO),
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            text=True,
            bufsize=1,
        )
        assert proc.stdout is not None
        for line in proc.stdout:
            logf.write(line)
            logf.flush()
            print(line, end="", flush=True)
        proc.wait()

    print(f"BENCH wait_done exit={proc.returncode}", flush=True)
    if proc.returncode != 0:
        print(f"BENCH FAIL config={args.config}", flush=True)
        return proc.returncode

    summary = summarize(log_path, warmup_steps=args.warmup_steps)
    print(
        f"BENCHMARK config={args.config} "
        f"tps_steady={summary['tps_steady']:.0f} "
        f"bpb_at_500={summary['bpb_at_500']:.4f} "
        f"vram_peak={summary['vram_peak']:.0f}MiB "
        f"steps={summary['steps']}",
        flush=True,
    )
    if args.min_tps > 0 and summary["tps_steady"] < args.min_tps:
        print(
            f"BENCH FAIL config={args.config} tps_steady={summary['tps_steady']:.0f} min_tps={args.min_tps:.0f}",
            flush=True,
        )
        return 2
    return 0


if __name__ == "__main__":
    sys.exit(main())