Skip to content

Commit a1d0cdd

Browse files
physicsrobclaude
andcommitted
Ruff migration wave 1: manual fixes across all packages (9 parallel agents)
~2,300 violations fixed by hand across examples/, scripts/, tests/, torchwright/: D205/D415 docstring shaping, ANN annotations in non-test code, RUF001/2/3 unicode-to-ASCII conformance, PLR2004 constant extraction, PTH pathlib migration, E501 splits, E741 renames, ARG underscore renames for callback slots, ERA001 dead-code removal, PT018 assert splits, and singles. Integration repairs after the wave: export.py helpers re-annotated HeadlessTransformer (not CompiledHeadless), **attrs/**changes -> Any at ONNX emission and SchedulingProblem._replace, SupportsInt cast in replay_plan, IntVar cast at _and_presence, past_kvs variance widening. Recovered bucket-8's swiglu fixes from the stash a concurrent agent created mid-wave (git stash in the shared worktree captured ~140 in-flight files; the seven swiglu files were never restored). Gates: ruff 7846 -> 593 (remaining = deferred wave-2 families + reported false positives), mypy green, full import walk, format stable. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent d8a119e commit a1d0cdd

234 files changed

Lines changed: 5577 additions & 3794 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_common.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
the shared parse, comparison, and leading-zero trim are all constant-depth.)
3838
"""
3939

40+
from collections.abc import Callable
41+
4042
import torch
4143

4244
from torchwright.graph import Embedding, Linear, Node, RopeConfig
@@ -140,13 +142,13 @@ def _slice(node: Node, start: int, width: int, name: str = "slice") -> Node:
140142
def compare_digit_seqs(
141143
embedding: Embedding, seq1: list[Node], seq2: list[Node]
142144
) -> Node:
143-
"""Lexicographic comparison at constant depth: weight the digit flags so
144-
the first difference outweighs everything after it.
145+
"""Lexicographic comparison at constant depth.
145146
146-
This is how a person compares equal-width numbers — walk from the most
147-
significant digit, and the first position where the digits differ
148-
decides — encoded arithmetically in three FFN stages regardless of
149-
digit count:
147+
Weight the digit flags so the first difference outweighs everything
148+
after it. This is how a person compares equal-width numbers — walk
149+
from the most significant digit, and the first position where the
150+
digits differ decides — encoded arithmetically in three FFN stages
151+
regardless of digit count:
150152
151153
1. one ``onehot_lookup`` per position maps the concatenated digit pair
152154
to a three-way flag: ``+1`` (a > b), ``0`` (equal), ``-1`` (a < b)
@@ -325,8 +327,9 @@ def parse_expression(
325327

326328

327329
def _pad_result(embedding: Embedding, digits: list[Node], seq_len: int) -> list[Node]:
328-
"""Align a digit sequence into the shared result frame (free literals):
329-
leading ``"0"`` digits out to the widest answer width (``seq_len - 2``,
330+
"""Align a digit sequence into the shared result frame (free literals).
331+
332+
Leading ``"0"`` digits out to the widest answer width (``seq_len - 2``,
330333
multiplication's ``2·max_digits``), then ``<eos>`` padding. Every branch
331334
then has the same digit width, so the one post-dispatch trim's
332335
``max_removals`` cap keeps exactly one digit of an all-zero answer no
@@ -343,20 +346,24 @@ def emit_result(
343346
saw_newline: Node,
344347
result_digits: list[Node],
345348
) -> Node:
346-
"""Emit ``result_digits`` autoregressively once the newline fires, printing
347-
a space at every position before then.
349+
"""Emit ``result_digits`` autoregressively once the newline fires.
350+
351+
Prints a space at every position before then.
348352
"""
349353
return output_sequence(
350354
rope, saw_newline, result_digits, embedding.get_embedding(" ")
351355
)
352356

353357

358+
_DigitSeqOp = Callable[[Embedding, list[Node], list[Node]], list[Node]]
359+
360+
354361
def build_calculator(
355362
max_digits: int,
356363
*,
357-
add_digit_seqs,
358-
subtract_digit_seqs,
359-
multiply_digit_seqs,
364+
add_digit_seqs: _DigitSeqOp,
365+
subtract_digit_seqs: _DigitSeqOp,
366+
multiply_digit_seqs: _DigitSeqOp,
360367
) -> tuple[Node, Embedding]:
361368
"""Assemble the calculator graph from one variant's arithmetic.
362369

examples/adder.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def create_network_parts() -> tuple[Node, Embedding]:
3737
"""Build the 3-digit adder graph and return (output_node, embedding)."""
3838
vocab = [
3939
*list(
40-
" 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-+="
40+
" 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
41+
"!@#$%^&*()-+="
4142
),
4243
"\n",
4344
"<bos>",

examples/adder_1digit.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,26 @@ def check_is_num(embedding_value: Node, embedding: Embedding) -> Node:
3939
)
4040

4141

42+
_DECIMAL_BASE = 10
43+
44+
4245
def sum_numbers(embedding: Embedding, num1: Node, num2: Node) -> tuple[Node, Node]:
43-
"""Adds num1 with num2.
44-
Assumes num1 and num2 are both embedding-valued nodes.
45-
return result as embedding-valued node and carry as boolean.
46+
"""Add num1 with num2.
47+
48+
Assumes num1 and num2 are both embedding-valued nodes. Returns the
49+
result as an embedding-valued node and the carry as a boolean.
4650
"""
4751
result_table = {}
4852
carry_table = {}
49-
for A in range(10):
50-
for B in range(10):
53+
for A in range(_DECIMAL_BASE):
54+
for B in range(_DECIMAL_BASE):
5155
numcat = torch.cat(
5256
[embedding.get_embedding(str(A)), embedding.get_embedding(str(B))]
5357
)
54-
result_table[numcat] = embedding.get_embedding(str((A + B) % 10))
55-
carry_table[numcat] = torch.tensor([1.0 if (A + B) >= 10 else -1.0])
58+
result_table[numcat] = embedding.get_embedding(str((A + B) % _DECIMAL_BASE))
59+
carry_table[numcat] = torch.tensor(
60+
[1.0 if (A + B) >= _DECIMAL_BASE else -1.0]
61+
)
5662

5763
num1_num2 = concat([num1, num2])
5864
return (

examples/adder_v2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ def create_network(max_digits: int = 3) -> Unembedding:
5353
"""
5454
vocab = [
5555
*list(
56-
" 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-+="
56+
" 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
57+
"!@#$%^&*()-+="
5758
),
5859
"\n",
5960
"<bos>",

examples/calculator_advanced.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
"""Depth-optimized calculator: the same calculator as ``calculator_simple`` but
2-
with the logarithmic-depth arithmetic a hardware multiplier uses.
1+
"""Depth-optimized calculator.
2+
3+
The same calculator as ``calculator_simple`` but with the logarithmic-depth
4+
arithmetic a hardware multiplier uses.
35
46
This is one of two standalone calculator implementations. It computes the same
57
functions as ``calculator_simple`` but trades the legible serial folds for
@@ -39,6 +41,8 @@
3941
exact with no dropped nonzero carry.
4042
"""
4143

44+
from collections.abc import Callable
45+
4246
import torch
4347

4448
from examples._calculator_common import (
@@ -84,6 +88,7 @@
8488

8589
_KILL, _PROPAGATE, _GENERATE = 0, 1, 2
8690
_SEG_W = 3
91+
_DECIMAL_BASE = 10
8792

8893

8994
def _combine_table() -> dict[torch.Tensor, torch.Tensor]:
@@ -139,8 +144,10 @@ def carry_lookahead(segments: list[Node]) -> list[Node]:
139144
_state(_GENERATE, _SEG_W): _state(_YES, _CARRY_W),
140145
}
141146
carries: list[Node] = [create_literal_value(_state(_NO, _CARRY_W))]
142-
for j in range(1, n):
143-
carries.append(onehot_lookup(prefix[j - 1], generated, _state(_NO, _CARRY_W)))
147+
carries.extend(
148+
onehot_lookup(prefix[j - 1], generated, _state(_NO, _CARRY_W))
149+
for j in range(1, n)
150+
)
144151
return carries
145152

146153

@@ -149,8 +156,8 @@ def _carry_lookahead_op(
149156
seq1: list[Node],
150157
seq2: list[Node],
151158
*,
152-
status_of,
153-
digit_of,
159+
status_of: Callable[[int, int], int],
160+
digit_of: Callable[[int, int, int], int],
154161
) -> list[Node]:
155162
"""Add or subtract two MSB-first digit sequences via carry-lookahead.
156163
@@ -199,9 +206,13 @@ def add_digit_seqs(
199206
seq1,
200207
seq2,
201208
status_of=lambda a, b: (
202-
_GENERATE if a + b >= 10 else _PROPAGATE if a + b == 9 else _KILL
209+
_GENERATE
210+
if a + b >= _DECIMAL_BASE
211+
else _PROPAGATE
212+
if a + b == _DECIMAL_BASE - 1
213+
else _KILL
203214
),
204-
digit_of=lambda a, b, carry: (a + b + carry) % 10,
215+
digit_of=lambda a, b, carry: (a + b + carry) % _DECIMAL_BASE,
205216
)
206217

207218

@@ -264,7 +275,7 @@ def _digit_value_proj(embedding: Embedding) -> torch.Tensor:
264275
return proj
265276

266277

267-
def _make_compressor(embedding: Embedding):
278+
def _make_compressor(embedding: Embedding) -> Callable[[list[Node]], tuple[Node, Node]]:
268279
"""Return an 11:2 compressor ``(<=11 one-hot digits) -> (sum, carry)``.
269280
270281
Rather than a ``10**11``-row lookup (one neuron per input combination — fat
@@ -288,7 +299,7 @@ def _make_compressor(embedding: Embedding):
288299
for t in range(n_buckets)
289300
}
290301

291-
def compress(digits: list[Node]):
302+
def compress(digits: list[Node]) -> tuple[Node, Node]:
292303
values: list[Node] = [Linear(d, value_proj, name="digit_value") for d in digits]
293304
total = sum_nodes(values) # plain-number add of up to 11 digits, 0..99
294305
bucket = bool_to_01(in_range(total, add_const(total, 1.0), n_buckets))
@@ -359,14 +370,16 @@ def multiply_digit_seqs(
359370
# Step 2: carry-save reduction — up to _RADIX rows -> 2 rows per level, all
360371
# columns in parallel, until two rows survive.
361372
compress = _make_compressor(embedding)
373+
_FINAL_ROW_COUNT = 2
374+
_MIN_COMPRESSIBLE_CHUNK = 3
362375

363-
while len(rows) > 2:
376+
while len(rows) > _FINAL_ROW_COUNT:
364377
nxt: list[list[Node]] = []
365378
k = 0
366379
while k < len(rows):
367380
chunk = rows[k : k + _RADIX]
368381
k += len(chunk)
369-
if len(chunk) < 3: # 1 or 2 leftover rows: nothing to compress
382+
if len(chunk) < _MIN_COMPRESSIBLE_CHUNK: # 1-2 leftover rows: nothing
370383
nxt.extend(chunk)
371384
continue
372385
sum_row = [zero] * width
@@ -380,7 +393,7 @@ def multiply_digit_seqs(
380393
nxt.append(carry_row)
381394
rows = nxt
382395

383-
while len(rows) < 2: # pad to exactly two rows for the final add
396+
while len(rows) < _FINAL_ROW_COUNT: # pad to exactly two rows for the final add
384397
rows.append([zero] * width)
385398

386399
# Step 3: add the two surviving rows (MSB-first) with carry-lookahead.
@@ -392,7 +405,9 @@ def multiply_digit_seqs(
392405
def create_network_parts(
393406
max_digits: int = 3,
394407
) -> tuple[Node, Embedding]:
395-
"""The advanced calculator: the depth-optimized arithmetic wired up by
408+
"""Build the advanced calculator.
409+
410+
The depth-optimized arithmetic wired up by
396411
:func:`examples._calculator_common.build_calculator`.
397412
"""
398413
return build_calculator(

examples/calculator_memorize.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ def compiled_layers(max_digits: int, d_hidden: int = 16384) -> int:
133133

134134

135135
def _format_answer(embedding: Embedding, value: int, seq_len: int) -> torch.Tensor:
136-
"""The complete formatted answer as a flat ``seq_len * 17`` vector:
137-
sign and decimal digits, then <eos> padding — exactly what the other
136+
"""The complete formatted answer as a flat ``seq_len * 17`` vector.
137+
138+
Sign and decimal digits, then <eos> padding - exactly what the other
138139
calculators spend their comparison / borrow / trim machinery producing.
139140
"""
140141
text = str(value)

0 commit comments

Comments
 (0)