Cache integer/decimal token sets in ConstrainedJSONGenerator (like the string set already is) - #220
Open
oscarbc96 wants to merge 1 commit into
Open
Cache integer/decimal token sets in ConstrainedJSONGenerator (like the string set already is)#220oscarbc96 wants to merge 1 commit into
oscarbc96 wants to merge 1 commit into
Conversation
… are ConstrainedJSONGenerator.init unconditionally called buildValidIntegerTokens/buildValidDecimalTokens on every construction, each scanning the full vocabulary (0..<backend.vocabSize) and, per token, calling isSpecialToken (two tokenizer decodes) and tokenText (one more) - three decode calls per vocab entry. On a 151,936-token vocabulary (Qwen3-1.7B) that is roughly 456K decode calls per scan, ~911K for the pair, and it ran again on every single ConstrainedJSONGenerator construction regardless of whether the schema being constrained contained any numeric field at all. buildValidStringTokens already memoizes its result in StringTokenCache, keyed by tokenizer identity (vocabSize/eosToken/endTokens/sampleTexts). This mirrors that exact pattern for the integer and decimal scans: two new sibling dictionaries on StringTokenCache, guard-and-store around each build function, no other behavior change. Measured on Qwen3-1.7B-4bit/MLX, Apple Silicon: each scan drops from 1.8-1.9s (cold) to 44us/17us (cache hit) - five to six orders of magnitude. End-to-end constrained generation drops from 9.9-12.3s to 6.2-8.6s per call, a 30-40% reduction in warm decode time, with byte-identical cached token sets (output verified unchanged across runs).
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.
Problem
ConstrainedJSONGenerator.initunconditionally callsbuildValidIntegerTokensand
buildValidDecimalTokenson every construction (Sources/AnyLanguageModel/Shared/StructuredGeneration.swift:110-111).Each does a full
0 ..< backend.vocabSizescan and, per token, callsbackend.isSpecialToken(token)andbackend.tokenText(token). On the MLXbackend,
isSpecialTokencallstokenizer.decode(tokens:skipSpecialTokens:)twice and
tokenTextcalls it once more — three tokenizer decode calls pervocab entry.
On a 151,936-token vocabulary (Qwen3-1.7B), that's ~456K decode calls per
scan, ~911K for the pair, on every single
ConstrainedJSONGeneratorconstruction — regardless of whether the schema being constrained contains
any numeric field at all (the scan isn't gated on schema content).
buildValidStringTokensalready solves this exact problem: it's memoized inStringTokenCache, keyed by tokenizer identity (vocabSize/eosToken/endTokens/sampleTexts).The integer and decimal scans had no such cache.
Fix
Mirror the existing
StringTokenCachepattern for the integer and decimalscans: two new sibling
Locked<[Key: Set<Int>]>dictionaries, with aguard-and-store block around each build function. No other behavior change.
Measured (Qwen3-1.7B-4bit/MLX, Apple Silicon, M-series)
magnitude).
ConstrainedJSONGeneratorconstruction): 9.9–12.3s → 6.2–8.6s, a ~30–40% reduction in the warm decode
floor.
uncached computation, so this cannot itself introduce nondeterminism.
Testing
git apply --checkverified clean against0.8.0(163f385). Manualinstrumentation (temporary
ContinuousClockprints, not part of this diff)confirmed cache hits on the second call within the same process. No existing
test coverage exercises
ConstrainedJSONGeneratortiming directly, so thisis a pure perf fix with no behavior change to verify against.