Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Build the marlin_gemm call from the op schema so packed INT4 inference works on vLLM 0.29 (#12320)

Daniel Han7740a23862e2
diff --git a/tests/test_compressed_tensors_int4.py b/tests/test_compressed_tensors_int4.py
index 1bbf3d74cbb..ef78862efe4 100644
--- a/tests/test_compressed_tensors_int4.py
+++ b/tests/test_compressed_tensors_int4.py
@@ -207,6 +207,7 @@ def test_repacked_weights_dequantize_exactly_and_save_in_checkpoint_layout(
assert W.shape == checkpoint.shape and not torch.equal(W, checkpoint)
assert torch.equal(ip.int4_dequantize(W, qs), ref)
assert torch.equal(ip.int4_unpack(W, qs), checkpoint)
+ assert ip._fast_args(W, qs) is not None
fast = ip._fast_rows(layout, torch.cuda.current_device())
for rows in (1, 3, fast, fast + 1):
x = torch.randn(rows, 1024, device = "cuda", dtype = torch.bfloat16)
@@ -645,6 +646,57 @@ def test_training_single_row_forward_uses_the_backward_weights():
assert torch.equal(int4_matmul(x, W, qs, fast = False), want)
+_MARLIN_SCHEMAS = {
+ # vLLM 0.29 schema
+ "g_idx_perm": "(Tensor a, Tensor? c_or_none, Tensor b_q_weight, Tensor? b_bias_or_none, Tensor b_scales, "
+ "Tensor? a_scales, Tensor? global_scale, Tensor? b_zeros_or_none, Tensor? g_idx_or_none, Tensor? perm_or_none, "
+ "Tensor workspace, int b_type_id, SymInt size_m, SymInt size_n, SymInt size_k, bool is_k_full, "
+ "bool use_atomic_add, bool use_fp32_reduce, bool is_zp_float) -> Tensor",
+ "no_g_idx": "(Tensor a, Tensor? c_or_none, Tensor b_q_weight, Tensor? b_bias_or_none, Tensor b_scales, "
+ "Tensor? a_scales, Tensor? global_scale, Tensor? b_zeros_or_none, Tensor workspace, int b_type_id, "
+ "SymInt size_m, SymInt size_n, SymInt size_k, bool use_atomic_add, bool use_fp32_reduce, "
+ "bool is_zp_float) -> Tensor",
+ "unknown_required": "(Tensor a, Tensor b_q_weight, Tensor b_scales, Tensor workspace, int b_type_id, "
+ "Tensor new_required, SymInt size_m, SymInt size_n, SymInt size_k) -> Tensor",
+}
+
+
[email protected]("schema", sorted(_MARLIN_SCHEMAS))
+def test_marlin_gemm_arguments_follow_the_op_schema(schema):
+ import unsloth.kernels.int4_packed as ip
+
+ lib = torch.library.Library("unsloth_marlin_schema_test", "FRAGMENT")
+ name = f"marlin_gemm_{schema}"
+ lib.define(name + _MARLIN_SCHEMAS[schema])
+ op = getattr(torch.ops.unsloth_marlin_schema_test, name).default
+ mq, ms, mz, ws = (torch.zeros(i + 1) for i in range(4))
+ args = ip._marlin_gemm_args(
+ op, c_or_none = None, b_q_weight = mq, b_bias_or_none = None, b_scales = ms, b_zeros_or_none = mz,
+ workspace = ws, b_type_id = 7, size_n = 64, size_k = 128, is_k_full = True, use_atomic_add = False,
+ use_fp32_reduce = True, is_zp_float = False,
+ ) # fmt: skip
+ if schema == "unknown_required":
+ assert args is None
+ return
+ pre, post = args
+ names = [a.name for a in op._schema.arguments]
+ m = names.index("size_m")
+ by_name = dict(zip(names[1:m], pre)) | dict(zip(names[m + 1 :], post))
+ assert len(pre) == m - 1 and len(post) == len(names) - m - 1
+ assert (
+ by_name["b_q_weight"] is mq
+ and by_name["b_scales"] is ms
+ and by_name["b_zeros_or_none"] is mz
+ )
+ assert by_name["workspace"] is ws and by_name["b_type_id"] == 7
+ assert (by_name["size_n"], by_name["size_k"], by_name["use_fp32_reduce"]) == (64, 128, True)
+ assert (
+ by_name["a_scales"] is None
+ and by_name.get("g_idx_or_none") is None
+ and by_name.get("perm_or_none") is None
+ )
+
+
@needs_gpu
@needs_ct
@needs_sm80
diff --git a/unsloth/kernels/int4_packed.py b/unsloth/kernels/int4_packed.py
index 308b876da87..33165ca3b3b 100644
--- a/unsloth/kernels/int4_packed.py
+++ b/unsloth/kernels/int4_packed.py
@@ -689,11 +689,24 @@ def _fast_args(packed, qs):
mq, sid = packed.view(K // 16, 2 * N), types[zp is not None].id
# vLLM's Python wrapper costs ~2 us per call (decode is launch bound): call the op overload.
if new:
- call = (
- torch.ops._C.marlin_gemm.default,
- (None, mq, None, ms, None, None, mz, ws, sid),
- (N, K, False, True, False),
+ op = torch.ops._C.marlin_gemm.default
+ args = _marlin_gemm_args(
+ op,
+ c_or_none = None,
+ b_q_weight = mq,
+ b_bias_or_none = None,
+ b_scales = ms,
+ b_zeros_or_none = mz,
+ workspace = ws,
+ b_type_id = sid,
+ size_n = N,
+ size_k = K,
+ is_k_full = True,
+ use_atomic_add = False,
+ use_fp32_reduce = True,
+ is_zp_float = False,
)
+ call = None if args is None else (op, *args)
else:
call = (
torch.ops._C.gptq_marlin_gemm.default,
@@ -701,17 +714,40 @@ def _fast_args(packed, qs):
(N, K, True, False, True, False),
)
key = (qs.layout, packed.device, zp is not None, qs.dtype)
- if key not in _FAST_CHECKED:
+ if call is not None and key not in _FAST_CHECKED:
g = torch.Generator(device = packed.device).manual_seed(0)
probe = torch.randn(4, K, device = packed.device, dtype = qs.dtype, generator = g)
ref = probe.float() @ int4_dequantize(packed, qs, qs.dtype).float().t()
- got = _fast_call(call, probe).float()
- _FAST_CHECKED[key] = bool(((got - ref).abs().max() <= 1e-2 * ref.abs().max() + 1e-3).item())
- call = call if _FAST_CHECKED[key] else None
+ try:
+ got = _fast_call(call, probe).float()
+ _FAST_CHECKED[key] = bool(
+ ((got - ref).abs().max() <= 1e-2 * ref.abs().max() + 1e-3).item()
+ )
+ except RuntimeError:
+ # An op signature this code does not know: keep the exact dequantize + matmul path.
+ _FAST_CHECKED[key] = False
+ call = call if call is not None and _FAST_CHECKED[key] else None
qs._fast = (packed.data_ptr(), call)
return call
+def _marlin_gemm_args(op, **values):
+ """``marlin_gemm`` (pre, post) ``size_m`` args read from its schema (changes per vLLM release); None if unknown."""
+ pre, post, seen_m = [], [], False
+ for arg in op._schema.arguments[1:]:
+ if arg.name == "size_m":
+ seen_m = True
+ continue
+ if arg.name in values:
+ value = values[arg.name]
+ elif isinstance(arg.type, torch._C.OptionalType):
+ value = None
+ else:
+ return None
+ (post if seen_m else pre).append(value)
+ return (tuple(pre), tuple(post)) if seen_m else None
+
+
def _fast_call(call, x2):
if len(call) == 4:
return call[0](x2, call[1], call[2], call[3])