PyTorch Profiler
PyTorch Profiler provides detailed kernel execution time, call stack, and GPU utilization metrics.Denoising Stage Profiling
Profile the denoising stage with sampled timesteps (default: 5 steps after 1 warmup step):--profile: Enable profiling for the denoising stage--num-profiled-timesteps N: Number of timesteps to profile after warmup (default: 5)- Smaller values reduce trace file size
- Example:
--num-profiled-timesteps 10profiles 10 steps after 1 warmup step
Full Pipeline Profiling
Profile all pipeline stages (text encoding, denoising, VAE decoding, etc.):--profile-all-stages: Used with--profile, profile all pipeline stages instead of just denoising
Image API requests
For an already running image server, enable profiling on an individual request. The JSON generations endpoint accepts the same controls with underscores:/v1/images/edits, pass these controls as multipart form fields alongside
the image and prompt: -F profile=true -F num_profiled_timesteps=5.
Add -F profile_all_stages=true to profile the full pipeline instead.
Traces are written on the server, not returned as image-response attachments.
Use perf_dump_path for a lightweight timing report instead of a profiler trace.
Run latency benchmarks separately with profiling disabled, since collecting a
trace adds overhead.
Output Location
Trace output uses the first available setting:- An explicit
log_dirpassed to the Python profiler wrapper. SGLANG_DIFFUSION_TORCH_PROFILER_DIR.SGLANG_TORCH_PROFILER_DIR../logs/.
Ascend NPU traces
On Linux with an Ascend NPU deployment and its matchingtorch_npu/CANN installation, use the same --profile, --num-profiled-timesteps, and --profile-all-stages controls. No separate parsing command is required.
NPU traces use the native Ascend directory layout rather than the *.trace.json.gz filename above:
NPU profile analysis submitted; status: log identifies its status file, which may not exist until the new interpreter starts. Non-daemon callers keep native automatic parsing.
On a normal stop, daemon workers coordinate capture-stop errors across the participating ranks before submitting analysis. A capture-stop error is reported to all participating ranks; it is distinct from a background parser failure. If the request fails or is cancelled, profiling cleanup stays local, does not submit another parser, and preserves the original request error. Repeated cleanup does not stop or submit the same capture again.
The parser runs without a fixed timeout and can finish after the generation worker exits. It writes progress and results atomically to sglang_analysis.json:
running: analysis has started; a trace may not be ready yet.succeeded: analysis finished and produced a nonemptytrace_view.json.trace_pathidentifies the artifact.failed: analysis failed, or no nonempty trace was produced. Readerrorandsglang_analysis.log. Parser launch failures are also reported separately.
succeeded before opening the trace; completion of the generation command alone does not mean analysis is complete. If the parser is forcibly killed or its host shuts down, it may not write a final status. SGLang does not monitor or automatically retry these tasks.
Background analysis still consumes CPU, memory, and I/O resources. Profiled latency is not an unprofiled benchmark. A nonempty trace proves an artifact was produced, not that all expected events are present; inspect native warnings and the trace itself. Stopping the generation worker does not cancel an already submitted parser; destroying its container or host can still terminate it.
View Traces
Load and visualize trace files at:- https://ui.perfetto.dev/ (recommended)
- chrome://tracing (Chrome only)
--num-profiled-timesteps or avoid using --profile-all-stages.
--perf-dump-path (Stage/Step Timing Dump)
Besides profiler traces, you can also dump a lightweight JSON report that contains:
- stage-level timing breakdown for the full pipeline
- step-level timing breakdown for the denoising stage (per diffusion step)
denoise_steps_ms field formatted as an array of objects, each with a step key (the step index) and a duration_ms key.
Example:
Nsight Systems
Nsight Systems provides low-level CUDA profiling with kernel details, register usage, and memory access patterns.Installation
See the SGLang profiling guide for installation instructions.Basic Profiling
Profile the entire pipeline execution:Targeted Stage Profiling
Use--delay and --duration to capture specific stages and reduce file size:
--delay N: Wait N seconds before starting capture (skip initialization overhead)--duration N: Capture for N seconds (focus on specific stages)--force-overwrite: Overwrite existing output files
Notes
- Reduce trace size: Use
--num-profiled-timestepswith smaller values or--delay/--durationwith Nsight Systems - Stage-specific analysis: Use
--profilealone for denoising stage, add--profile-all-stagesfor full pipeline - Multiple runs: Profile with different prompts and resolutions to identify bottlenecks across workloads
FAQ
- If you are profiling
sglang generatewith Nsight Systems and find that the generated profiler file did not capture any CUDA kernels, you can resolve this issue by increasing the model’s inference steps to extend the execution time.
