-
Notifications
You must be signed in to change notification settings - Fork 4.7k
[Feature] Add UVM-based MoE expert offloading with all-GPU compute #20126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lichang98
wants to merge
2
commits into
sgl-project:main
Choose a base branch
from
lichang98:feat/expert_offload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Copyright 2024 SGLang Team | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # ============================================================================== | ||
| """UVM-based expert weight offloading for MoE layers. | ||
|
|
||
| Expert weights are stored in CUDA Unified Memory (cudaMallocManaged). | ||
| Resident experts are kept in GPU VRAM (PREFER_GPU advice). | ||
| Offloaded experts live in CPU DRAM and are accessible to the GPU via PCIe | ||
| read-through (PREFER_CPU + ACCESSED_BY_GPU advice) — no page fault overhead, | ||
| no quality loss, CUDA graph compatible. | ||
|
|
||
| All computation remains on GPU (no CPU inference). | ||
| Mutually exclusive with KTransformers (--kt-weight-path). | ||
|
|
||
| Usage | ||
| ----- | ||
| Pass ``--expert-offload-num-resident N`` to enable. Additional options: | ||
|
|
||
| --expert-offload-prefetch none Prefetch strategy (none / speculative / frequency). | ||
| --expert-offload-resident-selection first_n How to choose resident experts. | ||
| --expert-offload-resident-ids None Comma-separated IDs for manual selection. | ||
| """ | ||
|
|
||
| from sglang.srt.layers.moe.expert_offload.config import ( | ||
| ExpertOffloadConfig, | ||
| create_expert_offload_config_from_server_args, | ||
| ) | ||
| from sglang.srt.layers.moe.expert_offload.wrapper import ExpertOffloadWrapperMethod | ||
|
|
||
| __all__ = [ | ||
| "ExpertOffloadConfig", | ||
| "ExpertOffloadWrapperMethod", | ||
| "create_expert_offload_config_from_server_args", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # Copyright 2024 SGLang Team | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # ============================================================================== | ||
| """Configuration for UVM-based expert weight offloading.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import TYPE_CHECKING, List, Optional | ||
|
|
||
| if TYPE_CHECKING: | ||
| from sglang.srt.server_args import ServerArgs | ||
|
|
||
|
|
||
| @dataclass | ||
| class ExpertOffloadConfig: | ||
| """Per-layer configuration for UVM expert offloading. | ||
|
|
||
| Expert weights are stored in CUDA Unified Memory (cudaMallocManaged). | ||
| Resident experts are advised PREFER_GPU (stay in VRAM). | ||
| Offloaded experts are advised PREFER_CPU + ACCESSED_BY_GPU (live in CPU | ||
| DRAM; the GPU reads them via PCIe without triggering a page fault). | ||
|
|
||
| No LRU cache, no assembly buffer, no ID remapping — UVM handles all of it. | ||
| """ | ||
|
|
||
| layer_idx: int | ||
| num_local_experts: int | ||
| num_resident_experts: int # experts whose pages are kept on GPU | ||
| prefetch_strategy: str # "none" | "speculative" | "frequency" | ||
| resident_selection: str # "first_n" | "frequency" | "manual" | ||
| resident_expert_ids: Optional[List[int]] # explicit IDs for "manual" mode | ||
| num_layers: Optional[int] # total MoE layers (for prefetch coordination) | ||
| warmup_tokens: int = 4096 # routed tokens to collect before readvise | ||
|
|
||
| @property | ||
| def num_offloaded_experts(self) -> int: | ||
| return self.num_local_experts - self.num_resident_experts | ||
|
|
||
|
|
||
| def create_expert_offload_config_from_server_args( | ||
| server_args: "ServerArgs", | ||
| layer_id: int, | ||
| num_local_experts: int, | ||
| ) -> Optional[ExpertOffloadConfig]: | ||
| """Return an ExpertOffloadConfig if expert offloading is enabled, else None.""" | ||
| if server_args.expert_offload_num_resident < 0: | ||
| return None | ||
|
|
||
| num_resident = min(server_args.expert_offload_num_resident, num_local_experts) | ||
|
|
||
| resident_ids: Optional[List[int]] = None | ||
| if ( | ||
| server_args.expert_offload_resident_selection == "manual" | ||
| and server_args.expert_offload_resident_ids is not None | ||
| ): | ||
| resident_ids = [ | ||
| int(x.strip()) | ||
| for x in server_args.expert_offload_resident_ids.split(",") | ||
| ] | ||
|
|
||
| return ExpertOffloadConfig( | ||
| layer_idx=layer_id, | ||
| num_local_experts=num_local_experts, | ||
| num_resident_experts=num_resident, | ||
| prefetch_strategy=server_args.expert_offload_prefetch, | ||
| resident_selection=server_args.expert_offload_resident_selection, | ||
| resident_expert_ids=resident_ids, | ||
| num_layers=None, | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation for parsing
expert_offload_resident_idsis susceptible to aValueErrorif the string contains consecutive commas (e.g.,'1,2,,3') or a trailing comma, asx.strip()would be an empty string, andint('')is invalid. It's safer to filter out empty strings before converting to integers.