Skip to content

Commit 46d6df0

Browse files
physicsrobclaude
andcommitted
Ruff migration wave 2: B023 audit, SLF001, FBT keyword-only, complexity, PLR0913
- B023 (38): all classified benign after call-path tracing; closures now bind loop variables explicitly via keyword-only defaults. - SLF001 (46 of 60): de-facto public attributes renamed public or given read-only properties; 5 left where the private cache write IS the documented choke-point contract. - FBT (142 of 145): boolean parameters keyword-only, every in-repo caller updated; torchwright_doom grepped read-only per signature - zero doom-blocked changes. - C901/PLR0912/PLR0915 (101 -> 29): same-module helper extraction with exact operation order preserved; the scheduler/solver monoliths and I1-I4-entangled functions deliberately left with reasons recorded. - PLR0913 (127 surveyed, 5 refactored): _FoldPassCtx dataclass in graph/optimize.py, _PLPassCtx reuse in collapse_pl.py; 47 sites are doom-called or public API, 75 honest keyword plumbing. - Dead private-helper params removed (export, collapse_analysis, relu/swiglu twins, test_literal_jit). Gates: ruff 593 -> 265 (all standing findings classified: pending- decision false-positive families + justified leaves), mypy green, import walk clean, format stable. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent a1d0cdd commit 46d6df0

76 files changed

Lines changed: 3874 additions & 2676 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/calculator_scratchpad.py

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -751,40 +751,17 @@ def _mul_op(
751751
# ---------------------------------------------------------------------------
752752

753753

754-
def _sub_op(
755-
rope: RopeConfig,
756-
embedding: Embedding,
757-
A: list[Node],
758-
B: list[Node],
759-
n: int,
760-
steps_since: Node,
761-
) -> tuple[list[Node], list[Node]]:
762-
"""Streamed subtraction.
763-
764-
Three streamed thinking phases precede the answer:
765-
766-
1. **comparison** (MSB-first) — a 3-state less/equal/greater verdict folded
767-
one column at a time, each column reading the prior verdict from the
768-
previous emitted token; the last (LSB) verdict is the overall ``A ≥ B``.
769-
2. **borrow sweep** (LSB-first) — ``|A - B|`` by a borrow fold over
770-
``bigger_k = (a_k if A≥B else b_k)`` / ``smaller_k``.
771-
3. **normalization** (MSB-first) — the leading-zero count of the magnitude.
754+
def _cmp_verdict_stream(
755+
embedding: Embedding, A: list[Node], B: list[Node], n: int
756+
) -> list[Node]:
757+
"""Streamed comparison phase: MSB-first 3-state verdict fold.
772758
773-
The answer is ``n+1`` MSB-first slots: a leading sign slot (``-`` if
774-
``A < B``) then the trimmed magnitude. Verdict, borrow, and count state are
775-
all read back from the emitted prefix at statically-known offsets.
759+
The serial form the graph calculators retired when compare_digit_seqs
760+
went constant-depth; here the recurrence rides the decode axis, where
761+
serial is the point. Each column reads the prior verdict from the
762+
previous emitted token; the last (LSB) verdict is the overall ``A ≥ B``.
776763
"""
777764
embed = embedding.get_embedding
778-
n_state = _n_thinking(n)
779-
W = 20 # shifted column total (bigger - smaller - borrow_in + 10) ∈ 0..19
780-
781-
a_scalar = [_digit_scalar(embedding, d) for d in A] # MSB-first
782-
b_scalar = [_digit_scalar(embedding, d) for d in B]
783-
784-
# --- comparison phase: MSB-first 3-state verdict fold — the serial form
785-
# the graph calculators retired when compare_digit_seqs went
786-
# constant-depth; here the recurrence rides the decode axis, where
787-
# serial is the point. ---
788765
combine_table: dict[torch.Tensor, torch.Tensor] = {}
789766
nxt: int | Node
790767
for v in range(_CMP_W):
@@ -817,6 +794,41 @@ def _sub_op(
817794
verdict_tok.append(
818795
onehot_lookup(nxt, verdict_to_token, embed(_thinking_text(_EQUAL)))
819796
)
797+
return verdict_tok
798+
799+
800+
def _sub_op(
801+
rope: RopeConfig,
802+
embedding: Embedding,
803+
A: list[Node],
804+
B: list[Node],
805+
n: int,
806+
steps_since: Node,
807+
) -> tuple[list[Node], list[Node]]:
808+
"""Streamed subtraction.
809+
810+
Three streamed thinking phases precede the answer:
811+
812+
1. **comparison** (MSB-first) — a 3-state less/equal/greater verdict folded
813+
one column at a time, each column reading the prior verdict from the
814+
previous emitted token; the last (LSB) verdict is the overall ``A ≥ B``.
815+
2. **borrow sweep** (LSB-first) — ``|A - B|`` by a borrow fold over
816+
``bigger_k = (a_k if A≥B else b_k)`` / ``smaller_k``.
817+
3. **normalization** (MSB-first) — the leading-zero count of the magnitude.
818+
819+
The answer is ``n+1`` MSB-first slots: a leading sign slot (``-`` if
820+
``A < B``) then the trimmed magnitude. Verdict, borrow, and count state are
821+
all read back from the emitted prefix at statically-known offsets.
822+
"""
823+
embed = embedding.get_embedding
824+
n_state = _n_thinking(n)
825+
W = 20 # shifted column total (bigger - smaller - borrow_in + 10) ∈ 0..19
826+
827+
a_scalar = [_digit_scalar(embedding, d) for d in A] # MSB-first
828+
b_scalar = [_digit_scalar(embedding, d) for d in B]
829+
830+
# --- comparison phase (see _cmp_verdict_stream). ---
831+
verdict_tok = _cmp_verdict_stream(embedding, A, B, n)
820832

821833
# The thinking region is verdict (n) ++ borrow (n) ++ scratch (n) ++ norm
822834
# (n); the answer is the sign slot ++ n magnitude slots. Name the boundaries

modal_run.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def main(
5858
module: str = "",
5959
script: str = "",
6060
args: str = "",
61+
*,
6162
cpu_only: bool = False,
6263
) -> None:
6364
if not module and not script:

scripts/calculator_stats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def operand(prefix: str, n: int) -> list[Node]:
101101
}
102102
for name, out in ops.items():
103103
compiled = compile_headless(out, d=D, d_head=D_HEAD)
104-
print(f" {name:10s}: {compiled._n_layers} layers")
104+
print(f" {name:10s}: {compiled.n_layers} layers")
105105

106106

107107
def addition_table_figure() -> None:

scripts/clip_gaps.py

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,15 @@ def build_scene() -> tuple[PyScene, PyGameState]:
3434
return py_scene, py_state
3535

3636

37-
def main() -> None:
38-
py_scene, py_state = build_scene()
39-
tokens = expected_ar_tokens(py_scene, py_state)
40-
print(
41-
f"PIXEL_WIDTH={PIXEL_WIDTH} COLUMN_COUNT={COLUMN_COUNT} "
42-
f"VIEW_HEIGHT={VIEW_HEIGHT}"
43-
)
44-
print(f"total tokens in frame: {len(tokens)}")
37+
def _scan_clip_writes(tokens: list) -> tuple[list[tuple[int, int]], int, int, int]:
38+
"""Scan the token stream for clip writes.
4539
40+
Returns (writes, n_clip_update, n_screen_range_total,
41+
n_screen_range_after_clip) where ``writes`` is a list of
42+
(position, column) for every clip write.
43+
"""
4644
cur_col = None
4745
prev_type = None
48-
# list of (position, column) for every clip write
4946
writes: list[tuple[int, int]] = []
5047
n_clip_update = 0
5148
n_screen_range_total = 0
@@ -64,6 +61,39 @@ def main() -> None:
6461
n_screen_range_after_clip += 1
6562
writes.append((pos, cur_col if cur_col is not None else -1))
6663
prev_type = t
64+
return writes, n_clip_update, n_screen_range_total, n_screen_range_after_clip
65+
66+
67+
def _summ(name: str, xs: list[int]) -> None:
68+
"""Print a one-line summary of a distribution."""
69+
if not xs:
70+
print(f" {name}: (none)")
71+
return
72+
xs = sorted(xs)
73+
n = len(xs)
74+
75+
def p(q: float) -> int:
76+
return xs[min(n - 1, int(q * n))]
77+
78+
print(
79+
f" {name}: n={n} min={xs[0]} median={statistics.median(xs):.0f} "
80+
f"mean={statistics.mean(xs):.0f} p90={p(0.90)} p95={p(0.95)} "
81+
f"p99={p(0.99)} max={xs[-1]}"
82+
)
83+
84+
85+
def main() -> None:
86+
py_scene, py_state = build_scene()
87+
tokens = expected_ar_tokens(py_scene, py_state)
88+
print(
89+
f"PIXEL_WIDTH={PIXEL_WIDTH} COLUMN_COUNT={COLUMN_COUNT} "
90+
f"VIEW_HEIGHT={VIEW_HEIGHT}"
91+
)
92+
print(f"total tokens in frame: {len(tokens)}")
93+
94+
writes, n_clip_update, n_screen_range_total, n_screen_range_after_clip = (
95+
_scan_clip_writes(tokens)
96+
)
6797

6898
print(f"CLIP_UPDATE tokens: {n_clip_update}")
6999
print(f"SCREEN_RANGE tokens total: {n_screen_range_total}")
@@ -95,33 +125,17 @@ def main() -> None:
95125
# than this. The MIN (not the median) is what matters.
96126
winner_runnerup_gaps = same_col_gaps
97127

98-
def summ(name: str, xs: list[int]) -> None:
99-
if not xs:
100-
print(f" {name}: (none)")
101-
return
102-
xs = sorted(xs)
103-
n = len(xs)
104-
105-
def p(q: float) -> int:
106-
return xs[min(n - 1, int(q * n))]
107-
108-
print(
109-
f" {name}: n={n} min={xs[0]} median={statistics.median(xs):.0f} "
110-
f"mean={statistics.mean(xs):.0f} p90={p(0.90)} p95={p(0.95)} "
111-
f"p99={p(0.99)} max={xs[-1]}"
112-
)
113-
114128
print("\n=== (a) position-gap between consecutive writes to the SAME column ===")
115-
summ("same_col_gap", same_col_gaps)
129+
_summ("same_col_gap", same_col_gaps)
116130

117131
print("\n=== writes per column ===")
118-
summ("writes_per_col", writes_per_col)
132+
_summ("writes_per_col", writes_per_col)
119133
# columns written exactly once never produce a same-col gap; report how many.
120134
once = sum(1 for c in writes_per_col if c == 1)
121135
print(f" columns written exactly once (no gap): {once} / {len(writes_per_col)}")
122136

123137
print("\n=== (b) recency winner-vs-runner-up gap (R13: MIN is binding) ===")
124-
summ("winner_vs_runnerup_gap", winner_runnerup_gaps)
138+
_summ("winner_vs_runnerup_gap", winner_runnerup_gaps)
125139

126140
if winner_runnerup_gaps:
127141
min_gap = min(winner_runnerup_gaps)

scripts/ffn_equivalence.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def equivalence_report(
289289
d_head=d_head,
290290
d_hidden=d_hidden,
291291
)
292-
n_in = sum(w for _, _, w in c_chain._input_specs)
292+
n_in = sum(w for _, _, w in c_chain.input_specs)
293293
if input_tensor is None:
294294
input_tensor = torch.randn(n_pos, n_in)
295295
oc = c_chain(input_tensor)

scripts/intralayer_example_sweep.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,12 @@ def sweep(graphs: list[str], budget: float, n_points: int) -> dict[str, list[dic
302302
}
303303
rows.append(row)
304304

305-
def _drop(family: str) -> str:
305+
def _drop(
306+
family: str,
307+
*,
308+
fam: dict[str, dict] = fam,
309+
sound_n: int | None = sound_n,
310+
) -> str:
306311
fn = fam[family]["n"]
307312
if fn is None or sound_n is None:
308313
return " -"

scripts/investigate_onehot_leak.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,24 @@ def _fmt(x: float) -> str:
263263
return f"{x:.3e}"
264264

265265

266+
def _report_thread_spread(chain: dict[str, Node], device: torch.device) -> None:
267+
"""P2b: environment sensitivity (CPU reduction order across thread counts)."""
268+
base = None
269+
spreads = []
270+
saved_threads = torch.get_num_threads()
271+
for nthreads in (1, 2, 4, saved_threads):
272+
torch.set_num_threads(nthreads)
273+
si = sweep_carry(chain, [0.0], device)
274+
if base is None:
275+
base = si["carry"]
276+
spreads.append((nthreads, float((si["carry"] - base).abs().max())))
277+
torch.set_num_threads(saved_threads)
278+
print(
279+
"P2b carry-value spread across torch thread counts: "
280+
+ ", ".join(f"{n}t:{_fmt(sp)}" for n, sp in spreads)
281+
)
282+
283+
266284
def run(device: torch.device) -> None:
267285
torch.set_default_dtype(torch.float32)
268286
fl_002 = float((_f32(2.0) / _f32(100.0)).item())
@@ -349,20 +367,7 @@ def run(device: torch.device) -> None:
349367

350368
# ---- P2b: environment sensitivity (CPU reduction order) ----
351369
if device.type == "cpu":
352-
base = None
353-
spreads = []
354-
saved_threads = torch.get_num_threads()
355-
for nthreads in (1, 2, 4, saved_threads):
356-
torch.set_num_threads(nthreads)
357-
si = sweep_carry(chain, [0.0], device)
358-
if base is None:
359-
base = si["carry"]
360-
spreads.append((nthreads, float((si["carry"] - base).abs().max())))
361-
torch.set_num_threads(saved_threads)
362-
print(
363-
"P2b carry-value spread across torch thread counts: "
364-
+ ", ".join(f"{n}t:{_fmt(sp)}" for n, sp in spreads)
365-
)
370+
_report_thread_spread(chain, device)
366371
print()
367372

368373

scripts/measure_fusion_opportunities.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ def _subgraph_details(
229229
def analyze(
230230
output: Node,
231231
name: str,
232+
*,
232233
detail_subgraphs: bool = False,
233234
collapse: bool = False,
234235
) -> None:

scripts/proto_rmsnorm_identity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ def run_case(d: int, max_digits: int, prompt: list[str]) -> None:
107107
gain = 2.0**m
108108
output_node, embedding = create_network_parts(max_digits=max_digits)
109109
compiled = compile_headless(output_node, d=d, d_head=D_HEAD)
110-
net = compiled._net
111-
out_idx = compiled._output_indices
110+
net = compiled.net
111+
out_idx = compiled.output_indices
112112
res0 = net.get_input_res_stream(len(prompt), {"embedding_input": prompt})
113113

114114
with torch.no_grad():

scripts/rope_global_recency_validate.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -193,29 +193,24 @@ def eval_swish_pwl(w_query: float, hinges: list, y0: float, K: float) -> float:
193193
# ---------------------------------------------------------------------------
194194

195195

196-
def main(machine: str = "relu") -> None:
197-
theta = theta_slow(D_HEAD, BASE)
198-
print(f"Production parameters: MAX_LEN={MAX_LEN}, D_HEAD={D_HEAD}, BASE={BASE:.0e}")
199-
print(f"θ_slow = {theta:.5e}")
200-
if machine == "swiglu":
201-
print("Machine: swiglu (sharpened-hinge PL inversion)")
202-
print()
203-
204-
# 1. Monotonicity and minimum adjacent gap
196+
def _check_monotonicity(theta: float, fp32_floor: float) -> float:
197+
"""Section 1: monotonicity and minimum adjacent gap. Returns min_gap."""
205198
print("=== 1. Monotonicity and adjacent gap ===")
206199
ws = [w_of_m(m, MAX_LEN, theta) for m in range(MAX_LEN + 1)]
207200
diffs = [ws[m] - ws[m + 1] for m in range(MAX_LEN)]
208201
min_gap = min(diffs)
209202
min_gap_pos = diffs.index(min_gap)
210-
fp32_floor = 6e-8
211203
assert min_gap > 0, f"w(m) is NOT monotone! Inversion at m={min_gap_pos}"
212204
print("w(m) strictly monotone: ✓")
213205
print(f"Min adjacent-m gap = {min_gap:.3e} (at m={min_gap_pos})")
214206
print(f"fp32 weight floor = {fp32_floor:.1e}")
215207
print(f"Safety margin = {min_gap / fp32_floor:.1f}x (need >> 1)")
216208
print()
209+
return min_gap
217210

218-
# 2. PWL inverse accuracy
211+
212+
def _measure_pwl_error(machine: str, theta: float) -> float:
213+
"""Section 2: PWL inverse accuracy. Returns the worst position error."""
219214
print(f"=== 2. PWL inverse accuracy ({N_BREAKPOINTS} log-uniform breakpoints) ===")
220215
w_bps, m_bps = build_pwl_table(MAX_LEN, theta, N_BREAKPOINTS)
221216
print(f"w range: [{w_bps[0]:.6f}, {w_bps[-1]:.6f}]")
@@ -275,10 +270,14 @@ def evaluate(w: float) -> float:
275270
print("Rounding threshold: 0.5 positions")
276271
print(f"Rounding margin: {0.5 / max_err:.1f}x below threshold (need >> 1)")
277272
print()
273+
return max_err
274+
278275

279-
# 3. fp32 softmax noise contribution
276+
def _measure_fp32_error(theta: float) -> float:
277+
"""Section 3: fp32 softmax noise contribution. Returns the worst error."""
280278
print("=== 3. fp32 softmax noise → error in m ===")
281279
# At each m, |δm| ≈ |dm/dw| · |δw| where |δw| ≈ w · FP32_EPS / 2
280+
step = 10
282281
max_fp32_err = 0.0
283282
worst_fp32_m = 0
284283
eps = FP32_EPS / 2.0
@@ -297,14 +296,11 @@ def evaluate(w: float) -> float:
297296
f"Worst-case fp32 contribution: {max_fp32_err:.4f} positions "
298297
f"(at m={worst_fp32_m})"
299298
)
299+
return max_fp32_err
300300

301-
combined = max_err + max_fp32_err
302-
print(f"Combined (PWL + fp32): {combined:.4f} positions")
303-
print("Rounding threshold: 0.5")
304-
print(f"Rounding margin: {0.5 / combined:.1f}x")
305-
print()
306301

307-
# 4. cosine attenuation magnitude at max_len
302+
def _check_cosine_attenuation(theta: float) -> None:
303+
"""Section 4: cosine attenuation magnitude at max_len."""
308304
print("=== 4. Cosine attenuation sanity check ===")
309305
cos_at_max = math.cos(MAX_LEN * theta)
310306
print(f"cos(MAX_LEN · θ_slow) = {cos_at_max:.6f} (1% would be 0.990)")
@@ -315,6 +311,28 @@ def evaluate(w: float) -> float:
315311
)
316312
print()
317313

314+
315+
def main(machine: str = "relu") -> None:
316+
theta = theta_slow(D_HEAD, BASE)
317+
print(f"Production parameters: MAX_LEN={MAX_LEN}, D_HEAD={D_HEAD}, BASE={BASE:.0e}")
318+
print(f"θ_slow = {theta:.5e}")
319+
if machine == "swiglu":
320+
print("Machine: swiglu (sharpened-hinge PL inversion)")
321+
print()
322+
323+
fp32_floor = 6e-8
324+
min_gap = _check_monotonicity(theta, fp32_floor)
325+
max_err = _measure_pwl_error(machine, theta)
326+
max_fp32_err = _measure_fp32_error(theta)
327+
328+
combined = max_err + max_fp32_err
329+
print(f"Combined (PWL + fp32): {combined:.4f} positions")
330+
print("Rounding threshold: 0.5")
331+
print(f"Rounding margin: {0.5 / combined:.1f}x")
332+
print()
333+
334+
_check_cosine_attenuation(theta)
335+
318336
print("=== Summary ===")
319337
if machine == "swiglu":
320338
# Pass criterion: max recovered-position error below the 0.5 rounding

0 commit comments

Comments
 (0)