Repository navigation
[data][llm] Support task_type="embed" in SGLangEngineProcessorConfig - #67548
fortunecookiee wants to merge 2 commits into
Conversation
|
This pull request has been automatically marked as stale because it has not had You can always ask for help on our discussion forum or Ray's public slack channel. If you'd like to keep this open, just leave any comment, and the stale label will be removed. |
30aa0f8 to
d18808c
Compare
d18808c to
0a8bc06
Compare
There was a problem hiding this comment.
Code Review
This pull request introduces support for the embed task type in the SGLang engine processor, enabling text prompt embedding via Engine.async_encode. The changes include updates to configuration files, the addition of the _SGLangEmbedTask task class, and new unit and release tests. The reviewer recommended a defensive programming improvement in embed.py to safely retrieve prompt_tokens from meta_info using .get() with a fallback to prevent potential KeyError or TypeError crashes.
| return { | ||
| "prompt": prompt, | ||
| "embeddings": np.asarray(output["embedding"], dtype=np.float32), | ||
| "num_input_tokens": output["meta_info"]["prompt_tokens"], |
There was a problem hiding this comment.
To ensure robustness and adhere to defensive programming practices, we should safely retrieve prompt_tokens from meta_info. If meta_info or prompt_tokens is missing or None in the engine output, direct dictionary access will raise a KeyError or TypeError and crash the pipeline. Using .get() with a fallback is safer.
| "num_input_tokens": output["meta_info"]["prompt_tokens"], | |
| "num_input_tokens": (output.get("meta_info") or {}).get("prompt_tokens", 0), |
There was a problem hiding this comment.
Thanks. I will keep the direct access. SGLang always sets meta_info["prompt_tokens"] in Engine.async_encode output, and the generate task reads it the same way. Defaulting to 0 would hide an SGLang regression as a wrong token count instead of failing loudly. The GPU release test checks num_input_tokens against Engine.encode, so a missing or changed field shows up there.
vLLM batch inference supports task_type="embed"; SGLang didn't. Add SGLangTaskType.EMBED as a task on the SGLang task framework. It runs the engine with is_embedding=True and calls Engine.async_encode, returning float32 embeddings in the same "embeddings" column as vLLM. SGLang tokenizes the text prompts, so the task forces skip_tokenizer_init=False and doesn't support the tokenize or detokenize stages. The chat template stage is off unless set. Rows can carry an optional embed_params dict with dimensions or lora_path, which SGLang validates; other keys are rejected. The params column records the arguments sent to the engine. The engine stage now applies the task's forced engine kwargs itself, so it's correct when built without the processor, and a task can record its own params column. Co-authored-by: Copilot <[email protected]> Signed-off-by: Jian Sheng <[email protected]>
0a8bc06 to
02f1a20
Compare
Drop the processor tests for forced engine kwargs and unsupported stages, which test_sglang_engine_proc.py already covers for the task framework. Fold the stage's engine-kwargs check into the wrapper test and keep one rejected embed_params key. Run the GPU release test once, with dimensions, which covers the default path too and halves engine starts. Co-authored-by: Copilot <[email protected]> Signed-off-by: Jian Sheng <[email protected]>
Description
Ray Data LLM can run embedding models with vLLM (
task_type="embed"), but not with SGLang. This PR adds the embed task toSGLangEngineProcessorConfig.Embed task (
tasks/sglang/embed.py), a new task in the SGLang task framework (#67655), next to generate and score:promptgoes toEngine.async_encode, on an engine started withis_embedding=True.embed_paramsdict setsdimensions(for Matryoshka models) orlora_path. Other keys are rejected, and SGLang validates the values.embeddings(float32 numpy).num_input_tokens, andnum_generated_tokens, which is always 0.params: the arguments sent to the engine.skip_tokenizer_init=Falseand rejects the tokenize and detokenize stages.chat_template_stageis set.Shared engine stage (
sglang_engine_stage.py), two small changes:paramscolumn. Generate and score are unchanged.Docs: an SGLang note in the "Generate embeddings" section of
working-with-llms.md, and updatedSGLangEngineProcessorConfigdocstrings.Not supported yet: token-ID input. It needs
input_idsonEngine.async_encode, which sgl-project/sglang#44152 added. That PR is merged, but no SGLang release includes it yet: the latest, v0.5.23 (Nov 2), came out before the merge, and Ray's SGLang release test pins it. Once a release includes it, a follow-up adds aprompt_token_idsfield to the embed request and allows the tokenize stage.Related issues
Related to #67655 (SGLang task framework) and sgl-project/sglang#44152 (token-ID input for
Engine.encode, merged, not yet released).Additional information
Duplicate check:
gh pr list --repo ray-project/ray --state open --search "sglang embed"finds only this PR and #67543. #67543 adds multimodal input to the generate path and doesn't overlap.Tests added:
test_sglang_embed_proc.py: the default stage list (chat template off unless set). The framework tests already cover forced engine kwargs and rejected stages.test_sglang_embed_engine_stage.py: engine kwargs set by the stage itself,embed_paramscleanup (ArrowNonefields, numpy scalars), a rejected key, and output conversion.test_sglang_embed_matches_engineinrelease/llm_tests/batch/test_batch_sglang.py. It compares embeddings (cosine > 0.999) and token counts with a directsglang.Engine.encodecall on Qwen3-Embedding-0.6B, withdimensions=256set per row. It runs in the existingllm_batch_sglangrelease test.Commands run:
pyteston the new embed tests plus the existing SGLang CPU tests (test_sglang_engine_proc.py,test_sglang_score_proc.py,test_sglang_engine_stage.py,test_sglang_score_engine_stage.py): 64 passed.pre-commit run --files <changed files>: passed.This PR was written with AI assistance (GitHub Copilot). I reviewed every changed line.