[MLX] Fix step-bounded profiling for bench tools on Apple Silicon (#29217)

This commit is contained in:
Lijuan Tang
2026-06-30 22:55:29 -07:00
committed by GitHub
parent a7390b17f8
commit 3cdc2415b1
3 changed files with 96 additions and 0 deletions
@@ -284,7 +284,15 @@ def throughput_test_once(
dir = os.getenv("SGLANG_TORCH_PROFILER_DIR")
if not profile_steps:
known_files = set(os.listdir(dir))
# With --profile-steps the scheduler auto-stops mid-run after N steps, so
# a second stop here raises "not in progress"; a run shorter than N steps
# never hit the target and still needs this explicit stop. Either way we
# must stop before monitor_trace_file, which loops forever waiting for a
# trace that would otherwise never be finalized.
try:
backend.stop_profile()
except RuntimeError:
pass
monitor_trace_file(known_files, dir)
if backend_name == "runtime":
@@ -86,6 +86,16 @@ class SchedulerMlxOverlapMixin:
"""Mixin that adds MLX overlap scheduling to :class:`Scheduler`."""
def _finalize_mlx_pending_job(self: Scheduler, pending: MlxPendingJob):
# Account for this completed forward step. The standard scheduler does
# this inside run_batch(), but the MLX overlap loop bypasses run_batch,
# so without this forward_ct never advances on MLX. That stalls the
# watchdog liveness counter and, more importantly, breaks step-bounded
# profiling: _profile_batch_predicate auto-starts/stops based on
# forward_ct, so `--profile-steps` (and the server /start_profile
# num_steps path) only takes effect once the counter moves here.
self.forward_ct += 1
self.profiler_manager._profile_batch_predicate(pending.schedule_batch)
result = self.tp_worker.finalize_mlx_result(
pending.prefills,
pending.extends,