Prhub

#32353 [Spec] Consolidate the grammar sync decision into ScheduleBatch.grammar_needs_sync

原始 PR 作者 hnyls2002 合并时间 2026-07-25 11:42 文件变更 4 提交数 5 评论 3 代码增减 +12 / -7

执行摘要

将 grammar 同步判断集中到 ScheduleBatch.grammar_needs_sync

PR body 指出本次变更为纯注释和重构,用于明确 supports_grammar_overlap 的实际判定条件——它要求一个 GPU 草稿阶段,其目标验证前向可以隐藏 grammar CPU 工作——并记录主机端推测算法(如 NGRAM)有意停留在同步 grammar 路径上,而非待迁移状态。

这是一个小型但值得关注的重构,展示了如何逐步将分散的条件逻辑抽象为命名的语义方法,提高可读性和一致性。对于需要理解语法约束与推测解码交互的开发者,建议精读 supports_grammar_overlapgrammar_needs_sync 的文档注释。

讨论亮点

本 PR 无 review 讨论,由作者直接合并。仅包含作者发起的 CI 重跑命令。

实现拆解

  1. 新增 grammar_needs_sync 方法:在 schedule_batch.pyScheduleBatch 类中添加 grammar_needs_sync() 方法,封装 has_grammar and not spec_algorithm.supports_grammar_overlap() 判断。
  2. 更新调度器条件:在 scheduler.pyis_disable_overlap_for_batch 中将原本内联的 not supports_grammar_overlap and has_grammar 替换为调用 batch.grammar_needs_sync(),并更新注释说明同步路径对 host-draft 算法是永久设计。
  3. 对齐 ngram 工作节点:在 ngram_worker.py_prepare_draft_tokens_update_ngram_corpus 中,将 not batch.has_grammar 替换为 not batch.grammar_needs_sync(),保持行为一致。
  4. 完善算法文档:在 spec_info.pysupports_grammar_overlap 方法中添加注释,阐明 NGRAM 因依赖主机端语料库查询而无法参与 overlap。
文件 模块 状态 重要度
python/sglang/srt/managers/schedule_batch.py 批次管理 modified 4.96
python/sglang/srt/managers/scheduler.py 调度器 modified 4.81
python/sglang/srt/speculative/ngram_worker.py ngram 推测 modified 4.27
python/sglang/srt/speculative/spec_info.py 推测算法定义 modified 3.83

关键符号

grammar_needs_sync is_disable_overlap_for_batch _prepare_draft_tokens _update_ngram_corpus

关键源码片段

python/sglang/srt/managers/schedule_batch.py core-logic

新增 grammar_needs_sync 方法,集中了 grammar 同步判断逻辑,是本次重构的核心。

# python/sglang/srt/managers/schedule_batch.py (class ScheduleBatch)
​
    def is_empty(self):
        return len(self.reqs) == 0
​
    def is_dllm(self):
        return self.dllm_config is not None
​
    def grammar_needs_sync(self) -> bool:
        """Whether grammar forces this batch onto the synchronous path, i.e. the
        previous batch's result is resolved before this forward."""
        return self.has_grammar and not self.spec_algorithm.supports_grammar_overlap()
​
    def prepare_encoder_info_extend(
        self, input_ids: List[array[int]], seq_lens: List[int]
    ):
        # ... (subsequent method body unchanged)
python/sglang/srt/managers/scheduler.py core-logic

在 is_disable_overlap_for_batch 中使用新方法替代内联判断,并更新注释。

# python/sglang/srt/managers/scheduler.py (method is_disable_overlap_for_batch)
​
        # Sync so the FSM advance lands before the next batch's bitmask. Permanent
        # path for host-draft algorithms, not a pending migration.
        need_grammar_sync = (
            batch
            and not batch.spec_algorithm.is_none()
            and batch.grammar_needs_sync() # ← consolidated check
            and batch.forward_mode.is_decode()
            and len(self.result_queue) > 0
        )
​
        # Algorithms that support grammar overlap advance the FSM inside verify()
        # via the grammar barrier (overlapping the target forward), which resolves
        # whatever result is still pending in the queue — including the
        # extend->decode boundary — so no grammar-specific overlap disable is needed.
        return disable_overlap_for_batch or need_grammar_sync

评论区精华

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

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

风险与影响

由于仅涉及谓词提取和注释调整,未改变任何执行逻辑,且通过现有 CI 测试验证,回归风险极低。但需注意:如果未来新增推测算法时忘记在 supports_grammar_overlap 中更新,grammar_needs_sync 可能返回错误值。建议在算法注册时添加文档提醒。

对用户无直接影响;对开发人员改进了代码可读性,集中了 grammar 同步策略的判定点,降低了未来误解和维护成本。

无行为变更 集中决策逻辑

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论