Prhub

#29121 Handle input-embed-only batches in eager runner

原始 PR 作者 merrymercy 合并时间 2026-06-25 01:33 文件变更 3 提交数 3 评论 2 代码增减 +9 / -7

执行摘要

修复 eager runner 对无 input_ids 的 batch 支持

根据 PR body:"Use input_embeds to derive the token count when input_ids is absent",支持输入嵌入(embedding)直接传入而不需要 token ID 的场景。这是对 eager runner 的一个缺漏修复。

建议快速合并。这是一个简单且必要的修复,完善了对嵌入输入的支持。

讨论亮点

该 PR 无讨论 comment。

实现拆解

  1. python/sglang/srt/model_executor/runner/eager_runner.pyload_batch 方法中,将原先直接使用 forward_batch.input_ids.shape[0] 的代码改为三路条件判断:优先取 input_ids 的长度,若为 None 则取 input_embeds 的长度,若两者均为 None 则置为 0。
  2. 对应测试文件 test_attention_patching.pytest_mlx_runner_pool_contract.py 中仅有格式化调整(行折叠、EOF 换行符),不涉及功能变更。
文件 模块 状态 重要度
python/sglang/srt/model_executor/runner/eager_runner.py 执行器 modified 6.01
test/registered/unit/hardware_backend/mlx/test_attention_patching.py 测试 modified 3.35
test/registered/unit/hardware_backend/mlx/test_mlx_runner_pool_contract.py 测试 modified 2.45

关键源码片段

python/sglang/srt/model_executor/runner/eager_runner.py data-contract

核心变更文件,修复 load_batch 方法中对 input_ids 为 None 时的处理。

def load_batch(
    self, forward_batch: ForwardBatch, pp_proxy_tensors=None, **kwargs
) -> ForwardBatch:
    # ... 前面的代码不变 ...
    raw_bs = forward_batch.batch_size
    # 优先使用 input_ids 确定 token 数量
    if forward_batch.input_ids is not None:
        raw_num_tokens = forward_batch.input_ids.shape[0]
    elif forward_batch.input_embeds is not None:
        # 若 input_ids 为空但提供了 input_embeds, 则使用嵌入张量长度
        raw_num_tokens = forward_batch.input_embeds.shape[0]
    else:
        # 兜底 : 两者均无则标记为 0 ( 零 token batch)
        raw_num_tokens = 0
    # 后续代码不变 , 使用 raw_num_tokens 填充缓冲区
    registry = self._eager_registry
    registry.fill_from(
        forward_batch,
        raw_bs=raw_bs,
        padded_bs=raw_bs,
        raw_num_tokens=raw_num_tokens,
        padded_num_tokens=raw_num_tokens,
        pp_proxy_tensors=pp_proxy_tensors,
    )
    return registry.extract_buffer(
        padded_bs=raw_bs,
        padded_num_tokens=raw_num_tokens,
        forward_batch_template=forward_batch,
    )

评论区精华

没有提炼出高价值讨论线程

当前评论区没有形成足够清晰的争议点或结论,后续有更多讨论时会体现在这里。

风险与影响

风险极低。更改仅影响 eager runner 中一个分支(input_ids 为 None 时),且逻辑直白(回退到 input_embeds 或 0);不会影响 CUDA graph 或其他 runner。测试文件仅格式化调整,无实质性改动。

影响范围局限在 eager runner 的 load_batch 流程,使得使用 input_embeds 直接输入的 batch(例如某些嵌入模型或多模态输入)能够在 eager 模式下正常运行。对已有功能无负面影响。

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论