Skip to content

Optimize conflicts between CUDA graph and vocab mask tensors - #1392

Merged
merrymercy merged 11 commits into
mainfrom
vocab-mask-cuda-graph
Sep 14, 2024
Merged

merrymercy merged 11 commits into
mainfrom
vocab-mask-cuda-graph

Conversation

@hnyls2002

@hnyls2002 hnyls2002 commented Sep 11, 2024 •

Copy link
Copy Markdown
Collaborator

Motivation

This PR moves the sampler out of the CUDA graph and removes the synchronization in the cuda graph replay.

  • Vocab mask, penalties calculation are done between model forward and sampling
  • Make sampling a part of model_runner.

Modifications

Checklist

  • Format your code according to the Contributor Guide.
  • Add unit tests as outlined in the Contributor Guide.
  • Update documentation as needed, including docstrings or example tutorials.

@hnyls2002
hnyls2002 force-pushed the vocab-mask-cuda-graph branch from 0d2f2c5 to 1ef88ff Compare September 11, 2024 21:12
@hnyls2002
hnyls2002 marked this pull request as draft September 12, 2024 19:58
@hnyls2002
hnyls2002 force-pushed the vocab-mask-cuda-graph branch from 09c900d to 92b9594 Compare September 13, 2024 00:15
@hnyls2002
hnyls2002 force-pushed the vocab-mask-cuda-graph branch 3 times, most recently from 6e20c93 to 4f0d0cb Compare September 13, 2024 22:32
@hnyls2002
hnyls2002 force-pushed the vocab-mask-cuda-graph branch from 4f0d0cb to e893a95 Compare September 13, 2024 22:36
@hnyls2002
hnyls2002 force-pushed the vocab-mask-cuda-graph branch 2 times, most recently from f46a050 to 3273d3f Compare September 14, 2024 00:22
@hnyls2002
hnyls2002 marked this pull request as ready for review September 14, 2024 00:24
@hnyls2002
hnyls2002 force-pushed the vocab-mask-cuda-graph branch from 01486b3 to 184e444 Compare September 14, 2024 03:06
@merrymercy
merrymercy merged commit 70b6802 into main Sep 14, 2024
@merrymercy
merrymercy deleted the vocab-mask-cuda-graph branch September 14, 2024 03:27
timethink pushed a commit to timethink/sglang that referenced this pull request Mar 9, 2025
efschu pushed a commit to efschu/htsglang that referenced this pull request Sep 14, 2026
Precision from the coordinator, not a defect: choose() already prices
whatever the box holds RIGHT NOW into every arm (cg_current_bytes -> the
origin -> the predicted peak) -- sgl-project#1390 made that seam testable, it did not
make the dry run box-independent, and should not: it is supposed to measure
the live box. What was missing is honesty about which reading a verdict was
measured against. Today a FUNDABLE measured on a quiet box and a FUNDABLE
measured under ~2.5 GiB of parallel load (the exact incident named in the
dry-run-misst-die-live-box memory note, 2026-09-13: the same argv worked to
ARM 95.18 on a quiet box and refused W97 97.67 under a parallel pytest
suite) print identically. The asymmetry: foreign load tightens the bound
(a false refusal, safe and attributable), but the dangerous case is the
reverse -- measured quiet, funded, then booted later once the box has
picked up load the dry run never saw.

Adds to host_ledger.py:
- QUIET_BASELINE_CG_CURRENT_GIB (13.10, this box, measured 2026-09-14 --
  the SAME reading test_weg2_hicache_disabled_1386.py's quiet-box fixture
  already pins, reused rather than a second number for the same fact).
- FOREIGN_LOAD_PROVISIONAL_THRESHOLD_GIB (2.5, named from the
  dry-run-misst-die-live-box incident -- a measured magnitude, not a
  guessed safety factor).
- foreign_load_now_gib(cg_current_bytes): the box's current reading minus
  the quiet baseline, floored at 0, None when unmeasured.
- choose() gains box_state_at/cg_anon_bytes/cg_shmem_bytes -- PRINT-ONLY,
  following the exact precedent xchg_bounce_prov already set ("it never
  reaches price, because nothing here changes a number"). A new
  WEG2-DRY-RUN-BOX-STATE line (always printed, funded or refused, on the
  sgl-project#1386/sgl-project#1378-established "on or off" idiom) carries the raw memavail/anon/
  shmem reading plus the computed foreign_load_now, and appends a
  PROVISIONAL clause above the threshold -- worded for BOTH verdict
  directions (a refusal may be the box, not the form; a FUNDABLE should be
  re-verified before the actual boot).

THE DECISION, stated explicitly per the order ("dein Urteil, welches von
beiden ehrlicher ist"): PROVISIONAL text, never a second refusal gate.
Crossing the threshold changes no arm's fundability. Reasons: (1) a hard
refusal keyed on instantaneous box noise would misattribute the box's
transient state to the form -- exactly what the existing memory note
already warns against; (2) any genuine danger from current foreign load is
already caught by the EXISTING peak/cushion/headroom checks, since foreign
load raises cg_current -> raises the origin -> raises the predicted peak;
a second gate on the same signal would be the sgl-project#1358 double-bookkeeping
shape one layer up; (3) PROVISIONAL preserves every fact for the reader
without new binding semantics, the same "indicator, not Freibrief" shape
sgl-project#1378 Stage 2 already established for cushion_headroom_gib.

launcher.py: choose_host_ledger captures the timestamp AT the cgroup read
it is stamping (not reconstructed later), and forwards
box_state_at/cg_anon_bytes/cg_shmem_bytes from the SAME `cg` reading
(cg_ceiling_bytes/reclaimable_bytes/cg_oom_kill already price from) --
never the earlier `_cg0` PEAK-BASELINE snapshot, which would make the line
lie about which instant the rest of the ARM line was priced against.

Tests (test_weg2_dry_run_box_state_1392.py, 16 cases, hermetic): the three
numbers always print (measured, unreadable, and on a total refusal);
foreign_load_now_gib's arithmetic (baseline, floor, delta, unmeasured);
the decision itself -- a fundable arm's identity and headroom are BYTE-
IDENTICAL under heavy simulated foreign load (structural proof the
threshold never touches `ok`), PROVISIONAL fires only strictly above the
threshold and never at/below it, and a refusal under heavy load also names
the box; the launcher's timestamp-before-read ordering and its forwarding
from the correct `cg` reading.

Manually verified the required danger-direction mutant (foreign_load_now_gib
call replaced with a hardcoded None): 3 of 16 tests go red (the two
PROVISIONAL-text tests and the structural real-call guard), confirming the
line's honesty text is load-bearing rather than decorative. Restored, diff
empty. Broader regression slice (14 files) green but for the one already-
known pre-existing "retry() exceed maximum number of retries" failure.
efschu pushed a commit to efschu/htsglang that referenced this pull request Sep 14, 2026
Precision from the coordinator, not a defect: choose() already prices
whatever the box holds RIGHT NOW into every arm (cg_current_bytes -> the
origin -> the predicted peak) -- sgl-project#1390 made that seam testable, it did not
make the dry run box-independent, and should not: it is supposed to measure
the live box. What was missing is honesty about which reading a verdict was
measured against. Today a FUNDABLE measured on a quiet box and a FUNDABLE
measured under ~2.5 GiB of parallel load (the exact incident named in the
dry-run-misst-die-live-box memory note, 2026-09-13: the same argv worked to
ARM 95.18 on a quiet box and refused W97 97.67 under a parallel pytest
suite) print identically. The asymmetry: foreign load tightens the bound
(a false refusal, safe and attributable), but the dangerous case is the
reverse -- measured quiet, funded, then booted later once the box has
picked up load the dry run never saw.

Adds to host_ledger.py:
- QUIET_BASELINE_CG_CURRENT_GIB (13.10, this box, measured 2026-09-14 --
  the SAME reading test_weg2_hicache_disabled_1386.py's quiet-box fixture
  already pins, reused rather than a second number for the same fact).
- FOREIGN_LOAD_PROVISIONAL_THRESHOLD_GIB (2.5, named from the
  dry-run-misst-die-live-box incident -- a measured magnitude, not a
  guessed safety factor).
- foreign_load_now_gib(cg_current_bytes): the box's current reading minus
  the quiet baseline, floored at 0, None when unmeasured.
- choose() gains box_state_at/cg_anon_bytes/cg_shmem_bytes -- PRINT-ONLY,
  following the exact precedent xchg_bounce_prov already set ("it never
  reaches price, because nothing here changes a number"). A new
  WEG2-DRY-RUN-BOX-STATE line (always printed, funded or refused, on the
  sgl-project#1386/sgl-project#1378-established "on or off" idiom) carries the raw memavail/anon/
  shmem reading plus the computed foreign_load_now, and appends a
  PROVISIONAL clause above the threshold -- worded for BOTH verdict
  directions (a refusal may be the box, not the form; a FUNDABLE should be
  re-verified before the actual boot).

THE DECISION, stated explicitly per the order ("dein Urteil, welches von
beiden ehrlicher ist"): PROVISIONAL text, never a second refusal gate.
Crossing the threshold changes no arm's fundability. Reasons: (1) a hard
refusal keyed on instantaneous box noise would misattribute the box's
transient state to the form -- exactly what the existing memory note
already warns against; (2) any genuine danger from current foreign load is
already caught by the EXISTING peak/cushion/headroom checks, since foreign
load raises cg_current -> raises the origin -> raises the predicted peak;
a second gate on the same signal would be the sgl-project#1358 double-bookkeeping
shape one layer up; (3) PROVISIONAL preserves every fact for the reader
without new binding semantics, the same "indicator, not Freibrief" shape
sgl-project#1378 Stage 2 already established for cushion_headroom_gib.

launcher.py: choose_host_ledger captures the timestamp AT the cgroup read
it is stamping (not reconstructed later), and forwards
box_state_at/cg_anon_bytes/cg_shmem_bytes from the SAME `cg` reading
(cg_ceiling_bytes/reclaimable_bytes/cg_oom_kill already price from) --
never the earlier `_cg0` PEAK-BASELINE snapshot, which would make the line
lie about which instant the rest of the ARM line was priced against.

Tests (test_weg2_dry_run_box_state_1392.py, 16 cases, hermetic): the three
numbers always print (measured, unreadable, and on a total refusal);
foreign_load_now_gib's arithmetic (baseline, floor, delta, unmeasured);
the decision itself -- a fundable arm's identity and headroom are BYTE-
IDENTICAL under heavy simulated foreign load (structural proof the
threshold never touches `ok`), PROVISIONAL fires only strictly above the
threshold and never at/below it, and a refusal under heavy load also names
the box; the launcher's timestamp-before-read ordering and its forwarding
from the correct `cg` reading.

Manually verified the required danger-direction mutant (foreign_load_now_gib
call replaced with a hardcoded None): 3 of 16 tests go red (the two
PROVISIONAL-text tests and the structural real-call guard), confirming the
line's honesty text is load-bearing rather than decorative. Restored, diff
empty. Broader regression slice (14 files) green but for the one already-
known pre-existing "retry() exceed maximum number of retries" failure.
efschu pushed a commit to efschu/htsglang that referenced this pull request Sep 14, 2026
…her.main(), not just choose()

Prior-art gate first, per the order: sgl-project#1392 itself was already built and on
the train (commit 5a1c3f0 / rebased to de7f875's ancestor chain,
"the dry run says WHICH box state it funded against") -- the ARM line
already carries memavail/anon/shmem plus provenance, the PROVISIONAL text
above FOREIGN_LOAD_PROVISIONAL_THRESHOLD_GIB already exists with the
directional-asymmetry reasoning the order restates almost verbatim, and no
second refusal gate was ever added. This is NOT a rebuild of that feature.

THE ONE GAP the order actually named: "Nutze die Ruhig-Box-Fixture und
_HermeticMainBase aus sgl-project#1390... Prior-Art-Gate zuerst darauf, kein Zweitbau."
The existing test file's `TheLauncherThreadsTheThreeReadingsThrough` class
only inspects `launcher.choose_host_ledger`'s SOURCE TEXT (string/AST
checks) and calls `host_ledger.choose()` directly -- never `launcher.main()`
end to end, and never through sgl-project#1390's own `_HermeticMainBase` /
`_fake_quiet_host` fixture, which exist for exactly this seam.

Closes it by importing (not reimplementing) `_HermeticMainBase`,
`_fake_quiet_host`'s pinned readings, `_main_argv` and `_NEEDS_CHECKPOINT`
from test_weg2_hicache_disabled_1386.py, the same cross-test-file reuse
convention test_weg2_band_credit_1397.py already uses for its own sibling
fixture:

1. TheRealDryRunLineOnAQuietBox: `launcher.main(['--dry-run', ...])`, the
   REAL wiring, on the pinned quiet-box reading -- the printed line says
   "not stale-suspect", never PROVISIONAL, and carries memavail=/anon=/
   shmem=.
2. A new `_fake_loaded_host` (the quiet fixture's numbers, `memory.current`
   pushed exactly QUIET_BASELINE + THRESHOLD + 1 GiB) and a matching
   `_HermeticMainBaseLoaded` harness (own setUp/tearDown -- not a subclass
   of `_HermeticMainBase` overriding setUp, which would leave two live
   mock.patch layers active): TheRealDryRunLineOnALoadedBox proves the SAME
   real `main()` run, ONLY the host reading changed, now prints PROVISIONAL
   and a real foreign_load_now figure.
3. THE REQUIRED DANGER-DIRECTION MUTANT, against the real file, not a
   planted fixture (mirroring how sgl-project#1395's mutant ran against the real
   reader): `mock.patch.object(hl, "FOREIGN_LOAD_PROVISIONAL_THRESHOLD_GIB",
   float("inf"))` around a live `main()` run on the LOADED host -- the line
   still prints a real, honest `foreign_load_now=` figure (the box IS
   loaded) but PROVISIONAL never fires, because the axis that would have
   said so was neutralised. `test_the_real_printed_line_says_PROVISIONAL`
   (a DIFFERENT, non-mutant-specific test) independently catches the same
   defect when the axis is instead removed at the SOURCE (see below),
   which is the broader, more convincing proof.

MUTANT APPLIED LIVE TO host_ledger.py ITSELF AND REVERTED (diff empty
after), exactly as ordered ("fahr ihn gegen die echte Datei"): the
`_fl_now > FOREIGN_LOAD_PROVISIONAL_THRESHOLD_GIB` condition replaced with
`False` (the foreign-load axis silently removed from the line's effect,
never from its printed number). Re-ran TheRealDryRunLineOnALoadedBox: the
loaded box's `main()` run still completes, the line still claims a verdict,
`foreign_load_now=3.50 GiB` (real, above threshold) still prints, but
"PROVISIONAL" is gone -- `test_the_real_printed_line_says_PROVISIONAL`
(the plain, non-mutant test) goes red on its own, which is the strongest
form of this proof: the feature's OWN direct test catches the mutation
without needing a dedicated mutant assertion. Reverted, diff empty,
19/19 green again.

No second launch-time guard: sgl-project#1360/sgl-project#1361's own re-read at LAUNCH is
untouched, and nothing here duplicates it -- this is dry-run-time honesty
about the reading a verdict was measured against, not a second gate.

Regression: test_weg2_dry_run_box_state_1392.py (19/19),
test_weg2_hicache_disabled_1386.py + test_weg2_dry_run_order_probe_1378.py +
test_weg2_cushion_headroom_gate_1378_stage2.py (48/48, 2 subtests) all
green. pyright: host_ledger.py untouched net (mutant reverted); the new
test file's import-resolution noise is the same reportMissingImports class
already present for every sglang.* import in this desk-venv, not a real
type error.

File boundary: host_ledger.py (touched only transiently for the live
mutant, reverted) + launcher.py (untouched this round) + own test file, as
ordered.
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.

2 participants