Skip to content

perf: build merge candidates with itertools.chain#37

Merged
AmitMY merged 1 commit into
mainfrom
perf/chain-get-merges
Jun 26, 2026
Merged

perf: build merge candidates with itertools.chain#37
AmitMY merged 1 commit into
mainfrom
perf/chain-get-merges

Conversation

@AmitMY

@AmitMY AmitMY commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

UnconnectedGraphs.get_merges is the source the trainer's Counter consumes every training step. It flattened subgraph candidates with a Python-level loop:

for subgraph in self.subgraphs:
    yield from subgraph.get_merges()

itertools.chain.from_iterable does the same flattening in C:

return chain.from_iterable(sg.get_merges() for sg in self.subgraphs)

Same candidates, same order → output identical (137 tests pass).

Measured back-to-back (50 texts / 200 merges; best of 3):

main this PR
BPE 0.424s 0.411s (−3%)
BNE n=4 0.829s 0.813s (−2%)
Boundless 0.506s 0.490s (−3%)

Memory unchanged.

🤖 Generated with Claude Code

UnconnectedGraphs.get_merges yielded candidates with a Python-level
`for sg: yield from sg.get_merges()` loop, run every training step to
feed the Counter. itertools.chain.from_iterable does the same flattening
in C. Same candidates, same order — output identical.

~2-3% faster across BPE/BNE/Boundless; memory unchanged. 137 tests pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@AmitMY AmitMY merged commit 1dbe2d3 into main Jun 26, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant