[CPU] refine CPU integration code (#7647)

This commit is contained in:
Chunyuan WU
2025-07-03 09:51:09 -07:00
committed by GitHub
parent ac49dac009
commit 9fcc9a80e7
9 changed files with 141 additions and 116 deletions
+29 -20
View File
@@ -36,6 +36,7 @@ from sglang.srt.eplb.expert_distribution import get_global_expert_distribution_r
from sglang.srt.eplb.expert_location import ModelConfigForExpertLocation
from sglang.srt.eplb.expert_location_dispatch import ExpertLocationDispatchInfo
from sglang.srt.layers.activation import SiluAndMul
from sglang.srt.layers.amx_utils import PackWeightMethod
from sglang.srt.layers.communicator import (
LayerCommunicator,
LayerScatterModes,
@@ -91,7 +92,6 @@ from sglang.srt.utils import (
BumpAllocator,
DeepEPMode,
LazyValue,
PackWeightMethod,
add_prefix,
bind_or_assign,
cpu_has_amx_support,
@@ -103,6 +103,7 @@ from sglang.srt.utils import (
is_hip,
is_non_idle_and_non_empty,
log_info_on_rank0,
use_intel_amx_backend,
)
_is_hip = is_hip()
@@ -224,7 +225,7 @@ class MoEGate(nn.Module):
self.quant_method = PackWeightMethod(weight_names=["weight"])
def forward(self, hidden_states):
if getattr(self, "use_intel_amx_backend", False):
if use_intel_amx_backend(self):
return torch.ops.sgl_kernel.weight_packed_linear(
hidden_states,
self.weight,
@@ -437,8 +438,8 @@ class DeepseekV2MoE(nn.Module):
return final_hidden_states
def forward_normal(self, hidden_states: torch.Tensor) -> torch.Tensor:
if hasattr(self, "shared_experts") and getattr(
self.shared_experts.gate_up_proj, "use_intel_amx_backend", False
if hasattr(self, "shared_experts") and use_intel_amx_backend(
self.shared_experts.gate_up_proj
):
return self.forward_cpu(hidden_states)
@@ -464,9 +465,9 @@ class DeepseekV2MoE(nn.Module):
hidden_states=hidden_states, router_logits=router_logits
)
assert getattr(
self.shared_experts.gate_up_proj, "use_intel_amx_backend", False
) == getattr(self.shared_experts.down_proj, "use_intel_amx_backend", False)
assert use_intel_amx_backend(
self.shared_experts.gate_up_proj
) == use_intel_amx_backend(self.shared_experts.down_proj)
# [Note] inplace should be False in fused_experts.
# If inplace is True in fused_experts (self.experts), hidden_states will be changed after fused_experts
# While hidden_states is still needed in shared_expert.
@@ -928,15 +929,23 @@ class DeepseekV2AttentionMLA(nn.Module):
)
self.weight_block_size = None
if self.qkv_proj_with_rope_is_fp8:
assert (
self.fused_qkv_a_proj_with_mqa.quant_method.quant_config.weight_block_size
== self.q_b_proj.quant_method.quant_config.weight_block_size
)
self.weight_block_size = (
self.fused_qkv_a_proj_with_mqa.quant_method.quant_config.weight_block_size
if self.qkv_proj_with_rope_is_fp8 and _is_cpu and _is_cpu_amx_available:
assert getattr(
self.fused_qkv_a_proj_with_mqa.quant_method, "block_quant", False
) == getattr(self.q_b_proj.quant_method, "block_quant", False)
use_block_quant = getattr(
self.fused_qkv_a_proj_with_mqa.quant_method, "block_quant", False
)
if use_block_quant:
assert (
self.fused_qkv_a_proj_with_mqa.quant_method.quant_config.weight_block_size
== self.q_b_proj.quant_method.quant_config.weight_block_size
)
self.weight_block_size = (
self.fused_qkv_a_proj_with_mqa.quant_method.quant_config.weight_block_size
)
def dispatch_attn_forward_method(
self, forward_batch: ForwardBatch
) -> AttnForwardMethod:
@@ -950,8 +959,8 @@ class DeepseekV2AttentionMLA(nn.Module):
else:
return AttnForwardMethod.MLA
else:
if hasattr(self, "fused_qkv_a_proj_with_mqa") and getattr(
self, "use_intel_amx_backend", False
if hasattr(self, "fused_qkv_a_proj_with_mqa") and use_intel_amx_backend(
self
):
return AttnForwardMethod.MLA_FUSED_ROPE_CPU
else:
@@ -1426,8 +1435,8 @@ class DeepseekV2AttentionMLA(nn.Module):
forward_batch: ForwardBatch,
zero_allocator: BumpAllocator,
):
assert self.q_lora_rank is not None and getattr(
self, "use_intel_amx_backend", False
assert self.q_lora_rank is not None and use_intel_amx_backend(
self
), "forward_absorb_fused_mla_rope_cpu_prepare requires q_lora_rank is not None and use_intel_amx_backend"
q_input, k_input, v_input = (
@@ -1546,8 +1555,8 @@ class DeepseekV2AttentionMLA(nn.Module):
def forward_absorb_fused_mla_rope_cpu_core(
self, q_input, k_input, v_input, forward_batch, zero_allocator
):
assert self.q_lora_rank is not None and getattr(
self, "use_intel_amx_backend", False
assert self.q_lora_rank is not None and use_intel_amx_backend(
self
), "forward_absorb_fused_mla_rope_cpu_core requires q_lora_rank is not None and use_intel_amx_backend"
attn_output = self.attn_mqa(q_input, k_input, v_input, forward_batch)