refactor(tokenizer): drop inert __slots__, inline single-use num_embeddings - #9
Merged
Merged
Conversation
…ddings __slots__ listed three attributes but did nothing: no ancestor of PreTrainedTokenizer defines __slots__, so instances carry a __dict__ and the declaration saves no memory. Verified by walking the MRO for a __slots__ definition. It read as an optimization that was not one. CharacterEmbedding.num_embeddings was set and then read once, two lines later, to build the embedding; inlined the 256. Dropped the _encode / _encode_and_truncate merge that was in the first version of this branch: the split, and hoisting the attribute lookups out of the comprehensions, is deliberate for the hot tokenization path. Left a comment saying so, since nothing in the code said it and the two methods look like accidental duplication. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
AmitMY
force-pushed
the
simplify/inert-slots-and-encode-merge
branch
from
July 27, 2026 11:25
b13bba5 to
fdcc9bd
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.
Updated: dropped the
_encode/_encode_and_truncatemerge — you were right that the split is there for speed. Rebased on currentmain. What is left is the two cleanups you were happy with, plus a comment so the split does not get "simplified" again.1.
__slots__was inert- __slots__ = ('_struct_format', '_bytes_per_unit', '_eos_encoded')No ancestor of
PreTrainedTokenizerdefines__slots__, so instances carry a__dict__regardless and the declaration saved no memory. Verified by walking the MRO looking for a__slots__definition — none. It read as a memory optimization that was not one.2.
num_embeddingsinlinedself.num_embeddings = 256was set, then read exactly once two lines later to build the embedding. OnlyCharacterEmbeddingreferenced it — thenum_embeddingsin the tests isnn.Embedding's own attribute, untouched.3. A comment on why the encode split exists
The reason I proposed merging them is that nothing in the code said not to: two methods with near-identical bodies read as accidental duplication. So:
An addition rather than a deletion, but it is the kind that prevents a future regression.
Also dropped from this PR
Inlining
_get_generation_params— it saves ~5 lines but deletes a unit-tested seam. Six tests call it directly; without it those cases could only run throughgenerate(), which needs a real model and would move them to the slow integration path.Verification
ruff check .clean.🤖 Generated with Claude Code
Note
Low Risk
Small refactors with no behavior change to tokenization or embedding math; risk is limited to maintainability and comments.
Overview
Removes
UTFTokenizer.__slots__, which did not reduce memory becausePreTrainedTokenizerancestors still use a normal instance dict.In
CharacterEmbedding, drops thenum_embeddings = 256indirection and constructsnn.Embedding(256, …)directly.Documents
_encode_and_truncateas the hot path and keeps local hoisting ofencoding,_bytes_per_unit, and_eos_encodedfor truncation;_encodeand the separate truncate helper remain as in this diff.Reviewed by Cursor Bugbot for commit fdcc9bd. Bugbot is set up for automated code reviews on this repo. Configure here.