Files
leleka/scripts/e2e/codex/speed/hit_share_layers.py
T
Pavlo RatushnyiandClaude Sonnet 5.5 350ff9f070 Hit-share gap research: slots (about 0.9 point), window width (0.55), per-layer quotas (second order), start of request; ranked experiments
Read-only analysis of the surviving replay and CUPTI logs on the 5090 server: VRAM hit share of ours against vanilla per cause, per-layer quota
evidence, start-of-request curve, tier knob A/Bs recomputed, ranked list of changes. Adds hit_share_gap.py (server) and hit_share_layers.py (local).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-11 17:19:34 +01:00

91 lines
4.2 KiB
Python

"""Miss-leg frequency per layer, ours against vanilla, against the layer's slot quota (vram-hit-share-gap.md, cause b).
Local, stdlib only, no GPU. Input: the per-layer records of CUPTI windows written by `ffn_phase_timeline.py extract`
(reproduction in ffn-phase-loss-design.md section 6):
python3 hit_share_layers.py PROFILE.bin VANL.json.gz LEL.json.gz...
A layer-window is "with a miss leg" when its class is C (CPU rows late) or X (PCIe leg); F has neither.
Vanilla fills 12,575 slots by the global rank of the profile; ours gives every layer 254 (12,192 / 48).
"""
import os
import statistics as st
import struct
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
sys.dont_write_bytecode = True
import ffn_phase_timeline as F # noqa: E402
LAYERS, OURS_QUOTA, VANILLA_SLOTS = 48, 254, 12575
def global_quota(profile_path):
raw = open(profile_path, "rb").read()
pairs = [struct.unpack("<HH", raw[24 + 4 * i:28 + 4 * i]) for i in range(LAYERS * 512)]
return [sum(1 for layer, _ in pairs[:VANILLA_SLOTS] if layer == li) for li in range(LAYERS)]
def share(cells, li, classes):
sel = [c for c in cells if c["li"] == li]
return sum(1 for c in sel if c["cls"] in classes) / len(sel)
def pearson(xs, ys):
mx, my = st.mean(xs), st.mean(ys)
cov = sum((a - mx) * (b - my) for a, b in zip(xs, ys))
return cov / (sum((a - mx) ** 2 for a in xs) ** 0.5 * sum((b - my) ** 2 for b in ys) ** 0.5)
def report(vanl, lels, quota, classes, rows_t):
pick = (lambda cs: cs) if rows_t is None else (lambda cs: [c for c in cs if c["T"] in rows_t])
ours = [st.mean(share(pick(c), li, classes) for c in lels) for li in range(LAYERS)]
theirs = [share(pick(vanl), li, classes) for li in range(LAYERS)]
diff = [a - b for a, b in zip(ours, theirs)]
dq = [q - OURS_QUOTA for q in quota]
slope = sum((a - st.mean(dq)) * (b - st.mean(diff)) for a, b in zip(dq, diff)) / sum(
(a - st.mean(dq)) ** 2 for a in dq)
print("classes %s, window rows %s: mean share ours %.3f vanilla %.3f, pearson(quota - 254, ours - vanilla) %.2f, "
"slope %.4f per slot" % ("|".join(classes), rows_t or "all", st.mean(ours), st.mean(theirs),
pearson(dq, diff), slope))
for name, sel in (("receivers (+20 slots or more)", [i for i in range(LAYERS) if dq[i] > 20]),
("neutral", [i for i in range(LAYERS) if abs(dq[i]) <= 20]),
("donors (-20 slots or fewer)", [i for i in range(LAYERS) if dq[i] < -20])):
print(" %-30s %2d layers, %+4.0f slots each, ours - vanilla %+.3f" % (
name, len(sel), st.mean(dq[i] for i in sel), st.mean(diff[i] for i in sel)))
def uniform_ranks(pairs, per_layer):
taken, ranks = [0] * LAYERS, []
for rank, (layer, _) in enumerate(pairs):
if taken[layer] < per_layer:
taken[layer] += 1
ranks.append(rank)
return ranks
def model_gain(profile_path):
"""Static coverage of a power-law routing model f(rank) ~ rank^-alpha over the profile's global ranks (E)."""
raw = open(profile_path, "rb").read()
pairs = [struct.unpack("<HH", raw[24 + 4 * i:28 + 4 * i]) for i in range(LAYERS * 512)]
for alpha in (0.5, 0.7, 0.9):
total = sum((r + 1) ** -alpha for r in range(len(pairs)))
cover = lambda ranks: 100 * sum((r + 1) ** -alpha for r in ranks) / total # noqa: E731
uni, glob12192, glob12575 = cover(uniform_ranks(pairs, OURS_QUOTA)), cover(range(12192)), cover(range(12575))
print("model alpha %.1f: global fill at equal slots %+.2f points, +383 slots (12,575 global) %+.2f points" % (
alpha, glob12192 - uni, glob12575 - glob12192))
def main(profile, vanl_path, *lel_paths):
vanl = F.load(vanl_path)["cells"]
lels = [F.load(p)["cells"] for p in lel_paths]
quota = global_quota(profile)
print("global quota per layer (min %d, max %d, layers above 254: %d, slots moved: %d)" % (
min(quota), max(quota), sum(q > OURS_QUOTA for q in quota), sum(max(0, q - OURS_QUOTA) for q in quota)))
for classes in (("C", "X"), ("X",)):
for rows_t in (None, (3, 4)):
report(vanl, lels, quota, classes, rows_t)
model_gain(profile)
if __name__ == "__main__":
main(*sys.argv[1:])