3737the shared parse, comparison, and leading-zero trim are all constant-depth.)
3838"""
3939
40- from typing import Dict , List , Tuple
41-
4240import torch
4341
4442from torchwright .graph import Embedding , Linear , Node , RopeConfig
4543from 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
4844from torchwright .ops .attention_ops import get_prev_value
4945from 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
5452from 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
6159from torchwright .ops .swiglu .onehot_table import onehot_lookup
6260from 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
142140def 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``,
0 commit comments