Skip to content

Commit d8a119e

Browse files
physicsrobclaude
andcommitted
Ruff migration: unsafe autofixes (672) + typing repairs
ruff check --fix --unsafe-fixes: return-type annotations (ANN2xx), zip(strict=False) (B905, made explicit and behavior-preserving; a strict=True audit is a follow-up), dict()/{} literals (C408), unused unpacked vars (RUF059), TYPE_CHECKING import moves (TC001/TC003), pytest composite-assert splits (PT006/PT018). The new annotations made mypy check 4 previously-untyped bodies, surfacing 19 latent type errors; fixed for real: int() on tensor .item() indices (mlp_sublayer), record union annotation (token_model), bos/eos tokens are str | None (custom tokenizer contract), typed DirectShardSink.meta/weight_map, typed _CONFIGS table. Gates: mypy green (296 files), full pkgutil import walk clean. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent caa39fc commit d8a119e

170 files changed

Lines changed: 1006 additions & 878 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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Shared scaffolding for the three calculator examples.
1+
r"""Shared scaffolding for the three calculator examples.
22
33
``calculator_simple`` and ``calculator_advanced`` are two *independent*
44
implementations of the same calculator: they differ only in their arithmetic —
@@ -185,7 +185,7 @@ def compare_digit_seqs(
185185

186186
flags = [
187187
onehot_lookup(concat([a, b]), pair_table, default_flag)
188-
for a, b in zip(seq1, seq2) # MSB-first
188+
for a, b in zip(seq1, seq2, strict=False) # MSB-first
189189
]
190190
# Sharpen each flag to an exact three-level value: gt = 1 iff flag > 0.5,
191191
# lt = 1 iff flag < -0.5 (both bit-exact in saturation; the flag sits
@@ -215,7 +215,7 @@ def parse_expression(
215215
embedding: Embedding,
216216
max_digits: int,
217217
) -> tuple[list[Node], list[Node], Node, Node, Node, Node]:
218-
"""Parse ``"A op B\\n"`` from the token stream, at constant depth.
218+
r"""Parse ``"A op B\\n"`` from the token stream, at constant depth.
219219
220220
Returns ``(first, second, is_plus, is_minus, is_times, saw_newline)``:
221221
the two operand digit windows (MSB-first, zero-padded to ``max_digits``),
@@ -383,13 +383,13 @@ def build_calculator(
383383

384384
# --- Addition: pad each operand by one digit so the top carry has a home. ---
385385
add_seq = _pad_result(
386-
embedding, add_digit_seqs(embedding, [zero] + first, [zero] + second), seq_len
386+
embedding, add_digit_seqs(embedding, [zero, *first], [zero, *second]), seq_len
387387
)
388388

389389
# --- Subtraction: |A - B| by a borrow fold, sign from the comparison. ---
390390
a_ge_b = compare_digit_seqs(embedding, first, second)
391-
bigger = [select(a_ge_b, a, b) for a, b in zip(first, second)]
392-
smaller = [select(a_ge_b, b, a) for a, b in zip(first, second)]
391+
bigger = [select(a_ge_b, a, b) for a, b in zip(first, second, strict=False)]
392+
smaller = [select(a_ge_b, b, a) for a, b in zip(first, second, strict=False)]
393393
sub_seq = _pad_result(
394394
embedding, subtract_digit_seqs(embedding, bigger, smaller), seq_len
395395
)

examples/adder.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""3-digit adder using embedding-space arithmetic.
1+
r"""3-digit adder using embedding-space arithmetic.
22
33
Parses "A+B\\n" where A and B are up to 3-digit numbers, and outputs their
44
sum autoregressively. All arithmetic is done digit-by-digit in embedding
@@ -35,9 +35,15 @@
3535

3636
def create_network_parts() -> tuple[Node, Embedding]:
3737
"""Build the 3-digit adder graph and return (output_node, embedding)."""
38-
vocab = list(
39-
" 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-+="
40-
) + ["\n", "<bos>", "<eos>", "default"]
38+
vocab = [
39+
*list(
40+
" 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-+="
41+
),
42+
"\n",
43+
"<bos>",
44+
"<eos>",
45+
"default",
46+
]
4147
embedding = create_embedding(vocab=vocab)
4248
rope = create_rope_config(d_head=D_HEAD, max_positions=MAX_POSITIONS)
4349

@@ -58,8 +64,9 @@ def create_network_parts() -> tuple[Node, Embedding]:
5864
second_num_digits = num_seq.get_digits_at_event(is_end_of_second_num)
5965

6066
# --- Phase 2: Add digit-by-digit with carry propagation ---
61-
sum_digits = sum_digit_seqs(embedding, first_num_digits, second_num_digits) + [
62-
create_literal_value(embedding.get_embedding("<eos>"))
67+
sum_digits = [
68+
*sum_digit_seqs(embedding, first_num_digits, second_num_digits),
69+
create_literal_value(embedding.get_embedding("<eos>")),
6370
]
6471
sum_digits = remove_leading_0s(embedding, sum_digits, max_removals=max_digits - 1)
6572

examples/adder_1digit.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""1-digit adder: the simplest example of programming a transformer.
1+
r"""1-digit adder: the simplest example of programming a transformer.
22
33
Parses "A+B\\n" where A and B are single digits, and outputs their sum.
44
All arithmetic is done via a single 100-entry lookup table that maps
@@ -67,9 +67,15 @@ def sum_numbers(embedding: Embedding, num1: Node, num2: Node) -> tuple[Node, Nod
6767

6868
def create_network() -> Unembedding:
6969
# --- Phase 1: Vocabulary and parsing ---
70-
vocab = list(
71-
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-+="
72-
) + ["\n", "<bos>", "<eos>", "default"]
70+
vocab = [
71+
*list(
72+
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-+="
73+
),
74+
"\n",
75+
"<bos>",
76+
"<eos>",
77+
"default",
78+
]
7379
embedding = create_embedding(vocab=vocab)
7480
rope = create_rope_config(d_head=D_HEAD, max_positions=MAX_POSITIONS)
7581

@@ -90,7 +96,7 @@ def create_network() -> Unembedding:
9096
# Latch: remember the digit at "=".
9197
second_num = get_prev_value(rope, just_completed_num, is_second_num)
9298

93-
summed, carry = sum_numbers(embedding, first_num, second_num)
99+
summed, _carry = sum_numbers(embedding, first_num, second_num)
94100

95101
# --- Phase 3: Output ---
96102
return create_unembedding(summed, embedding)

examples/adder_v2.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,22 @@
4444

4545

4646
def create_network(max_digits: int = 3) -> Unembedding:
47-
"""Create a v2 adder: parses "A+B\\n", outputs result digits autoregressively.
47+
r"""Create a v2 adder: parses "A+B\\n", outputs result digits autoregressively.
4848
4949
Three phases:
5050
1. Parse: extract digit embeddings for A and B from the token stream
5151
2. Compute: embeddings → scalars → add → scalars → embeddings
5252
3. Output: emit result digits left-to-right, then <eos>
5353
"""
54-
vocab = list(
55-
" 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-+="
56-
) + ["\n", "<bos>", "<eos>", "default"]
54+
vocab = [
55+
*list(
56+
" 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-+="
57+
),
58+
"\n",
59+
"<bos>",
60+
"<eos>",
61+
"default",
62+
]
5763
embedding = create_embedding(vocab=vocab)
5864
rope = create_rope_config(d_head=D_HEAD, max_positions=MAX_POSITIONS)
5965

@@ -83,8 +89,9 @@ def create_network(max_digits: int = 3) -> Unembedding:
8389
result_digits = [scalar_to_embedding(d, embedding) for d in digit_scalars]
8490

8591
# --- Phase 3: Format and output ---
86-
result_digits = result_digits + [
87-
create_literal_value(embedding.get_embedding("<eos>"))
92+
result_digits = [
93+
*result_digits,
94+
create_literal_value(embedding.get_embedding("<eos>")),
8895
]
8996
result_digits = remove_leading_0s(embedding, result_digits, max_removals=max_digits)
9097

examples/binary_increment.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Binary increment.
1+
r"""Binary increment.
22
33
Parses a binary string like "1011\\n" and outputs the incremented value
44
"1100". Handles carry propagation through consecutive trailing 1s
@@ -48,7 +48,7 @@ def create_network_parts(
4848
max_bits: Maximum number of input bits (handles up to max_bits-bit
4949
binary numbers). Output may be max_bits+1 bits on overflow.
5050
"""
51-
vocab = list("01 ") + ["\n", "<bos>", "<eos>"]
51+
vocab = [*list("01 "), "\n", "<bos>", "<eos>"]
5252
embedding = create_embedding(vocab=vocab)
5353
rope = create_rope_config(d_head=D_HEAD, max_positions=MAX_POSITIONS)
5454
embed = embedding.get_embedding
@@ -92,7 +92,7 @@ def create_network_parts(
9292
overflow_bit = select(carry[max_bits], one_embed, zero_embed)
9393

9494
# --- Build output (MSB first) ---
95-
output_seq = [overflow_bit] + list(reversed(new_bits)) + [eos_embed]
95+
output_seq = [overflow_bit, *list(reversed(new_bits)), eos_embed]
9696
output_seq = remove_leading_0s(embedding, output_seq, max_removals=max_bits)
9797

9898
output_node = output_sequence(

examples/caesar_cipher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Caesar cipher with runtime shift parameter.
1+
r"""Caesar cipher with runtime shift parameter.
22
33
Parses "<shift> <letters>\\n" where <shift> is a single digit (0-9) and
44
<letters> is a sequence of lowercase letters (fixed length). Outputs each

examples/calculator_advanced.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,15 @@ def _carry_lookahead_op(
172172
key = torch.cat([ab, _state(carry, _CARRY_W)])
173173
digit_table[key] = embedding.get_embedding(str(digit_of(a, b, carry)))
174174

175-
pairs = list(reversed(list(zip(seq1, seq2)))) # LSB-first
175+
pairs = list(reversed(list(zip(seq1, seq2, strict=False)))) # LSB-first
176176
segments = [
177177
onehot_lookup(concat([a, b]), status_table, _state(_KILL, _SEG_W))
178178
for a, b in pairs
179179
]
180180
carries = carry_lookahead(segments)
181181
out_lsb = [
182182
onehot_lookup(concat([a, b, carry]), digit_table, embedding.get_embedding("0"))
183-
for (a, b), carry in zip(pairs, carries)
183+
for (a, b), carry in zip(pairs, carries, strict=False)
184184
]
185185
return list(reversed(out_lsb))
186186

examples/calculator_scratchpad.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Flat-depth, scratchpad ("thinking tokens") calculator.
1+
r"""Flat-depth, scratchpad ("thinking tokens") calculator.
22
33
``calculator_simple`` and ``calculator_advanced`` compute ``A op B`` (``+ - *``,
44
one-hot digits) with the serial carry/borrow/comparison work folded *inside the
@@ -338,7 +338,7 @@ def __init__(
338338
n_norm: int,
339339
n_answer: int,
340340
n_state: int,
341-
):
341+
) -> None:
342342
self.carry_start = 1 # first carry/verdict glyph (THINKING_START at 0)
343343
self.scratch_start = 1 + n_carry # first scratch digit
344344
self.norm_start = self.scratch_start + n_scratch # first normalization glyph
@@ -907,13 +907,13 @@ def _assemble(
907907
encodes, so every read offset lines up by construction.
908908
"""
909909
embed = embedding.get_embedding
910-
return (
911-
[create_literal_value(embed(THINKING_START))]
912-
+ thinking
913-
+ [create_literal_value(embed(RESULT))]
914-
+ answer
915-
+ [create_literal_value(embed("<eos>"))]
916-
)
910+
return [
911+
create_literal_value(embed(THINKING_START)),
912+
*thinking,
913+
create_literal_value(embed(RESULT)),
914+
*answer,
915+
create_literal_value(embed("<eos>")),
916+
]
917917

918918

919919
def _emit_by_slot_index(

examples/calculator_simple.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Calculator compiled into a transformer: ``+``, ``-``, ``*`` on integers up
1+
r"""Calculator compiled into a transformer: ``+``, ``-``, ``*`` on integers up
22
to ``max_digits`` digits, parsed from ``"A op B\\n"`` and emitted digit by
33
digit. This is the *legible* variant, built to be read line by line.
44
@@ -103,7 +103,7 @@ def digitwise_fold(
103103
default_state = init_state
104104
state = create_literal_value(init_state)
105105
out: list[Node] = []
106-
for a, b in reversed(list(zip(seq1, seq2))):
106+
for a, b in reversed(list(zip(seq1, seq2, strict=False))):
107107
key = concat([a, b, state])
108108
out.append(onehot_lookup(key, digit_table, default_digit))
109109
state = onehot_lookup(key, state_table, default_state)

examples/fibonacci.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Autoregressive Fibonacci generator.
1+
r"""Autoregressive Fibonacci generator.
22
33
After a trigger token, generates Fibonacci numbers autoregressively.
44
Each number is emitted as a fixed-width block of W digit tokens (zero-padded),
@@ -62,11 +62,7 @@ def create_network_parts(
6262
Determines the maximum representable value (10^digit_width - 1).
6363
n_terms: Number of Fibonacci terms to generate (including the two seeds).
6464
"""
65-
vocab = list("0123456789 abcdefghijklmnopqrstuvwxyz") + [
66-
"\n",
67-
"<bos>",
68-
"<eos>",
69-
]
65+
vocab = [*list("0123456789 abcdefghijklmnopqrstuvwxyz"), "\n", "<bos>", "<eos>"]
7066
embedding = create_embedding(vocab=vocab)
7167
rope = create_rope_config(d_head=D_HEAD, max_positions=MAX_POSITIONS)
7268
embed = embedding.get_embedding

0 commit comments

Comments
 (0)