Expose EmbeddingGemma's tokenizer, as CoreMLLLM already does - #171
Open
gen-0s wants to merge 1 commit into
Open
Conversation
`EmbeddingGemma.load` builds an `AutoTokenizer` from `hf_model` and keeps it private. A caller that needs a token count — to window text against `config.maxSeqLen`, which `encode` otherwise truncates to silently — has no way to reach it, so it builds a second one from the same four files. For EmbeddingGemma's 262,144-entry vocab and 514,906 merges that costs about 4.5 s and 92 MB on every load, duplicating an object already resident in the process. `CoreMLLLM` in this same package already solves this with `tokenizerRef` and says so in its doc comment. This is that accessor, on the embedding class, for the same reason.
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.
EmbeddingGemma.loadbuilds anAutoTokenizerfromhf_modeland keeps itprivate. A caller that needs a token count has no way to reach it.That count is not a nicety for this model:
encodetruncates atconfig.maxSeqLensilently —— no throw, no flag, and the published
embeddinggemma-300m-coremlbundle setsmax_seq_lento 128. Anything longer is indexed as its first ~128 tokens and reports success. Splitting text into windows before callingencodeis the fix, and that needs to know how many tokens a span costs.Today the only way to get that is to build a second
AutoTokenizerover the same folder this instance already read. For EmbeddingGemma's 262,144-entry vocab and 514,906 merges that measures 1.9 s and +92 MB in our app, duplicating an object already resident in the process.CoreMLLLMin this same package already solves exactly this, atSources/CoreMLLLM/CoreMLLLM.swift:161-163:This PR is that accessor on
EmbeddingGemma, for the same reason. One line plus a doc comment; no behaviour change, no new dependency, nothing else touched.Happy to also send a follow-up making the truncation visible (returning the token count, or throwing when
ids.count > L) if you'd take it — that's the underlying defect, and this accessor only lets callers work around it.