Skip to content

refactor(tokenizer): drop inert __slots__, inline single-use num_embeddings - #9

Merged
AmitMY merged 1 commit into
mainfrom
simplify/inert-slots-and-encode-merge
Jul 27, 2026
Merged

refactor(tokenizer): drop inert __slots__, inline single-use num_embeddings#9
AmitMY merged 1 commit into
mainfrom
simplify/inert-slots-and-encode-merge

Conversation

@AmitMY

@AmitMY AmitMY commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Updated: dropped the _encode / _encode_and_truncate merge — you were right that the split is there for speed. Rebased on current main. 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 PreTrainedTokenizer defines __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_embeddings inlined

self.num_embeddings = 256 was set, then read exactly once two lines later to build the embedding. Only CharacterEmbedding referenced it — the num_embeddings in the tests is nn.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:

    def _encode_and_truncate(...):
        # Kept separate from _encode, and hoisting these lookups out of the comprehensions,
        # because this is the hot tokenization path
        encoding, bytes_per_unit, eos_encoded = self.encoding, self._bytes_per_unit, self._eos_encoded

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 through generate(), which needs a real model and would move them to the slow integration path.

Verification

  • 245 passed on transformers 4.57.6 (no numba) and 5.14.1 (with numba) — both padding paths.
  • 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 because PreTrainedTokenizer ancestors still use a normal instance dict.

In CharacterEmbedding, drops the num_embeddings = 256 indirection and constructs nn.Embedding(256, …) directly.

Documents _encode_and_truncate as the hot path and keeps local hoisting of encoding, _bytes_per_unit, and _eos_encoded for truncation; _encode and 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.

…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
AmitMY force-pushed the simplify/inert-slots-and-encode-merge branch from b13bba5 to fdcc9bd Compare July 27, 2026 11:25
@AmitMY AmitMY changed the title refactor(tokenizer): drop inert __slots__, merge the two encode paths refactor(tokenizer): drop inert __slots__, inline single-use num_embeddings Jul 27, 2026
@AmitMY
AmitMY merged commit 47e2a4a into main Jul 27, 2026
6 checks passed
@AmitMY
AmitMY deleted the simplify/inert-slots-and-encode-merge branch July 27, 2026 11:27
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