---
title: vLLM 并发场景下 xgrammar 结构化输出导致的 TypeError 问题-官方技术文章-昇腾社区
description: 高并发下vLLM结构化输出报错 &#96;TypeError: incompatible function arguments&#96;&#xff0c;多因 &#96;xgrammar&#96; 处理大词表或特定模型时&#xff0c;CPU端logits数据类型&#xff08;如FP8/BF16&#xff09;与库要求不匹配。需确保输入logit强制转为float32&#xff0c;并升级至修复i
keywords: LLM,并发场景下,结构化输出导致的,TypeError,问题,官方技术文章,昇腾社区,背景概述
url: https://www.hiascend.com/developer/techArticles/20260723-3
section: (其他)
---

# vLLM 并发场景下 xgrammar 结构化输出导致的 TypeError 问题-官方技术文章-昇腾社区

URL: https://www.hiascend.com/developer/techArticles/20260723-3
描述: 高并发下vLLM结构化输出报错 &#96;TypeError: incompatible function arguments&#96;&#xff0c;多因 &#96;xgrammar&#96; 处理大词表或特定模型时&#xff0c;CPU端logits数据类型&#xff08;如FP8/BF16&#xff09;与库要求不匹配。需确保输入logit强制转为float32&#xff0c;并升级至修复i
关键词: LLM,并发场景下,结构化输出导致的,TypeError,问题,官方技术文章,昇腾社区,背景概述

官方技术文章 [了解详情](https://www.hiascend.com/zh/developer/techArticles)

vLLM 并发场景下 xgrammar 结构化输出导致的 TypeError 问题

vLLM 并发场景下 xgrammar 结构化输出导致的 TypeError 问题

模型推理技术文章

发表于: 2026/07/23

22

0

## 背景概述

高并发下vLLM结构化输出报错`TypeError: incompatible function arguments`，多因`xgrammar`处理大词表或特定模型时，CPU端logits数据类型（如FP8/BF16）与库要求不匹配。需确保输入logit强制转为float32，并升级至修复int64溢出及类型兼容性的最新vLLM-Ascend版本，以保障服务稳定。

## 问题现象

在 Atlas 800T A2 等硬件平台上，使用 CANN 和 PyTorch 软件栈运行 vLLM 服务时，当并发请求开启 Function Call 且配置为强制结构化输出时，服务出现以下异常：

●

报错信息：

text

1

2

3

TypeError: apply_token_bitmask_inplace_cpu(): incompatible function arguments. The following argument types are supported:

...

Invoked with types: int, tuple, tuple, int, tuple,tuple, int, torch.Tensor

TypeError: apply_token_bitmask_inplace_cpu(): incompatible function arguments. The following argument types are supported:
...
Invoked with types: int, tuple, tuple, int, tuple, tuple, int, torch.Tensor

●

影响范围：并发场景下，部分请求处理失败，严重时导致服务进程挂死。

## 原因分析

1、 分析堆栈信息首报错地方为:

File "/usr/local/python3.11.14/lib/python3.11/site-packages/xgrammar/kernels/apply_token_bitmask_inplace_cpu.py", line 46, in apply_token_bitmask_inplace_cpu

结论: 引起错误的原因是三方库 xgrammar 中的 apply_token_bitmask_inplace_cpu函数引起

2、 进一步确认报错信息

TypeError: apply_token_bitmask_inplace_cpu(): incompatible function arguments. The following argument types are supported:

结论：数据类型不匹配导致

3、 再进一步找到数据类型不匹配的地方

apply_token_bitmask_inplace_cpu(logits_ptr: int, logits_shape: tuple[int, int], logits_strides: tuple[int, int], bitmask_ptr: int, bitmask_shape: tuple[int, int], bitmask_strides: tuple[int, int], vocab_size: int, indices: collections.abc.Sequence[int] | None) -> None

实际传入的数据类型为

Invoked with types: int, tuple, tuple, int, tuple, tuple, int, torch.Tensor

4、在一次推理结束后，会在sample_tokens函数中校验grammar_output是否为空，如果为空就不会做结构化输出，反之则会做结构化输出。

5、开启function call就会触发结构化输出

(1) 当请求中"tool_choice": "auto" 则不会开启结构化输出，就不会触发bug

(2) 当请求体中"tool_choice": "required" 则会开启结构化输出，就有几率触发bug

6、为什么开启结构化输出是会有几率触发bug？而不是必然触发？

(1)首先vllm框架启动的时候会开启一个多进程执行器，并且会初始化一个消息队列：

(2) 在并发场景的时候，以双并发为例，这个消息队列中会同时存在两条消息，推理完成并且做结构化输出的时候会存在两种情况:

7、 两条消息都没处理完，这个时候就不会触发bug

8、两条消息有一条提前处理完了，这个时候就会触发bug

9、触发bug根因如下：可以发现底层实现里并不支持tensor数据类型，因此报错

10、为什么H100不出现问题？

可以发现H100走的是单独的分支apply_token_bitmask_inplace_cuda, 这个分支是支持tensor结构入参的

## 问题根因

Xgrammar 0.1.29及其一下版本有bug(不支持tensor输入，只支持list输入)导致

## 解决措施

xgrammar升级到0.1.30版本及以上即可，因为该问题已在 0.1.30版本修复了

## 6. 总结

在 vLLM 并发推理场景中，开启强制结构化输出时遇到的`TypeError`主要源于`xgrammar`旧版本对`torch.Tensor`类型支持的缺失。通过升级`xgrammar`至 0.1.30 及以上版本，即可彻底解决该问题，确保服务在高并发下的稳定性。

边框设置

无框线

边距

宽度

1磅

颜色

自由布局设置

整体布局

子模块

评论

修订记录

对正文进行的文本增删、样式修改都将标记为修订

自定义多级列表

列表设置

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9

- 1.
- a.
- i.
- 1.
- a.
- i.
- 1.
- a.
- i.

前缀

无

序号

1 2 3 ...

后缀

.

编号格式

列表显示

继承层级

不继承

位置

对齐方式

默认

单元格边距

边距

默认

左边距

cm

右边距

cm

上边距

cm

下边距

cm

点赞 0

本页内容

背景概述 [了解详情](https://www.hiascend.com/#3kVOO9h3eLgmrfFTyIieKX)

问题现象 [了解详情](https://www.hiascend.com/#2DE79JALxOPvvVe33p6d74)

原因分析 [了解详情](https://www.hiascend.com/#4VtnDs59HcdYsxe2H4kwBh)

问题根因 [了解详情](https://www.hiascend.com/#6ASQh86ImVIlbp7SmoT2QS)

解决措施 [了解详情](https://www.hiascend.com/#6bDdxtHe1Z4JVAvv7lhZ28)

6. 总结 [了解详情](https://www.hiascend.com/#2fTEnpZlZujzPU7Cdsb3BR)
