Skip to content

[BUG] Regression from #5178 (fix of #5174): FP8 per-token _scaled_mm probe runs inside forward - a cold first call under CUDA-graph capture fails and caches 'unsupported' for the process; fullgraph compile fails ('opaque object') #5195

Closed

Description

@Qubitium

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:

  1. 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 = False then caches False for the whole process, logs a misleading "_scaled_mm does 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).
  2. 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 (the torch.Generator). The class comment says forward is trace-clean and that fullgraph=True traces without a break. With a cold cache it now fails. The optimize() 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: under torch.compiler.is_compiling() or torch.cuda.is_current_stream_capturing(), do not probe. Use a decision computed earlier, or take the dense path without caching anything.
  • Never cache a False that came from an exception raised by the probing context itself (capture, OOM, tracing). Cache False only 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

  1. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-4e3a0639ecaf is 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

  2. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-4e3a0639ecaf finished this issue.

    PR: #5257 — base main, head 9b06d3323 (branch fix/5195-fp8-probe-capture), 2 files, +585/−31.

    Fix

    The W8A8 _scaled_mm capability answer is no longer measured inside forward():

    • The probe is now trace-safe (deterministic FP8 codes instead of torch.Generator, a 0-dim device-side verdict instead of a host allclose).
    • It is resolved eagerly at post_init(), set_activation_quant_method("per_token") (the documented W8A8 opt-in) and _apply() device moves.
    • Inside a capture/trace the answer is "unknown", so that call takes the correct dequantize+matmul fallback without caching; the next eager call measures normally. An OOM is also not cached; a real kernel rejection still is.
    • GPTQMODEL_FP8_SCALED_MM=auto|on|off lets a deployment skip the measurement.
    • The decoded-weight FP16 range bound is warmed in the same eager points, so capture no longer hits its device→host copy.

    Evidence

    Case pre-fix post-fix
    cold first forward inside torch.cuda.graph false warning + sticky {(False,0,fp16): False}, fast path dead capture ok, cache True, fast path after
    cold capture + cold range bound RuntimeError: Cannot copy between CPU and CUDA tensors during CUDA graph capture capture ok, 0.0 vs dense
    cold torch.compile(fullgraph=True) (inductor) Unsupported: An opaque object was created in the middle of the program traces, 4.0e-2, cache True
    opt-in then capture sticky False True at opt-in, fast path
    unsupported kernel / #5174 numeric mismatch fallback, False cached same
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