Skip to content

[data][llm] Support task_type="embed" in SGLangEngineProcessorConfig - #67548

Open
fortunecookiee wants to merge 2 commits into
ray-project:masterfrom
fortunecookiee:sglang-embed
Open

fortunecookiee wants to merge 2 commits into
ray-project:masterfrom
fortunecookiee:sglang-embed

Conversation

@fortunecookiee

@fortunecookiee fortunecookiee commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Description

Ray Data LLM can run embedding models with vLLM (task_type="embed"), but not with SGLang. This PR adds the embed task to SGLangEngineProcessorConfig.

config = SGLangEngineProcessorConfig(
    model_source="Qwen/Qwen3-Embedding-0.6B",
    task_type="embed",
)
processor = build_processor(
    config,
    preprocess=lambda row: dict(prompt=row["text"], embed_params={"dimensions": 256}),
    postprocess=lambda row: dict(embedding=row["embeddings"]),
)

Embed task (tasks/sglang/embed.py), a new task in the SGLang task framework (#67655), next to generate and score:

  • Input:
    • Each row's text prompt goes to Engine.async_encode, on an engine started with is_embedding=True.
    • An optional per-row embed_params dict sets dimensions (for Matryoshka models) or lora_path. Other keys are rejected, and SGLang validates the values.
  • Output: the same columns as vLLM's embed task:
    • embeddings (float32 numpy).
    • num_input_tokens, and num_generated_tokens, which is always 0.
    • params: the arguments sent to the engine.
  • Stages:
    • SGLang tokenizes the prompt, so the task sets skip_tokenizer_init=False and rejects the tokenize and detokenize stages.
    • The chat template stage is off unless chat_template_stage is set.

Shared engine stage (sglang_engine_stage.py), two small changes:

  • It applies a task's forced engine kwargs itself, so the stage is correct even when built without the processor.
  • A task can write its own params column. Generate and score are unchanged.

Docs: an SGLang note in the "Generate embeddings" section of working-with-llms.md, and updated SGLangEngineProcessorConfig docstrings.

Not supported yet: token-ID input. It needs input_ids on Engine.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 a prompt_token_ids field 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:

  • CPU unit tests (SGLang mocked):
    • 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_params cleanup (Arrow None fields, numpy scalars), a rejected key, and output conversion.
  • GPU release test: test_sglang_embed_matches_engine in release/llm_tests/batch/test_batch_sglang.py. It compares embeddings (cosine > 0.999) and token counts with a direct sglang.Engine.encode call on Qwen3-Embedding-0.6B, with dimensions=256 set per row. It runs in the existing llm_batch_sglang release test.

Commands run:

  • pytest on 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.
  • Not run locally: the GPU release test, which needs a GPU.

This PR was written with AI assistance (GitHub Copilot). I reviewed every changed line.

@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

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.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Nov 14, 2026
@github-actions github-actions Bot added unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it. and removed stale The issue is stale. It will be closed within 7 days unless there are further conversation labels Nov 25, 2026
@fortunecookiee fortunecookiee changed the title [data][llm] Add the embed task to the SGLang batch engine [data][llm] Add the embed task to the SGLang batch processor Nov 25, 2026
@fortunecookiee
fortunecookiee marked this pull request as ready for review November 25, 2026 20:58
@fortunecookiee
fortunecookiee requested review from a team as code owners November 25, 2026 20:58

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
"num_input_tokens": output["meta_info"]["prompt_tokens"],
"num_input_tokens": (output.get("meta_info") or {}).get("prompt_tokens", 0),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@fortunecookiee fortunecookiee changed the title [data][llm] Add the embed task to the SGLang batch processor [data][llm] Support task_type="embed" in SGLangEngineProcessorConfig Nov 25, 2026
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]>
@ray-gardener ray-gardener Bot added docs An issue or change related to documentation data Ray Data-related issues release-test release test community-contribution Contributed by the community labels Nov 26, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community data Ray Data-related issues docs An issue or change related to documentation release-test release test unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant