mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
cuda_spill_guard: a per-architecture baseline of the known violations
The guard has been red on the base on every architecture (sm_120: 9 instances, sm_86: 16, sm_60: about 400), so it could not tell a new spill from an inherited one, and raising ALLOWED to turn it green would hide any later growth. tools/cuda_spill_baseline.json lists each inherited violation by exact instance name (demangled, template arguments kept, spelled the same for cu++filt and c++filt) with the bytes measured and the reason "inherited, not yet fixed": sm_120 has 9 (tp_band_kernel and 8 iq4_w4a8_kernel), sm_86 has 16 (3 gemm_rows_kernel, rope_table_inv_kernel, 7 iq4_w4a8_kernel, 5 K-quant fused native_kernel). The numbers come from the base's libraries built with CUDA 13.2 and equal the ones measured on CUDA 13.1. The guard now passes a violation that is a listed instance at or below its listed bytes and counts it as known (the report line and a per-architecture summary print the known count). A new instance, or a listed instance above its bytes, fails. An architecture without a baseline (sm_60) prints "no baseline for sm_60" and its violations fail as before. A baseline file that cannot be read ends with exit 2. ALLOWED is unchanged. --baseline FILE, --print-baseline (how the file was made) and -v are new. Tests (tools/test_check_cuda_spills.py, 10 -> 34): instance names across both demanglers, a known instance at / below / above its bytes, a new instance, the per-architecture lists, no baseline, a broken baseline file, --print-baseline feeding the check, and for every entry of the shipped file that it is a real violation at its own bytes and fails when grown.
This commit is contained in:
1 parent
02020478b8
commit
6f1613cc1c
3 files changed
+541
-8
No files matched your search
+159
-8
@@ -23,16 +23,31 @@ sits in `strata::` or in an anonymous namespace. The report line counts what wa
|
||||
nothing judged: this is how the numbers in ALLOWED were produced
|
||||
--cuobjdump PATH the toolkit's cuobjdump when bin/ is not on PATH (CMake passes it)
|
||||
|
||||
Exit 0 = clean, 1 = a violation, 77 = cannot run here (cuobjdump missing, a library missing; ctest SKIP_RETURN_CODE).
|
||||
Known violations (tools/cuda_spill_baseline.json): some kernels are over their allowance on the base and have not been fixed
|
||||
yet. Raising ALLOWED for them would hide any later growth, so they are listed per architecture by exact instance name (the
|
||||
demangled name with its template arguments, without the parameter list) with the bytes measured, each with the reason
|
||||
"inherited, not yet fixed". A violation that is such an instance, at or below its listed bytes, is counted as known and does not
|
||||
fail the check; a violation that is not listed (a new instance), and a listed instance above its bytes, fail. The report line
|
||||
prints the known count. An architecture with no baseline (sm_60: about 400 violations, nothing measured per instance) prints
|
||||
"no baseline for <sm>" and its violations fail as before. --baseline FILE reads another baseline; --print-baseline prints the
|
||||
file's entries for the violations found (how the baseline was made).
|
||||
|
||||
Exit 0 = clean (or only known violations), 1 = a new or grown violation, 2 = the baseline file cannot be read, 77 = cannot run
|
||||
here (cuobjdump missing, a library missing; ctest SKIP_RETURN_CODE).
|
||||
"""
|
||||
import argparse
|
||||
import csv
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
# the known violations (see the docstring): per architecture, each inherited instance by exact name with its measured bytes
|
||||
BASELINE_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "cuda_spill_baseline.json")
|
||||
KNOWN_REASON = "inherited, not yet fixed"
|
||||
|
||||
# 0.2.7 (f-027-gh-eng): re-measured on f-027-int with CUDA 13.0 for sm_120 (Windows, MSVC) and sm_86 (Linux, cuobjdump 12.9
|
||||
# reading the CUDA 13.0 objects); an allowance is only ever raised here, never lowered, and the entries that now read 0 with
|
||||
# CUDA 13.0 keep theirs (another toolkit may spill where this one does not). New: the DFlash2 kernels gemm8_kernel and
|
||||
@@ -384,10 +399,80 @@ def demangle(demangler, names):
|
||||
return dict(zip(names, out))
|
||||
|
||||
|
||||
def check(path, rows, names):
|
||||
archs = sorted({sm for _, sm, _, _ in rows})
|
||||
def instance_key(demangled):
|
||||
"""The exact instance name: the demangled name with its template arguments, without return type and parameter list, spelled the
|
||||
same whichever demangler printed it (cu++filt: <unnamed>, (bool)1, (int)96; binutils' c++filt: (anonymous namespace), true, 96,
|
||||
96u, "> >"). The baseline's key."""
|
||||
s = demangled.replace("(anonymous namespace)", "{anonymous}").replace("<unnamed>", "{anonymous}")
|
||||
s = s.replace("(bool)1", "true").replace("(bool)0", "false")
|
||||
s = re.sub(r"\((?:unsigned )?(?:long long|long|int|short|char)\)(-?\d+)", r"\1", s) # cu++filt's cast before a literal
|
||||
s = re.sub(r"(?<=\d)[ul]+(?![\w])", "", s) # c++filt's 96u / 96ul suffixes
|
||||
while re.search(r">\s+>", s):
|
||||
s = re.sub(r">\s+>", ">>", s)
|
||||
depth, cut = 0, len(s)
|
||||
for i, c in enumerate(s): # the parameter list starts at the first '(' outside <...>
|
||||
if c == "<":
|
||||
depth += 1
|
||||
elif c == ">":
|
||||
depth -= 1
|
||||
elif c == "(" and depth == 0:
|
||||
cut = i
|
||||
break
|
||||
s = s[:cut].strip()
|
||||
depth, start = 0, 0
|
||||
for i, c in enumerate(s): # a template kernel is printed with its return type: "void ns::k<1, 2>"
|
||||
if c == "<":
|
||||
depth += 1
|
||||
elif c == ">":
|
||||
depth -= 1
|
||||
elif c == " " and depth == 0:
|
||||
start = i + 1
|
||||
return s[start:] or demangled
|
||||
|
||||
|
||||
def load_baseline(path):
|
||||
"""{sm: {instance name: (stack, local, reason)}} from the baseline file; an architecture that is not in the file has no
|
||||
baseline. A file that cannot be read, or an entry that is incomplete, is an error (ValueError), never an empty baseline."""
|
||||
try:
|
||||
with open(path, encoding="utf-8") as f:
|
||||
raw = json.load(f)
|
||||
except (OSError, ValueError) as e:
|
||||
raise ValueError("cannot read the baseline %s: %s" % (path, e))
|
||||
if not isinstance(raw, dict):
|
||||
raise ValueError("the baseline %s is not a JSON object" % path)
|
||||
out = {}
|
||||
for sm, block in raw.items():
|
||||
if sm.startswith("_") or block is None: # "_about" and the like; null = stated as having no baseline
|
||||
continue
|
||||
entries = block.get("instances") if isinstance(block, dict) else None
|
||||
if not isinstance(entries, list):
|
||||
raise ValueError("the baseline %s: %s has no \"instances\" list" % (path, sm))
|
||||
per = {}
|
||||
for e in entries:
|
||||
ok = (isinstance(e, dict) and isinstance(e.get("name"), str) and e["name"]
|
||||
and all(isinstance(e.get(k), int) and not isinstance(e.get(k), bool) and e[k] >= 0 for k in ("stack", "local"))
|
||||
and isinstance(e.get("reason"), str) and e["reason"].strip())
|
||||
if not ok:
|
||||
raise ValueError("the baseline %s: %s has an entry without name, stack, local and reason: %r" % (path, sm, e))
|
||||
if e["name"] in per:
|
||||
raise ValueError("the baseline %s: %s lists %s twice" % (path, sm, e["name"]))
|
||||
per[e["name"]] = (e["stack"], e["local"], e["reason"])
|
||||
out[sm] = per
|
||||
return out
|
||||
|
||||
|
||||
def default_baseline():
|
||||
"""The baseline beside this script; a missing file is an empty baseline (every architecture reports "no baseline")."""
|
||||
if not os.path.exists(BASELINE_FILE):
|
||||
return {}
|
||||
return load_baseline(BASELINE_FILE)
|
||||
|
||||
|
||||
def find_violations(rows, names):
|
||||
"""(gated entries, entries that use stack or local memory, [violation]) for the rows of one library: a violation is an entry over
|
||||
ALLOWED (or not in it), as (demangled name, instance key, sm, stack, local, why, object)."""
|
||||
gated = [(obj, sm, func, cols) for obj, sm, func, cols in rows if not vendored(kernel_key(names[func]))]
|
||||
held, bad = 0, []
|
||||
held, viols = 0, []
|
||||
for obj, sm, func, cols in gated:
|
||||
stack, local = cols.get("STACK", 0), cols.get("LOCAL", 0)
|
||||
if stack == 0 and local == 0:
|
||||
@@ -400,20 +485,65 @@ def check(path, rows, names):
|
||||
why = "over its allowance STACK %d LOCAL %d" % allow
|
||||
else:
|
||||
continue
|
||||
bad.append((names[func], sm, stack, local, why, obj))
|
||||
viols.append((names[func], instance_key(names[func]), sm, stack, local, why, obj))
|
||||
return gated, held, viols
|
||||
|
||||
|
||||
def check(path, rows, names, baseline=None, seen=None, verbose=False):
|
||||
"""Judge one library. `baseline` is load_baseline's result (default: the file beside this script); `seen`, when given, collects
|
||||
{sm: {instance names}} of the known instances found, for the caller's count over several libraries."""
|
||||
if baseline is None:
|
||||
baseline = default_baseline()
|
||||
archs = sorted({sm for _, sm, _, _ in rows})
|
||||
gated, held, viols = find_violations(rows, names)
|
||||
known, bad, no_base = [], [], {}
|
||||
for name, inst, sm, stack, local, why, obj in viols:
|
||||
base = baseline.get(sm)
|
||||
if base is None:
|
||||
no_base[sm] = no_base.get(sm, 0) + 1
|
||||
elif inst in base:
|
||||
b_stack, b_local, _ = base[inst]
|
||||
if stack <= b_stack and local <= b_local:
|
||||
known.append((name, sm, stack, local, obj))
|
||||
if seen is not None:
|
||||
seen.setdefault(sm, set()).add(inst)
|
||||
continue
|
||||
why = "known instance grew: over its baseline STACK %d LOCAL %d" % (b_stack, b_local)
|
||||
bad.append((name, sm, stack, local, why, obj))
|
||||
print("check_cuda_spills: %s: %d kernels on %s (%d entries; %d vendored ggml entries skipped), %d use stack / "
|
||||
"local memory, %d not allowed" % (path, len({f for _, _, f, _ in gated}), ", ".join(archs), len(gated),
|
||||
len(rows) - len(gated), held, len(bad)))
|
||||
"local memory, %d of them known from the baseline, %d not allowed" % (
|
||||
path, len({f for _, _, f, _ in gated}), ", ".join(archs), len(gated), len(rows) - len(gated), held, len(known),
|
||||
len(bad)))
|
||||
for sm in sorted(no_base):
|
||||
print("check_cuda_spills: no baseline for %s: its %d violations cannot be told from new ones (baselines: %s)" % (
|
||||
sm, no_base[sm], ", ".join(sorted(baseline)) or "none"))
|
||||
if verbose:
|
||||
for name, sm, stack, local, obj in known:
|
||||
print(" known %s %s: STACK %d LOCAL %d [%s]" % (name[:160], sm, stack, local, obj))
|
||||
for name, sm, stack, local, why, obj in bad:
|
||||
print(" VIOLATION %s %s: STACK %d LOCAL %d (%s) [%s]" % (name[:160], sm, stack, local, why, obj))
|
||||
return 1 if bad else 0
|
||||
|
||||
|
||||
def toolkit_line(cuobjdump):
|
||||
"""The last line of `cuobjdump --version` (the toolkit release), for the baseline's "measured_with"."""
|
||||
try:
|
||||
run = subprocess.run([cuobjdump, "--version"], capture_output=True, text=True, timeout=30)
|
||||
lines = [l.strip() for l in run.stdout.splitlines() if l.strip()]
|
||||
return lines[-1] if lines else "unknown"
|
||||
except (OSError, subprocess.SubprocessError):
|
||||
return "unknown"
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("library", nargs="+", help="a static library, object, shared library or executable with SASS")
|
||||
ap.add_argument("--list", action="store_true", help="print every kernel on every sm_ as CSV, judge nothing")
|
||||
ap.add_argument("--cuobjdump", help="the toolkit's cuobjdump (default: on PATH)")
|
||||
ap.add_argument("--baseline", help="the known-violations file (default: cuda_spill_baseline.json beside this script)")
|
||||
ap.add_argument("--print-baseline", action="store_true",
|
||||
help="print the baseline entries (JSON) for the violations found, judge nothing: how the baseline was made")
|
||||
ap.add_argument("-v", "--verbose", action="store_true", help="also print every known violation")
|
||||
a = ap.parse_args()
|
||||
cuobjdump, demangler = find_tools(a.cuobjdump)
|
||||
if cuobjdump is None:
|
||||
@@ -428,10 +558,18 @@ def main():
|
||||
if not os.path.exists(lib):
|
||||
print("check_cuda_spills: %s does not exist" % lib)
|
||||
return 77
|
||||
baseline = {}
|
||||
if not a.list and not a.print_baseline:
|
||||
try:
|
||||
baseline = load_baseline(a.baseline) if a.baseline else default_baseline()
|
||||
except ValueError as e: # a baseline that cannot be read is never an empty one: that would pass nothing known and hide it
|
||||
print("check_cuda_spills: %s" % e)
|
||||
return 2
|
||||
out = csv.writer(sys.stdout, lineterminator="\n") if a.list else None
|
||||
if out:
|
||||
out.writerow(["library", "object", "sm", "reg", "stack", "local", "shared", "kernel", "mangled", "gated"])
|
||||
rc = 0
|
||||
seen, archs_seen, found = {}, set(), {}
|
||||
for lib in a.library:
|
||||
rows = resource_usage(cuobjdump, lib)
|
||||
if rows is None:
|
||||
@@ -450,8 +588,21 @@ def main():
|
||||
out.writerow([os.path.basename(lib), obj, sm, cols.get("REG", 0), cols.get("STACK", 0),
|
||||
cols.get("LOCAL", 0), cols.get("SHARED", 0), names[func], func,
|
||||
0 if vendored(kernel_key(names[func])) else 1])
|
||||
elif a.print_baseline:
|
||||
for _, inst, sm, stack, local, _, _ in find_violations(rows, names)[2]:
|
||||
old = found.setdefault(sm, {}).get(inst, (0, 0))
|
||||
found[sm][inst] = (max(stack, old[0]), max(local, old[1]))
|
||||
else:
|
||||
rc = max(rc, check(lib, rows, names))
|
||||
archs_seen.update(sm for _, sm, _, _ in rows)
|
||||
rc = max(rc, check(lib, rows, names, baseline, seen, a.verbose))
|
||||
if a.print_baseline:
|
||||
print(json.dumps({sm: {"measured_with": toolkit_line(cuobjdump),
|
||||
"instances": [{"name": n, "stack": s, "local": l, "reason": KNOWN_REASON}
|
||||
for n, (s, l) in sorted(per.items())]}
|
||||
for sm, per in sorted(found.items())}, indent=2))
|
||||
elif not a.list:
|
||||
for sm in sorted(archs_seen & set(baseline)): # the known count over all the libraries given
|
||||
print("check_cuda_spills: baseline %s: %d known violations seen, %d listed" % (sm, len(seen.get(sm, ())), len(baseline[sm])))
|
||||
return rc
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
{
|
||||
"_about": "Known cuda_spill_guard violations: kernel instances over their ALLOWED allowance on the base that are inherited and not yet fixed. Per architecture, each by its exact instance name (tools/check_cuda_spills.py instance_key) with the bytes measured. A violation listed here at or below its bytes passes as known; a new instance, or a listed one above its bytes, fails. Another CUDA toolkit may list a slightly different set of instances; add what it measures here, never raise a number to hide growth. sm_60 has no baseline (about 400 violations, nothing measured per instance): the guard says so and its violations fail as before.",
|
||||
"sm_120": {
|
||||
"measured_with": "cuobjdump Build cuda_13.2.r13.2/compiler.37953736_0; the guard libraries of the base bb1bdb4da (MMQ on, tests on, CUDA 13.2, sm_120-real)",
|
||||
"instances": [
|
||||
{
|
||||
"name": "strata::kernels::{anonymous}::tp_band_kernel",
|
||||
"stack": 8,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::mmq::{anonymous}::iq4_w4a8_kernel<false, strata::prefill::mmq::{anonymous}::Cfg<104, 2, 1, 1, 72, 1>>",
|
||||
"stack": 48,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::mmq::{anonymous}::iq4_w4a8_kernel<false, strata::prefill::mmq::{anonymous}::Cfg<112, 2, 1, 1, 72, 1>>",
|
||||
"stack": 48,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::mmq::{anonymous}::iq4_w4a8_kernel<false, strata::prefill::mmq::{anonymous}::Cfg<128, 2, 1, 1, 72, 1>>",
|
||||
"stack": 48,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::mmq::{anonymous}::iq4_w4a8_kernel<false, strata::prefill::mmq::{anonymous}::Cfg<96, 2, 1, 1, 72, 1>>",
|
||||
"stack": 32,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::mmq::{anonymous}::iq4_w4a8_kernel<true, strata::prefill::mmq::{anonymous}::Cfg<104, 2, 1, 1, 72, 1>>",
|
||||
"stack": 48,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::mmq::{anonymous}::iq4_w4a8_kernel<true, strata::prefill::mmq::{anonymous}::Cfg<112, 2, 1, 1, 72, 1>>",
|
||||
"stack": 48,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::mmq::{anonymous}::iq4_w4a8_kernel<true, strata::prefill::mmq::{anonymous}::Cfg<128, 2, 1, 1, 72, 1>>",
|
||||
"stack": 56,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::mmq::{anonymous}::iq4_w4a8_kernel<true, strata::prefill::mmq::{anonymous}::Cfg<96, 2, 1, 1, 72, 1>>",
|
||||
"stack": 48,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
}
|
||||
]
|
||||
},
|
||||
"sm_86": {
|
||||
"measured_with": "cuobjdump Build cuda_13.2.r13.2/compiler.37953736_0; the guard libraries of the base bb1bdb4da (MMQ on, tests on) compiled for sm_86-real on a CUDA 13.2 box: the same 16 instances and bytes as measured on CUDA 13.1 on an RTX A6000",
|
||||
"instances": [
|
||||
{
|
||||
"name": "strata::kernels::{anonymous}::gemm_rows_kernel<2, 2, 2>",
|
||||
"stack": 40,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::kernels::{anonymous}::gemm_rows_kernel<4, 2, 2>",
|
||||
"stack": 40,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::kernels::{anonymous}::gemm_rows_kernel<8, 2, 4>",
|
||||
"stack": 8,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::kernels::{anonymous}::rope_table_inv_kernel",
|
||||
"stack": 32,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::fused::{anonymous}::native_kernel<12, true, 2, 512, false>",
|
||||
"stack": 64,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::fused::{anonymous}::native_kernel<12, true, 4, 512, false>",
|
||||
"stack": 72,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::fused::{anonymous}::native_kernel<13, true, 2, 512, false>",
|
||||
"stack": 72,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::fused::{anonymous}::native_kernel<13, true, 4, 512, false>",
|
||||
"stack": 80,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::fused::{anonymous}::native_kernel<14, true, 4, 512, false>",
|
||||
"stack": 64,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::mmq::{anonymous}::iq4_w4a8_kernel<false, strata::prefill::mmq::{anonymous}::Cfg<104, 2, 1, 1, 72, 1>>",
|
||||
"stack": 32,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::mmq::{anonymous}::iq4_w4a8_kernel<false, strata::prefill::mmq::{anonymous}::Cfg<112, 2, 1, 1, 72, 1>>",
|
||||
"stack": 40,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::mmq::{anonymous}::iq4_w4a8_kernel<false, strata::prefill::mmq::{anonymous}::Cfg<128, 2, 1, 1, 72, 1>>",
|
||||
"stack": 40,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::mmq::{anonymous}::iq4_w4a8_kernel<false, strata::prefill::mmq::{anonymous}::Cfg<96, 2, 1, 1, 72, 1>>",
|
||||
"stack": 16,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::mmq::{anonymous}::iq4_w4a8_kernel<true, strata::prefill::mmq::{anonymous}::Cfg<104, 2, 1, 1, 72, 1>>",
|
||||
"stack": 8,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::mmq::{anonymous}::iq4_w4a8_kernel<true, strata::prefill::mmq::{anonymous}::Cfg<112, 2, 1, 1, 72, 1>>",
|
||||
"stack": 40,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
},
|
||||
{
|
||||
"name": "strata::prefill::mmq::{anonymous}::iq4_w4a8_kernel<true, strata::prefill::mmq::{anonymous}::Cfg<128, 2, 1, 1, 72, 1>>",
|
||||
"stack": 40,
|
||||
"local": 0,
|
||||
"reason": "inherited, not yet fixed"
|
||||
}
|
||||
]
|
||||
},
|
||||
"sm_60": null
|
||||
}
|
||||
@@ -6,6 +6,7 @@ from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import stat
|
||||
import sys
|
||||
@@ -73,6 +74,223 @@ class Classification(unittest.TestCase):
|
||||
self.assertIn("1 vendored ggml entries skipped", out)
|
||||
|
||||
|
||||
KNOWN_NAME = "void strata::prefill::(anonymous namespace)::iq4_w4a8_kernel<12, 4, true>(int const*, int)"
|
||||
KNOWN_MANGLED = "_ZN6strata7prefill12_GLOBAL__N_116iq4_w4a8_kernelILi12ELi4ELb1EEEvPKii"
|
||||
KNOWN_KEY = "strata::prefill::{anonymous}::iq4_w4a8_kernel<12, 4, true>"
|
||||
OTHER_NAME = "void strata::prefill::(anonymous namespace)::iq4_w4a8_kernel<16, 4, true>(int const*, int)"
|
||||
OTHER_MANGLED = "_ZN6strata7prefill12_GLOBAL__N_116iq4_w4a8_kernelILi16ELi4ELb1EEEvPKii"
|
||||
NAMES = {KNOWN_MANGLED: KNOWN_NAME, OTHER_MANGLED: OTHER_NAME}
|
||||
BASE = {"sm_86": {KNOWN_KEY: (48, 0, CS.KNOWN_REASON)}, "sm_120": {KNOWN_KEY: (32, 0, CS.KNOWN_REASON)}}
|
||||
|
||||
|
||||
def row(sm="sm_86", mangled=KNOWN_MANGLED, stack=48, local=0):
|
||||
return ("moe_iq4_w4a8.cu.o", sm, mangled, {"REG": 64, "STACK": stack, "LOCAL": local})
|
||||
|
||||
|
||||
class InstanceKey(unittest.TestCase):
|
||||
def test_the_exact_instance_without_return_type_and_parameters(self):
|
||||
self.assertEqual(CS.instance_key(KNOWN_NAME), KNOWN_KEY)
|
||||
self.assertEqual(CS.instance_key(OTHER_NAME), "strata::prefill::{anonymous}::iq4_w4a8_kernel<16, 4, true>")
|
||||
|
||||
def test_both_demanglers_spell_the_anonymous_namespace_alike(self):
|
||||
self.assertEqual(CS.instance_key(KNOWN_NAME.replace("(anonymous namespace)", "<unnamed>")), KNOWN_KEY)
|
||||
|
||||
def test_cu_filt_and_c_filt_spell_one_instance_alike(self):
|
||||
cu = ("void strata::prefill::mmq::<unnamed>::iq4_w4a8_kernel<(bool)1, strata::prefill::mmq::<unnamed>::Cfg<(int)96, (int)2, "
|
||||
"(unsigned int)7, (long)-1>>(strata::prefill::mmq::Args)")
|
||||
gnu = ("void strata::prefill::mmq::(anonymous namespace)::iq4_w4a8_kernel<true, strata::prefill::mmq::(anonymous namespace)::Cfg<96, 2, "
|
||||
"7u, -1l> >(strata::prefill::mmq::Args)")
|
||||
want = "strata::prefill::mmq::{anonymous}::iq4_w4a8_kernel<true, strata::prefill::mmq::{anonymous}::Cfg<96, 2, 7, -1>>"
|
||||
self.assertEqual(CS.instance_key(cu), want)
|
||||
self.assertEqual(CS.instance_key(gnu), want)
|
||||
self.assertEqual(CS.instance_key(cu.replace("(bool)1", "(bool)0")), want.replace("<true,", "<false,")) # a bool stays a bool
|
||||
|
||||
def test_template_arguments_with_spaces_and_parentheses_stay_whole(self):
|
||||
self.assertEqual(CS.instance_key("void ns::k<(ns::E)3, ns::T<1, 2>, true>(float*)"), "ns::k<(ns::E)3, ns::T<1, 2>, true>") # an enum cast is kept
|
||||
|
||||
def test_a_plain_kernel_and_a_name_without_parameters(self):
|
||||
self.assertEqual(CS.instance_key("void strata::kernels::tp_band_kernel(int)"), "strata::kernels::tp_band_kernel")
|
||||
self.assertEqual(CS.instance_key("strata::k"), "strata::k")
|
||||
|
||||
def test_instances_of_one_template_have_different_keys_but_one_allowance_key(self):
|
||||
self.assertNotEqual(CS.instance_key(KNOWN_NAME), CS.instance_key(OTHER_NAME))
|
||||
self.assertEqual(CS.kernel_key(KNOWN_NAME), CS.kernel_key(OTHER_NAME))
|
||||
|
||||
|
||||
class Baseline(unittest.TestCase):
|
||||
"""A known violation passes at or below its listed bytes; a new instance and a known instance that grew fail; an architecture
|
||||
with no baseline says so."""
|
||||
|
||||
def judge(self, rows, baseline=BASE):
|
||||
return quiet(CS.check, "x.a", rows, NAMES, baseline)
|
||||
|
||||
def test_the_kernel_of_the_baseline_is_not_in_allowed(self):
|
||||
# the instances below are violations because the kernel has no allowance: a baseline entry is not an allowance
|
||||
self.assertNotIn(CS.kernel_key(KNOWN_NAME), CS.ALLOWED)
|
||||
|
||||
def test_a_known_instance_at_its_listed_bytes_passes_and_is_counted(self):
|
||||
rc, out = self.judge([row()])
|
||||
self.assertEqual(rc, 0, out)
|
||||
self.assertIn("1 use stack / local memory, 1 of them known from the baseline, 0 not allowed", out)
|
||||
self.assertNotIn("VIOLATION", out)
|
||||
|
||||
def test_a_known_instance_below_its_listed_bytes_passes(self):
|
||||
rc, out = self.judge([row(stack=32)])
|
||||
self.assertEqual(rc, 0, out)
|
||||
|
||||
def test_a_known_instance_that_grew_fails_in_stack_and_in_local(self):
|
||||
for stack, local in ((56, 0), (48, 8), (0, 8)):
|
||||
rc, out = self.judge([row(stack=stack, local=local)])
|
||||
self.assertEqual(rc, 1, (stack, local, out))
|
||||
self.assertIn("known instance grew: over its baseline STACK 48 LOCAL 0", out)
|
||||
self.assertIn("0 of them known from the baseline, 1 not allowed", out)
|
||||
|
||||
def test_a_new_instance_of_a_known_kernel_fails_beside_the_known_one(self):
|
||||
rc, out = self.judge([row(), row(mangled=OTHER_MANGLED, stack=8)])
|
||||
self.assertEqual(rc, 1, out)
|
||||
self.assertIn("2 use stack / local memory, 1 of them known from the baseline, 1 not allowed", out)
|
||||
self.assertIn("VIOLATION %s sm_86: STACK 8 LOCAL 0 (not in ALLOWED)" % OTHER_NAME, out)
|
||||
self.assertNotIn("VIOLATION %s" % KNOWN_NAME, out)
|
||||
|
||||
def test_the_baseline_is_per_architecture(self):
|
||||
rc, out = self.judge([row(sm="sm_120", stack=32)])
|
||||
self.assertEqual(rc, 0, out)
|
||||
rc, out = self.judge([row(sm="sm_120", stack=40)]) # sm_120 lists 32, not sm_86's 48
|
||||
self.assertEqual(rc, 1, out)
|
||||
only_86 = {"sm_86": BASE["sm_86"], "sm_120": {}}
|
||||
rc, out = self.judge([row(sm="sm_120", stack=32)], only_86) # an architecture with an empty baseline knows nothing
|
||||
self.assertEqual(rc, 1, out)
|
||||
self.assertNotIn("no baseline", out)
|
||||
|
||||
def test_an_architecture_without_a_baseline_says_so_and_still_fails(self):
|
||||
rc, out = self.judge([row(sm="sm_60", stack=48)])
|
||||
self.assertEqual(rc, 1, out)
|
||||
self.assertIn("no baseline for sm_60: its 1 violations cannot be told from new ones (baselines: sm_120, sm_86)", out)
|
||||
self.assertIn("VIOLATION", out)
|
||||
|
||||
def test_an_architecture_without_a_baseline_and_without_violations_is_quiet(self):
|
||||
rc, out = self.judge([row(sm="sm_60", stack=0)])
|
||||
self.assertEqual(rc, 0, out)
|
||||
self.assertNotIn("no baseline", out)
|
||||
|
||||
def test_no_baseline_at_all_is_not_a_pass(self):
|
||||
rc, out = self.judge([row()], {})
|
||||
self.assertEqual(rc, 1, out)
|
||||
self.assertIn("no baseline for sm_86", out)
|
||||
self.assertIn("baselines: none", out)
|
||||
|
||||
def test_a_violation_that_is_allowed_is_not_counted_as_known(self):
|
||||
names = {"_ZN6strata7kernels12_GLOBAL__N_113rope_table_kernelEv": "void strata::kernels::(anonymous namespace)::rope_table_kernel()"}
|
||||
rows = [("d.cu.o", "sm_86", next(iter(names)), {"STACK": 32})]
|
||||
rc, out = quiet(CS.check, "x.a", rows, names, BASE)
|
||||
self.assertEqual(rc, 0, out)
|
||||
self.assertIn("1 use stack / local memory, 0 of them known from the baseline, 0 not allowed", out)
|
||||
|
||||
def test_seen_collects_the_known_instances_and_verbose_prints_them(self):
|
||||
seen = {}
|
||||
rc, out = quiet(CS.check, "x.a", [row()], NAMES, BASE, seen, True)
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertEqual(seen, {"sm_86": {KNOWN_KEY}})
|
||||
self.assertIn("known %s sm_86: STACK 48 LOCAL 0" % KNOWN_NAME, out)
|
||||
|
||||
|
||||
class BaselineFile(unittest.TestCase):
|
||||
def write(self, d, obj):
|
||||
p = Path(d, "baseline.json")
|
||||
p.write_text(obj if isinstance(obj, str) else json.dumps(obj), encoding="utf-8")
|
||||
return str(p)
|
||||
|
||||
def entry(self, **kw):
|
||||
e = {"name": KNOWN_KEY, "stack": 48, "local": 0, "reason": CS.KNOWN_REASON}
|
||||
e.update(kw)
|
||||
return e
|
||||
|
||||
def test_a_good_file_loads_and_null_and_underscore_keys_are_not_baselines(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
p = self.write(d, {"_about": "x", "sm_86": {"measured_with": "t", "instances": [self.entry()]}, "sm_60": None})
|
||||
self.assertEqual(CS.load_baseline(p), {"sm_86": {KNOWN_KEY: (48, 0, CS.KNOWN_REASON)}})
|
||||
|
||||
def test_a_file_that_cannot_be_read_or_is_incomplete_is_an_error(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
bad = [self.write(d, "{not json"), self.write(d, "[]"), os.path.join(d, "missing.json"),
|
||||
self.write(d, {"sm_86": {}}),
|
||||
self.write(d, {"sm_86": {"instances": [self.entry(reason="")]}}),
|
||||
self.write(d, {"sm_86": {"instances": [self.entry(stack="48")]}}),
|
||||
self.write(d, {"sm_86": {"instances": [self.entry(stack=-1)]}}),
|
||||
self.write(d, {"sm_86": {"instances": [{"name": KNOWN_KEY, "stack": 1, "local": 0}]}}),
|
||||
self.write(d, {"sm_86": {"instances": [self.entry(), self.entry(stack=8)]}})]
|
||||
for p in bad:
|
||||
with self.assertRaises(ValueError, msg=p):
|
||||
CS.load_baseline(p)
|
||||
|
||||
def test_the_default_baseline_is_the_file_beside_the_script_and_a_missing_one_is_empty(self):
|
||||
self.assertEqual(Path(CS.BASELINE_FILE), ROOT / "tools" / "cuda_spill_baseline.json")
|
||||
with mock.patch.object(CS, "BASELINE_FILE", str(ROOT / "tools" / "no_such_baseline.json")):
|
||||
self.assertEqual(CS.default_baseline(), {})
|
||||
|
||||
def test_the_shipped_baseline(self):
|
||||
base = CS.load_baseline(CS.BASELINE_FILE)
|
||||
self.assertIn("sm_120", base)
|
||||
self.assertIn("sm_86", base)
|
||||
self.assertNotIn("sm_60", base) # about 400 violations, nothing measured per instance: no baseline
|
||||
for sm, per in base.items():
|
||||
for name, (stack, local, reason) in per.items():
|
||||
self.assertTrue(reason.strip(), (sm, name))
|
||||
mangled = "_Z%d" % len(name) # one stand-in kernel per entry, at its own bytes
|
||||
names = {mangled: "void %s()" % name}
|
||||
gated, held, viols = CS.find_violations([("o.cu.o", sm, mangled, {"STACK": stack, "LOCAL": local})], names)
|
||||
self.assertEqual(len(viols), 1, "%s %s is not a violation at its own bytes: a dead entry" % (sm, name))
|
||||
rc, out = quiet(CS.check, "x.a", [("o.cu.o", sm, mangled, {"STACK": stack, "LOCAL": local})], names, base)
|
||||
self.assertEqual(rc, 0, out)
|
||||
for grown in ((stack + 8, local), (stack, local + 8)):
|
||||
rc, out = quiet(CS.check, "x.a", [("o.cu.o", sm, mangled, {"STACK": grown[0], "LOCAL": grown[1]})], names, base)
|
||||
self.assertEqual(rc, 1, (sm, name, grown))
|
||||
|
||||
|
||||
@unittest.skipIf(os.name == "nt", "the stand-in demangler is a #!/bin/sh script; Windows cannot run it as a program (needs a POSIX shell and exec of a shebang file)")
|
||||
class BaselineMain(unittest.TestCase):
|
||||
"""main(): the --baseline file, the known count over the libraries, a baseline that cannot be read, --print-baseline."""
|
||||
|
||||
def setUp(self):
|
||||
self.d = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.d.cleanup)
|
||||
self.filt = demangler(self.d.name, "while read -r n; do echo '%s'; done" % KNOWN_NAME)
|
||||
self.path = Path(self.d.name, "baseline.json")
|
||||
self.path.write_text(json.dumps({"sm_86": {"instances": [
|
||||
{"name": KNOWN_KEY, "stack": 48, "local": 0, "reason": CS.KNOWN_REASON}]}}), encoding="utf-8")
|
||||
|
||||
def test_known_passes_prints_the_known_count_and_a_grown_one_fails(self):
|
||||
rc, out = run_main(self.d.name, self.filt, [row()], ["--baseline", str(self.path)])
|
||||
self.assertEqual(rc, 0, out)
|
||||
self.assertIn("check_cuda_spills: baseline sm_86: 1 known violations seen, 1 listed", out)
|
||||
rc, out = run_main(self.d.name, self.filt, [row(stack=56)], ["--baseline", str(self.path)])
|
||||
self.assertEqual(rc, 1, out)
|
||||
self.assertIn("known instance grew", out)
|
||||
|
||||
def test_an_unreadable_baseline_is_exit_2_and_judges_nothing(self):
|
||||
for text in ("{not json", '{"sm_86": {"instances": [{"name": "x"}]}}'):
|
||||
self.path.write_text(text, encoding="utf-8")
|
||||
rc, out = run_main(self.d.name, self.filt, [row()], ["--baseline", str(self.path)])
|
||||
self.assertEqual(rc, 2, out)
|
||||
self.assertIn("baseline", out)
|
||||
self.assertNotIn("VIOLATION", out)
|
||||
rc, out = run_main(self.d.name, self.filt, [row()], ["--baseline", str(Path(self.d.name, "none.json"))])
|
||||
self.assertEqual(rc, 2, out)
|
||||
|
||||
def test_print_baseline_makes_a_file_the_check_accepts(self):
|
||||
with mock.patch.object(CS, "toolkit_line", return_value="Cuda compilation tools, release 99.9"):
|
||||
rc, out = run_main(self.d.name, self.filt, [row(), row(sm="sm_120", stack=32)], ["--print-baseline"])
|
||||
self.assertEqual(rc, 0)
|
||||
made = json.loads(out)
|
||||
self.assertEqual(sorted(made), ["sm_120", "sm_86"])
|
||||
self.assertEqual(made["sm_86"]["measured_with"], "Cuda compilation tools, release 99.9")
|
||||
self.assertEqual(made["sm_86"]["instances"], [{"name": KNOWN_KEY, "stack": 48, "local": 0, "reason": CS.KNOWN_REASON}])
|
||||
self.path.write_text(out, encoding="utf-8")
|
||||
rc, out = run_main(self.d.name, self.filt, [row(), row(sm="sm_120", stack=32)], ["--baseline", str(self.path)])
|
||||
self.assertEqual(rc, 0, out)
|
||||
self.assertIn("2 use stack / local memory, 2 of them known", out)
|
||||
|
||||
|
||||
@unittest.skipIf(os.name == "nt", "the stand-in demangler is a #!/bin/sh script; Windows cannot run it as a program (needs a POSIX shell and exec of a shebang file)")
|
||||
class Demangling(unittest.TestCase):
|
||||
"""A mangled name has no "::", so a kernel the demangler did not translate would read as vendored and be skipped:
|
||||
|
||||
Reference in new issue
Block a user