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 3 commits 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>
📝 WalkthroughWalkthroughThe RAG handler 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.
|
Thanks @rhatdan, @gemini-code-assist — good catch, adopted in 2c65fd2. When |
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
ramalama/plugins/runtimes/inference/rag/handler.py (1)
66-81: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winAdd regression coverage for the batch/context contract.
Please test that the default configuration produces
2048forctx_size,--batch-size, and--ubatch-size, while an explicit--embed-ctx-sizepreserves that configured value. This protects thebatch <= contextfix and prevents accidental regressions in explicit-context behavior.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@ramalama/plugins/runtimes/inference/rag/handler.py` around lines 66 - 81, Add regression tests for the embedding serve-argument construction around _build_serve_args: verify the default configuration sets ctx_size, --batch-size, and --ubatch-size to 2048, and verify an explicit --embed-ctx-size sets all three values to that configured size.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@ramalama/plugins/runtimes/inference/rag/handler.py`:
- Around line 66-81: Add regression tests for the embedding serve-argument
construction around _build_serve_args: verify the default configuration sets
ctx_size, --batch-size, and --ubatch-size to 2048, and verify an explicit
--embed-ctx-size sets all three values to that configured size.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: aeba4d4e-eeb4-4b61-9045-2bb2fda59f5e
📒 Files selected for processing (1)
ramalama/plugins/runtimes/inference/rag/handler.py
|
@bmahabirbu PTAL |
Address @gemini-code-assist review: when --embed-ctx-size is unset the embedding server was started with ctx=None (server default) while the batch was forced to 2048, which violates llama.cpp's batch<=ctx rule if the default ctx is smaller. Tie ctx_size to embed_batch_size. Signed-off-by: Tai An <antai12232931@outlook.com>
2c65fd2 to
9fc3958
Compare
|
@Anai-Guo Fix the lint issue. |
ac8c4f7 to
9fc3958
Compare
Signed-off-by: Tai An <antai12232931@outlook.com>
|
@rhatdan I looked into it — I don't think there's anything left to fix in this branch. The failing Checked locally against current So I pushed a signed-off empty commit ( |
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