Skip to content

Commit 6ce52a6

Browse files
physicsrobclaude
andcommitted
Ruff migration: safe autofixes (2753)
ruff check --fix: UP006/UP045 typing modernization, import sorting, quoted annotations, D209 docstring closers, and friends. Re-formatted after fixing. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 969adfe commit 6ce52a6

225 files changed

Lines changed: 3115 additions & 2692 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: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,25 @@
3737
the shared parse, comparison, and leading-zero trim are all constant-depth.)
3838
"""
3939

40-
from typing import Dict, List, Tuple
41-
4240
import torch
4341

4442
from torchwright.graph import Embedding, Linear, Node, RopeConfig
4543
from torchwright.graph.embedding import bos_token
46-
from torchwright.ops.swiglu.arithmetic_ops import compare as _compare_scalar
47-
from torchwright.ops.linear import add_const, bool_to_01, concat, sum_nodes
4844
from torchwright.ops.attention_ops import get_prev_value
4945
from torchwright.ops.inout_nodes import (
5046
create_literal_value,
5147
create_onehot_embedding,
5248
create_rope_config,
5349
)
50+
from torchwright.ops.linear import add_const, concat
51+
from torchwright.ops.swiglu.arithmetic_ops import compare as _compare_scalar
5452
from torchwright.ops.swiglu.logic_ops import (
5553
bool_all_true,
5654
bool_any_true,
5755
bool_not,
5856
equals_vector,
5957
)
60-
from torchwright.ops.swiglu.map_select import broadcast_select, in_range, select, switch
58+
from torchwright.ops.swiglu.map_select import select, switch
6159
from torchwright.ops.swiglu.onehot_table import onehot_lookup
6260
from torchwright.ops.swiglu.sequence_ops import (
6361
IndexedRegion,
@@ -140,7 +138,7 @@ def _slice(node: Node, start: int, width: int, name: str = "slice") -> Node:
140138

141139

142140
def compare_digit_seqs(
143-
embedding: Embedding, seq1: List[Node], seq2: List[Node]
141+
embedding: Embedding, seq1: list[Node], seq2: list[Node]
144142
) -> Node:
145143
"""Lexicographic comparison at constant depth: weight the digit flags so
146144
the first difference outweighs everything after it.
@@ -174,7 +172,7 @@ def compare_digit_seqs(
174172

175173
# key = concat([a, b]) -> three-way flag. The default reads "equal"
176174
# (0.0) — a non-digit pair defers the verdict to later positions.
177-
pair_table: Dict[torch.Tensor, torch.Tensor] = {}
175+
pair_table: dict[torch.Tensor, torch.Tensor] = {}
178176
for a in range(10):
179177
for b in range(10):
180178
key = torch.cat(
@@ -216,7 +214,7 @@ def parse_expression(
216214
rope: RopeConfig,
217215
embedding: Embedding,
218216
max_digits: int,
219-
) -> Tuple[List[Node], List[Node], Node, Node, Node, Node]:
217+
) -> tuple[list[Node], list[Node], Node, Node, Node, Node]:
220218
"""Parse ``"A op B\\n"`` from the token stream, at constant depth.
221219
222220
Returns ``(first, second, is_plus, is_minus, is_times, saw_newline)``:
@@ -326,13 +324,14 @@ def parse_expression(
326324
return first, second, which_plus, which_minus, which_times, saw_newline
327325

328326

329-
def _pad_result(embedding: Embedding, digits: List[Node], seq_len: int) -> List[Node]:
327+
def _pad_result(embedding: Embedding, digits: list[Node], seq_len: int) -> list[Node]:
330328
"""Align a digit sequence into the shared result frame (free literals):
331329
leading ``"0"`` digits out to the widest answer width (``seq_len - 2``,
332330
multiplication's ``2·max_digits``), then ``<eos>`` padding. Every branch
333331
then has the same digit width, so the one post-dispatch trim's
334332
``max_removals`` cap keeps exactly one digit of an all-zero answer no
335-
matter which branch won."""
333+
matter which branch won.
334+
"""
336335
zero = create_literal_value(embedding.get_embedding("0"))
337336
eos = create_literal_value(embedding.get_embedding("<eos>"))
338337
return [zero] * (seq_len - 2 - len(digits)) + digits + [eos, eos]
@@ -342,10 +341,11 @@ def emit_result(
342341
rope: RopeConfig,
343342
embedding: Embedding,
344343
saw_newline: Node,
345-
result_digits: List[Node],
344+
result_digits: list[Node],
346345
) -> Node:
347346
"""Emit ``result_digits`` autoregressively once the newline fires, printing
348-
a space at every position before then."""
347+
a space at every position before then.
348+
"""
349349
return output_sequence(
350350
rope, saw_newline, result_digits, embedding.get_embedding(" ")
351351
)
@@ -357,7 +357,7 @@ def build_calculator(
357357
add_digit_seqs,
358358
subtract_digit_seqs,
359359
multiply_digit_seqs,
360-
) -> Tuple[Node, Embedding]:
360+
) -> tuple[Node, Embedding]:
361361
"""Assemble the calculator graph from one variant's arithmetic.
362362
363363
The three depth-differentiating algorithms — ``add_digit_seqs``,

examples/adder.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,16 @@
99
a single number, adds as a scalar, and converts back.
1010
"""
1111

12-
from typing import Tuple
13-
14-
import torch
15-
16-
from torchwright.graph import Node, Embedding, RopeConfig
12+
from torchwright.graph import Embedding, Node
1713
from torchwright.graph.embedding import Unembedding
1814
from torchwright.ops.inout_nodes import (
19-
create_literal_value,
2015
create_embedding,
16+
create_literal_value,
2117
create_rope_config,
2218
create_unembedding,
2319
)
24-
from torchwright.ops.swiglu.logic_ops import equals_vector
2520
from torchwright.ops.swiglu.embedding_arithmetic import sum_digit_seqs
21+
from torchwright.ops.swiglu.logic_ops import equals_vector
2622
from torchwright.ops.swiglu.sequence_ops import (
2723
NumericSequence,
2824
output_sequence,
@@ -37,7 +33,7 @@
3733
MAX_POSITIONS = 512
3834

3935

40-
def create_network_parts() -> Tuple[Node, Embedding]:
36+
def create_network_parts() -> tuple[Node, Embedding]:
4137
"""Build the 3-digit adder graph and return (output_node, embedding)."""
4238
vocab = list(
4339
" 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-+="

examples/adder_1digit.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,18 @@
88
embedding_arithmetic for the general multi-digit versions.
99
"""
1010

11-
from typing import Tuple
12-
1311
import torch
1412

15-
from torchwright.graph import Node, Embedding
13+
from torchwright.graph import Embedding, Node
1614
from torchwright.graph.embedding import Unembedding
17-
from torchwright.ops.linear import concat
1815
from torchwright.ops.attention_ops import attend_to_offset, get_prev_value
1916
from torchwright.ops.inout_nodes import (
20-
create_literal_value,
2117
create_embedding,
18+
create_literal_value,
2219
create_rope_config,
2320
create_unembedding,
2421
)
22+
from torchwright.ops.linear import concat
2523
from torchwright.ops.swiglu.logic_ops import equals_vector
2624
from torchwright.ops.swiglu.map_select import map_to_table, select
2725

@@ -41,9 +39,8 @@ def check_is_num(embedding_value: Node, embedding: Embedding) -> Node:
4139
)
4240

4341

44-
def sum_numbers(embedding: Embedding, num1: Node, num2: Node) -> Tuple[Node, Node]:
45-
"""
46-
Adds num1 with num2.
42+
def sum_numbers(embedding: Embedding, num1: Node, num2: Node) -> tuple[Node, Node]:
43+
"""Adds num1 with num2.
4744
Assumes num1 and num2 are both embedding-valued nodes.
4845
return result as embedding-valued node and carry as boolean.
4946
"""

examples/adder_v2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
"""
1919

2020
from torchwright.graph.embedding import Unembedding
21-
from torchwright.ops.linear import add
2221
from torchwright.ops.inout_nodes import (
23-
create_literal_value,
2422
create_embedding,
23+
create_literal_value,
2524
create_rope_config,
2625
create_unembedding,
2726
)
27+
from torchwright.ops.linear import add
2828
from torchwright.ops.swiglu.logic_ops import equals_vector
2929
from torchwright.ops.swiglu.scalar_encoding import (
3030
digits_to_number,

examples/binary_increment.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,17 @@
1717
Output: 1 0 0 0 0 (overflow adds a bit)
1818
"""
1919

20-
from typing import Tuple
21-
2220
import torch
2321

24-
from torchwright.graph import Node, Embedding, RopeConfig
22+
from torchwright.graph import Embedding, Node
2523
from torchwright.graph.embedding import Unembedding
2624
from torchwright.ops.attention_ops import attend_to_offset, get_prev_value
2725
from torchwright.ops.inout_nodes import (
28-
create_literal_value,
2926
create_embedding,
27+
create_literal_value,
3028
create_rope_config,
3129
create_unembedding,
3230
)
33-
3431
from torchwright.ops.swiglu.logic_ops import bool_all_true, equals_vector
3532
from torchwright.ops.swiglu.map_select import map_to_table, select
3633
from torchwright.ops.swiglu.sequence_ops import output_sequence, remove_leading_0s
@@ -44,7 +41,7 @@
4441

4542
def create_network_parts(
4643
max_bits: int = 4,
47-
) -> Tuple[Node, Embedding]:
44+
) -> tuple[Node, Embedding]:
4845
"""Build the binary increment computation graph.
4946
5047
Args:

examples/caesar_cipher.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,18 @@
1616
with switch dispatch.
1717
"""
1818

19-
from typing import Tuple
20-
2119
import torch
2220

23-
from torchwright.graph import Node, Embedding, Concatenate, RopeConfig
21+
from torchwright.graph import Embedding, Node
2422
from torchwright.graph.embedding import Unembedding
25-
from torchwright.ops.linear import concat
2623
from torchwright.ops.attention_ops import attend_to_offset, get_prev_value
2724
from torchwright.ops.inout_nodes import (
28-
create_literal_value,
2925
create_embedding,
26+
create_literal_value,
3027
create_rope_config,
3128
create_unembedding,
3229
)
30+
from torchwright.ops.linear import concat
3331
from torchwright.ops.swiglu.logic_ops import equals_vector
3432
from torchwright.ops.swiglu.map_select import map_to_table
3533
from torchwright.ops.swiglu.sequence_ops import output_sequence
@@ -47,7 +45,7 @@
4745

4846
def create_network_parts(
4947
max_letters: int = MAX_LETTERS,
50-
) -> Tuple[Node, Embedding]:
48+
) -> tuple[Node, Embedding]:
5149
"""Build the Caesar cipher computation graph.
5250
5351
Args:

0 commit comments

Comments
 (0)