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
1 change: 1 addition & 0 deletions python/sglang/srt/managers/schedule_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"torchao_config",
"triton_attention_reduce_in_fp32",
"num_reserved_decode_tokens",
"weight_loader_disable_mmap",
]

# Put some global args for easy access
Expand Down
9 changes: 8 additions & 1 deletion python/sglang/srt/model_loader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,14 @@ def _get_weights_iterator(
hf_weights_files,
)
elif use_safetensors:
weights_iterator = safetensors_weights_iterator(hf_weights_files)
from sglang.srt.managers.schedule_batch import global_server_args_dict

weight_loader_disable_mmap = global_server_args_dict.get(
"weight_loader_disable_mmap"
)
weights_iterator = safetensors_weights_iterator(
hf_weights_files, disable_mmap=weight_loader_disable_mmap
)
else:
weights_iterator = pt_weights_iterator(hf_weights_files)

Expand Down
7 changes: 6 additions & 1 deletion python/sglang/srt/model_loader/weight_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ def safetensors_weights_iterator(
hf_weights_files: List[str],
is_all_weights_sharded: bool = False,
decryption_key: Optional[str] = None,
disable_mmap: bool = False,
) -> Generator[Tuple[str, torch.Tensor], None, None]:
"""Iterate over the weights in the model safetensor files.

Expand All @@ -443,7 +444,11 @@ def safetensors_weights_iterator(
disable=not enable_tqdm,
bar_format=_BAR_FORMAT,
):
result = safetensors.torch.load_file(st_file, device="cpu")
if disable_mmap:
with open(st_file, "rb") as f:
result = safetensors.torch.load(f.read())
else:
result = safetensors.torch.load_file(st_file, device="cpu")
for name, param in result.items():
yield name, param

Expand Down
6 changes: 6 additions & 0 deletions python/sglang/srt/server_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ class ServerArgs:

# For model weight update
custom_weight_loader: Optional[List[str]] = None
weight_loader_disable_mmap: bool = False

def __post_init__(self):
# Expert parallelism
Expand Down Expand Up @@ -1599,6 +1600,11 @@ def add_cli_args(parser: argparse.ArgumentParser):
default=None,
help="The custom dataloader which used to update the model. Should be set with a valid import path, such as my_package.weight_load_func",
)
parser.add_argument(
"--weight-loader-disable-mmap",
action="store_true",
help="Disable mmap while loading weight using safetensors.",
)

@classmethod
def from_cli_args(cls, args: argparse.Namespace):
Expand Down
Loading