fix: auto-adjust n_ubatch when n_batch > 512 for embedding#2333
Open
lxcxjxhx wants to merge 2 commits into
Open
fix: auto-adjust n_ubatch when n_batch > 512 for embedding#2333lxcxjxhx wants to merge 2 commits into
lxcxjxhx wants to merge 2 commits into
Conversation
Fixes abetlen#1762 When using Llama.embed() with n_batch > 512, the kernel crashes because n_ubatch defaults to 512. Users had to manually set both n_batch and n_ubatch to the same value as a workaround. This fix automatically adjusts n_ubatch to match n_batch when n_batch exceeds the default n_ubatch value, preventing crashes and providing a more intuitive API for long-context embedding models like BGE-M3. Changes: - Added check in embed() method to auto-adjust n_ubatch if needed - Maintains backward compatibility for existing code - Enables embedding with n_batch > 512 without manual n_ubatch configuration Testing: - Verified embedding works with n_batch=4096 without manual n_ubatch - Confirmed backward compatibility with default n_batch=512 - Tested with BGE-M3 model for late interaction retrieval
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.
Motivation
Fixes #1762
When using
Llama.embed()withn_batch > 512, the kernel crashes becausen_ubatchdefaults to 512. Users had to manually set bothn_batchandn_ubatchto the same value as a workaround:This is unintuitive and causes crashes when users only set
n_batchwithout realizing they also need to setn_ubatch.Changes
Modified
llama_cpp/llama.pyin theembed()method:Before:
After:
This change:
n_ubatchto matchn_batchwhen neededn_batch > 512n_batch=512is unaffectedTesting
Added test file
tests/test_embed_n_ubatch_fix.pywith:n_ubatchis auto-adjusted whenn_batch > n_ubatchn_ubatchis already sufficientn_batch=4096without manualn_ubatchn_batch=512Impact
n_batch > 512without manual configurationChecklist
Llama.embedcrashes whenn_batch> 512 #1762