-
Notifications
You must be signed in to change notification settings - Fork 194
fix(pymllm): reduce scheduler CPU busy-loop from 100% to ~2% during decode #655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FarmersWrap
wants to merge
3
commits into
UbiquitousLearning:main
Choose a base branch
from
FarmersWrap:fix/scheduler-cpu-busy-loop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| #!/usr/bin/env python3 | ||
| """Benchmark: CPU busy-loop vs brief-poll in the scheduler event loop. | ||
|
|
||
| Simulates the scheduler's decode loop (poll → "forward" → poll → ...) | ||
| and measures CPU usage under both strategies. | ||
|
|
||
| Usage: | ||
| python pymllm/tests/bench_cpu_busy_loop.py | ||
|
|
||
| What to look for: | ||
| - "CPU usage" percentage: spin-poll should be ~100%, brief-poll should be <10% | ||
| - "Wall time" should be similar (brief-poll adds ~1ms per iteration) | ||
| - "Throughput" (iterations/sec) shows the latency cost of the brief poll | ||
| """ | ||
|
|
||
| import os | ||
| import time | ||
|
|
||
| import zmq | ||
|
|
||
|
|
||
| def run_loop(poller, sock, poll_timeout_ms: int, duration_s: float = 2.0): | ||
| """Run the scheduler-style poll loop for *duration_s* seconds. | ||
|
|
||
| The loop body does NO simulated work — this isolates the poll overhead, | ||
| which is exactly what happens in the real scheduler between GPU kernel | ||
| launches (the CPU thread is free while the GPU computes; it's the poll | ||
| call that either spins or yields). | ||
|
|
||
| Returns (wall_time, cpu_time, iterations). | ||
| """ | ||
| iterations = 0 | ||
| t0_wall = time.monotonic() | ||
| t0_cpu = time.process_time() | ||
| deadline = t0_wall + duration_s | ||
|
|
||
| while time.monotonic() < deadline: | ||
| # Poll for new requests (this is where CPU spins or yields) | ||
| timeout = poll_timeout_ms | ||
| while True: | ||
| events = dict(poller.poll(timeout=timeout)) | ||
| if sock not in events: | ||
| break | ||
| timeout = 0 # drain remaining | ||
| sock.recv(zmq.NOBLOCK) # consume message | ||
| iterations += 1 | ||
|
|
||
| wall = time.monotonic() - t0_wall | ||
| cpu = time.process_time() - t0_cpu | ||
| return wall, cpu, iterations | ||
|
|
||
|
|
||
| def main(): | ||
| ctx = zmq.Context() | ||
| sock = ctx.socket(zmq.PULL) | ||
| addr = f"inproc://bench-{os.getpid()}" | ||
| sock.bind(addr) | ||
|
|
||
| poller = zmq.Poller() | ||
| poller.register(sock, zmq.POLLIN) | ||
|
|
||
| duration = 3.0 # seconds per test | ||
|
|
||
| print("=" * 64) | ||
| print("Scheduler CPU Busy-Loop Benchmark") | ||
| print("=" * 64) | ||
| print(f"Each test runs for {duration:.0f}s simulating the scheduler poll loop") | ||
| print(f"(poll for requests → loop back, no simulated GPU work)") | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| print() | ||
|
|
||
| # --- Spin poll (timeout=0) --- | ||
| print("Running SPIN POLL (timeout=0) ...") | ||
| spin_wall, spin_cpu, spin_iters = run_loop(poller, sock, 0, duration) | ||
| spin_pct = 100.0 * spin_cpu / max(spin_wall, 1e-9) | ||
| spin_throughput = spin_iters / max(spin_wall, 1e-9) | ||
|
|
||
| # --- Brief poll (timeout=1ms) --- | ||
| print("Running BRIEF POLL (timeout=1ms) ...") | ||
| brief_wall, brief_cpu, brief_iters = run_loop(poller, sock, 1, duration) | ||
| brief_pct = 100.0 * brief_cpu / max(brief_wall, 1e-9) | ||
| brief_throughput = brief_iters / max(brief_wall, 1e-9) | ||
|
|
||
| sock.close() | ||
| ctx.term() | ||
|
|
||
| # --- Results --- | ||
| print() | ||
| print("-" * 64) | ||
| print(f"{'Metric':<30} {'Spin (before)':>15} {'Brief (after)':>15}") | ||
| print("-" * 64) | ||
| print(f"{'Wall time (s)':<30} {spin_wall:>15.3f} {brief_wall:>15.3f}") | ||
| print(f"{'CPU time (s)':<30} {spin_cpu:>15.3f} {brief_cpu:>15.3f}") | ||
| print(f"{'CPU usage (%)':<30} {spin_pct:>14.1f}% {brief_pct:>14.1f}%") | ||
| print(f"{'Iterations':<30} {spin_iters:>15d} {brief_iters:>15d}") | ||
| print(f"{'Throughput (iter/s)':<30} {spin_throughput:>15.1f} {brief_throughput:>15.1f}") | ||
| print("-" * 64) | ||
|
|
||
| reduction = spin_pct - brief_pct | ||
| throughput_cost = 100.0 * (1 - brief_throughput / max(spin_throughput, 1)) if spin_throughput > 0 else 0 | ||
| print() | ||
| print(f"CPU usage reduction: {reduction:+.1f} percentage points") | ||
| print(f"Throughput cost: {throughput_cost:.1f}% fewer iterations/sec") | ||
| print() | ||
| if reduction > 20: | ||
| print("RESULT: Significant CPU savings with negligible throughput cost.") | ||
| elif reduction > 5: | ||
| print("RESULT: Moderate CPU savings.") | ||
| else: | ||
| print("RESULT: Minimal difference (forward pass dominates loop time).") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.