sgl-project/sglang

[Feature] Reduce constrained-decoding overhead in TP

オープン

#13,809 opened on 2025/11/23

 (2 件のコメント) (0 件のリアクション) (1 人の担当者)Python (6,216 件のフォーク)auto 404
good first issue

Repository metrics

Stars
 (28,442 個のスター)
PR merge metrics
 (平均マージ 2d 1h) (30d で 1,000 merged PRs)

説明

Checklist

Motivation

Currently, when tensor-parallelism (TP) and constrained-decoding are enabled, each TP worker will compile the same grammar across different TP ranks. This will incur non-trivial CPU overhead (for TP $n$, the overhead is $n \times$. In fact, we only need to compile the grammar and sample the result on the first rank.

A possible implementation can be:

  1. Only apply grammar token mask on the rank 0.
  2. Broadcast the next tokens id from rank 0 when there're grammars in batch.
  3. Keep the old code path when there's no grammar in batch (i.e. no extra all-reduce).

https://github.com/sgl-project/sglang/blob/5c2915494c83f076a11afb2c3382eeb8a41f1974/python/sglang/srt/layers/sampler.py#L205-L217

Related resources

No response

コントリビューターガイド