Skip to content

Profile SGLang decode and MTP on GH200: attribution, head baseline, bytes per step - #13

Merged
SamMausberg merged 25 commits into
mainfrom
profile/attribution
Sep 30, 2026
Merged

SamMausberg merged 25 commits into
mainfrom
profile/attribution

Conversation

@SamMausberg

@SamMausberg SamMausberg commented Sep 30, 2026 •

Copy link
Copy Markdown
Owner

Profiles of SGLang serving Qwen3.5-4B on the GH200 in the production configuration (CUDA graphs, overlap scheduler, FlashInfer attention, greedy), for plain decode at B = 1, 8, 32, 128 and native-MTP speculation at B = 1, 8, 32: per-step attribution, the LM-head baseline microbenchmark, the measured HBM bandwidth, a bytes-per-step model, the host functions behind MTP's idle time, and unprofiled throughput baselines. This answers H1 and gives the first ranked list of stack-wide bottlenecks. evidence/profiles/README.md has the findings, the command behind every file and the caveats.

Findings (all in evidence/profiles/)

  • HBM: read peak 3.83 TB/s over 4 GiB, 3.79 TB/s over the head's 1.27 GB; D2D copy 2.60 TB/s counting read and write.
  • Head kernels: torch.matmul -> cuBLAS nvjet_sm90_tst_* with BF16 logits, an in-graph FP32 copy, and an eager torch.argmax (plain and verify) or SGLang's Triton top-1 (MTP drafts). The GEMM runs at 93-95% of the read peak up to M = 32 (354.5 us at M = 1), so a kernel reading the same bytes cannot gain much; at M >= 64 the FP32 copy and argmax add 22-50%.
  • H1, plain decode: head chain = 10.3 / 9.4 / 7.8 / 6.1% of a step at B = 1 / 8 / 32 / 128.
  • H1, MTP: head chain = 17.2 / 15.3 / 12.4% of a cycle at B = 1 / 8 / 32 (three draft heads + one verify head per cycle), 27.5% of GPU-busy time at B = 1.
  • Recurrent state: the FP32 GDN decode kernel streams 100.7 MB per request per step at 3.47 TB/s; 18.0% of a plain step at B = 32 and 41.6% at B = 128; its bytes equal the weights' at B = 84 (derived). MTP verification writes one FP32 state per draft position and commits one back (352 MB per request per cycle; 27% of the cycle at B = 32).
  • MTP host gap: 37 / 33 / 24% of the profiled cycle is GPU idle; about 21 / 20 / 15% remains without the profiler (derived). The GPU waits mostly on target-verify attention planning on the host (eagle_prepare_for_verify > load_batch > FlashInfer plan, 1.2-1.5 ms per cycle) and the draft's kv_indptr copy (0.5 ms).
  • Throughput without a profiler (3 windows each): plain 289 / 2048 / 6298 / 14388 tok/s; MTP 384 / 2573 / 7087 tok/s (1.33 / 1.26 / 1.13x plain).
  • P5: layer 0's input projections are 0.33-0.60% of a plain step, below P5's 1% threshold.

Deliberately deferred to a follow-up PR

These scripts are in this PR but have not produced committed outputs yet; their runs are queued in the GPU lock: the plain traces regenerated with the committed code (the README documents the producing state of the current ones; their eager records are complete), gdn_kernel_bench.json, ncu_key_kernels.json, microbench_clocks.json (with the cuBLAS API trace), the eager-run GEMM-label validation against module NVTX ranges, the /start_profile comparison, the graph-level-trace calibration of MTP idle time, and the DFlash attribution for the drafter workstream.

Testing and review

  • pre-commit run on the changed files passes (ruff, mypy after merging main, codespell, shellcheck). Early commits used SKIP=mypy while main had 9 unrelated mypy errors; they are fixed on main.
  • A reviewer reproduced attribution/plain_bs{1,128} and tables.md byte for byte from the raw traces. label_structure_check.json confirms the GEMM labels against the model's per-step structure in every complete plain and MTP replay; every committed trace has a kernel record for every eager launch.
  • Review items: evidence README with commands (added); nsys windows labelled as perturbation checks, throughput only from unprofiled repeats (done); plain-trace provenance (documented, rerun queued); PR body, 93-95% (fixed); Codex P1s on hard-coded paths and ncu CSV parsing (fixed in 87f5e65, see the PR comment).
  • Installed nvtx==0.2.16 into ~/sglang/.venv (uv) for the host-function trace; py-spy and ncu run under sudo because of ptrace_scope=1 and admin-only GPU counters.

hbm_bandwidth.py measures the local read and copy bandwidth that the
attribution uses as its peak (3.83 TB/s read, 3.79 TB/s over the head's
1.27 GB footprint). head_microbench.py times the exact PyTorch calls SGLang
runs for the tied 248320 x 2560 BF16 head (matmul, FP32 logits copy, greedy
argmax, and the EAGLE top-1 kernels) under CUDA graphs at M = 1..256 with
warm and cold L2; head_kernel_names.py recovers the cuBLAS kernel behind
each shape from an nsys trace.
run_profiles.py launches one server per arm under nsys launch, drives C
concurrent long greedy generations (drive_decode.py) and collects a 2 s
steady-state window per concurrency with CUDA-graph node tracing, plus an
uncollected window for the perturbation check. attribute.py splits each
decode step into kernel categories (GEMMs labelled by their neighbours in
the graph replay), eager work, copies and idle time inside and outside graph
replays; bytes_model.py converts the step into HBM bytes by component and
checks them against the kernel times.
attribute.py now reports, per step, the host lead of each graph launch, the
host synchronizations, per-category wall time and whether every eager launch
has a kernel record (CUPTI can drop them when its buffers fill).
bytes_model.py adds per-GEMM achieved bandwidth and a derived bytes sweep over
batch size for plain decode and MTP; summarize.py writes tables.md and the
CSVs the paper figures read.
run_all.sh reproduces every profile, one exclusive hold per step (or several
steps under one hold with VP_LOCKED=1). New pieces: a CUPTI flush interval
for nsys (the MTP windows lost eager kernel records without it), run
metadata with repo SHA and nsys version, an NVTX host-function trace for the
speculative worker (diagnostic), a standalone GDN decode/verify kernel bench,
Nsight Compute on the head GEMM and the GDN kernels, and a check of the GEMM
labels against module NVTX ranges in an eager run.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-30T19:24:17.693569Z 153b6d7 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 153b6d78c8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread experiments/profiling/run_all.sh Outdated
fi
RUN=(python "$REPO/experiments/profiling/run_profiles.py")
# shellcheck source=/dev/null
source "$HOME/verified-progress/scripts/sglang_env.sh"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Resolve helper scripts from the computed repository root

When the checkout is not exactly $HOME/verified-progress—including this configured /workspace/verified-progress checkout—the runner exits here before starting any experiment because the sourced file does not exist. The GPU-lock path on line 20 and the same environment-source path in run_microbench.sh and run_ncu.sh are hard-coded likewise; use the already computed $REPO/scripts/... paths so the documented runner works from the actual checkout.

AGENTS.md reference: AGENTS.md:L7-L10

Useful? React with 👍 / 👎.

Comment thread experiments/profiling/ncu_summary.py Outdated
Comment on lines +74 to +77
header, units, first = rows[0], rows[1], rows[2]
out: dict = {'report': report.name}
stalls = {}
for name, unit, value in zip(header, units, first, strict=True):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Parse every row of Nsight Compute raw CSV

ncu --csv --page raw emits a header followed by one row per metric, using columns such as Metric Name, Metric Unit, and Metric Value, rather than separate header/unit/value rows (Nsight Compute CLI guide). Consequently, this assignment treats the first two metric records as units and first, and the subsequent loop compares fixed column names such as ID and Metric Name against KEEP; the generated summary contains none of the requested duration, traffic, throughput, or stall measurements. Iterate over the metric rows and read those three named columns instead.

Useful? React with 👍 / 👎.

Speculative cycles are host-bound, so CPU work from other jobs during a
window would inflate their idle gaps; the window summary now carries the
1-minute load average before and after.
host_gaps.py intersects the GPU's idle intervals in each cycle with the
innermost NVTX range on the scheduler thread, from a run whose host
functions were annotated with nsys --python-functions-trace.
@SamMausberg

Copy link
Copy Markdown
Owner Author

Verdict: REQUEST CHANGES

Reviewed at 7e8a257, whole diff. The plain-decode attribution is solid and reproducible. What blocks the merge is completeness and provenance: the evidence README, the MTP evidence and the unprofiled baselines are missing, the throughput table is not labelled as profiler-attached, and the plain profiles were produced by code that differs from what is committed.

What I ran and checked

  • Attribution reproduces. I reran attribute.py on the raw traces (~/vp-data/profile/plain_nsys/plain_bs{1,128}.nsys-rep, copied to a scratch directory). The summary, categories and kernels JSON, and the categories CSV, are identical to the committed files.
  • Tables reproduce. summarize.py --evidence <copy of evidence/profiles> regenerates tables.md byte for byte.
  • Numbers are consistent.
    • The head chain is 10.26 / 9.36 / 7.80 / 6.12 % of a step, as quoted.
    • GDN recurrent time is 3.5 / 18.0 / 41.6 %.
    • The bytes model's implied bandwidth for the GDN state is 3.47-3.48 TB/s at B = 32 and 128.
    • The B = 84 crossover is 8.41 GB / 100.7 MB.
    • The contexts in bytes_per_step.json follow from the committed windows file: prompt 31.5 tokens + completion at window start + half the window's tokens.
  • Server configuration. The startup log shows overlap on (disable_overlap_schedule: False), decode CUDA graphs captured for bs 1-133, the flashinfer attention backend, enable_fp32_lm_head: False and max_running_requests 133.
  • SGLang call sites. The ones named in head_microbench.py exist at the pin: LogitsProcessor._compute_lm_head (logits_processor.py:955), _copy_logits_to_buffer (:1139), torch.argmax in Sampler.forward (sampler.py:193) and draft_topk1_postprocess (kernels/ops/speculative/topk1.py:74).
  • Hooks. pre-commit run --files <PR files> on a merge with current main passes every hook, including mypy.
  • Hygiene. No AI co-author lines, nothing private, and every file is under 1 MB (largest 56 KB).

Blocking

1. The evidence README and the evidence behind half of the PR's claims are missing.

  • No README. There is no evidence/profiles/README.md. The brief requires one with the exact command behind each file, and summarize.py itself says "Every number in the README's tables comes from this file".
  • Committed scripts write files that are not committed:
    • MTP attribution (mtp_bs{1,8,32});
    • the unprofiled --mode none --repeats 3 windows;
    • microbench_clocks.json (run_microbench.sh);
    • gdn_kernel_bench.json;
    • ncu_key_kernels.json;
    • label_validation.json.
  • Consequence. The PR body says it "answers H1 (the head's share)", but only plain decode is evidenced. The speculative half of H1 (the notes report 17.3 % of an MTP cycle and 27.5 % of GPU-busy time) has no committed artifact. And every category in the attribution depends on the neighbour-based GEMM labels, which have no committed validation.
  • Fix. Commit these files with the README. Or narrow the PR to plain decode: drop the "answers H1" claim, and move the MTP, ncu and GDN-bench scripts to a follow-up PR that includes their outputs.

2. The throughput table is not labelled as profiler-attached.

  • Where. tables.md, "Throughput of the profiled configurations".
  • What it is. Both rows per batch come from a server running under nsys launch, with one window each ("sd 0" means n = 1). The profile notes then quote "plain 286" tok/s at B = 1 as unprofiled, but it is the nsys-attached window.
  • Fix. Label the table, in summarize.py, as profiler-attached, single-window and not a throughput result. Commit the unprofiled --mode none --repeats 3 baseline and use only that for tok/s. Profiler timings (the nsys step times, the attribution microseconds, and ncu durations under --cache-control all) should be described as perturbed and used only for shares and attribution.

3. The plain profiles were not produced by the committed code, and the provenance is incomplete.

  • Timing. windows/plain_nsys_meta.json shows the run started at 18:28:41, before the first commit of this PR (fab07d7, 18:39).
  • Command differs. The recorded nsys command has no --cuda-flush-interval, but the committed run_profiles.py always adds --cuda-flush-interval=250. So run_all.sh plain does not reproduce the recorded command.
  • Missing fields. The meta file has no repo_sha and no nsys version, and the attribution JSONs record neither.
  • Fix. Rerun run_all.sh plain with the committed code (about 10 minutes under the lock), which gives meta with the repo SHA and nsys version. Otherwise, state in the README the exact command and the working-tree state that produced the traces, and record the nsys version.

4. The PR body is stale.

  • mypy passes on the merge with main, so the SKIP=mypy note should go.
  • The WIP paragraph needs updating once item 1 is resolved.
  • "At M <= 32 it runs at 94-95 % of the read peak" should be 93-95 %: M = 16 is 93 % in tables.md.

Profile-specific checklist

  • Profiler timings labelled as perturbed and kept out of throughput results: partly. The attribution is labelled "(us, nsys)", but the throughput table is not (item 2).
  • Head fraction and bytes per step trace to committed CSVs and reproducible commands: the committed files do trace (both reproduce exactly). The traces themselves were not produced by the committed command (item 3).
  • HBM bandwidth method stated: yes. The hbm_bandwidth.py docstring and the JSON's method field describe it: CUDA-graph replays timed with CUDA events; the D2D copy counts read plus write; the read kernel is Triton grid-stride. Say in the README that 3.83 TB/s is the best of a 9-point launch-configuration sweep (medians of 30 repeats each).
  • Amdahl ceilings computed from measured fractions: none in the PR yet. The notes' 1/(1 - 0.25) = 1.33x host-gap ceiling mixes an unprofiled cycle time (7.0 ms) with profiled GPU work (5.3 ms). When it lands, state both sources, or compute the fraction from a single unprofiled measurement.
  • LM-head kernel and argmax call sites named precisely: yes for plain decode: the nvjet_sm90_tst_* kernels per M, plus the calls above. Put the file:line references in the README. For MTP, name which draft top-1 runs in the captured draft graph: eagle_worker_v2.py has both draft_topk1_argmax_only (l. 1157) and the draft_topk1_postprocess path the microbenchmark uses.

Suggestions (non-blocking)

  1. The bytes_model.py docstring's command reads windows from ~/vp-data. Point it at evidence/profiles/windows/*.jsonl so the committed JSON regenerates from committed inputs.
  2. --prompt-tokens 31.5 is hard-coded. Derive it from the tokenizer as drive_decode.py builds the prompts, or state how it was obtained.
  3. run_ncu.sh needs sudo because RmProfilingAdminOnly=1. Say so in the README as a machine-specific requirement.
  4. Collect at least two nsys windows per batch, so the step-time p10-p90 reflects more than one 2 s window.

Removing layer 0's in_proj GEMMs entirely would save 0.33-0.60% of a plain
decode step at batch 1-128 (ceiling at most 1.006x), below P5's pre-registered
1% threshold.
…ghput

run_profiles.py gains dflash16/dflash8 arms (flags from the drafter
workstream; one server covers B = 1-64) and an optional py-spy sample of the
scheduler during collected windows; attribute.py and host_gaps.py attribute
DFlash cycles verify-to-verify with the draft graph's non-head work as
draft_model. summarize.py now says that only unprofiled repeated windows are
throughput results; the nsys windows measure perturbation.
SamMausberg added a commit that referenced this pull request Sep 30, 2026
…s review

Per the per-workstream patch convention, the series moves to
engine/sglang/patches/moonshot/0001-0007 and engine/sglang/README.md keeps the
geometry section with a moonshot section below it. Figures from unmerged PRs
are marked as under review (profile PR #13), the drafter's tau cites
evidence/drafter on main, strict replay is described as designed to be
bit-identical with validation pending, the FP8 W8A8 sentence matches the CSV,
and reproduction uses repository paths (bench is on main).
Review (Codex, PR #13): the shell scripts now take gpu_lock.sh and
sglang_env.sh from the checkout they live in, and run_profiles.py reads the
SGLang SHA from the imported package. ncu_summary.py accepts both the
one-row-per-metric and the one-column-per-metric layouts of
'ncu --csv --page raw'; no ncu numbers had been committed yet.
@SamMausberg

Copy link
Copy Markdown
Owner Author

Handled the two Codex P1 comments in 87f5e65:

  1. Hard-coded $HOME/verified-progress/scripts/... paths: run_all.sh, run_microbench.sh, run_ncu.sh and analyze_all.sh now use $REPO/scripts/gpu_lock.sh and $REPO/scripts/sglang_env.sh from the checkout they live in (the branch is merged with main, so these are the current FIFO lock and env scripts). run_profiles.py also stopped assuming ~/sglang for the SGLang SHA; it reads the directory of the sglang package the interpreter imports, so an engine worktree is recorded correctly.
  2. ncu_summary.py parsing of ncu --csv --page raw: it now accepts both layouts, one row per metric (Metric Name, Metric Unit, Metric Value, first result ID only) and one column per metric with a units row. No Nsight Compute numbers had been committed, so nothing needed re-deriving; the first real reports (queued run_ncu.sh) will be checked against the layout actually emitted before any number is committed.

The window summary now carries the mean number of busy cores machine-wide,
for the server's process tree plus this client, and their difference
(load from other jobs), from /proc/stat and psutil CPU times. Host-bound
phases (the speculative cycle's gaps) are only reported from windows with
low foreign load.
The MTP rerun (CUPTI flush interval, all eager kernel records present)
attributes each speculative cycle; host_gaps_mtp_bs*.json names the host
functions running while the GPU idles (NVTX on the EAGLE worker's host
functions) and pyspy_*.json summarizes py-spy samples of the scheduler.
Foreign CPU load stayed below 1.4 cores in every window.
py-spy hung for 18 minutes on the traced MTP scheduler at batch 8 during the
host-gap run; the driver now waits at most the window plus 20 s and then
kills the sampler (that window's py-spy output was lost).
windows/{plain,mtp}_none.jsonl hold three no-profiler windows per
configuration (the only throughput numbers here); tables.md separates them
from nsys-attached and host-trace windows. check_labels.py confirms, for every
complete plain and MTP target replay, the per-step GEMM counts and kernel
configurations the model's structure implies. The README documents the plain
traces' producing state and the MTP perturbation measured against the
no-profiler cycle.
@SamMausberg

Copy link
Copy Markdown
Owner Author

Verdict: APPROVE

This re-review covers head 0aec3e0 (86 behind main; a trial merge into main is clean and pre-commit passes on the merged code). All four blocking items from my review at 7e8a257, and both Codex P1s, are resolved.

  • 1. README and evidence.
    • evidence/profiles/README.md now names the script behind every file, and analyze_all.sh and run_all.sh hold the exact commands.
    • The MTP attribution (mtp_bs{1,8,32}), the unprofiled --mode none windows, the host-gap and py-spy diagnostics, and label_structure_check.json are committed.
    • The scripts without outputs yet are listed as pending in both the README and the PR body: ncu, the GDN kernel bench, clock-logged microbenchmarks, the eager NVTX label validation, /start_profile, the graph-level calibration and DFlash.
  • 2. Throughput labelling. tables.md says only the "no profiler" rows are throughput results and that the nsys rows measure perturbation. Every tok/s figure in the README and PR body comes from the unprofiled repeats.
  • 3. Provenance of the plain traces. The README states the producing state: fab07d7 plus the uncommitted driver, differing only in --cuda-flush-interval and the missing repo_sha/nsys_version. It records the nsys version (2025.3.2), and the rerun with committed code is listed as pending.
  • 4. PR body. It is updated, including 93-95% and the mypy note.
  • Codex P1s. run_all.sh, run_microbench.sh and run_ncu.sh resolve scripts/ from $REPO. ncu_summary.metric_rows handles both raw-CSV layouts: one row per metric, and one column per metric with a units row.

What I reran (CPU, no GPU):

  • MTP attribution. attribute.py --kind spec on the raw mtp_bs1 and mtp_bs32 traces reproduces attribution/mtp_bs{1,32}.json and _categories.csv byte for byte.
  • GEMM labels. check_labels.py on all seven traces (plain from ~/vp-data/profile/plain_nsys_v0, MTP from mtp_nsys) reproduces label_structure_check.json byte for byte. Every replay passes, with the expected per-step counts and one kernel configuration per label (two for in_proj).
  • Tables. summarize.py regenerates tables.md, step_share.csv and breakdown.csv byte for byte.
  • Throughput. The means of the three unprofiled windows are plain 288.8 / 2047.7 / 6298.2 / 14387.6 tok/s and MTP 384.1 / 2573.3 / 7087.3 tok/s, with cycles of 6.70 / 8.27 / 12.49 ms, as quoted. Foreign load stayed at or below 1.37 cores in every window.
  • Arithmetic. The derived unprofiled idle (6.70 - 5.27 = 1.43 ms, 21%; 20%; 15%) and the ceilings (1/(1 - 0.373) = 1.59x; about 1.27x) are consistent, and the idle row is labelled as combining two measurements.

Suggestions (not blocking):

  • Trace location. analyze_all.sh looks for plain traces in $VP_DATA/plain_nsys, but the traces behind the committed plain evidence are now in plain_nsys_v0. Until the rerun replaces them, the documented analysis would silently skip the plain label check and p5_layer0_in_proj.json. A README note would help.
  • Bandwidth figure. Say in the README that 3.83 TB/s is the best point of the read sweep (read_sweep_median_tb_per_s).

@SamMausberg
SamMausberg merged commit 0f0c025 into main Sep 30, 2026
SamMausberg added a commit that referenced this pull request Sep 30, 2026
…n byte arithmetic

With PR #13 merged, bandwidth, kernel attribution and the MTP idle share cite
evidence/profiles. README section 2b derives the MTP cycle at c = 128 from bench's
tuning throughput and shows that snapshot-free verification beyond ReplaySSM-spec
is worth ~4% of the cycle, while the cycle is 2-2.7x the sum of its parts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant