From d779835a1196d45225c8429ee23bff293517f77e Mon Sep 17 00:00:00 2001 From: Xiaochang Wu Date: Thu, 6 Aug 2026 13:17:48 +0800 Subject: [PATCH] [XPU] Support MXFP8 linear weights for INC DeepSeek V4 model (#48476) Signed-off-by: Wu, Xiaochang Signed-off-by: Xiaochang Wu Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Kunshang Ji --- .../kernels/linear/mxfp8/xpu.py | 41 ++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/vllm/model_executor/kernels/linear/mxfp8/xpu.py b/vllm/model_executor/kernels/linear/mxfp8/xpu.py index d64e175c29..e13d6d0958 100644 --- a/vllm/model_executor/kernels/linear/mxfp8/xpu.py +++ b/vllm/model_executor/kernels/linear/mxfp8/xpu.py @@ -28,10 +28,38 @@ class XPUMxFp8LinearKernel(Mxfp8LinearKernel): return True, None def process_weights_after_loading(self, layer: torch.nn.Module) -> None: + # Checkpoint scale is [N, K//32] (one E8M0 scale per 32 elements). + # oneDNN fp8_gemm requires contiguous [K//32, N] layout. Store the + # transposed contiguous buffer as a .t() view so that: + # - dequantization consumers still see the checkpoint shape + # - apply_weights recovers the oneDNN layout via .t() at zero cost weight_scale = layer.weight_scale.view(torch.float8_e8m0fnu) - weight_scale = weight_scale.t().contiguous() - replace_parameter(layer, "weight", layer.weight.t()) - replace_parameter(layer, "weight_scale", weight_scale.data) + scale_kn = weight_scale.data.t().contiguous() + replace_parameter(layer, "weight_scale", scale_kn.t()) + + if getattr(layer, "is_bmm", False): + self._prepare_bmm_params(layer, scale_kn) + + def _prepare_bmm_params( + self, layer: torch.nn.Module, scale_kn: torch.Tensor + ) -> None: + """Precompute batched weight and scale for grouped fp8_bmm (e.g. wo_a). + + Splits scale [K//32, N_total] into [G, K//32, N_per_group] and weight + [N_total, K] into contiguous [G, K, N_per_group] for batch GEMM. + """ + batch = layer.bmm_batch_size + k_blocks, n_blocks = scale_kn.shape + layer.bmm_scale = ( + scale_kn.reshape(k_blocks, batch, n_blocks // batch) + .permute(1, 0, 2) + .contiguous() + ) + w = layer.weight + n_total, k = w.shape + layer.bmm_weight = ( + w.reshape(batch, n_total // batch, k).permute(0, 2, 1).contiguous() + ) def apply_weights( self, @@ -41,11 +69,14 @@ class XPUMxFp8LinearKernel(Mxfp8LinearKernel): ) -> torch.Tensor: out_dtype = x.dtype x_fp8, x_scale = quant_mxfp8(x) + # Weight is [N, K]; .t() gives a [K, N] view without copying. + # Scale is stored as [N, K//32]; .t() recovers the contiguous + # [K//32, N] buffer that oneDNN expects. return torch.ops._xpu_C.fp8_gemm( x_fp8, - layer.weight, + layer.weight.t(), out_dtype, x_scale, - layer.weight_scale, + layer.weight_scale.t(), bias, )