perf: incremental candidate counting for disconnected training#42
Open
AmitMY wants to merge 1 commit into
Open
perf: incremental candidate counting for disconnected training#42AmitMY wants to merge 1 commit into
AmitMY wants to merge 1 commit into
Conversation
c92c053 to
6627325
Compare
The training loop rebuilt Counter(graph.get_merges()) from scratch every step — recounting the whole forest, even the subgraphs the chosen merge never touched. For a disconnected forest (BPE/BNE, where each merge hits only the few words containing the pair) almost all of it is wasted. _train_incremental keeps each subgraph's candidate counts plus a running global total, and after a merge updates only the subgraphs that contained it (located via an index). total is summed in subgraph order, so picking the first max-score candidate reproduces max(Counter(...), key=score) exactly, including tie-breaks — output is byte-identical. Gated to the disconnected case (Tokenizer passes incremental=not connected); connected/draw/verbose keep the plain recount loop, so Boundless/SuperBPE are unchanged. The two loops share _steps() for the progress scaffolding; their cores differ (Counter-rebuild vs total+index update). Trade-off: the persistent count state raises peak memory. Stacked on the bytes-Node change: BPE ~6.6x, BNE ~7.4x over the original baseline. 138 tests pass; digests identical. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
6627325 to
25b3773
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The training loop rebuilt
Counter(graph.get_merges())from scratch every step — re-counting and re-hashing every candidate in the whole forest, even the subgraphs the chosen merge never touched. Profiling showed this recount is ~70% of training time. For a disconnected forest (BPE/BNE — each merge hits only the few words containing the pair) almost all of it is wasted._train_incrementalkeeps each subgraph's candidate counts plus a running globaltotal, and after a merge updates only the subgraphs that contained it (located via anindex):Correctness:
totalis summed in subgraph order, so picking the first max-score candidate reproducesmax(Counter(graph.get_merges()), key=score)exactly, including tie-breaks. Output is byte-identical — merge digests unchanged, 138 tests pass.Gated to the disconnected case (
Tokenizerpassesincremental=not connected); connected / draw / verbose keep the plain recount loop, so Boundless and SuperBPE are unchanged (incremental doesn't help when a merge touches most of the few large components).Trade-off: the persistent per-subgraph count state (
comp_counts+index+total) raises peak memory ~3× for the disconnected tokenizers — a deliberate speed-for-memory choice for the dominant BPE/BNE path.🤖 Generated with Claude Code