From 53bceded76728295b336b01bd7f4333a3e8874b8 Mon Sep 17 00:00:00 2001 From: Spencer Bryngelson Date: Thu, 26 Nov 2026 17:57:00 -0600 Subject: [PATCH] bench: add --cases to run chosen benchmark cases The PR template shows how to rerun a case whose grind speedup is below the threshold. --- .github/pull_request_template.md | 19 +++++++- toolchain/mfc/bench.py | 7 +++ toolchain/mfc/cli/commands.py | 10 ++++ toolchain/mfc/test_bench_cases.py | 78 +++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 toolchain/mfc/test_bench_cases.py diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index f83180f47e..4ecfa8038d 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -56,7 +56,7 @@ PR commit: -Explain any grind speedup below 0.95 that shows up twice. If no benchmark case uses your change, add master and PR grind times for a case that does. +Explain any grind speedup below 0.95 that shows up twice: rerun those cases with the second command below and paste that table too. If no benchmark case uses your change, add master and PR grind times for a case that does.
Commands @@ -86,4 +86,21 @@ echo "Base commit: $base"; echo "PR commit: $(git rev-parse --short HEAD)" ) ``` +To rerun cases, then paste `build/rerun.md`: + +```bash +# is the base commit; are names from the table. Use the same --gpu, -c and -n as above. +# If ../mfc- is too old to accept --cases, leave it out to rerun every case. +( +set -e +rm -f build/rerun.md +for tree in ../mfc- .; do + (cd $tree && + rm -f build/rerun.yaml && + ./mfc.sh bench --cases --mem 4 -o build/rerun.yaml -- --gpu acc -n 1) +done +GITHUB_STEP_SUMMARY=build/rerun.md ./mfc.sh bench_diff ../mfc-/build/rerun.yaml build/rerun.yaml +) +``` +
diff --git a/toolchain/mfc/bench.py b/toolchain/mfc/bench.py index c5aedc42da..a71a8c3313 100644 --- a/toolchain/mfc/bench.py +++ b/toolchain/mfc/bench.py @@ -61,6 +61,13 @@ def bench(targets=None): CASES = [BenchCase(**case) for case in file_load_yaml(MFC_BENCH_FILEPATH)] + if ARG("cases"): + slugs = [case.slug for case in CASES] + unknown = [name for name in ARG("cases") if name not in slugs] + if unknown: + raise MFCException(f"Unknown benchmark case: {', '.join(unknown)}. Choose from: {', '.join(slugs)}") + CASES = [case for case in CASES if case.slug in ARG("cases")] + for case in CASES: case.args = case.args + ARG("--") case.path = os.path.abspath(case.path) diff --git a/toolchain/mfc/cli/commands.py b/toolchain/mfc/cli/commands.py index 984a7c333e..1db25b7700 100644 --- a/toolchain/mfc/cli/commands.py +++ b/toolchain/mfc/cli/commands.py @@ -858,13 +858,23 @@ default=1, metavar="MEM", ), + Argument( + name="cases", + help="Only run these cases (names from toolchain/bench.yaml).", + nargs="+", + type=str, + default=[], + metavar="CASE", + ), ], examples=[ Example("./mfc.sh bench -o results.yaml", "Run benchmarks and save results"), + Example("./mfc.sh bench -o rerun.yaml --cases ibm igr", "Rerun two cases"), ], key_options=[ ("-o, --output FILE", "Output file for benchmark results (required)"), ("-m, --mem SIZE", "Memory limit for benchmarks"), + ("--cases CASE ...", "Only run these cases"), ], ) diff --git a/toolchain/mfc/test_bench_cases.py b/toolchain/mfc/test_bench_cases.py new file mode 100644 index 0000000000..40e01c9642 --- /dev/null +++ b/toolchain/mfc/test_bench_cases.py @@ -0,0 +1,78 @@ +"""bench --cases reruns chosen benchmark cases. + +The PR template asks an author to rerun a case whose grind speedup is below the +threshold. Running all of bench.yaml again for one case is far slower than +needed. +""" + +from pathlib import Path + +import pytest +import yaml + +from mfc import bench, state +from mfc.cli.argparse_gen import generate_parser +from mfc.cli.commands import MFC_CLI_SCHEMA +from mfc.common import MFC_BENCH_FILEPATH, MFCException + +REPO = Path(__file__).resolve().parents[2] +ALL = [case["slug"] for case in yaml.safe_load(Path(MFC_BENCH_FILEPATH).read_text())] + + +def _parse(*argv): + parser, _ = generate_parser(MFC_CLI_SCHEMA, state.MFCConfig()) + return vars(parser.parse_args(["bench", "-o", "out.yaml", *argv])) + + +def _bench(tmp_path, monkeypatch, *argv): + """Run bench() as the command line would, with the case runs faked. + + Returns the slugs that were run and the slugs in the results file. + """ + ran = [] + + def fake_run(command, **_): + ran.append(Path(command[2]).parent.name) + summary = Path(command[command.index("--output-summary") + 1]) + summary.write_text(yaml.safe_dump({"simulation": {"exec": 1.0, "grind": 1.0}})) + return 0 + + monkeypatch.chdir(REPO) + monkeypatch.setattr(bench, "system", fake_run) + monkeypatch.setattr(bench, "MFC_BUILD_DIR", str(tmp_path)) + output = tmp_path / "out.yaml" + args = {**_parse(*argv), "targets": ["simulation"], "output": str(output), "--": []} + for key, value in args.items(): + monkeypatch.setitem(state.gARG, key, value) + + bench.bench() + + return ran, list(yaml.safe_load(output.read_text())["cases"]) + + +def test_runs_every_case_by_default(tmp_path, monkeypatch): + assert _bench(tmp_path, monkeypatch) == (ALL, sorted(ALL)) + + +def test_runs_only_the_named_cases(tmp_path, monkeypatch): + ran, results = _bench(tmp_path, monkeypatch, "--cases", "igr", "ibm") + + # bench.yaml order, whatever order they were named in. + assert ran == ["ibm", "igr"] + assert results == ["ibm", "igr"] + + +def test_an_unknown_case_fails_before_anything_runs(tmp_path, monkeypatch): + ran = [] + monkeypatch.setattr(bench, "system", lambda *_, **__: ran.append(1)) + + with pytest.raises(MFCException) as error: + monkeypatch.chdir(REPO) + monkeypatch.setattr(bench, "MFC_BUILD_DIR", str(tmp_path)) + for key, value in {**_parse("--cases", "ibm", "hllc"), "targets": ["simulation"], "--": []}.items(): + monkeypatch.setitem(state.gARG, key, value) + bench.bench() + + assert "hllc" in str(error.value) + assert all(slug in str(error.value) for slug in ALL) + assert ran == []