Skip to content

[PERF] FP8 dequant materializes full-size fp32 scales every forward (-45% time/-57% memory, bitwise); AWQ GEMM native-kernel threshold too high (1.6x at M=1024) #3984

Closed

Description

@Qubitium

Perf audit of main @ 93d3ae7 on RTX 4090, torch 2.15.

1. FP8 dequant (default row scale, and block scale) builds full-size fp32 scale tensors on every forward — bitwise-safe

qlinear/fp8.py:466-467 (_expanded_scale_inv) expands a block scale to an N×K fp32 tensor with two repeat_interleave. fp8.py:515-519 (_dequantize_native) then computes weight.float() / expanded.

Row scale is the default, and _scaled_mm only covers tensor scale. So the default path dequantizes the whole weight on every forward, including decode, and pays for these temporaries each time.

Dequant time and transient peak, before → after:

scale shape time (µs) peak (MiB)
block 128×128 4096² 494.6 → 254.0 224 → 96
block 128×128 4096×14336 1968 → 1459 784 → 336
row 4096² 356 → 269 160 → 96
row 4096×14336 1463 → 1459 560 → 336

At the module level, the M=1 forward (row, 4096²) goes from 394 to 308 µs. I re-ran the block 4096² case independently: 498 → 270 µs, 224 → 128 MiB, bitwise equal.

Equivalence: 72 cases (row/block/tensor × 2 shapes × bias × fp16/bf16, comparing the dequantized weight and the forward at M ∈ {1, 64}), 0 mismatches. FP8 → FP32 is exact and the per-element division is unchanged.

deq = weight.to(torch.float32)
if method == "block":
    deq.view(n // br, br, k // bc, bc).div_(scale_inv[:, None, :, None])
else:
    deq.div_(scale_inv if method == "tensor" else scale_inv.view(-1, 1))
return deq.to(target_dtype).transpose(0, 1).contiguous()

Shapes that aren't block-divisible need the existing padded/cropped expansion as a fallback. Dividing the FP8 tensor in place isn't possible, because torch doesn't promote FP8.

2. AWQ GEMM keeps the slower native split-K kernel for M in [640, 1024] — not bitwise, accuracy-neutral

qlinear/gemm_awq.py:84 only switches from the native kernel to dequant + GEMM when rows > 1024.

fp16 module forward, GPU µs, native → dequant + torch.mm(out_dtype=fp32):

shape M=640 M=768 M=1024
4096² 280 → 234 350 → 255 487 → 300 (1.6×)
4096×14336 1074 → 921 1269 → 1030 1685 → 1070 (1.57×)
14336×4096 751 → 731 897 → 764 1160 → 976
  • The transient peak also drops (476 → 196 MiB at 4096×14336, M=1024), because there is no split-K fp32 workspace.
  • At M ≤ 512 the native kernel wins, so leave that range as it is.

Accuracy: against an fp64 reference with the same dequantized weight, RMS error matches the native kernel within ~0.6% (3.9148e-4 vs 3.9172e-4; 1.2285e-3 vs 1.2268e-3). Max-relative error is 3.6–4.0e-4, against native's 3.6–4.4e-4.

This requires an fp32-output GEMM. Today's rows > 1024 branch uses an fp16-output torch.matmul, which is measurably less accurate (rms 4.59e-4 vs 3.92e-4). That ties in with #3977 item 3, where the same branch adds the bias after fp16 rounding.

elif rows >= 640 and fp32_accum:
    w = awq_dequantize_weights(qweight, scales, qzeros, 0, 0, 0, False).to(x.dtype)
    out = torch.mm(x_2d, w, out_dtype=torch.float32)  # add bias here in fp32, then cast

Moving the existing M > 1024 branch to fp32 output too improves precision, at a cost of 3–20% at M ≥ 2048.

Unmeasured / needs design

  • GGUF Triton keeps both weight copies resident. The original qweight sits alongside the reorganised _gguf_triton_cache: Q4_K 4096² is 9.0 + 10.0 MiB, Q6_K 13.1 + 14.0 MiB, so ~2.1× weight memory. Dropping qweight breaks state_dict and tests/kernels/test_gguf_triton_cache_invalidation.py, so this needs an opt-in or a rebuild-on-save design.
  • GGUF Triton bias epilogue: 2 tiny kernels raise M=1 wall from 31 to 44 µs (q4_k); the bias could be fused into the Triton epilogue.
  • W4A NVFP4: w4a_activation.py:144 and w4a_boundary.py:115 force 2 host syncs per activation pack (bool(isfinite(...)), bool(> 0)). Needs Blackwell hardware to measure.
  • CPU TorchFused / TorchAten: they keep a full packed snapshot (capture_fused_train_state) alongside the int4pack copy, which is 2.08× resident (8.3 → 17.3 MiB per 4096² layer). It is documented as intentional for train() / save; rebuilding it lazily from int4pack would avoid it.

🤖 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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions