From 898beca5a8f173e458b4a1f0bac0b2fead0a35a7 Mon Sep 17 00:00:00 2001 From: Liangliang Ma Date: Mon, 20 Apr 2026 08:24:50 +0800 Subject: [PATCH] [BugFix][XPU] fix lora ops bgmv_expand size not match (#39989) Signed-off-by: Ma, Liangliang Co-authored-by: Kunshang Ji --- vllm/lora/ops/xpu_ops/lora_ops.py | 39 ++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/vllm/lora/ops/xpu_ops/lora_ops.py b/vllm/lora/ops/xpu_ops/lora_ops.py index 6d1751c373..070fd86458 100644 --- a/vllm/lora/ops/xpu_ops/lora_ops.py +++ b/vllm/lora/ops/xpu_ops/lora_ops.py @@ -27,9 +27,42 @@ def bgmv_expand( lora_indices_tensor: torch.Tensor, add_inputs: bool = True, ) -> None: - torch.ops._xpu_C.bgmv_expand( - output_tensor, inputs, lora_b_weights, lora_indices_tensor, add_inputs - ) + weight_out_dim = lora_b_weights.size(-2) + output_dim = output_tensor.size(1) + + if weight_out_dim == output_dim: + torch.ops._xpu_C.bgmv_expand( + output_tensor, + inputs, + lora_b_weights, + lora_indices_tensor, + add_inputs, + ) + elif weight_out_dim < output_dim: + # LoRA weight output dim can be smaller than the output tensor + # (e.g. vocab_size vs padded logits). Use expand_slice to write + # only the matching portion, mirroring torch_ops common_len logic. + torch.ops._xpu_C.bgmv_expand_slice( + output_tensor, + inputs, + lora_b_weights, + lora_indices_tensor, + 0, + weight_out_dim, + add_inputs, + ) + else: + # Weight output dim larger than output tensor: truncate weights. + lora_b_weights = lora_b_weights[..., :output_dim, :].contiguous() + torch.ops._xpu_C.bgmv_expand_slice( + output_tensor, + inputs, + lora_b_weights, + lora_indices_tensor, + 0, + output_dim, + add_inputs, + ) def bgmv_expand_slice(