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
72 changes: 61 additions & 11 deletions scripts/pr_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@
FRAMES_KEPT = 0.8 # the PR's frames at least this share of main's
COMMIT_GROWTH = 1.25 # the PR's peak commit at most this times main's
# the lines a smoke block needs to be compared with the other (the crash and device-removal
# counts are checked where they are used), and those of them that are numbers
SMOKE_FIELDS = ("title", "seconds", "frames shown")
# counts are checked where they are used), and those of them that are numbers; `gpu` because
# frames and memory of runs on two GPUs say nothing about each other (#2232)
SMOKE_FIELDS = ("title", "seconds", "frames shown", "gpu")
SMOKE_NUMBERS = ("seconds", "frames shown")


Expand Down Expand Up @@ -100,10 +101,30 @@ def section_has_content(body, section):
return False


def check(body, head, files, is_main_ancestor, *, test_only=False):
def gpu_name(block):
"""The block's `gpu:` line, with case and runs of whitespace not counting (#2232)."""
return " ".join((block.get("gpu") or "").split()).casefold()


def main_baseline(candidates, main_behind):
"""The main block the PR's run is compared with, of those with its title and time: the one of
the newest main commit when `main_behind(sha)` (commits main is ahead of `sha`) is given, so
pasting an older block first does not choose it (#2226); else, or when the distance of any of
them is unknown (a failed API call must not make an older block win), the first."""
if not candidates:
return None
if main_behind is None:
return candidates[0]
behind = [main_behind(b["commit"].split()[0]) for b in candidates]
if None in behind:
return candidates[0]
return candidates[behind.index(min(behind))]


def check(body, head, files, is_main_ancestor, *, test_only=False, main_behind=None):
"""The problems (empty: mergeable as far as this check goes). `files`: (path, status, binary,
size) with `size` a function giving the file's bytes; `is_main_ancestor(sha)`: whether the
commit is on main."""
commit is on main; `main_behind(sha)`: how many commits main is ahead of it (None: unknown)."""
body = body.replace("\r\n", "\n")
problems = []
for section in SECTIONS:
Expand Down Expand Up @@ -135,8 +156,14 @@ def check(body, head, files, is_main_ancestor, *, test_only=False):
if not clean:
problems.append(f"the PR changes the runtime: **Regression run** needs an `xcl-smoke` block made on this PR's "
f"head commit {head[:12]} with no uncommitted changes (rerun scripts/smoke.ps1 after every push)")
main = [b for b in base if is_main_ancestor(b["commit"].split()[0])]
if not main:
on_main = [b for b in base if is_main_ancestor(b["commit"].split()[0])]
# a run with local changes is not main, for the baseline as for the PR's run (#2226)
main = [b for b in on_main if "uncommitted" not in b.get("commit", "")]
if not main and on_main:
problems.append("the main regression run (`xcl-smoke` block of commit "
f"{on_main[0]['commit'].split()[0]}) has uncommitted changes: rerun scripts/smoke.ps1 "
"on a clean checkout of main")
elif not main:
problems.append("**Regression run** needs an `xcl-smoke` block made on a commit of main (not the PR's), "
"with the same title and time as the PR's")
if clean:
Expand All @@ -148,18 +175,27 @@ def check(body, head, files, is_main_ancestor, *, test_only=False):
if pr.get("exit", "").startswith("killed"):
problems.append("the PR's regression run did not end by itself (killed)")
if main:
m = next((b for b in main if b.get("title") == pr.get("title") and b.get("seconds") == pr.get("seconds")), None)
same_run = [b for b in main if b.get("title") == pr.get("title") and b.get("seconds") == pr.get("seconds")]
m = main_baseline([b for b in same_run if gpu_name(b) == gpu_name(pr)], main_behind)
for b in [m] if m else main:
problems += field_problems(b, "the main")
if m is None:
if m is None and same_run and pr.get("gpu"):
gpus = sorted({b.get("gpu") or "none" for b in same_run})
problems.append(f"the main and PR regression runs are not on the same GPU ({pr.get('gpu')} against "
f"{', '.join(gpus)}): run both on one machine")
elif m is None:
problems.append("the main and PR regression runs are not of the same title for the same time")
else:
# an old baseline is named where the comparison fails, not only in the log
n = main_behind(m["commit"].split()[0]) if main_behind else None
old = (f"; the main block is {n} commit(s) behind main: rerun it before comparing"
if n else "")
pf, mf = number(pr.get("frames shown")), number(m.get("frames shown"))
if mf and (pf is None or pf < FRAMES_KEPT * mf):
problems.append(f"the PR's run shows fewer frames than main's ({pf} against {mf})")
problems.append(f"the PR's run shows fewer frames than main's ({pf} against {mf}){old}")
pc, mc = number(pr.get("peak commit")), number(m.get("peak commit"))
if pc and mc and pc > COMMIT_GROWTH * mc:
problems.append(f"the PR's run commits much more memory than main's ({pc} MB against {mc} MB)")
problems.append(f"the PR's run commits much more memory than main's ({pc} MB against {mc} MB){old}")
return problems


Expand Down Expand Up @@ -193,7 +229,21 @@ def is_main_ancestor(sha):
r = subprocess.run(["gh", "api", f"repos/{o.repo}/compare/{sha}...main", "--jq", ".status"], capture_output=True, text=True)
return r.returncode == 0 and r.stdout.strip() in ("ahead", "identical")

problems = check(body, o.head, files, is_main_ancestor, test_only=o.test_only)
def main_behind(sha):
if o.local:
r = subprocess.run(["git", "rev-list", "--count", f"{sha}..origin/main"], capture_output=True, text=True)
else:
r = subprocess.run(["gh", "api", f"repos/{o.repo}/compare/{sha}...main", "--jq", ".ahead_by"], capture_output=True, text=True)
return int(r.stdout.strip()) if r.returncode == 0 and r.stdout.strip().isdigit() else None

problems = check(body, o.head, files, is_main_ancestor, test_only=o.test_only, main_behind=main_behind)
# how old the baseline is: shown, not judged (the limit is the maintainer's call, #2226)
for b in smoke_blocks(body.replace("\r\n", "\n")):
sha = (b.get("commit") or "").split()[0] if b.get("commit") else ""
if len(sha) >= 7 and not o.head.startswith(sha) and is_main_ancestor(sha):
n = main_behind(sha)
if n is not None:
print(f"main regression run {sha[:12]}: {n} commit(s) behind main")
if problems:
print("This PR cannot be merged yet (see CONTRIBUTING.md, 'What a PR needs to be merged'):")
for p in problems:
Expand Down
58 changes: 55 additions & 3 deletions scripts/test_pr_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_frontier_models_and_ordinary_words_pass(self):

class SmokeBlockTests(unittest.TestCase):
def test_two_blocks_in_one_fence_are_two_blocks(self):
run = "title: G\nseconds: 60\nframes shown: 100\ndevice removals: 0\ncrashes: 0\n"
run = "title: G\ngpu: GPU A\nseconds: 60\nframes shown: 100\ndevice removals: 0\ncrashes: 0\n"
body = ("```\nxcl-smoke v1\ncommit: 1111111aaaa\n" + run + "\n"
"xcl-smoke v1\ncommit: 1234567890\n" + run + "```\n")
blocks = smoke_blocks(body)
Expand All @@ -92,12 +92,64 @@ def smoke_problems(self, main_run, pr_run):

def test_blocks_with_only_commit_lines_fail(self):
problems = self.smoke_problems("", "")
for field in ("title", "seconds", "frames shown"):
for field in ("title", "seconds", "frames shown", "gpu"):
for run in ("the PR's", "the main"):
self.assertTrue(any(p.startswith(run) and f"no `{field}` line" in p for p in problems), problems)

RUN = "title: G\ngpu: {gpu}\nseconds: 60\ndevice removals: 0\ncrashes: 0\npeak commit: {} MB\nframes shown: {}\n"

def block(self, commit, peak, frames, gpu="GPU A"):
return f"```\nxcl-smoke v1\ncommit: {commit}\n" + self.RUN.format(peak, frames, gpu=gpu) + "```\n"

def test_runs_on_different_gpus_are_not_compared(self):
files = [changed("crates/xcl/src/hle/d3d12x/vram.rs")]
on_main = lambda sha: sha in ("1111111aaaa", "2222222bbbb")
# main only on a slower GPU: refused, though the PR's numbers would pass against it
body = self.block("1111111aaaa", 4000, 50, "GPU B") + self.block("1234567890", 4000, 100)
self.assertEqual(["the main and PR regression runs are not on the same GPU (GPU A against GPU B): "
"run both on one machine"], check(BODY + body, HEAD, files, on_main))
# a main run on the PR's GPU next to one on another: compared with the one on the same GPU
body = (self.block("1111111aaaa", 1000, 300, "GPU B") + self.block("2222222bbbb", 4000, 100)
+ self.block("1234567890", 4000, 100))
self.assertEqual([], check(BODY + body, HEAD, files, on_main))
# the same GPU written with other case or spacing is the same GPU
body = self.block("1111111aaaa", 4000, 100, "gpu a") + self.block("1234567890", 4000, 100)
self.assertEqual([], check(BODY + body, HEAD, files, on_main))
# a block without a gpu line is incomplete
body = self.block("1111111aaaa", 4000, 100).replace("gpu: GPU A\n", "") + self.block("1234567890", 4000, 100)
self.assertIn("the main regression run (`xcl-smoke` block of commit 1111111aaaa) has no `gpu` line",
check(BODY + body, HEAD, files, on_main))

def test_a_main_run_with_uncommitted_changes_is_not_a_baseline(self):
body = ("```\nxcl-smoke v1\ncommit: 1111111aaaa (uncommitted changes)\n" + self.RUN.format(100, 100, gpu="GPU A") + "```\n"
"```\nxcl-smoke v1\ncommit: 1234567890\n" + self.RUN.format(100, 100, gpu="GPU A") + "```\n")
problems = check(BODY + body, HEAD, [changed("crates/xcl/src/hle/d3d12x/vram.rs")], lambda sha: sha == "1111111aaaa")
self.assertEqual(["the main regression run (`xcl-smoke` block of commit 1111111aaaa) has uncommitted "
"changes: rerun scripts/smoke.ps1 on a clean checkout of main"], problems)

def test_the_newest_main_block_is_the_baseline_whatever_its_place(self):
# an old main run with a low peak pasted first, the current main run after it
old = "```\nxcl-smoke v1\ncommit: 1111111aaaa\n" + self.RUN.format(1000, 100, gpu="GPU A") + "```\n"
new = "```\nxcl-smoke v1\ncommit: 2222222bbbb\n" + self.RUN.format(4000, 100, gpu="GPU A") + "```\n"
pr = "```\nxcl-smoke v1\ncommit: 1234567890\n" + self.RUN.format(4000, 100, gpu="GPU A") + "```\n"
files = [changed("crates/xcl/src/hle/d3d12x/vram.rs")]
on_main = lambda sha: sha in ("1111111aaaa", "2222222bbbb")
behind = {"1111111aaaa": 40, "2222222bbbb": 2}.get
for order in (old + new, new + old):
self.assertEqual([], check(BODY + order + pr, HEAD, files, on_main, main_behind=behind))
# without the distance the first block is taken, as before
self.assertIn("the PR's run commits much more memory than main's (4000 MB against 1000 MB)",
check(BODY + old + new + pr, HEAD, files, on_main))
# an unknown distance (a failed API call) does not reorder: the first block, not the older one
flaky = {"1111111aaaa": 40, "2222222bbbb": None}.get
self.assertEqual([], check(BODY + new + old + pr, HEAD, files, on_main, main_behind=flaky))
# a failing comparison with an old baseline says how old it is
self.assertIn("the PR's run commits much more memory than main's (4000 MB against 1000 MB); "
"the main block is 40 commit(s) behind main: rerun it before comparing",
check(BODY + old + pr, HEAD, files, on_main, main_behind=behind))

def test_non_numeric_frames_shown_fails(self):
run = "title: G\nseconds: 60\ndevice removals: 0\ncrashes: 0\n"
run = "title: G\ngpu: GPU A\nseconds: 60\ndevice removals: 0\ncrashes: 0\n"
problems = self.smoke_problems(run + "frames shown: 100\n", run + "frames shown: many\n")
self.assertIn("the PR's regression run (`xcl-smoke` block of commit 1234567890) "
"has a non-numeric `frames shown`: many", problems)
Expand Down
Loading