Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/docs/advanced_features/model_loading.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ Set with `--load-format`:
<td style={{padding: "9px 12px", fontWeight: 500, backgroundColor: "rgba(255,255,255,0.02)"}}><code>safetensors</code></td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}>Load weights in the safetensors format.</td>
</tr>
<tr>
<td style={{padding: "9px 12px", fontWeight: 500, backgroundColor: "rgba(255,255,255,0.02)"}}><code>instanttensor</code></td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}>Load Safetensors weights quickly with InstantTensor's distributed loading and pipelined prefetching. Supports CUDA and ROCm; requires PyTorch's <code>cuda</code> device interface. Distributed loading may retain communicator memory for the server lifetime.</td>
</tr>
<tr>
<td style={{padding: "9px 12px", fontWeight: 500, backgroundColor: "rgba(255,255,255,0.02)"}}><code>pt</code></td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}>Load weights in the PyTorch <code>.bin</code> format.</td>
Expand Down Expand Up @@ -151,6 +155,30 @@ python -m sglang.launch_server \
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.02)"}}>Use GPU Direct Storage. Set to <code>false</code> when the host does not provide the NVIDIA GPUDirect Storage kernel driver, such as in a gVisor sandbox.</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}><code>true</code></td>
</tr>
<tr>
<td style={{padding: "9px 12px", fontWeight: 500, backgroundColor: "rgba(255,255,255,0.02)"}}><code>instanttensor</code></td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}><code>buffer_size</code> (int)</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.02)"}}>GPU tensor buffer size in bytes. See the <a href="https://github.com/scitix/InstantTensor">InstantTensor GitHub homepage</a> for the latest optional parameters.</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}><code>null</code></td>
</tr>
<tr>
<td style={{padding: "9px 12px", fontWeight: 500, backgroundColor: "rgba(255,255,255,0.02)"}}><code>instanttensor</code></td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}><code>chunk_size</code> (int)</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.02)"}}>File I/O operation size in bytes.</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}><code>null</code></td>
</tr>
<tr>
<td style={{padding: "9px 12px", fontWeight: 500, backgroundColor: "rgba(255,255,255,0.02)"}}><code>instanttensor</code></td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}><code>io_depth</code> (int)</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.02)"}}>Maximum number of rank-local I/O operations in flight.</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}><code>null</code></td>
</tr>
<tr>
<td style={{padding: "9px 12px", fontWeight: 500, backgroundColor: "rgba(255,255,255,0.02)"}}><code>instanttensor</code></td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}><code>backend</code> (str / list)</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.02)"}}>A backend name or an ordered list of candidates: <code>AIO</code>, <code>AIO_BUFFERED</code>, <code>URING</code>, <code>URING_BUFFERED</code>, <code>CUFILE</code>, <code>MMAP</code>, or the <code>BUFFERED</code> policy.</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}><code>null</code></td>
</tr>
<tr>
<td style={{padding: "9px 12px", fontWeight: 500, backgroundColor: "rgba(255,255,255,0.02)"}}><code>bitsandbytes</code></td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}><code>qlora_adapter_name_or_path</code> (str)</td>
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/advanced_features/server_arguments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ Please consult the documentation below and [server_args.py](https://github.com/s
</tr>
<tr>
<td style={{padding: "9px 12px", fontWeight: 500, backgroundColor: "rgba(255,255,255,0.02)"}}>`--load-format`</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}>The format of the model weights to load. "auto" will try to load the weights in the safetensors format and fall back to the pytorch bin format if safetensors format is not available. "pt" will load the weights in the pytorch bin format. "safetensors" will load the weights in the safetensors format. "npcache" will load the weights in pytorch format and store a numpy cache to speed up the loading. "dummy" will initialize the weights with random values, which is mainly for profiling."gguf" will load the weights in the gguf format. "bitsandbytes" will load the weights using bitsandbytes quantization."layered" loads weights layer by layer so that one can quantize a layer before loading another to make the peak memory envelope smaller. "flash_rl" will load the weights in flash_rl format. "fastsafetensors" and "private" are also supported. "runai_streamer" enables direct model loading from object storage and shared file systems.</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}>The format of the model weights to load. "auto" will try to load the weights in the safetensors format and fall back to the pytorch bin format if safetensors format is not available. "pt" will load the weights in the pytorch bin format. "safetensors" will load the weights in the safetensors format. "instanttensor" will load Safetensors weights using InstantTensor's high-performance distributed loader. "npcache" will load the weights in pytorch format and store a numpy cache to speed up the loading. "dummy" will initialize the weights with random values, which is mainly for profiling."gguf" will load the weights in the gguf format. "bitsandbytes" will load the weights using bitsandbytes quantization."layered" loads weights layer by layer so that one can quantize a layer before loading another to make the peak memory envelope smaller. "flash_rl" will load the weights in flash_rl format. "fastsafetensors" and "private" are also supported. "runai_streamer" enables direct model loading from object storage and shared file systems.</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.02)"}}>`auto`</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}><code>auto</code>, <code>pt</code>, <code>safetensors</code>, <code>npcache</code>, <code>dummy</code>, <code>sharded_state</code>, <code>gguf</code>, <code>bitsandbytes</code>, <code>mistral</code>, <code>layered</code>, <code>flash_rl</code>, <code>remote</code>, <code>remote_instance</code>, <code>fastsafetensors</code>, <code>private</code>, <code>runai_streamer</code></td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}><code>auto</code>, <code>pt</code>, <code>safetensors</code>, <code>instanttensor</code>, <code>npcache</code>, <code>dummy</code>, <code>sharded_state</code>, <code>gguf</code>, <code>bitsandbytes</code>, <code>mistral</code>, <code>layered</code>, <code>flash_rl</code>, <code>remote</code>, <code>remote_instance</code>, <code>fastsafetensors</code>, <code>private</code>, <code>runai_streamer</code></td>
</tr>
<tr>
<td style={{padding: "9px 12px", fontWeight: 500, backgroundColor: "rgba(255,255,255,0.02)"}}>`--model-loader-extra-config`</td>
Expand Down Expand Up @@ -1574,7 +1574,7 @@ Combining `--enable-response-store` with `--disaggregation-mode=prefill` or `dec
<td style={{padding: "9px 12px", fontWeight: 500, backgroundColor: "rgba(255,255,255,0.02)"}}>`--speculative-draft-load-format`</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}>The format of the draft model weights to load. If not specified, will use the same format as `--load-format`. Use 'dummy' to initialize draft model weights with random values for profiling.</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.02)"}}>`None`</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}><code>auto</code>, <code>pt</code>, <code>safetensors</code>, <code>npcache</code>, <code>dummy</code>, <code>sharded_state</code>, <code>gguf</code>, <code>bitsandbytes</code>, <code>mistral</code>, <code>layered</code>, <code>flash_rl</code>, <code>remote</code>, <code>remote_instance</code>, <code>fastsafetensors</code>, <code>private</code>, <code>runai_streamer</code></td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}><code>auto</code>, <code>pt</code>, <code>safetensors</code>, <code>instanttensor</code>, <code>npcache</code>, <code>dummy</code>, <code>sharded_state</code>, <code>gguf</code>, <code>bitsandbytes</code>, <code>mistral</code>, <code>layered</code>, <code>flash_rl</code>, <code>remote</code>, <code>remote_instance</code>, <code>fastsafetensors</code>, <code>private</code>, <code>runai_streamer</code></td>
</tr>
<tr>
<td style={{padding: "9px 12px", fontWeight: 500, backgroundColor: "rgba(255,255,255,0.02)"}}>`--speculative-num-steps`</td>
Expand Down
2 changes: 2 additions & 0 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ default = true

[project.optional-dependencies]
checkpoint-engine = ["checkpoint-engine==0.1.2"]
instanttensor = ["instanttensor>=0.1.9"]
runai = ["runai-model-streamer[s3,gcs,azure]>=0.15.7"]
diffusion = [
"addict==2.4.0",
Expand Down Expand Up @@ -170,6 +171,7 @@ test = [
"diff-cover",
"expecttest",
"granian>=2.6.0",
"instanttensor>=0.1.9",
"jsonlines",
"lm-eval[api]>=0.4.9.2",
"matplotlib",
Expand Down
1 change: 1 addition & 0 deletions python/sglang/srt/arg_groups/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"auto",
"pt",
"safetensors",
"instanttensor",
"npcache",
"dummy",
"sharded_state",
Expand Down
2 changes: 2 additions & 0 deletions python/sglang/srt/arg_groups/fields/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class Model(msgspec.Struct):
"is not available. "
'"pt" will load the weights in the pytorch bin format. '
'"safetensors" will load the weights in the safetensors format. '
'"instanttensor" will load Safetensors weights using '
"InstantTensor's high-performance distributed loader. "
'"npcache" will load the weights in pytorch format and store '
"a numpy cache to speed up the loading. "
'"dummy" will initialize the weights with random values, '
Expand Down
7 changes: 7 additions & 0 deletions python/sglang/srt/arg_groups/validation_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ def check_server_args(server_args: Any):
"MindSpore model impl is only supported on Ascend npu."
)

# ROCm also uses PyTorch's "cuda" device type.
if cfg.load_format == "instanttensor" and cfg.device != "cuda":
raise ValueError(
"InstantTensor requires a CUDA-compatible device (including CUDA and ROCm); "
f"got {cfg.device!r}."
)

# Check metrics labels
if (
not cfg.tokenizer_metrics_custom_labels_header
Expand Down
21 changes: 20 additions & 1 deletion python/sglang/srt/configs/load_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import enum
import logging
from dataclasses import dataclass, field
from typing import Any, List, Optional, Union
from typing import TYPE_CHECKING, Any, List, Optional, Union

import orjson

Expand All @@ -13,11 +13,23 @@

logger = logging.getLogger(__name__)

if TYPE_CHECKING:
from sglang.srt.distributed.parallel_state import GroupCoordinator


class _DefaultLoadGroup(enum.Enum):
DEFAULT = enum.auto()


_DEFAULT_LOAD_GROUP = _DefaultLoadGroup.DEFAULT
LoadGroup = Union["GroupCoordinator", _DefaultLoadGroup, None]


class LoadFormat(str, enum.Enum):
AUTO = "auto"
PT = "pt"
SAFETENSORS = "safetensors"
INSTANTTENSOR = "instanttensor"
NPCACHE = "npcache"
DUMMY = "dummy"
SHARDED_STATE = "sharded_state"
Expand Down Expand Up @@ -50,6 +62,8 @@ class LoadConfig:
not available.
"pt" will load the weights in the pytorch bin format.
"safetensors" will load the weights in the safetensors format.
"instanttensor" will load Safetensors weights using InstantTensor's
high-performance distributed loader.
"npcache" will load the weights in pytorch format and store
a numpy cache to speed up the loading.
"dummy" will initialize the weights with random values, which is
Expand Down Expand Up @@ -112,6 +126,11 @@ class LoadConfig:
weight_cache_socket: Optional[str] = None # Path to daemon socket (for client mode)
fallback_load_format: Union[str, "LoadFormat"] = LoadFormat.AUTO

# Runtime-only load group, currently used by InstantTensor; None disables
# collective loading. Other distributed loaders, such as fastsafetensors
# and runai_model_streamer, could also benefit from honoring this group.
load_group: LoadGroup = field(default=_DEFAULT_LOAD_GROUP, repr=False)

def __post_init__(self):
model_loader_extra_config = self.model_loader_extra_config or {}
if isinstance(model_loader_extra_config, str):
Expand Down
5 changes: 5 additions & 0 deletions python/sglang/srt/managers/tp_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import torch

from sglang.srt.beam_search.logits_capture import capture_pre_sample_logits
from sglang.srt.configs.load_config import _DEFAULT_LOAD_GROUP, LoadGroup
from sglang.srt.environ import envs
from sglang.srt.managers.io_struct import (
DestroyWeightsUpdateGroupReqInput,
Expand Down Expand Up @@ -291,9 +292,11 @@ def __init__(
context_length: Optional[int] = None,
draft_attention_backend: Optional[str] = None,
random_seed: Optional[int] = None,
load_group: LoadGroup = _DEFAULT_LOAD_GROUP,
):
# Parse args
self.server_args = server_args
self.load_group = load_group
self.gpu_id = gpu_id
self.nccl_port = nccl_port
self.is_draft_worker = is_draft_worker
Expand Down Expand Up @@ -486,6 +489,7 @@ def _init_model_runner(self):
memory_pool_config=self.memory_pool_config,
draft_attention_backend=self.draft_attention_backend,
draft_model_idx=0 if self.is_multi_layer_eagle else None,
load_group=self.load_group,
)

def _init_multi_layer_eagle_model_runners(self):
Expand All @@ -506,6 +510,7 @@ def _init_multi_layer_eagle_model_runners(self):
memory_pool_config=self.memory_pool_config,
draft_attention_backend=self.draft_attention_backend,
draft_model_idx=i,
load_group=self.load_group,
)
)

Expand Down
9 changes: 8 additions & 1 deletion python/sglang/srt/model_executor/model_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
import torch
import torch.distributed as dist

from sglang.srt.configs.load_config import LoadConfig
from sglang.srt.configs.load_config import (
_DEFAULT_LOAD_GROUP,
LoadConfig,
LoadGroup,
)
from sglang.srt.configs.model_config import (
AttentionArch,
ModelConfig,
Expand Down Expand Up @@ -335,9 +339,11 @@ def __init__(
memory_pool_config: Optional[MemoryPoolConfig] = None,
draft_model_idx: Optional[int] = None,
draft_attention_backend: Optional[str] = None,
load_group: LoadGroup = _DEFAULT_LOAD_GROUP,
):
# Parse args
self.mem_fraction_static = mem_fraction_static
self.load_group = load_group
# Set on target by `_resolve_memory_pool_config`; passed in for draft
# workers so they reuse target's resolved sizes (replaces legacy
# `server_args._draft_pool_config` mutation hack).
Expand Down Expand Up @@ -1226,6 +1232,7 @@ def load_model(self):
draft_model_idx=self.draft_model_idx,
weight_cache_mode=get_model().weight_cache_mode,
weight_cache_socket=get_model().weight_cache_socket,
load_group=self.load_group,
)

maybe_enable_ipc_weight_cache(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
modelexpress_url_of,
)
from sglang.srt.configs.device_config import DeviceConfig
from sglang.srt.configs.load_config import LoadConfig, LoadFormat
from sglang.srt.configs.load_config import (
_DEFAULT_LOAD_GROUP,
LoadConfig,
LoadFormat,
LoadGroup,
)
from sglang.srt.constants import GPU_MEMORY_TYPE_WEIGHTS
from sglang.srt.debug_utils.tensor_dump_forward_hook import (
register_forward_hook_for_model,
Expand Down Expand Up @@ -202,6 +207,7 @@ def build_load_config(
draft_model_idx: int | None,
weight_cache_mode: str,
weight_cache_socket: str | None,
load_group: LoadGroup = _DEFAULT_LOAD_GROUP,
) -> LoadConfig:
from sglang.srt.configs.modelopt_config import ModelOptConfig

Expand Down Expand Up @@ -231,6 +237,7 @@ def build_load_config(
draft_model_idx=draft_model_idx,
weight_cache_mode=weight_cache_mode,
weight_cache_socket=weight_cache_socket,
load_group=load_group,
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
post_load_weights,
)
from sglang.srt.model_loader.utils import set_default_torch_dtype
from sglang.srt.model_loader.weight_utils import default_weight_loader
from sglang.srt.model_loader.weight_utils import (
default_weight_loader,
get_pp_stage_load_group,
)
from sglang.srt.platforms import current_platform
from sglang.srt.runtime_context import get_model, get_parallel
from sglang.srt.utils import (
Expand Down Expand Up @@ -188,7 +191,10 @@ def update_weights_from_disk(

target_device = torch.device(self.device)
self.model_config.model_path = model_path
load_config = LoadConfig(load_format=load_format)
# PP stages handle disk updates sequentially, unlike cold startup.
load_config = LoadConfig(
load_format=load_format, load_group=get_pp_stage_load_group()
)

# Only support DefaultModelLoader for now
loader = get_model_loader(load_config, self.model_config)
Expand Down
19 changes: 16 additions & 3 deletions python/sglang/srt/model_loader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
gguf_quant_weights_iterator,
initialize_capture_safe_weights,
initialize_dummy_weights,
instanttensor_weights_iterator,
maybe_add_mtp_safetensors,
multi_thread_pt_weights_iterator,
np_cache_weights_iterator,
Expand Down Expand Up @@ -359,6 +360,11 @@ def load_model(
def _validate_default_loader_extra_config(
*, extra_config: dict, load_format: LoadFormat
) -> None:
if load_format == LoadFormat.INSTANTTENSOR:
# Pass extra config directly to InstantTensor and let it report invalid
# options, so newer versions can add parameters without a SGLang update.
return
Comment thread
YitaoYuan marked this conversation as resolved.
Resolved

allowed_keys = {"enable_multithread_load", "num_threads"}
if load_format == LoadFormat.FASTSAFETENSORS:
allowed_keys.add("enable_gds")
Expand Down Expand Up @@ -490,9 +496,10 @@ def _prepare_weights(
# Some quantized models use .pt files for storing the weights.
if load_format == LoadFormat.AUTO:
allow_patterns = ["*.safetensors", "*.bin"]
elif (
load_format == LoadFormat.SAFETENSORS
or load_format == LoadFormat.FASTSAFETENSORS
elif load_format in (
LoadFormat.SAFETENSORS,
LoadFormat.FASTSAFETENSORS,
LoadFormat.INSTANTTENSOR,
):
use_safetensors = True
allow_patterns = ["*.safetensors"]
Expand Down Expand Up @@ -631,6 +638,12 @@ def _get_weights_iterator(
hf_folder,
hf_weights_files,
)
elif self.load_config.load_format == LoadFormat.INSTANTTENSOR:
weights_iterator = instanttensor_weights_iterator(
hf_weights_files,
extra_config=extra_config,
load_group=self.load_config.load_group,
)
elif use_safetensors:
weight_loader_disable_mmap = get_model().weight_loader_disable_mmap
configured_prefetch = get_model().weight_loader_prefetch_checkpoints
Expand Down
Loading
Loading