Skip to content

PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度

add the wrapper for woqgemm (#1965)

Zhenzhong Xu3ab9520773c9
diff --git a/auto_round_extension/ark/auto_round_kernel/__init__.py b/auto_round_extension/ark/auto_round_kernel/__init__.py
index fffec91c0..8eb831f40 100644
--- a/auto_round_extension/ark/auto_round_kernel/__init__.py
+++ b/auto_round_extension/ark/auto_round_kernel/__init__.py
@@ -1608,6 +1608,51 @@ def packed_weight_size(A: torch.Tensor, n, k, groupsize, compute_type, weight_ty
return lib.packed_weight_size(stream, n, k, groupsize, ct, wt, st, asym)
+def woqgemm_linear(
+ A: torch.Tensor,
+ B: torch.Tensor,
+ bias: torch.Tensor | None,
+ n,
+ k,
+ groupsize,
+ compute_type,
+ weight_type,
+ scale_type,
+ asym,
+):
+ n = int(n)
+ k = int(k)
+ groupsize = int(groupsize)
+ asym = bool(asym)
+
+ if A.shape[-1] != k:
+ raise ValueError(f"k must match A.shape[-1] ({A.shape[-1]}), got {k}")
+
+ raw_input_dtype = A.dtype
+ target_dtype = torch.float16 if A.device.type == "xpu" else torch.float32
+ out_shape = A.shape[:-1] + (n,)
+ A_2d = A.to(target_dtype).reshape(-1, A.shape[-1])
+ if bias is None or bias.numel() == 0:
+ bias = torch.empty(0, dtype=target_dtype, device=A.device)
+ else:
+ bias = bias.to(device=A.device, dtype=target_dtype)
+
+ out = woqgemm(
+ A_2d,
+ B,
+ bias,
+ n,
+ k,
+ groupsize,
+ compute_type,
+ weight_type,
+ scale_type,
+ asym,
+ )
+
+ return out.to(raw_input_dtype).reshape(out_shape)
+
+
def woq_linear(
A: torch.Tensor,
packed_B: torch.Tensor,