Skip to content

fix: vllm forwardpass metric for async scheduling - #7537

Merged
tedzhouhk merged 2 commits into
mainfrom
hzhou/fpm-vllm-async
Mar 23, 2026
Merged

tedzhouhk merged 2 commits into
mainfrom
hzhou/fpm-vllm-async

Conversation

@tedzhouhk

@tedzhouhk tedzhouhk commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Fix InstrumentedScheduler to work correctly with vLLM's default async scheduling mode by extending AsyncScheduler instead of Scheduler.
  • Simplify the metrics emission model: remove schedule() override, use scheduler_output parameter directly, measure wall_time as interval between consecutive update_from_output() calls.
  • Add --save-plot <path.png> to recv_forward_pass_metrics.py for visualizing FPM time-series data.

Reviewer guide

Background: sync vs async scheduling in vLLM

vLLM 0.16+ defaults to async scheduling (async_scheduling=True), where the engine overlaps CPU scheduling with GPU execution via step_with_batch_queue. This means schedule() is called twice before the first update_from_output():

  1. schedule(N) -> submit to GPU -> return early (don't wait)
  2. schedule(N+1) -> submit -> pop batch N -> block for GPU(N) -> update_from_output(N)

AsyncScheduler handles this by adding output placeholders in _update_after_schedule(), keeping num_new_tokens == 1 for all running requests so the second schedule() can schedule them again.

The bug (before this PR)

InstrumentedScheduler extended Scheduler (no placeholders). In async mode, the second schedule() saw num_new_tokens == 0 for all requests advanced by _update_after_schedule, producing partial batches (e.g. 22/28 split of 50 requests). This made num_decode_requests, sum_decode_kv_tokens, and wall_time all incorrect.

Additionally, the old code saved _pending_output in schedule() and read it in update_from_output(). In async mode, the second schedule() overwrote _pending_output before the first update_from_output() could read it (state corruption).

The fix

  1. Extend AsyncScheduler — output placeholders ensure all requests are scheduled every step, no partial batches.
  2. Remove schedule() override — no pending state to save. Use the scheduler_output parameter passed to update_from_output() directly (always correct, even in async mode).
  3. wall_time = update-to-update interval — the EngineCore blocks on future.result() right before calling update_from_output(), so the interval between consecutive calls approximates GPU forward pass time.
  4. Skip empty batches — total_num_scheduled_tokens == 0 means no real work; reset _last_update_time on idle.

Files changed

File What changed
instrumented_scheduler.py Extend AsyncScheduler, rewrite update_from_output(), remove schedule() override, add detailed module docstring
recv_forward_pass_metrics.py Add --save-plot <path.png> CLI arg for 5-panel time-series visualization

How to test

# Terminal 1: start vLLM with FPM enabled (async mode, default)
DYN_FORWARDPASS_METRIC_PORT=20380 \
  examples/backends/vllm/launch/agg.sh \
  --gpu-memory-utilization 0.9 --max-num-seqs 128 --max-num-batched-tokens 4096

# Terminal 2: receive and plot
.venv/bin/python -m dynamo.common.recv_forward_pass_metrics \
  --namespace dynamo --component backend --endpoint generate \
  --save-plot fpm_metrics.png

# Terminal 3: send load
.venv/bin/aiperf profile -m Qwen/Qwen3-0.6B --endpoint-type chat --streaming \
  --concurrency 50 --synthetic-input-tokens-mean 128 --synthetic-input-tokens-stddev 0 \
  --request-count 200 --ui none

After aiperf finishes, Ctrl+C Terminal 2. Check:

  • num_decode_requests reaches 50 at steady state (not 22/28 split)
  • wall_time > 0 during active load, 0.0 on first message
  • fpm_metrics.png shows the 5-panel plot

Also test with --no-async-scheduling added to Terminal 1 to verify sync mode works identically.

Made with Cursor

Summary by CodeRabbit

  • New Features

    • Added optional CLI flag --save-plot to export metric visualizations as PNG files on process exit.
  • Refactor

    • Updated scheduler component architecture and modified metrics collection and timing calculation behavior.

Extend AsyncScheduler instead of Scheduler so the InstrumentedScheduler
works correctly with vLLM's default async scheduling mode.  Without
output placeholders the async engine produces partial batches (e.g.
22/28 split of 50 requests).
Additional changes:
- Remove schedule() override; emit metrics in update_from_output() using
  the scheduler_output parameter directly (always correct in both modes).
- Measure wall_time as interval between consecutive update_from_output()
  calls (approximates GPU forward pass time).
- Skip empty batches (total_num_scheduled_tokens == 0) and reset
  wall_time on idle transitions.
- Add --save-plot <path.png> to recv_forward_pass_metrics.py for
  visualizing FPM time-series data.

Signed-off-by: hongkuanz <[email protected]>
@tedzhouhk
tedzhouhk requested a review from a team as a code owner March 19, 2026 22:52
@tedzhouhk
tedzhouhk requested a review from a team March 19, 2026 22:52
@github-actions github-actions Bot added feat backend::vllm Relates to the vllm backend labels Mar 19, 2026
@tedzhouhk

Copy link
Copy Markdown
Contributor Author
fpm_metrics qwen3-0.6b on RTX A6000 i8192o1024c20n80

@tedzhouhk tedzhouhk changed the title feat: fix FPM for async scheduling and add time-series plot fix: vllm forwardpass metric for async scheduling Mar 19, 2026
@github-actions github-actions Bot added fix and removed feat labels Mar 19, 2026
@coderabbitai

coderabbitai Bot commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

Two files updated: one adds optional CLI flag for saving ForwardPassMetrics plots on exit, implementing in-memory metric recording with matplotlib-based visualization; the other refactors instrumented scheduler to use AsyncScheduler base class with revised wall-time computation and simplified metric publishing logic.

Changes

Cohort / File(s) Summary
ForwardPassMetrics visualization
components/src/dynamo/common/recv_forward_pass_metrics.py
Added --save-plot PATH CLI flag to persist received metrics as 5-panel time-series PNG. Introduced in-memory history tracking with time.monotonic() timestamps and matplotlib-based rendering using Agg backend on shutdown. Conditional accumulation occurs only when flag is set. Refactored imports to move FpmEventSubscriber from top-level to local scope inside run().
AsyncScheduler base class migration
components/src/dynamo/vllm/instrumented_scheduler.py
Changed base class from Scheduler to AsyncScheduler. Removed schedule() method override. Refactored wall-time computation to occur inside update_from_output() using _last_update_time (reset to 0.0 for empty batches), replacing prior _schedule_time tracking. Simplified metrics publishing to extract directly from scheduler_output argument and fresh self._compute_queued() calls rather than buffering in _pending_output/_pending_queued. Removed obsolete state fields.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description check ✅ Passed The PR description provides a comprehensive overview, detailed background, explanation of the bug and fix, files changed, and testing instructions, covering all template sections.
Title check ✅ Passed The PR title mentions 'vllm forwardpass metric for async scheduling' but the actual changes include both the InstrumentedScheduler fix for async scheduling AND a new time-series plot visualization feature in recv_forward_pass_metrics.py. The title only partially captures the scope.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

You can customize the high-level summary generated by CodeRabbit.

Configure the reviews.high_level_summary_instructions setting to provide custom instructions for generating the high-level summary.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
components/src/dynamo/common/recv_forward_pass_metrics.py (1)

108-110: Move FpmEventSubscriber back to module scope.

This import is not behind an optional code path, so moving it into run() only defers failures until execution and violates the repo's import rule. If dynamo.llm is intentionally optional here, make that explicit; otherwise import it with the rest of the module imports.

As per coding guidelines, keep all import statements at the top of the file; imports inside functions should be flagged unless an explicit optional-dependency exception applies.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@components/src/dynamo/common/recv_forward_pass_metrics.py` around lines 108 -
110, The import of FpmEventSubscriber is moved into run(), which defers
import-time failures and violates the module-import rule; move the line "from
dynamo.llm import FpmEventSubscriber" back to module scope with the other
imports (or, if dynamo.llm truly is optional, add explicit optional-dependency
handling and document it), updating the top of the file so FpmEventSubscriber is
imported at import time rather than inside the run() function.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@components/src/dynamo/common/recv_forward_pass_metrics.py`:
- Around line 24-32: Move the plotting imports and backend selection into the
lazy helper: remove top-level imports of matplotlib and matplotlib.pyplot and
instead call matplotlib.use("Agg") immediately inside _save_plot() just before
importing matplotlib.pyplot so the backend is set before pyplot is loaded; keep
msgspec and other existing module imports at top. Also move the unconditional
import of FpmEventSubscriber out of run() to the module-level imports (alongside
ForwardPassMetrics and DistributedRuntime) so FpmEventSubscriber is imported at
file top and not inside run(). Ensure references to _save_plot() and
FpmEventSubscriber continue to work after the change.

---

Nitpick comments:
In `@components/src/dynamo/common/recv_forward_pass_metrics.py`:
- Around line 108-110: The import of FpmEventSubscriber is moved into run(),
which defers import-time failures and violates the module-import rule; move the
line "from dynamo.llm import FpmEventSubscriber" back to module scope with the
other imports (or, if dynamo.llm truly is optional, add explicit
optional-dependency handling and document it), updating the top of the file so
FpmEventSubscriber is imported at import time rather than inside the run()
function.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 471a11aa-3661-4f64-b636-49f304588aa0

📥 Commits

Reviewing files that changed from the base of the PR and between e7bcf65 and 3c6c1c3.

📒 Files selected for processing (2)
  • components/src/dynamo/common/recv_forward_pass_metrics.py
  • components/src/dynamo/vllm/instrumented_scheduler.py

@tedzhouhk
tedzhouhk merged commit cbdab50 into main Mar 23, 2026
105 checks passed
@tedzhouhk
tedzhouhk deleted the hzhou/fpm-vllm-async branch March 23, 2026 17:49
WenjiaoYue pushed a commit to WenjiaoYue/dynamo that referenced this pull request Mar 25, 2026
yao531441 pushed a commit to yao531441/dynamo that referenced this pull request May 13, 2026

This branch was previously deployed

1 inactive deployment
GITLAB — 9c60ff45 Deployed Mar 23, 2026 by copy-pr-bot[bot] via Trigger CI Pipeline #21482
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend::vllm Relates to the vllm backend fix size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants