Skip to content

[None][perf] disagg: skip per-chunk JSON parse on streaming usage rewrite - #15042

Merged
peihu-nv merged 1 commit into
NVIDIA:feat/deepseek_v4from
lancelly:perf/disagg-skip-sse-usage-parse
Jun 8, 2026
Merged

[None][perf] disagg: skip per-chunk JSON parse on streaming usage rewrite#15042
peihu-nv merged 1 commit into
NVIDIA:feat/deepseek_v4from
lancelly:perf/disagg-skip-sse-usage-parse

Conversation

@lancelly

@lancelly lancelly commented Jun 6, 2026

Copy link
Copy Markdown
Collaborator

@coderabbitai summary

Description

In disaggregated serving the orchestrator relays the generation server's streaming
SSE response back to the client, and rewrites the usage object so the reported
prompt_tokens / cached_tokens reflect the context server (where prefill
actually happened) rather than the generation server.

_rewrite_usage_sse_event_from_ctx was invoked for every relayed SSE chunk and
ran splitlines() + json.loads() on each one — but a top-level usage object
only appears on the final chunk (and only when the client requested usage).
So for essentially every streamed output token the orchestrator did a full JSON
parse just to discover "no usage here, nothing to rewrite". On the orchestrator's
single asyncio event loop this is pure per-token overhead that serializes all
concurrent streams: ~5% of one core at high concurrency (~15k relayed tok/s).

This PR adds a cheap byte pre-filter before the parse:

if b'"usage"' not in event:
    return event

A top-level usage field cannot exist unless the chunk bytes contain "usage",
so chunks without it skip the splitlines() + json.loads() entirely and are
returned verbatim — byte-for-byte identical to the previous no-usage behavior.
The rewrite is never dropped (when a usage field exists, "usage" is present in
the bytes, so the chunk still takes the full path); a rare token whose content is
literally "usage" simply falls through and re-parses harmlessly (the nested
match is not a top-level key, so it returns unchanged).

Measured (json.dumps-shaped chat.completion.chunk, in-container aarch64):
_rewrite_usage_sse_event_from_ctx drops from 3.46 µs → 0.25 µs per chunk
(~5% → ~0.4% of one core at 15.4k tok/s). Behavior is unchanged; this only avoids
work on chunks that have nothing to rewrite.

@lancelly
lancelly force-pushed the perf/disagg-skip-sse-usage-parse branch from 7ab5ff8 to 641f858 Compare June 6, 2026 13:41
…rite

When rewriting a disaggregated streaming response's usage from the context
server, _rewrite_usage_sse_event_from_ctx ran json.loads on EVERY relayed SSE
chunk, even though a top-level usage object only appears on the final chunk (and
only when the client requested usage). On the orchestrator's single event loop
this full JSON parse of every relayed output token is ~5% of one core at high
concurrency (~15k tok/s).

Add a byte pre-filter: a top-level usage field cannot exist unless the chunk
bytes contain "usage", so skip the splitlines + json.loads for chunks that
don't. The event is returned verbatim on the fast path (identical to the prior
no-usage behavior), so the rewrite is never dropped; a rare token whose content
is literally "usage" just re-parses harmlessly. Micro-bench: 3.46us -> 0.25us
per chunk (~5%% -> ~0.4%% of one core). Existing streaming usage-rewrite unit
test (test_send_disagg_request) covers both paths and passes.

Signed-off-by: Lance Liao <108499334+lancelly@users.noreply.github.com>
@lancelly
lancelly force-pushed the perf/disagg-skip-sse-usage-parse branch from 641f858 to 2ce78b4 Compare June 6, 2026 13:45
@lancelly
lancelly marked this pull request as ready for review June 8, 2026 06:48
@lancelly
lancelly requested a review from a team as a code owner June 8, 2026 06:48
@lancelly
lancelly requested review from zhenhuaw-me and removed request for a team June 8, 2026 06:48
@lancelly

lancelly commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #52691 [ run ] triggered by Bot. Commit: 2ce78b4 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #52691 [ run ] completed with state FAILURE. Commit: 2ce78b4
/LLM/main/L0_MergeRequest_PR pipeline #41958 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@lancelly

lancelly commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #52721 [ run ] triggered by Bot. Commit: 2ce78b4 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #52721 [ run ] completed with state SUCCESS. Commit: 2ce78b4
/LLM/main/L0_MergeRequest_PR pipeline #41987 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@lancelly

lancelly commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #52768 [ run ] triggered by Bot. Commit: 2ce78b4 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #52768 [ run ] completed with state SUCCESS. Commit: 2ce78b4
/LLM/main/L0_MergeRequest_PR pipeline #42028 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@peihu-nv

peihu-nv commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #52822 [ run ] triggered by Bot. Commit: 2ce78b4 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #52822 [ run ] completed with state SUCCESS. Commit: 2ce78b4
/LLM/main/L0_MergeRequest_PR pipeline #42075 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@peihu-nv

peihu-nv commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #52848 [ run ] triggered by Bot. Commit: 2ce78b4 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #52848 [ run ] completed with state SUCCESS. Commit: 2ce78b4
/LLM/main/L0_MergeRequest_PR pipeline #42101 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@peihu-nv peihu-nv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI failure looks to be flaky test. Not due to the PR

@peihu-nv
peihu-nv merged commit eba87a6 into NVIDIA:feat/deepseek_v4 Jun 8, 2026
6 of 7 checks passed
lfr-0531 pushed a commit to lfr-0531/TensorRT-LLM that referenced this pull request Jun 26, 2026
…rite (NVIDIA#15042)

Signed-off-by: Lance Liao <108499334+lancelly@users.noreply.github.com>
Signed-off-by: Fanrong Li <lfr-0531@users.noreply.github.com>
lancelly added a commit to lfr-0531/TensorRT-LLM that referenced this pull request Jun 26, 2026
…VIDIA#15042

NVIDIA#15042 ("skip per-chunk JSON parse on streaming usage rewrite") was a 5-line
optimization on feat/deepseek_v4, layered on the method-based usage-rewrite
machinery introduced by NVIDIA#13620. When this PR carved NVIDIA#15042 onto main, that
machinery did not exist there: main's NVIDIA#14177 ("Fix disaggregated usage
propagation") had refactored NVIDIA#13620 into postprocess_handlers
(rewrite_usage_info_from_ctx, operating on parsed chunk objects). The carve
folded the obsolete pre-refactor machinery back in to make the patch apply.

The result is dead code: _rewrite_disagg_usage (the chain root) has no caller,
so the whole _ctx_usage_for_client / _rewrite_usage_* /
_rewrite_streaming_usage_from_ctx subtree is unreachable, and NVIDIA#15042's
json.loads-skip optimization sits inside it (a no-op). main already rewrites
disagg usage via postprocess_handlers and relays streaming bytes untouched, so
the optimized per-chunk-parse path no longer exists.

Remove the dead methods and the now-orphaned imports (json, AsyncIterator,
PromptTokensDetails). No behavior change: the live usage path is unaffected.

Signed-off-by: Lance Liao <108499334+lancelly@users.noreply.github.com>
Shixiaowei02 pushed a commit to lfr-0531/TensorRT-LLM that referenced this pull request Jun 29, 2026
…rite (NVIDIA#15042)

Signed-off-by: Lance Liao <108499334+lancelly@users.noreply.github.com>
Signed-off-by: Fanrong Li <lfr-0531@users.noreply.github.com>
Shixiaowei02 pushed a commit to lfr-0531/TensorRT-LLM that referenced this pull request Jun 29, 2026
…VIDIA#15042

NVIDIA#15042 ("skip per-chunk JSON parse on streaming usage rewrite") was a 5-line
optimization on feat/deepseek_v4, layered on the method-based usage-rewrite
machinery introduced by NVIDIA#13620. When this PR carved NVIDIA#15042 onto main, that
machinery did not exist there: main's NVIDIA#14177 ("Fix disaggregated usage
propagation") had refactored NVIDIA#13620 into postprocess_handlers
(rewrite_usage_info_from_ctx, operating on parsed chunk objects). The carve
folded the obsolete pre-refactor machinery back in to make the patch apply.

The result is dead code: _rewrite_disagg_usage (the chain root) has no caller,
so the whole _ctx_usage_for_client / _rewrite_usage_* /
_rewrite_streaming_usage_from_ctx subtree is unreachable, and NVIDIA#15042's
json.loads-skip optimization sits inside it (a no-op). main already rewrites
disagg usage via postprocess_handlers and relays streaming bytes untouched, so
the optimized per-chunk-parse path no longer exists.

Remove the dead methods and the now-orphaned imports (json, AsyncIterator,
PromptTokensDetails). No behavior change: the live usage path is unaffected.

Signed-off-by: Lance Liao <108499334+lancelly@users.noreply.github.com>
lancelly added a commit to lfr-0531/TensorRT-LLM that referenced this pull request Jun 29, 2026
…rite (NVIDIA#15042)

Signed-off-by: Lance Liao <108499334+lancelly@users.noreply.github.com>
Signed-off-by: Fanrong Li <lfr-0531@users.noreply.github.com>
Signed-off-by: Liao Lanyu <108499334+lancelly@users.noreply.github.com>
lancelly added a commit to lfr-0531/TensorRT-LLM that referenced this pull request Jun 29, 2026
…VIDIA#15042

NVIDIA#15042 ("skip per-chunk JSON parse on streaming usage rewrite") was a 5-line
optimization on feat/deepseek_v4, layered on the method-based usage-rewrite
machinery introduced by NVIDIA#13620. When this PR carved NVIDIA#15042 onto main, that
machinery did not exist there: main's NVIDIA#14177 ("Fix disaggregated usage
propagation") had refactored NVIDIA#13620 into postprocess_handlers
(rewrite_usage_info_from_ctx, operating on parsed chunk objects). The carve
folded the obsolete pre-refactor machinery back in to make the patch apply.

The result is dead code: _rewrite_disagg_usage (the chain root) has no caller,
so the whole _ctx_usage_for_client / _rewrite_usage_* /
_rewrite_streaming_usage_from_ctx subtree is unreachable, and NVIDIA#15042's
json.loads-skip optimization sits inside it (a no-op). main already rewrites
disagg usage via postprocess_handlers and relays streaming bytes untouched, so
the optimized per-chunk-parse path no longer exists.

Remove the dead methods and the now-orphaned imports (json, AsyncIterator,
PromptTokensDetails). No behavior change: the live usage path is unaffected.

Signed-off-by: Lance Liao <108499334+lancelly@users.noreply.github.com>
Shixiaowei02 pushed a commit to lfr-0531/TensorRT-LLM that referenced this pull request Jun 30, 2026
…rite (NVIDIA#15042)

Signed-off-by: Lance Liao <108499334+lancelly@users.noreply.github.com>
Signed-off-by: Fanrong Li <lfr-0531@users.noreply.github.com>
Signed-off-by: Liao Lanyu <108499334+lancelly@users.noreply.github.com>
Shixiaowei02 pushed a commit to lfr-0531/TensorRT-LLM that referenced this pull request Jun 30, 2026
…VIDIA#15042

NVIDIA#15042 ("skip per-chunk JSON parse on streaming usage rewrite") was a 5-line
optimization on feat/deepseek_v4, layered on the method-based usage-rewrite
machinery introduced by NVIDIA#13620. When this PR carved NVIDIA#15042 onto main, that
machinery did not exist there: main's NVIDIA#14177 ("Fix disaggregated usage
propagation") had refactored NVIDIA#13620 into postprocess_handlers
(rewrite_usage_info_from_ctx, operating on parsed chunk objects). The carve
folded the obsolete pre-refactor machinery back in to make the patch apply.

The result is dead code: _rewrite_disagg_usage (the chain root) has no caller,
so the whole _ctx_usage_for_client / _rewrite_usage_* /
_rewrite_streaming_usage_from_ctx subtree is unreachable, and NVIDIA#15042's
json.loads-skip optimization sits inside it (a no-op). main already rewrites
disagg usage via postprocess_handlers and relays streaming bytes untouched, so
the optimized per-chunk-parse path no longer exists.

Remove the dead methods and the now-orphaned imports (json, AsyncIterator,
PromptTokensDetails). No behavior change: the live usage path is unaffected.

Signed-off-by: Lance Liao <108499334+lancelly@users.noreply.github.com>
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.

3 participants