[NPU] Fix xgrammar apply_vocab_mask device dispatch to use torch.ops.npu - #36120
Open
zhujianwei-ops wants to merge 1 commit into
Open
[NPU] Fix xgrammar apply_vocab_mask device dispatch to use torch.ops.npu#36120zhujianwei-ops wants to merge 1 commit into
zhujianwei-ops wants to merge 1 commit into
Conversation
zhujianwei-ops
requested review from
DarkSharpness,
JustinTong0323 and
hnyls2002
as code owners
August 24, 2026 01:34
Author
|
/tag-and-rerun-ci |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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