Repository navigation
Description
System Info
- TensorRT-LLM main at 687359e (
tensorrt_llm/serve/tool_parser/deepseekv3_parser.py,deepseekv31_parser.py,deepseekv32_parser.py, whichdeepseekv4_parser.pyanddeepseekv41_parser.pyinherit), pure Python. A CPU is enough to reproduce. - Python 3.12
- Serving path:
trtllm-serve ... --tool_parser deepseek_v3(ordeepseek_v31,deepseek_v32,deepseek_v4,deepseek_v41), streaming chat completions with tools
Who can help?
@NVIDIA/trt-llm-runtime-devs (CODEOWNERS for tensorrt_llm/serve)
Reproduction
Stream one write_file call whose content argument is ordinary code, four characters per increment:
import json
from tensorrt_llm.serve.openai_protocol import ChatCompletionToolsParam
from tensorrt_llm.serve.tool_parser.deepseekv31_parser import DeepSeekV31Parser
tools = [ChatCompletionToolsParam(function={
"name": "write_file", "parameters": {"type": "object", "properties": {
"path": {"type": "string"}, "content": {"type": "string"}}}})]
content = ("if (a > b) { p->x = q; } // ok\n" * 2200)[:65536]
text = ("<|tool▁calls▁begin|><|tool▁call▁begin|>write_file<|tool▁sep|>"
+ json.dumps({"path": "a.c", "content": content})
+ "<|tool▁call▁end|><|tool▁calls▁end|>")
parser = DeepSeekV31Parser()
for i in range(0, len(text), 4):
parser.parse_streaming_increment(text[i:i + 4], tools)Total streaming time for one call (main at 687359e, one run, the same shape for each parser's format):
| argument length | deepseek_v3 | deepseek_v31 | deepseek_v32 | deepseek_v4 | deepseek_v41 |
|---|---|---|---|---|---|
| 1k chars | 0.02 s | 0.04 s | 0.03 s | 0.03 s | 0.05 s |
| 4k chars | 0.18 s | 0.21 s | 0.19 s | 0.19 s | 0.24 s |
| 16k chars | 1.7 s | 2.3 s | 2.2 s | 2.2 s | 2.4 s |
| 64k chars | 23.4 s | 33.1 s | 32.1 s | 31.8 s | 31.7 s |
Each fourfold step in length costs 10 to 15 times more. detect_and_parse on the same 64k text takes about 5 ms.
A model that repeats the DSML opener (<|DSML|function_calls><|DSML|invoke name="w"><|DSML|parameter name="c" string="true">) until max_tokens is worse. 16k characters of it take 53 s with deepseek_v32 and 62 s with deepseek_v4.
Expected behavior
Streaming one tool call costs time close to linear in its length, near the detect_and_parse cost.
actual behavior
Time grows with the square of the tool call length. With the default num_postprocess_workers=0 the parser runs inside chat_stream_generator on the server event loop. One 64k-character tool call (a 2000-line file written by a coding agent) spends about 30 s of event loop time, in slices that reach 15 to 23 ms for a single increment near the end of the call (measured for deepseek_v3, deepseek_v31 and deepseek_v32). Every other request on the server waits during those slices, and several such calls at once add up.
additional notes
On every increment each parser runs a regular expression over the whole buffered tool call:
deepseekv3_parser.pyparse_streaming_incrementsearches<|tool▁call▁begin|>(.*)<|tool▁sep|>(.*)\n```json\n(.*)\n```.*. The leading greedy group backtracks over the whole buffer on every increment, and the pattern can only match once the closing fence arrives.deepseekv31_parser.pysearches<|tool▁call▁begin|>(.*)<|tool▁sep|>(.*?)(<|tool▁call▁end|>|$)and then strips, compares andjson.loadsthe whole argument text.deepseekv32_parser.pysearches the invoke pattern with(.*?)(</|DSML|invoke>|$)and runs_parse_parameters_from_xmlplusjson.dumpson the open invoke, although the result is only used once</|DSML|invoke>arrives.
I have a fix with tests ready that keeps every streamed result the same and will open a PR that references this issue.
Activity
[NVIDIA#20989][fix] DeepSeek V3.1: hold back a partial call end token
[NVIDIA#20989][fix] DeepSeek streaming tool parsers: scan only new te…
[NVIDIA#20989][fix] DeepSeek V3.1: hold back a partial call end token
[NVIDIA#20989][fix] Preserve final DeepSeek tool arguments and delimi…
[NVIDIA#20989][test] Check completion state when finish emits arguments
[NVIDIA#20989][fix] DeepSeek V3.1: send the text before a partial tok…
[NVIDIA#20989][test] Cover a split call end token and check stored ar…
[NVIDIA#20989][fix] Preserve V3.1 content whitespace while a call end…
[NVIDIA#20989][fix] Keep V3.1 separators inside JSON argument strings
[NVIDIA#20989][fix] DeepSeek streaming tool parsers: scan only new te…
[NVIDIA#20989][fix] DeepSeek V3.1: hold back a partial call end token
[NVIDIA#20989][fix] Preserve final DeepSeek tool arguments and delimi…
[NVIDIA#20989][test] Check completion state when finish emits arguments
[NVIDIA#20989][fix] DeepSeek V3.1: send the text before a partial tok…
[NVIDIA#20989][test] Cover a split call end token and check stored ar…
[NVIDIA#20989][fix] Preserve V3.1 content whitespace while a call end…
[NVIDIA#20989][fix] Keep V3.1 separators inside JSON argument strings
[NVIDIA#20989][fix] Leave the end of a DeepSeek V3 and V3.1 stream to…