fix(rag): size the embedding server batch to its context so large chunks embed (#2836)#2852
fix(rag): size the embedding server batch to its context so large chunks embed (#2836)#2852Anai-Guo wants to merge 1 commit into
Conversation
…nks embed
`ramalama rag` starts the embedding llama-server with only `--embedding`
and no batch override, so it keeps llama-server's default 512-token
physical batch (`-ub`). doc2rag, however, sizes chunks with
tiktoken/cl100k_base while the embedding model counts the same text with
its own tokenizer, which runs substantially higher on URL-, code- and
YAML-dense content (up to ~1.9x). A chunk measured at <=400 tokens can
therefore reach the server as 500-750 real tokens and overflow the
physical batch, aborting the whole run with:
llama-server embedding request returned 500: input (596 tokens) is
too large to process. increase the physical batch size
Since an embedding request must fit entirely in one physical batch, pass
`--batch-size`/`--ubatch-size` sized to the embedding context (falling
back to 2048) when launching the embedding server, so any chunk that
fits the context also fits a single batch. Plain-prose corpora were
unaffected, which is why the failure looked random across document sets.
Fixes containers#2836
Signed-off-by: Anai-Guo <antai12232931@outlook.com>
Assisted-by: Claude <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughSummary by CodeRabbit
WalkthroughThe RAG handler now derives an embedding batch size from ChangesRAG embedding batch sizing
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request configures the embedding batch size and micro-batch size based on the embedding context size (defaulting to 2048) to prevent failures when processing large chunks. The reviewer pointed out a potential issue where if embed_ctx_size is not specified, the batch size is set to 2048 but the context size remains None. Since llama.cpp requires the context size to be at least as large as the batch size, they suggested passing embed_batch_size as the ctx_size parameter to avoid startup or processing failures.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| embed_batch_size = embed_ctx_size or 2048 | ||
| embed_serve_args = _build_serve_args( | ||
| args, | ||
| embedding_model, | ||
| embed_port, | ||
| runtime_args=["--embedding"], | ||
| runtime_args=[ | ||
| "--embedding", | ||
| "--batch-size", | ||
| str(embed_batch_size), | ||
| "--ubatch-size", | ||
| str(embed_batch_size), | ||
| ], | ||
| ctx_size=embed_ctx_size, | ||
| cache_reuse=0, | ||
| ) |
There was a problem hiding this comment.
If embed_ctx_size is not specified (i.e., it is None), embed_batch_size defaults to 2048. However, ctx_size is still passed as embed_ctx_size (None) to _build_serve_args. In llama.cpp, the batch size (--batch-size / --ubatch-size) cannot exceed the context size (--ctx-size). If the default context size of the server/model is smaller than 2048 (e.g., 512), llama-server may fail to start or fail to process chunks larger than 512 tokens. Passing embed_batch_size as the ctx_size ensures that the context size is always at least as large as the batch size.
| embed_batch_size = embed_ctx_size or 2048 | |
| embed_serve_args = _build_serve_args( | |
| args, | |
| embedding_model, | |
| embed_port, | |
| runtime_args=["--embedding"], | |
| runtime_args=[ | |
| "--embedding", | |
| "--batch-size", | |
| str(embed_batch_size), | |
| "--ubatch-size", | |
| str(embed_batch_size), | |
| ], | |
| ctx_size=embed_ctx_size, | |
| cache_reuse=0, | |
| ) | |
| embed_batch_size = embed_ctx_size or 2048 | |
| embed_serve_args = _build_serve_args( | |
| args, | |
| embedding_model, | |
| embed_port, | |
| runtime_args=[ | |
| "--embedding", | |
| "--batch-size", | |
| str(embed_batch_size), | |
| "--ubatch-size", | |
| str(embed_batch_size), | |
| ], | |
| ctx_size=embed_batch_size, | |
| cache_reuse=0, | |
| ) |
There was a problem hiding this comment.
@Anai-Guo respond to @gemini-code-assist suggestion.
What
ramalama ragfails during the embedding phase on real-world doc sets with:Root cause
The embedding llama-server is started (in
rag_handler) with only--embeddingand no batch override, so it keeps llama-server's default 512-token physical batch (-ub). An embedding request must fit entirely within one physical batch.Meanwhile
doc2ragsizes chunks withtiktoken/cl100k_base(--chunk-size 400default), but the embedding model (embeddinggemma-300m) counts the same text with its own tokenizer. On URL-, code- and YAML-dense content the embedding-model count comes out up to ~1.9x higher, so a chunk measured<=400tokens can arrive at the server as 500-750 real tokens — over the 512 batch limit. Plain prose rarely triggers it, which is why the failure looked random across doc sets (as reported in #2836).Fix
Pass
--batch-size/--ubatch-sizesized to the embedding context (falling back to2048) when launching the embedding server, so any chunk that fits the context also fits a single physical batch. This mirrors how embedding servers are normally configured (whole input in one batch) and needs no new user-facing flag.Minimal change, confined to
rag_handler._build_serve_argscall for the embedding server.Fixes #2836
🤖 Generated with Claude Code