Skip to content

[NPU] Fix xgrammar apply_vocab_mask device dispatch to use torch.ops.npu - #36120

Open
zhujianwei-ops wants to merge 1 commit into
sgl-project:mainfrom
zhujianwei-ops:fix/xgrammar-npu-vocab-mask
Open

[NPU] Fix xgrammar apply_vocab_mask device dispatch to use torch.ops.npu#36120
zhujianwei-ops wants to merge 1 commit into
sgl-project:mainfrom
zhujianwei-ops:fix/xgrammar-npu-vocab-mask

Conversation

@zhujianwei-ops

@zhujianwei-ops zhujianwei-ops commented Aug 24, 2026

Copy link
Copy Markdown

Motivation

XGrammarGrammarBackend.apply_vocab_mask (the static entry used by the strict-thinking token-filter path) routed NPU logits into the triton branch: "npu" was a member of the {"cuda", "xpu", "musa"} set, so on Ascend NPU the first strict-thinking decode crashed. The instance method already carried a dedicated torch.ops.npu.apply_token_bitmask branch; the static entry must route the same way.

Modifications

python/sglang/srt/constrained/xgrammar_backend.py: removed "npu" from the {"cuda", "xpu", "musa"} set in the static apply_vocab_mask and added a dedicated elif logits.device.type == "npu": branch calling torch.ops.npu.apply_token_bitmask, matching the instance method.
test/registered/unit/constrained/test_npu_xgrammar_vocab_mask_wiring.py (new): mock-based regression test asserting the NPU path reaches torch.ops.npu.apply_token_bitmask and never the triton kernel, and that CUDA keeps the triton path. No hardware dependency; registered for the NPU unit-test suite.
Accuracy Tests

Not applicable — device-routing-onlyact. Manually verified thestrict-thinking decode path on 910A3 hardware.

Speed Tests and Profiling

Not applicable — no inference-speed impact; the change fixes a crash.


CI States

Latest PR Test (Base): ❌ Run #32680217283
Latest PR Test (Extra): ❌ Run #32680216834
Latest PR Test (AMD ROCm 7.2): ❌ Run #32680216993

@zhujianwei-ops

Copy link
Copy Markdown
Author

/tag-and-rerun-ci

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant