Skip to content

[1/N] elastic-ep: Add runtime EP scale-up - #30164

Merged
ch-wan merged 18 commits into
sgl-project:mainfrom
zackyoray:elastic-ep-scale
Jul 16, 2026
Merged

ch-wan merged 18 commits into
sgl-project:mainfrom
zackyoray:elastic-ep-scale

Conversation

@zackyoray

@zackyoray zackyoray commented Jul 5, 2026 •

Copy link
Copy Markdown
Contributor

[1/N] elastic-ep: Add runtime EP scale-up

Summary

This is the first PR in the runtime Expert Parallel (EP) Scaling series. It
allows a running MoE deployment to grow through one or more append-only scale
operations up to a reserved --max-ep-size, without restarting the primary
server. Each operation may add any positive number of ranks that fits within
the reserved capacity.

Motivation

SGLang currently fixes the EP topology when the server starts. Adding GPUs to
an MoE deployment therefore requires stopping the server, rebuilding its
distributed topology, and reloading the model.

SGLang already has most of the lower-level machinery needed to admit ranks at
runtime through Elastic EP rank recovery. Runtime scale-up can reuse that
model when the deployment reserves its maximum size up front: inactive rank
slots and backend buffers are allocated at launch, a joiner loads the same
model independently, and Mooncake admits the new rank range when the operator
requests scale-up.

This PR implements that scale-up slice of the [RFC] Elastic EP Scale](#22788).

What This PR Does

Scale API and state

  • Adds POST /scale_elastic_ep with an absolute new_ep_size target.
  • Adds GET /is_scaling_elastic_ep for the effective size, pending target,
    coarse request state, and last error. Internal model-runner phases are not
    reported through this endpoint.
  • Adds --elastic-ep-join-mode to distinguish new-rank admission (scale)
    from existing rank recovery (recover); the latter replaces the deprecated
    --elastic-ep-rejoin flag.
  • Tracks the committed and pending sizes in ElasticEPStateManager and
    propagates scale requests through the tokenizer-to-scheduler control path.
  • Supports repeated monotonic scale-up to any larger integer target at or below
    max_ep_size.

Reserved capacity and size semantics

--max-ep-size reserves the scale-up ceiling; it does not start the reserved
rank processes or add them to the serving topology. For a deployment launched
at EP=4 with capacity reserved through EP=8, launch-time state is:

launch EP size:     4
max_ep_size:        8
effective_ep_size:  4
pending_ep_size:    None
active_ranks:       [1, 1, 1, 1, 0, 0, 0, 0]
                     < active >  < reserved >

The active_ranks tensor and NIXL rank/buffer capacity are sized for
max_ep_size at launch. The inactive tail has no running processes and no
established peer connections. Other state that depends on the live topology,
including DPC worker slots and expanded EPLB metadata, is activated or rebuilt
during the scale commit.

effective_ep_size is the committed size used for serving. For example, after
a 4-to-5 request is accepted, it remains 4 while pending_ep_size is 5.
Only after the joiner is admitted and every rank has installed the expanded
runtime state does the commit produce:

effective_ep_size:  5
pending_ep_size:    None
active_ranks:       [1, 1, 1, 1, 1, 0, 0, 0]

A subsequent operation can grow the same deployment from EP=5 to EP=6.

Rank admission and runtime commit

Scale-up is split into an asynchronous request stage and a synchronized runtime
commit:

  1. POST /scale_elastic_ep sends the target to the current schedulers, records
    pending_ep_size, and returns after the request is accepted. The HTTP
    response does not wait for rank admission or runtime reconfiguration.
  2. At the end of a forward pass, the model runner checks whether the requested
    joiners are ready. Once they have joined, it commits the expanded topology
    and reports completion to the control plane.

Clients use GET /is_scaling_elastic_ep to observe completion.

Data and control planes

  • Sizes NIXL elastic buffers for max_ep_size and lazily connects the newly
    admitted peers on MoE dispatch.
  • Expands expert-location metadata for the new physical rank slots and rebuilds
    the expert-distribution recorder.
  • Routes expanded DP-attention metadata and hidden-state collectives over the
    live Mooncake WORLD rather than launch-time groups that cannot grow.
  • Updates the logits-gather gate and DP metadata width for the committed size.
  • Activates the joiner scheduler slots in the primary
    DataParallelController and grows tokenizer/control fan-out to include them.

Architecture and Flow

flowchart TD
    P1["Primary ranks 0..S-1<br/>Start at EP=S; reserve max EP=M"]
    P2["Serve requests at EP=S"]
    P3["Accept target EP=T<br/>continue routing at EP=S"]
    P4["At the next forward boundary,<br/>admit ranks and install EP=T state"]
    P6["Activate joiner schedulers<br/>and grow control fan-out"]
    P7["First EP=T forward connects NIXL<br/>serve requests at EP=T"]

    J1["Joiner ranks S..T-1<br/>Launch later and load model"]
    J2["Wait in scale join mode"]
    J3["Join Mooncake WORLD and<br/>install EP=T runtime state"]
    J5["Become active<br/>scheduler ranks"]
    J6["First EP=T forward connects NIXL<br/>execute as active EP ranks"]

    M["Mooncake WORLD expanded<br/>runtime state coordinated"]
    B["Expanded-WORLD readiness barrier<br/>all primary and joiner ranks"]

    P1 --> P2 --> P3 --> P4 --> M
    P1 -. "Operator launches joiners after the primary starts" .-> J1
    J1 --> J2 --> J3 --> M
    M --> B
    B --> P6 --> P7
    B --> J5 --> J6
Loading

How to Use

The following example grows a four-rank wide-EP deployment to eight effective
EP/DP ranks by adding a second four-rank launch cohort. Each cohort remains
local TP4; scale-up does not create a TP8 group. The primary and joiner must use
the same model, distributed initialization address, initial EP size, maximum
EP size, and backend configuration.

Start the primary:

sglang serve \
  --model-path <MODEL_PATH> \
  --trust-remote-code \
  --tp 4 --dp 4 \
  --enable-dp-attention \
  --enable-dp-lm-head \
  --elastic-ep-backend mooncake \
  --mooncake-ib-device <IB_DEVICE_LIST> \
  --moe-a2a-backend nixl \
  --deepep-mode low_latency \
  --enable-eplb \
  --ep-num-redundant-experts 24 \
  --elastic-ep-initial-size 4 \
  --load-balance-method round_robin \
  --max-ep-size 8 \
  --cuda-graph-backend-decode disabled \
  --cuda-graph-backend-prefill disabled \
  --nnodes 1 --node-rank 0 \
  --dist-init-addr <PRIMARY_IP>:<DIST_PORT> \
  --host <PRIMARY_IP> --port 21000

Start the joining group. It may be launched before the scale request and wait
for the primary to admit it:

sglang serve \
  --model-path <MODEL_PATH> \
  --trust-remote-code \
  --tp 4 --dp 4 \
  --enable-dp-attention \
  --enable-dp-lm-head \
  --elastic-ep-backend mooncake \
  --mooncake-ib-device <IB_DEVICE_LIST> \
  --moe-a2a-backend nixl \
  --deepep-mode low_latency \
  --enable-eplb \
  --ep-num-redundant-experts 24 \
  --elastic-ep-initial-size 4 \
  --load-balance-method round_robin \
  --max-ep-size 8 \
  --cuda-graph-backend-decode disabled \
  --cuda-graph-backend-prefill disabled \
  --elastic-ep-join-mode scale \
  --elastic-ep-join-rank-offset 4 \
  --nnodes 2 --node-rank 1 \
  --dist-init-addr <PRIMARY_IP>:<DIST_PORT> \
  --host <JOINER_IP> --port 21001

Request scale-up through the primary:

curl -X POST http://<PRIMARY_IP>:21000/scale_elastic_ep \
  -H 'Content-Type: application/json' \
  -d '{"new_ep_size": 8}'

Poll the asynchronous operation:

curl http://<PRIMARY_IP>:21000/is_scaling_elastic_ep

A completed operation reports effective_ep_size: 8,
pending_ep_size: null, and is_scaling_elastic_ep: false.

For a direct 4-to-6 operation, launch a two-rank joiner with --tp 2 --dp 2,
--elastic-ep-join-rank-offset 4, and request new_ep_size: 6. For repeated
4-to-5-to-6 scale-up, launch a one-rank joiner at offset 4 and commit EP=5,
then launch a new one-rank joiner at offset 5 and commit EP=6.

Follow-up Work

The following capabilities are planned as follow-up PRs in this Elastic EP
series:

  • CUDA graph recapture: Recapture decode graphs after the effective EP
    topology changes.
  • Scale-down: Remove active tail ranks and commit a smaller effective EP
    topology without restarting the deployment.
  • Full post-scale rank recovery: Replace an original or joined rank after
    one or more successful scale-up operations while preserving the committed EP
    size.
  • Periodic EPLB reshuffle: This PR expands expert-location metadata for new
    ranks but disables periodic rebalance after scale-up.
    The follow-up PR [2/N] elastic-ep: Enable EPLB after scale-up #30553
    coordinates EPLB collectives with the expanded WORLD and re-enables
    runtime expert reshuffling.
  • Downtime optimization: Move joiner preparation and data-plane connection
    work out of the request-visible commit and first post-scale request paths.

Known Limitations

  • DP-attention topology: The supported deployment uses
    --enable-dp-attention, --enable-dp-lm-head,
    --pp-size 1, --attention-context-parallel-size 1,
    --moe-data-parallel-size 1, and --tp == --dp == --ep within each launch
    cohort.
  • Tokenizer workers: Runtime scale-up supports only
    --tokenizer-worker-num 1.
  • Backend scope: Runtime scale-up supports Mooncake as the Elastic EP
    backend and NIXL as the MoE all-to-all backend.

Test Plan

Manual six-GPU E2E tests used for this PR:

python -m pytest -v -s test/manual/ep/test_elastic_scale.py -k TestElasticScaleUp4To6
python -m pytest -v -s test/manual/ep/test_elastic_scale.py -k TestElasticScaleUp4To8
python -m pytest -v -s test/manual/ep/test_elastic_scale.py -k TestElasticScaleUp4To5To6 # Multi-scale

Together the tests cover pre-launched joiners waiting for admission, multi-rank and repeated scale-up, asynchronous
completion polling, generation and input logprobs after each scale, direct
routing to every newly activated rank, and post-scale GSM8K correctness.

Scenario Type Result GSM8K
Direct 4-to-6 scale-up Arbitrary-size addition Passed 64.06%
Direct 4-to-8 scale-up Scale to reserved maximum Passed 63.67%
Repeated 4-to-5-to-6 scale-up Multiple scale operations Passed 62.50%

Cross-References


CI States

Latest PR Test (Base): ❌ Run #29324601378
Latest PR Test (Extra): ❌ Run #29324601422

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@zackyoray zackyoray changed the title Elastic ep scale [1/N] elastic-ep: Add runtime EP scale-up Jul 5, 2026
@zackyoray

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request implements dynamic elastic EP (Expert Parallelism) scale-up, enabling new ranks to join a deployment at runtime. It introduces state management, HTTP endpoints, and updates the distributed state, data parallel controller, and token dispatchers to support the expanded world size. The review highlighted several critical bugs: a potential AttributeError in the scheduler when accessing the model runner, a missing enable_elastic_ep attribute on ModelRunner that disables uneven token count handling, and a bug preventing recovered rank 0 from starting the master TCPStore server. Additionally, the reviewer noted that NIXL mask updates do not use the effective EP size, _LOCAL_ATTN_DP_SIZE is not updated post-scale, torch.arange lacks an explicit dtype, and server arguments overly restrict node ranks to exactly 1.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread python/sglang/srt/managers/scheduler.py Resolved
Comment thread python/sglang/srt/server_args.py Resolved
@ShangmingCai

Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@ishandhanani ishandhanani left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

First AI review pass

Comment thread python/sglang/srt/managers/scheduler.py Resolved
ch-wan and others added 4 commits July 13, 2026 16:51
# Conflicts:
#	python/sglang/srt/elastic_ep/elastic_ep.py
#	python/sglang/srt/managers/tp_worker.py
#	python/sglang/srt/model_executor/model_runner.py
@ch-wan
ch-wan merged commit 77d23a7 into sgl-project:main Jul 16, 2026
145 of 168 checks passed
KMSorSMS added a commit to KMSorSMS/sglang that referenced this pull request Jul 21, 2026
Re-express the elastic-EP fault-tolerance work on top of current main
(2499 commits, incl. runtime EP scale-up sgl-project#30164 and the sock_send/sock_recv
IPC refactor sgl-project#29012). Key resolution decisions:

- Fault detection/recovery stays at the scheduler result boundary (this
  PR's architecture); the forward-path rebalance re-run and per-forward
  recovery polling are removed. The scale-up driver (maybe_join_ep_ranks)
  is kept intact; its recovery arm reduces to the has_scaled ->
  fail_recovery latch, mirrored by a guard in the scheduler mixin
  (recovery after append-only scale-up stays unsupported).
- ActiveRanksOutput moves to the direct scheduler->controller channel
  (sock_send/sock_recv msgpack end to end; port_base+6, since +5 is now
  the load collector). ElasticScaleUpdateReq keeps its tokenizer relay
  because the TokenizerManager consumes it for fan-out updates.
- New ElasticEPState.realign_snapshots_to_active() re-baselines the
  staging ring after legitimate active_ranks rewrites (scale admission,
  recovery reset) so the AND-sticky committed mirror cannot re-kill newly
  admitted ranks or trigger a spurious retract; pending slots are
  preserved so each submitted forward still commits exactly once. pg
  masks are applied prefix-based against the max-ep-size capacity.
- Fixes folded in while rebasing: draft-worker snapshot double-submit
  guard (battle-tested downstream); disagg-prefill submit gate (those
  loops have no result boundary); parked chunked-prefill request release
  on retract; EPLB active mask sliced to num_gpus; controller send
  timeout no longer mutates the authoritative worker status (liveness is
  decided solely by scheduler-published active-rank updates); poller-based
  controller event loop with bounded fair drain.

Co-Authored-By: Claude Fable 5 <[email protected]>
Co-Authored-By: codex (gpt-5.6-sol)
Chronostasys pushed a commit to MindLab-Research/sglang that referenced this pull request Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bypass-fastfail run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants