Skip to content

[bugfix] Parse token usage for TGI/Triton/MindIE backends - #615

Open
lsyx6399 wants to merge 1 commit into
AISBench:masterfrom
lsyx6399:fix/non-openai-backend-usage
Open

lsyx6399 wants to merge 1 commit into
AISBench:masterfrom
lsyx6399:fix/non-openai-backend-usage

Conversation

@lsyx6399

@lsyx6399 lsyx6399 commented Oct 28, 2026 •

Copy link
Copy Markdown

PR 描述

PR Type

  • Bugfix(Bug 修复)

Related Issue
Fixes #616

🔍 Motivation / 变更动机

TGI / Triton / MindIE 三个服务化后端都不解析 token 数量:

  • tgi_api.py:parse_text_response/parse_stream_response
  • triton_api.py:parse_text_response/parse_stream_response
  • mindie_stream_api.py:parse_stream_response

它们只写 output.content,从不设置 output.input_tokens/output_tokens。
后果是这三个后端的性能报告里 Input/Output Token Throughput 恒为 0,
generate_tokens_speed 也是 0,只有延迟类指标可用。

证据:仓库自带的 mock 服务其实一直在返回 token 数
(tools/infer_serve_simulator/api/tgi_api.py:32-39、triton_api.py:28-33、
mindie_origin.py:61-70 的 details.generated_tokens),说明是解析侧漏了。

第二个问题在 summarizers/default_perf.py:154-162:本该"服务没上报就退回 tokenizer 估算"的兜底
逻辑永远不触发。Output.__init__ 把 input_tokens/output_tokens 初始化为 0,
jsonl 里这两个键一定存在且不为 None,而判断写的是"键不存在或 is None"。
于是任何未上报 usage 的后端在报告里都是 0 token,兜底形同虚设。

📝 Modification / 修改内容

  1. BaseAPIModel._update_usage_from_details()(base_api.py):新增一个受保护的小工具,
    从 details.generated_tokens 取输出 token 数(三个后端共用,避免三份重复代码)。
  2. 三个后端的 parse_*_response 调用该工具。TGI/MindIE 的 details 只出现在最后一帧;
    Triton 的 generated_tokens 是累计值,直接赋值即可。
  3. default_perf.py:兜底判断改为"值缺失或 <= 0 才回退 tokenizer";
    output_tokens 的回退额外要求 prediction 是字符串(多模态输出不会误入)。

📐 Associated Test Results / 关联测试结果

新增/增强的用例:

  • test_tgi_api.py:文本响应、流式最后一帧、无 details 三种情况
  • test_triton_api.py:文本响应、流式累计值(3 → 5)、无 details
  • test_mindie_stream_api.py:流式最后一帧、无 details
  • test_default_perf.py::test_calc_perf_data_success:原来 output_tokens: 0
    只被断言"不报错",现在断言兜底后等于 len("test output")
$ python -m pytest tests/UT/models/api_models/test_tgi_api.py \
      tests/UT/models/api_models/test_triton_api.py \
      tests/UT/models/api_models/test_mindie_stream_api.py -q
56 passed

$ python -m pytest tests/UT/summarizers/test_default_perf.py -q
19 passed, 2 failed

这 2 个失败是 Windows 路径分隔符导致的既有用例问题
(test_load_tmp_result_no_dir 期望 /tmp/a/b,实际 \tmp\a\b;
test_dump_calculated_perf_data 同类),与本次改动无关,在 Linux CI 上不会出现。

⚠️ BC-breaking

无。仅在服务返回 details.generated_tokens 时补充计数;不改变请求格式。
<= 0 的兜底会让"服务确实返回 0 token 且 prediction 非空"的场景改用 tokenizer 估算,
这是期望行为(0 与"未上报"在现有数据结构里无法区分)。

⚠️ Performance degradation

无(只是多读一个字段)。

@github-actions github-actions Bot added bugfix no_issue PR 未关联任何 Issue,合入前必须关联 Issue no_review PR 未包含任何检视意见,合入前必须包含检视意见 labels Oct 28, 2026
@github-actions

Copy link
Copy Markdown

🚫 PR 合入质量检查未通过,PR 已被禁止合入。

未通过项:no_issue、no_review

  • 未关联 Issue:请在 PR 描述中关联 Issue,例如 Fixes #123 或 Relates to #123。
  • 无检视意见:请至少邀请一位 reviewer 提交检视意见(Approve / Request changes / Comment 均可)。

请按上述提示整改后,重新推送(push)或评论本 PR 即可触发再次检查。

Those backends report usage as details.generated_tokens instead of the
OpenAI usage object, and the parsers dropped it, so every performance
report showed zero output-token throughput.

Also fix the default_perf fallback: Output initializes the counters to 0,
so the "missing" checks never fired and the tokenizer fallback was dead
code for any backend that did not report usage.
@lsyx6399
lsyx6399 force-pushed the fix/non-openai-backend-usage branch from e48fedb to 8b7dcf3 Compare October 28, 2026 03:22
@github-actions

Copy link
Copy Markdown

🚫 PR 合入质量检查未通过,PR 已被禁止合入。

未通过项:no_issue、no_review

  • 未关联 Issue:请在 PR 描述中关联 Issue,例如 Fixes #123 或 Relates to #123。
  • 无检视意见:请至少邀请一位 reviewer 提交检视意见(Approve / Request changes / Comment 均可)。

请按上述提示整改后,重新推送(push)或评论本 PR 即可触发再次检查。

@lsyx6399
lsyx6399 marked this pull request as draft October 28, 2026 03:29
@lsyx6399
lsyx6399 marked this pull request as ready for review October 28, 2026 03:30
@lsyx6399

Copy link
Copy Markdown
Author

Hi @Libotry @SJTUyh, could you take a look when you have time?

#614 fixes #609 (custom url should make host_ip/host_port optional).
#615 parses details.generated_tokens for the TGI/Triton/MindIE backends and fixes the
dead tokenizer fallback in default_perf, so those backends stop reporting zero throughput.

Both come with unit tests. Thanks!

@lsyx6399

Copy link
Copy Markdown
Author

C:/Program Files/Git/pr_check

@lsyx6399

Copy link
Copy Markdown
Author

Hi @Libotry @SJTUyh, could you take a look when you have time?

This fixes #616. The TGI / Triton / MindIE backends never parsed details.generated_tokens,
so every performance report for them showed zero output-token throughput. The change adds a
shared _update_usage_from_details() helper and calls it from the three parsers; it also fixes
the dead tokenizer fallback in default_perf (Output initializes counters to 0, so the old
"is None" check never fired).

Evidence the services do return the count: tools/infer_serve_simulator/api/tgi_api.py:32-39,
triton_api.py:28-33, mindie_origin.py:61-70 all emit details.generated_tokens.

pytest tests/UT/models/api_models/test_{tgi,triton,mindie}_api.py -q → 56 passed.

Note: as an external contributor I can't request reviewers from the UI — could a maintainer
pick this up? The no_review gate should clear once a review (Approve / Request changes /
Comment) is submitted. Thanks!

@lsyx6399

Copy link
Copy Markdown
Author

/pr_check 重检:PR body 已含 Fixes #616,#616 OPEN,no_issue 标签应为门禁误判,请重新检查关联状态。

@github-actions github-actions Bot removed the no_issue PR 未关联任何 Issue,合入前必须关联 Issue label Oct 29, 2026
@github-actions

Copy link
Copy Markdown

🚫 PR 合入质量检查未通过,PR 已被禁止合入。

未通过项:no_review

  • 无检视意见:请至少邀请一位 reviewer 提交检视意见(Approve / Request changes / Comment 均可)。

请按上述提示整改后,重新推送(push)或评论本 PR 即可触发再次检查。

@lsyx6399

lsyx6399 commented Nov 2, 2026

Copy link
Copy Markdown
Author

Hi @Libotry @Keithwwa @SJTUyh, a gentle nudge — this PR is ready for review. The issue analysis on #616 classified it as P1 (TGI/Triton/MindIE Input/Output Token Throughput is always 0 because the parsers drop details.generated_tokens). I've replied to the analysis on #616 covering the scope; happy to address any feedback.

This branch has not been deployed

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

Labels

bugfix no_review PR 未包含任何检视意见,合入前必须包含检视意见

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] TGI/Triton/MindIE 服务化后端不解析 details.generated_tokens,性能报告 Token Throughput 恒为 0

1 participant