Skip to content
Merged
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
19 changes: 18 additions & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ PR commit:

<!-- Paste build/speed.md here -->

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.

<details>
<summary>Commands</summary>
Expand Down Expand Up @@ -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
# <base> is the base commit; <cases> are names from the table. Use the same --gpu, -c and -n as above.
# If ../mfc-<base> is too old to accept --cases, leave it out to rerun every case.
(
set -e
rm -f build/rerun.md
for tree in ../mfc-<base> .; do
(cd $tree &&
rm -f build/rerun.yaml &&
./mfc.sh bench --cases <cases> --mem 4 -o build/rerun.yaml -- --gpu acc -n 1)
done
GITHUB_STEP_SUMMARY=build/rerun.md ./mfc.sh bench_diff ../mfc-<base>/build/rerun.yaml build/rerun.yaml
)
```

</details>
7 changes: 7 additions & 0 deletions toolchain/mfc/bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions toolchain/mfc/cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
],
)

Expand Down
78 changes: 78 additions & 0 deletions toolchain/mfc/test_bench_cases.py
Original file line number Diff line number Diff line change
@@ -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 == []
Loading