fix(pymllm): reduce scheduler CPU busy-loop from 100% to ~2% during decode
Summary
- The scheduler event loop used
poll(timeout=0)(non-blocking spin) between decode batches, burning 100% of a CPU core while waiting for new requests - Track decode state in
event_loopand use a 1ms poll timeout during active decode to yield the CPU, dropping usage to ~2% - Apply the same optimization to both ZMQ and shared-queue receive paths
Details
During continuous decode (token generation), the scheduler's recv_requests() path called zmq.Poller.poll(timeout=0) in a tight loop. Since decode batches are always available, the idle sleeper never kicks in, and the CPU spins at 100%.
The fix tracks whether the previous iteration ran a decode batch (_in_decode flag). When true, the first poll uses _DECODE_POLL_TIMEOUT_MS (1ms) which lets the OS schedule other work on the core. Subsequent polls in the same recv_requests call drain any queued messages non-blocking, so no latency is added for burst arrivals.
Why 1ms? Decode forward passes on GPU typically take >1ms even for small models, so a 1ms yield adds no measurable generation latency. Prefill (extend) batches remain fully non-blocking since they are latency-sensitive.
Benchmark results
Integration test using the real SchedulerProcess with ZMQ sockets and a mock model runner:
| Metric | Before (spin) | After (brief) |
|---|---|---|
| CPU usage | 100.0% | 1.8% |
| CPU time (5s window) | 5.00s | 0.09s |
Test plan
- [x] 10 unit tests covering poll timeout values, parameter forwarding, event loop state tracking, ForwardMode behavior, CPU measurement, and constant validation
- [x] Standalone ZMQ poll micro-benchmark
- [x] Full SchedulerProcess integration test with real ZMQ sockets
- [x] All existing tests still pass
๐ค Generated with Claude Code
Summary by CodeRabbit
-
Performance
- Reduced CPU usage between batch decode operations by using an adaptive, brief-polling receive strategy; improves efficiency during sustained workloads.
- Exposed a configurable timeout (via environment variable) to fine-tune brief-poll behavior for different deployment loads.
-
Documentation
- Updated runtime docs to describe the decode-between-batches yield and polling behavior.
Introduces an environment-configurable ๐ฏ 3 (Moderate) | โฑ๏ธ ~20 minutes ๐ I nibble milliseconds, then hop away, โ๏ธ Tip: You can configure your own custom pre-merge checks in the settings.๐ Walkthrough
Walkthrough
_DECODE_POLL_TIMEOUT_MS and a brief_poll flag to the scheduler's receive methods. When brief_poll is true, ZMQ and shared-queue receive paths use a short initial blocking poll/get (then switch to draining) to yield CPU between decode batches.Changes
Cohort / File(s)
Summary
Scheduler Process Decode Polling
pymllm/orchestrator/scheduler_process.pyAdded module-level
_DECODE_POLL_TIMEOUT_MS (from MLLM_DECODE_POLL_TIMEOUT_MS, default 1 ms) and _read_decode_poll_timeout_ms(); added brief_poll: bool parameter to recv_requests(), _recv_from_zmq(), and _recv_from_shared_queue(); ZMQ path uses initial poll timeout when brief_poll=True then switches to non-blocking drain; shared-queue path uses initial Queue.get() timeout (_DECODE_POLL_TIMEOUT_MS/1000.0) then reverts to short draining timeout. Docstrings updated.Estimated code review effort
Poem
Brief polls hush the bustle of the day.
Between decode batches I softly rest,
Tiny pauses make the whole run best. ๐ฅ๐ฅ Pre-merge checks | โ
2 | โ 1
โ Failed checks (1 warning)
Check name
Status
Explanation
Resolution
Docstring Coverage
โ ๏ธ Warning
Docstring coverage is 47.06% 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
Title check
โ
Passed
The title clearly and concisely summarizes the main change: reducing CPU usage during decode from 100% to ~2% by optimizing the scheduler's polling mechanism.
Description check
โ
Passed
The description is comprehensive and well-structured, covering the problem statement, solution details, benchmark results, and test plan; however, it does not follow the repository's description template which requests a reference to contributing guidelines.
โจ Finishing Touches
๐งช Generate unit tests (beta)
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.
Comment @coderabbitai help to get the list of available commands and usage tips.