fix: retry with token-level truncation on ONNX OOM in embedding worker#457
Merged
Conversation
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.
Summary
Fixes ONNX Runtime OOM errors (e.g.
284432024) that occur when embedding dense-token content like code, base64, CJK text, or JSON with short keys, where the chars-per-token ratio is much lower than the ~4 assumed for English prose.Problem
The existing character-level pre-truncation (
LOCAL_MAX_CHARS = 16384, assuming ~4 chars/token) can produce 6000-8000+ tokens for dense content — well above the ~4096 safe threshold for ONNX inference. TheFeatureExtractionPipelinein transformers.js hardcodes{ truncation: true }without forwardingmax_length, so its built-in truncation caps at the model max (8192 tokens), which still OOMs.Fix
Instead of aggressively pre-truncating every request, the worker now:
isOomError()detectorencode → slice → decode), progressively halving the limit: full → 4096 → 2048 → 1024 tokensThis preserves maximum semantic content for normal texts while adaptively handling dense-token edge cases.
Changes
runInference()helper fromprocessEmbed()for retry-abilitytruncateTexts()that uses the real tokenizer for exact content-token counting (add_special_tokens: falseto exclude[CLS]/[SEP])pipe.tokenizerduring pipeline initconsole.warnon each retry for observabilityLOCAL_MAX_CHARScomment to reference the new worker-level defense