mllm icon indicating copy to clipboard operation
mllm copied to clipboard

fix(pymllm): reduce scheduler CPU busy-loop from 100% to ~2% during decode

Open FarmersWrap opened this issue 8 months ago โ€ข 1 comments

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_loop and 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.

FarmersWrap avatar Mar 20 '26 19:03 FarmersWrap

๐Ÿ“ Walkthrough

Walkthrough

Introduces an environment-configurable _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.py
Added 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

๐ŸŽฏ 3 (Moderate) | โฑ๏ธ ~20 minutes

Poem

๐Ÿ‡ I nibble milliseconds, then hop away,
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.

โœ๏ธ Tip: You can configure your own custom pre-merge checks in the settings.

โœจ Finishing Touches
๐Ÿงช Generate unit tests (beta)
  • [ ] Create PR with unit tests

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.

coderabbitai[bot] avatar Mar 20 '26 19:03 coderabbitai[bot]