[CPU][Whisper] Support W4A16 quantized Whisper on the CPU WNA16 kernel (#58268)

Signed-off-by: Harshal Adhav <harshal.adhav@amd.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Li, Jiang <jiang1.li@intel.com>
This commit is contained in:
Harshal Adhav
2026-09-30 13:52:53 +08:00
committed by GitHub
co-authored by Cursor Li, Jiang <jiang1.li@intel.com>
parent fcf4750466
commit 5faf81a429
5 changed files with 144 additions and 15 deletions
+5 -4
View File
@@ -203,7 +203,7 @@ class TestInt4ScaledMmCpu:
print(f" [PASS] INT4 GEMM with bias: M={M}")
def test_gemm_3d_input(self):
"""apply() reshapes 3D input [B, S, K] -> [B*S, K] -> back to 3D."""
"""int4_scaled_mm_cpu flattens 3D [B, S, K] to 2D and reshapes back."""
K, N, group_size = 256, 128, 128
(packed_qweight, packed_qzeros, scales, float_ref, _, _) = (
make_awq_checkpoint_data(K, N, group_size)
@@ -220,12 +220,13 @@ class TestInt4ScaledMmCpu:
x_3d = torch.randn(B, S, K, dtype=torch.bfloat16)
x_2d = x_3d.reshape(-1, K)
out_3d = int4_scaled_mm_cpu(x_3d, blocked_w, blocked_zp, blocked_s, None)
out_2d = int4_scaled_mm_cpu(x_2d, blocked_w, blocked_zp, blocked_s, None)
out_3d = out_2d.reshape(B, S, N)
ref_out = torch.mm(x_2d.float(), float_ref).reshape(B, S, N)
assert out_3d.shape == (B, S, N)
torch.testing.assert_close(out_3d.reshape(-1, N), out_2d)
ref_out = torch.mm(x_2d.float(), float_ref).reshape(B, S, N)
abs_diff = (out_3d.float() - ref_out).abs()
mean_abs = abs_diff.mean().item()
ref_mag = ref_out.abs().mean().item() + 1e-6
@@ -5,6 +5,7 @@ from collections.abc import Sequence
from typing import Any
import pytest
import torch
from transformers import AutoModelForSpeechSeq2Seq
from vllm.assets.audio import AudioAsset
@@ -264,6 +265,49 @@ def test_parse_language_detection_output():
cls.parse_language_detection_output([], make_tokenizer("anything"))
def test_create_fake_bias_for_k_proj_injects_out_features():
"""Packed k_proj weights get a zeros bias of length ``out_features``."""
from vllm.model_executor.models.whisper import _create_fake_bias_for_k_proj
packed = torch.ones(2, 16)
result = dict(
_create_fake_bias_for_k_proj(
[("model.encoder.layers.0.self_attn.k_proj.weight_packed", packed)],
".k_proj.weight_packed",
out_features=8,
)
)
bias_name = "model.encoder.layers.0.self_attn.k_proj.bias"
assert bias_name in result
assert "model.encoder.layers.0.self_attn.k_proj.bias_packed" not in result
assert tuple(result[bias_name].shape) == (8,)
assert torch.equal(result[bias_name], torch.zeros(8))
def test_create_fake_bias_for_k_proj_forwards_real_bias():
"""A checkpoint bias is forwarded and no extra fake bias is emitted."""
from vllm.model_executor.models.whisper import _create_fake_bias_for_k_proj
weight = torch.ones(8, 16)
real_bias = torch.ones(8)
items = list(
_create_fake_bias_for_k_proj(
[
("layers.0.self_attn.k_proj.weight", weight),
("layers.0.self_attn.k_proj.bias", real_bias),
],
".k_proj.weight",
out_features=8,
)
)
names = [n for n, _ in items]
assert names == [
"layers.0.self_attn.k_proj.weight",
"layers.0.self_attn.k_proj.bias",
]
assert items[1][1] is real_bias
@pytest.mark.core_model
@pytest.mark.cpu_model
@pytest.mark.parametrize("model", ["openai/whisper-large-v3-turbo"])
+45
View File
@@ -1,12 +1,57 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
import torch
from vllm.platforms import current_platform
if not current_platform.is_cpu():
pytest.skip("skipping CPU-only tests", allow_module_level=True)
from vllm._custom_ops import cpu_gemm_wna16 # noqa: E402
from vllm.model_executor.kernels.linear.mixed_precision.cpu import ( # noqa: E402
_get_isa_hint,
)
# Isolated runs of cpu_gemm_wna16 can hang unless AMX tiles are initialized.
# Guard so non-AMX hosts still take the vec/rvv path.
if torch.cpu._is_amx_tile_supported():
torch.cpu._init_amx()
@pytest.mark.cpu_test
def test_cpu_gemm_wna16_3d_input():
"""cpu_gemm_wna16 flattens 3D [B, T, K] to 2D and reshapes the output back."""
if not hasattr(torch.ops._C, "cpu_gemm_wna16"):
pytest.skip("cpu_gemm_wna16 op is not available")
B, T, K, N = 2, 5, 64, 64
pack_factor = 8
group_size = 32
dtype = torch.bfloat16
q_weight = torch.zeros(N // 16, K * 16 // pack_factor, dtype=torch.int32)
scales = torch.ones(K // group_size, N, dtype=dtype)
x_3d = torch.randn(B, T, K, dtype=dtype)
x_2d = x_3d.reshape(-1, K)
isa_hint = _get_isa_hint(dtype)
kwargs = dict(
q_weight=q_weight,
scales=scales,
zeros=None,
bias=None,
pack_factor=pack_factor,
isa_hint=isa_hint,
)
out_3d = cpu_gemm_wna16(input=x_3d, **kwargs)
out_2d = cpu_gemm_wna16(input=x_2d, **kwargs)
assert out_3d.shape == (B, T, N)
assert out_2d.shape == (B * T, N)
torch.testing.assert_close(out_3d.reshape(-1, N), out_2d)
MODELS = [
"TheBloke/TinyLlama-1.1B-Chat-v1.0-AWQ",
"Qwen/Qwen1.5-0.5B-Chat-GPTQ-Int4",