Skip to content

[BUG] GGUFTorchLinear declares SUPPORTS_GRAPH_CAPTURE=True, but every K-quant (Q4_K/Q5_K/Q6_K, the default q_k_m format) fails torch.cuda.graph capture: _u8_shift builds a host tensor per forward #5014

Closed

Description

@Qubitium

Found on main @ c2b0bf6, AMD Radeon AI PRO R9700 (gfx1201), torch 2.16 dev + ROCm 10.1. The code path is device-independent, so CUDA is affected the same way.

What is wrong

GGUFTorchLinear declares SUPPORTS_GRAPH_CAPTURE = True (gptqmodel/nn_modules/qlinear/gguf.py:1179-1184, "measured on GB10"), and docs/kernel-compile-matrix.md:82 lists it as capture-safe. That only holds for Q4_0/Q8_0. Every K-quant dense dequant path builds its shift table on the host on every forward:

# gguf.py:1431-1433
@staticmethod
def _u8_shift(values, device):
    return torch.tensor(values, dtype=torch.uint8, device=device)

It is called from the Q4_K / Q5_K / Q6_K (and Q2_K/Q3_K) block dequantizers (gguf.py:1550, 1583, 1589, 1610, 1617). So an explicit torch.cuda.graph capture of a GGUF_TORCH layer fails for every K-quant checkpoint, which includes the default GGUFConfig(bits=4, format="q_k_m") output:

File ".../qlinear/gguf.py", line 1955, in forward
    output = self._forward_dequant_matmul(x_flat, bias=bias)
File ".../qlinear/gguf.py", line 1663, in dequantize_weight
    weight = self._dequantize_q4_k_blocks(blocks, target_dtype, compute_dtype=compute_dtype)
File ".../qlinear/gguf.py", line 1550, in _dequantize_q4_k_blocks
    self._u8_shift((0, 4), device=blocks.device).view(1, 1, 1, 2, 1),
File ".../qlinear/gguf.py", line 1433, in _u8_shift
    return torch.tensor(values, dtype=torch.uint8, device=device)
RuntimeError: Cannot copy between CPU and CUDA tensors during CUDA graph capture unless the CPU tensor is pinned.

The fused K path (_forward_fused_k) only applies to M <= 32 and in*out >= 8,388,608 (utils/kernel.py:193-194). Every smaller layer, and every prefill, takes the dense path above, on CUDA and on ROCm alike. Outside capture, the same code also issues 1-3 small pageable H2D copies per layer per forward.

Repro (2-D GGUFTorchLinear, 512x256, fp16, warm-up then capture)

from gptqmodel.nn_modules.qlinear.gguf import GGUFTorchLinear
for bits in ["q4_0", "q8_0", "q4_k", "q5_k", "q6_k"]:
    lin = nn.Linear(512, 256, bias=False, dtype=torch.float32)
    mod = GGUFTorchLinear(bits=bits, group_size=-1, sym=True, desc_act=False,
                          in_features=512, out_features=256, bias=False)
    GGUFTorchLinear.pack_original(mod, lin, scales=None, zeros=None)
    mod = mod.cuda().eval(); x = torch.randn(1, 512, device="cuda", dtype=torch.float16)
    with torch.inference_mode():
        mod(x); mod(x); torch.cuda.synchronize()
        g = torch.cuda.CUDAGraph()
        with torch.cuda.graph(g):
            y = mod(x)

Output (M=1 and M=64 give the same result):

declared SUPPORTS_GRAPH_CAPTURE = True
q4_0 capture OK, replay == eager
q8_0 capture OK, replay == eager
q4_k capture FAIL RuntimeError Cannot copy between CPU and CUDA tensors during CUDA graph capture ...
q5_k capture FAIL RuntimeError ...
q6_k capture FAIL RuntimeError ...

A real Qwen2.5-0.5B-Instruct q_k_m checkpoint loaded with BACKEND.GGUF_TORCH fails the same way on layers.0.mlp.down_proj (fp16 and bf16, M=1 and M=16). GGUF_TRITON on the same checkpoint captures, and replay is bitwise equal to eager.

Suggested fix

Cache the shift tables per device, for example a module-level functools.lru_cache keyed by (values, device), or a registered non-persistent buffer. Alternatively build them with torch.arange(...), which needs no host copy and captures fine. That also removes the per-forward H2D copies. If that is not done, declare SUPPORTS_GRAPH_CAPTURE per qtype or set it to False.

Test that should have caught it

tests/kernels/test_kernel_compile_levels_quant.py::_build_gguf_torch (line 143) only builds q4_0, which is the one family without _u8_shift. The capture probe should also run on at least one K-quant (q4_k), since that is the default GGUF quantize format.

Severity

Low-medium. The declared capability, and the documented matrix entry, are wrong for the default GGUF format. Any caller that captures a GGUF_TORCH model based on the declaration crashes.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions