help wantedhigh priority
Repository metrics
- Stars
- (4,990 stars)
- PR merge metrics
- (PR metrics pending)
Description
Motivation.
Optimize DiffusionEngine concurrency, telemetry, and CPU utilization
Description:
The DiffusionEngine currently has a few architectural bottlenecks that severely limit concurrency, waste host CPU cycles, and report inaccurate telemetry. This issue tracks the refactoring needed to make the engine truly asynchronous, improve memory handling during the warmup phase, and fix bugs in our metric reporting.
Problems Identified:
- CPU Starvation (Busy-Wait): The
add_req_and_wait_for_responsemethod uses an unthrottledwhile True:loop to poll the scheduler, which pins a CPU core at 100% and starves other threads. - Sequential Bottleneck:
_rpc_lockwraps the entire request lifecycle (scheduling + execution). This prevents any other requests from being queued while the GPU is processing a task, defeating the purpose of the scheduler. - Telemetry Bugs: The
metricsdictionary instep()swaps the names for execution time and total time, duplicates thepreprocess_time_mskey, and mixes telemetry times with request parameters. - Redundant Computation:
supports_audio_outputis evaluated inside a loop for every prompt, and_dummy_runexpensively allocates and slices random numpy arrays instead of generating precisely what is needed. - Readability: Deeply nested audio-slicing logic in the multiple-request handling block makes the
step()method difficult to maintain.
Proposed Change.
- Fix Concurrency & Locking:
- Introduce a sleep (e.g.,
time.sleep(0.001)) or anasyncio.Event/threading.Conditionin theadd_req_and_wait_for_responsepolling loop. - Scope
_rpc_lockto cover only the scheduler state updates, releasing it before callingself.executor.add_req(req).
- Introduce a sleep (e.g.,
- Fix Telemetry & Metrics:
- Correct the logical bug where
diffusion_engine_exec_time_msanddiffusion_engine_total_time_msare swapped. - Standardize metric keys (e.g., prefix times with
time_and parameters withparam_). - Remove the redundant
preprocessing_time_msappend block.
- Correct the logical bug where
- Optimize Performance & Memory:
- Evaluate
supports_audio_output(self.od_config.model_class_name)once outside the prompt loop. - Change
_dummy_runto generate an exact-sized array (e.g.,np.random.randn(audio_sr * 2)) or usenp.zerosif the model allows it. - Evaluate using
.to("cpu", non_blocking=True)instead of.cpu()for offloading, if post-processing allows.
- Evaluate
- Refactor Code Quality:
- Extract the complex audio slicing logic in
step()into a private helper function (e.g.,_extract_audio_slice).
- Extract the complex audio slicing logic in
Expected Impact:
- Vastly improved CPU utilization (no more busy-waiting).
- Ability to queue multiple requests asynchronously.
- Accurate, dashboard-ready telemetry.
- Cleaner, more maintainable code in the core engine
step()function.
Feedback Period.
No response
CC List.
@ZJY0516 @SamitHuang @wtomin @asukaqaq-s
Any Other Things.
No response
Before submitting a new issue...
- Make sure you already searched for relevant issues, and asked the chatbot living at the bottom right corner of the documentation page, which can answer lots of frequently asked questions.