[CPU] Add FP8 Bmm support (#9744)
Co-authored-by: Fan Yin <1106310035@qq.com>
This commit is contained in:
@@ -1208,14 +1208,6 @@ class BailingMoELinearForCausalLM(nn.Module):
|
||||
)
|
||||
if _is_hip:
|
||||
self_attn.w_scale *= 2.0
|
||||
# TODO: remove this after adding FP8 support in bmm cpu kernel
|
||||
if _is_cpu and _is_cpu_amx_available and w.dtype == torch.float8_e4m3fn:
|
||||
self_attn.w_kc = (
|
||||
self_attn.w_kc.to(torch.bfloat16) * self_attn.w_scale
|
||||
)
|
||||
self_attn.w_vc = (
|
||||
self_attn.w_vc.to(torch.bfloat16) * self_attn.w_scale
|
||||
)
|
||||
else:
|
||||
num_tiles_k = self_attn.qk_nope_head_dim // weight_block_size[1]
|
||||
num_tiles_n = self_attn.v_head_dim // weight_block_size[0]
|
||||
|
||||
@@ -16,6 +16,7 @@ from sglang.srt.layers.quantization.fp8_kernel import (
|
||||
from sglang.srt.model_executor.forward_batch_info import ForwardBatch
|
||||
from sglang.srt.models.deepseek_common.utils import (
|
||||
FORWARD_ABSORB_CORE_ATTENTION_BACKENDS,
|
||||
_is_cpu,
|
||||
_is_cublas_ge_129,
|
||||
_is_cuda,
|
||||
_is_gfx95_supported,
|
||||
@@ -268,18 +269,24 @@ class DeepseekMLAForwardMixin:
|
||||
)
|
||||
|
||||
elif self.w_kc.dtype == torch.float8_e4m3fn:
|
||||
# fix bmm_fp8 error under cublas12.9 caused by bumpallocator, detail in pr#11612
|
||||
q_nope_val, q_nope_scale = per_tensor_quant_mla_fp8(
|
||||
q_nope.transpose(0, 1),
|
||||
(
|
||||
torch.zeros((1,), dtype=torch.float32, device=q_nope.device)
|
||||
if _is_cublas_ge_129
|
||||
else zero_allocator.allocate(1)
|
||||
),
|
||||
)
|
||||
q_nope_out = bmm_fp8(
|
||||
q_nope_val, self.w_kc, q_nope_scale, self.w_scale, torch.bfloat16
|
||||
)
|
||||
if _is_cpu:
|
||||
q_nope_out = torch.bmm(
|
||||
q_nope.to(torch.bfloat16).transpose(0, 1),
|
||||
self.w_kc.to(torch.bfloat16) * self.w_scale,
|
||||
)
|
||||
else:
|
||||
# fix bmm_fp8 error under cublas12.9 caused by bumpallocator, detail in pr#11612
|
||||
q_nope_val, q_nope_scale = per_tensor_quant_mla_fp8(
|
||||
q_nope.transpose(0, 1),
|
||||
(
|
||||
torch.zeros((1,), dtype=torch.float32, device=q_nope.device)
|
||||
if _is_cublas_ge_129
|
||||
else zero_allocator.allocate(1)
|
||||
),
|
||||
)
|
||||
q_nope_out = bmm_fp8(
|
||||
q_nope_val, self.w_kc, q_nope_scale, self.w_scale, torch.bfloat16
|
||||
)
|
||||
else:
|
||||
q_nope_out = torch.bmm(q_nope.transpose(0, 1), self.w_kc)
|
||||
|
||||
@@ -455,22 +462,31 @@ class DeepseekMLAForwardMixin:
|
||||
attn_bmm_output = attn_bmm_output.transpose(0, 1).flatten(1, 2)
|
||||
|
||||
elif self.w_vc.dtype == torch.float8_e4m3fn:
|
||||
attn_output_val, attn_output_scale = per_tensor_quant_mla_fp8(
|
||||
attn_output.transpose(0, 1),
|
||||
(
|
||||
torch.zeros((1,), dtype=torch.float32, device=attn_output.device)
|
||||
if _is_cublas_ge_129
|
||||
else zero_allocator.allocate(1)
|
||||
),
|
||||
)
|
||||
attn_bmm_output = bmm_fp8(
|
||||
attn_output_val,
|
||||
self.w_vc,
|
||||
attn_output_scale,
|
||||
self.w_scale,
|
||||
torch.bfloat16,
|
||||
)
|
||||
attn_bmm_output = attn_bmm_output.transpose(0, 1).flatten(1, 2)
|
||||
if _is_cpu:
|
||||
attn_bmm_output = torch.bmm(
|
||||
attn_output.to(torch.bfloat16).transpose(0, 1),
|
||||
self.w_vc.to(torch.bfloat16) * self.w_scale,
|
||||
)
|
||||
attn_bmm_output = attn_bmm_output.transpose(0, 1).flatten(1, 2)
|
||||
else:
|
||||
attn_output_val, attn_output_scale = per_tensor_quant_mla_fp8(
|
||||
attn_output.transpose(0, 1),
|
||||
(
|
||||
torch.zeros(
|
||||
(1,), dtype=torch.float32, device=attn_output.device
|
||||
)
|
||||
if _is_cublas_ge_129
|
||||
else zero_allocator.allocate(1)
|
||||
),
|
||||
)
|
||||
attn_bmm_output = bmm_fp8(
|
||||
attn_output_val,
|
||||
self.w_vc,
|
||||
attn_output_scale,
|
||||
self.w_scale,
|
||||
torch.bfloat16,
|
||||
)
|
||||
attn_bmm_output = attn_bmm_output.transpose(0, 1).flatten(1, 2)
|
||||
else:
|
||||
if is_in_piecewise_cuda_graph():
|
||||
# torch dynamo requires out= op was called where output tensor was non-contiguous
|
||||
|
||||
@@ -100,6 +100,7 @@ class DeepseekMLACpuForwardMixin:
|
||||
else None
|
||||
)
|
||||
),
|
||||
self.w_scale,
|
||||
True, # is_vnni
|
||||
self.weight_block_size,
|
||||
self.q_lora_rank,
|
||||
@@ -144,7 +145,7 @@ class DeepseekMLACpuForwardMixin:
|
||||
attn_output.transpose(0, 1),
|
||||
self.w_vc,
|
||||
True, # is_vnni
|
||||
None, # scale
|
||||
self.w_scale, # scale
|
||||
)
|
||||
attn_output = output
|
||||
output, _ = self.o_proj(attn_output)
|
||||
|
||||
@@ -46,8 +46,6 @@ from sglang.srt.model_loader.utils import (
|
||||
)
|
||||
from sglang.srt.model_loader.weight_utils import default_weight_loader
|
||||
from sglang.srt.models.deepseek_common.utils import (
|
||||
_is_cpu,
|
||||
_is_cpu_amx_available,
|
||||
_is_cuda,
|
||||
_is_fp8_fnuz,
|
||||
_is_hip,
|
||||
@@ -583,14 +581,6 @@ class DeepseekV2WeightLoaderMixin:
|
||||
)
|
||||
if _is_hip:
|
||||
self_attn.w_scale *= 2.0
|
||||
# TODO: remove this after adding FP8 support in bmm cpu kernel
|
||||
if _is_cpu and _is_cpu_amx_available and w.dtype == torch.float8_e4m3fn:
|
||||
self_attn.w_kc = (
|
||||
self_attn.w_kc.to(torch.bfloat16) * self_attn.w_scale
|
||||
)
|
||||
self_attn.w_vc = (
|
||||
self_attn.w_vc.to(torch.bfloat16) * self_attn.w_scale
|
||||
)
|
||||
else:
|
||||
num_tiles_k = self_attn.qk_nope_head_dim // weight_block_size[1]
|
||||
num_tiles_n = self_attn.v_head_dim // weight_block_size[0]
|
||||
|
||||
@@ -776,18 +776,6 @@ class LongcatFlashForCausalLM(nn.Module):
|
||||
)
|
||||
if _is_hip:
|
||||
self_attn.w_scale *= 2.0
|
||||
# TODO: remove this after adding FP8 support in bmm cpu kernel
|
||||
if (
|
||||
_is_cpu
|
||||
and _is_cpu_amx_available
|
||||
and w.dtype == torch.float8_e4m3fn
|
||||
):
|
||||
self_attn.w_kc = (
|
||||
self_attn.w_kc.to(torch.bfloat16) * self_attn.w_scale
|
||||
)
|
||||
self_attn.w_vc = (
|
||||
self_attn.w_vc.to(torch.bfloat16) * self_attn.w_scale
|
||||
)
|
||||
else:
|
||||
num_tiles_k = self_attn.qk_nope_head_dim // weight_block_size[1]
|
||||
num_tiles_n = self_attn.v_head_dim // weight_block_size[0]
|
||||
|
||||
@@ -426,10 +426,6 @@ class LongcatFlashForCausalLMNextN(LongcatFlashForCausalLM):
|
||||
)
|
||||
if _is_hip:
|
||||
self_attn.w_scale *= 2.0
|
||||
# TODO: remove this after adding FP8 support in bmm cpu kernel
|
||||
if _is_cpu and _is_cpu_amx_available and w.dtype == torch.float8_e4m3fn:
|
||||
self_attn.w_kc = self_attn.w_kc.to(torch.bfloat16) * self_attn.w_scale
|
||||
self_attn.w_vc = self_attn.w_vc.to(torch.bfloat16) * self_attn.w_scale
|
||||
else:
|
||||
num_tiles_k = self_attn.qk_nope_head_dim // weight_block_size[1]
|
||||
num_tiles_n = self_attn.v_head_dim // weight_block_size[0]
|
||||
|
||||
Reference in New Issue
Block a user