Repository navigation
Description
Found on main @ 396b5f0, RTX 4090 (sm_89), torch 2.15 + CUDA 13.2.
What is wrong
#5178 (the fix for #5174) added _scaled_mm_row_scale_supported() (gptqmodel/nn_modules/qlinear/fp8.py:46-103) and calls it from TorchFP8Linear._can_use_scaled_mm(), which means from inside forward(). On a cold process cache, the first per-token (W8A8) forward runs the probe: it creates a torch.Generator, makes random FP8 tensors, calls _scaled_mm, and ends with bool(torch.allclose(...)), which syncs device to host. That probe now runs in whatever context the first forward happens in:
- First forward inside a CUDA-graph capture. The class declares
SUPPORTS_GRAPH_CAPTURE = True. The host sync is illegal during capture, so the probe raises.except Exception: supported = Falsethen cachesFalsefor the whole process, logs a misleading "_scaled_mmdoes not compute the row-wise scale contract on this device", and the capture itself fails (RuntimeError: Cannot copy between CPU and CUDA tensors during CUDA graph capture...). Every later per-token forward in the process stays on the slower dequantize + matmul path, even on hardware where the native kernel is correct (sm_89 here). - First forward under
torch.compile(fullgraph=True)/model.optimize(fullgraph=True). Dynamo cannot trace the probe:Unsupported: An opaque object was created in the middle of the program(thetorch.Generator). The class comment says forward is trace-clean and thatfullgraph=Truetraces without a break. With a cold cache it now fails. Theoptimize()fullgraph contract is fail-closed, so the model forward raises.
Before #5178, both cases worked on the same checkpoint (A/B below).
Repro
Qwen2.5-0.5B-Instruct, 4 layers, FP8Config() (row weight scales), loaded with BACKEND.FP8_TORCH, W8A8 opted in as docs/fp8-weight-only-dispatch.md documents. Each case runs in a fresh process.
import torch
from gptqmodel import GPTQModel, BACKEND
import gptqmodel.nn_modules.qlinear.fp8 as F
m = GPTQModel.load(ckpt, backend=BACKEND.FP8_TORCH, dtype=torch.float16, device="cuda:0")
for mod in m.model.modules():
if isinstance(mod, F.TorchFP8Linear): mod.set_activation_quant_method("per_token")
lin = m.model.model.layers[0].mlp.down_proj
x = torch.randn(8, lin.in_features, dtype=torch.float16, device="cuda")
# case 1: capture the first call
g = torch.cuda.CUDAGraph(); s = torch.cuda.Stream()
with torch.inference_mode(), torch.cuda.stream(s), torch.cuda.graph(g):
y = lin(x)
# case 2 (fresh process): torch.compile(lin, fullgraph=True)(x) or m.optimize(fullgraph=True); m.model(ids)Output on main:
case 1: WARN FP8: `torch._scaled_mm` does not compute the row-wise scale contract on this device for `float16` output; per-token (W8A8) layers use the dequantize + matmul path.
capture FAILED RuntimeError Cannot copy between CPU and CUDA tensors during CUDA graph capture unless the CPU tensor is pinned.
_SCALED_MM_ROW_SCALE_SUPPORT == {(False, 0, torch.float16): False}
later eager forward: _can_use_scaled_mm() == False for the rest of the process
case 2: compile FAILED Unsupported An opaque object was created in the middle of the program.
optimize(fullgraph=True) + model forward: FAILED Unsupported (same message)
eager first call (control): {(False, 0, torch.float16): True} (the kernel is correct on this GPU)
warm eager call, then capture (control): capture ok, output == eager
On 43b7d62a8^ (parent of #5178), with the same script and checkpoint: capture ok and compile ok (fullgraph=True).
Suggested fix
- Do not run the probe from inside
forward. Run it eagerly when the module is set up:post_init(),set_activation_quant_method("per_token"), or the first eager call outside capture and tracing. - Inside
forward, if the answer is not cached: undertorch.compiler.is_compiling()ortorch.cuda.is_current_stream_capturing(), do not probe. Use a decision computed earlier, or take the dense path without caching anything. - Never cache a
Falsethat came from an exception raised by the probing context itself (capture, OOM, tracing). CacheFalseonly for a real numerical mismatch or a real kernel rejection.
Test that should have caught it
tests/kernels/test_fp8_kernel.py per-token tests always warm the cache eagerly first. The compile-level and capture probes (tests/kernels/test_kernel_compile_levels_quant.py-style) should build a per-token TorchFP8Linear in a process with an empty _SCALED_MM_ROW_SCALE_SUPPORT, then (a) capture the first call and (b) compile it with fullgraph=True.
Severity
Medium. W8A8 is opt-in. A cold-cache first call under capture or fullgraph compile, which is the class's declared capability, now fails. The capture case also silently and permanently disables the W8A8 kernel for the process, and logs a false hardware diagnosis.
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 18, 2026
Agent claim:
agent-4e3a0639ecafis working on this issue now.Started 2026-11-18T13:35:18Z. The claim expires 4.0h after this comment is last edited (heartbeat). Other agents: do not start this issue while the claim is live.
Scope: make the FP8 per-token _scaled_mm probe capture-safe: no device probe inside forward during CUDA-graph capture, and a capture-time failure must not cache 'unsupported' for the process; fullgraph compile must work
Qubitium commentedon Nov 18, 2026
Agent claim released:
agent-4e3a0639ecaffinished this issue.PR: #5257 — base
main, head9b06d3323(branchfix/5195-fp8-probe-capture), 2 files, +585/−31.Fix
The W8A8
_scaled_mmcapability answer is no longer measured insideforward():torch.Generator, a 0-dim device-side verdict instead of a hostallclose).post_init(),set_activation_quant_method("per_token")(the documented W8A8 opt-in) and_apply()device moves.GPTQMODEL_FP8_SCALED_MM=auto|on|offlets a deployment skip the measurement.Evidence
torch.cuda.graph{(False,0,fp16): False}, fast path deadTrue, fast path afterRuntimeError: Cannot copy between CPU and CUDA tensors during CUDA graph capturetorch.compile(fullgraph=True)(inductor)Unsupported: An opaque object was created in the middle of the programTrueFalseTrueat opt-in, fast pathFalsecachedtests/kernels/test_fp8_probe_5195.py: 15 passed (11 fail pre-fix).tests/test_fp8*.py: 109 passed, 1 skipped; FP8 kernel families 127 passed, 6 skipped; compile family 161 passed/177 skipped/3 failed, and all 3 fail identically on pristinemain(unrelated: FP8 quantize removal + two NF12 CPU cases).scale_a), ROCm_scaled_mm, and a full Qwen2.5-0.5Boptimize()run (checkpoint not in the offline cache).fix(fp8): resolve the W8A8 `_scaled_mm` contract outside forward (#5195…