[AMD] Speed up DSV4 MoE weight loading from mmap views (#32315)

This commit is contained in:
Bingxu Chen
2026-08-01 23:47:47 -07:00
committed by GitHub
parent 7e509f690e
commit 37be4e9247
6 changed files with 91 additions and 0 deletions
+3
View File
@@ -1095,6 +1095,9 @@ class Envs:
# Set False when using FP4-to-FP8 converted DeepSeek V4 checkpoint.
SGLANG_DSV4_FP4_EXPERTS = EnvBool(True)
SGLANG_DSV4_FP4_DEQUANT = EnvBool(False)
# Copy rank-local MoE slices into independent CPU storage before H2D when
# they reference a larger mmap-backed checkpoint storage.
SGLANG_MOE_COPY_WEIGHT_VIEWS_BEFORE_H2D = EnvBool(False)
# Default reasoning_effort for dsv4 chat encoder when request doesn't set it.
# Accepts "", "max", "high" (empty string means unset); other values filtered to None.
SGLANG_DSV4_REASONING_EFFORT = EnvStr("")
@@ -94,6 +94,29 @@ _is_npu = is_npu()
_use_aiter = get_bool_env_var("SGLANG_USE_AITER") and _is_hip
def _copy_weight_view_before_h2d(loaded_weight: torch.Tensor) -> torch.Tensor:
"""Copy a CPU tensor view into independent contiguous storage."""
if loaded_weight.device.type != "cpu":
return loaded_weight
tensor_bytes = loaded_weight.numel() * loaded_weight.element_size()
needs_copy = not (
loaded_weight.is_contiguous()
and loaded_weight.storage_offset() == 0
and loaded_weight.untyped_storage().nbytes() == tensor_bytes
)
if not needs_copy:
return loaded_weight
return loaded_weight.clone(memory_format=torch.contiguous_format)
def _maybe_copy_weight_view_before_h2d(
loaded_weight: torch.Tensor,
) -> torch.Tensor:
if not envs.SGLANG_MOE_COPY_WEIGHT_VIEWS_BEFORE_H2D.get():
return loaded_weight
return _copy_weight_view_before_h2d(loaded_weight)
def _get_deepep_comm_group(a2a_backend):
group = get_tp_group().device_group
@@ -559,6 +582,7 @@ class FusedMoE(torch.nn.Module):
):
# for per channel weight quantization
if shard_id == "w2":
loaded_weight = _maybe_copy_weight_view_before_h2d(loaded_weight)
expert_data.copy_(loaded_weight)
elif shard_id in ("w1", "w3"):
self._load_w13(
@@ -631,6 +655,7 @@ class FusedMoE(torch.nn.Module):
)
expert_data = expert_data.narrow(shard_dim, start, shard_size)
loaded_weight = _maybe_copy_weight_view_before_h2d(loaded_weight)
expert_data.copy_(loaded_weight)
def _load_w2(
@@ -701,6 +726,7 @@ class FusedMoE(torch.nn.Module):
)
# w2, down_proj: Load into only logical weight of w2.
loaded_weight = _maybe_copy_weight_view_before_h2d(loaded_weight)
expert_data.copy_(loaded_weight)
def _maybe_load_fp8_shared_expert_as_fp4(