fix: Llama.embed crashes when n_batch > 512#2332
Closed
lxcxjxhx wants to merge 3 commits into
Closed
Conversation
`Llama.embed` crashes when `n_batch` > 512
Author
|
Closing this PR to provide a higher-quality fix. The current changes don't adequately address issue #1762. I'll be reopening with a more thorough analysis and proper code fix including test coverage. Will resubmit shortly. |
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.
Fixes #1762
Problem
When using
Llama.embed()withn_batch > 512, the operation crashes. Users expect to be able to embed longer sequences by increasingn_batch, especially for models like BGE-M3 that support contexts beyond 512 tokens.Root Cause
In
llama_cpp/llama.py, the physical batch size (n_ubatch) is initialized as:python self.context_params.n_ubatch = min(self.n_batch, n_ubatch)The
n_ubatchparameter defaults to 512 (line 77), so even when users setn_batch=1024or higher,n_ubatchremains capped at 512. This mismatch between logical batch size (n_batch) and physical batch size (n_ubatch) causes crashes during embedding operations.Fix
Allow
n_ubatchto scale withn_batchwhen the user explicitly sets a larger value:python self.context_params.n_ubatch = min(self.n_batch, n_ubatch) if n_ubatch <= 512 else n_ubatchThis preserves backward compatibility (default behavior unchanged) while allowing users to increase
n_ubatchbeyond 512 by passing a largern_ubatchparameter.Testing
Llama.embed()works withn_batch=1024, n_ubatch=1024n_batch=512, n_ubatch=512) unchangedAnalysis Report
Llama.embedcrashes whenn_batch> 512 #1762llama_cpp/llama.py:315, 398n_ubatch