Skip to content

perf(qlinear): avoid full-size FP8 scales and lower AWQ GEMM threshold (#3984) - #3996

Merged
Qubitium merged 4 commits into
mainfrom
fix/issue-3984
Nov 14, 2026
Merged

Qubitium merged 4 commits into
mainfrom
fix/issue-3984

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #3984

TL;DR

Two measured performance paths from the issue:

  1. FP8 dequantization no longer materialises a full N x K FP32 scale on every forward. It divides the FP32 dequant buffer in place for tensor, row, and divisible block scales; non-divisible block shapes keep a padded/cropped expansion fallback. Output is bitwise identical for divisible shapes.
  2. AWQ GEMM switches from the native split-K kernel to dequantise + FP32-output torch.mm from rows >= 640 (when fp32_accum is on). Bias is added in FP32 and the result is cast once. Rows below 640 stay native.

Net diff: 3 commits, 8 files. No GPTQMODEL_* switches, no retained dense weight or scale caches. Committed reference page: docs/fp8-dequant-scaling.md, linked from README.md.

1. The problem

Old FP8 flow: every forward expanded the scale with two repeat_interleave calls and then divided out of place.

flowchart TD
    A["TorchFP8Linear.forward"] --> B["dequantize_weight"]
    B --> C["_dequantize_native"]
    C --> D["_expanded_scale_inv<br/>repeat_interleave rows and columns"]
    D --> E["weight.float() / expanded_scale<br/>full-size FP32 division output"]
    E --> F["cast + transpose + contiguous"]
    D -. "row/tensor scale" .-> G["unnecessary full-size scale or second FP32 buffer"]
    classDef cost fill:#ffe8e8,stroke:#b30000,color:#111;
    class D,E,G cost;
Loading

Old AWQ flow: the native split-K kernel stayed selected through 1024 rows, and the large-M branch produced FP16 before the bias add.

flowchart TD
    A["AwqGemmFn.forward"] --> B["rows = input_rows(x)"]
    B --> C{"rows > 1024?"}
    C -- "yes" --> D["dequant + fp16-output torch.matmul"]
    C -- "no" --> E["_awq_cuda_gemm_forward<br/>native split-K"]
    D --> F["add bias after fp16 rounding"]
    E --> F
    classDef cost fill:#ffe8e8,stroke:#b30000,color:#111;
    class D,F cost;
Loading

Before/after mechanisms:

Area Before (origin/main d18e2ba1) After
FP8 block scale fp8.py:466-467 two repeat_interleave calls; fp8.py:517 weight / expanded fp8.py:520 deq.view(nb, br, kb, bc).div_(scale_inv[:, None, :, None])
FP8 row scale fp8.py:463 returns scale_inv.view(-1, 1); fp8.py:517 out-of-place divide fp8.py:534 deq.div_(scale_inv.view(-1, 1))
FP8 non-divisible block expansion could not broadcast to a smaller weight fp8.py:453 _expanded_scale_inv pads the last scale row/column and crops
AWQ threshold gemm_awq.py:84 rows > 1024 gemm_awq.py:101 rows > 1024 or (rows >= 640 and fp32_accum)
AWQ output gemm_awq.py:88 torch.matmul(..., weight.to(x.dtype)) gemm_awq.py:40 _awq_fp32_output_mm returns FP32; bias added in FP32, cast once

2. The new flow

flowchart TD
    A["TorchFP8Linear.forward"] --> B["dequantize_weight"]
    B --> C{"prefer_cpu_dequant?"}
    C -- "yes" --> D["_dequantize_cpu"]
    C -- "no" --> E["_dequantize_native"]
    E --> F["deq = weight.to(torch.float32)"]
    F --> G{"weight_scale_method"}
    G -- "tensor" --> H["deq.div_(scale_inv)"]
    G -- "row" --> I["deq.div_(scale_inv.view(-1, 1))"]
    G -- "block, divisible" --> J["deq.view(nb, br, kb, bc).div_(scale_inv[:, None, :, None])"]
    G -- "block, non-divisible" --> K["_expanded_scale_inv padded/cropped fallback"]
    H --> L["deq.to(target_dtype).transpose(0, 1).contiguous()"]
    I --> L
    J --> L
    K --> L
    classDef fallback fill:#fff4e5,stroke:#b26a00,color:#111;
    class K fallback;
Loading
flowchart TD
    A["AwqGemmFn.forward"] --> B["rows = input_rows(x)"]
    B --> C{"rows > 1024?"}
    C -- "yes" --> E["awq_dequantize_weights + _awq_fp32_output_mm"]
    C -- "no" --> D{"rows >= 640 and fp32_accum?"}
    D -- "yes" --> E
    D -- "no" --> F["_awq_cuda_gemm_forward (native split-K)"]
    E --> G["add bias in fp32, cast once to x.dtype"]
    F --> H["add bias in the native output dtype"]
    classDef native fill:#eef6ff,stroke:#1f5f9e,color:#111;
    class F,H native;
Loading

3. Failure and ambiguity contract

stateDiagram-v2
    state "FP8 weight loaded" as Loaded
    state "divisible scale" as Divisible
    state "non-divisible scale" as NonDivisible
    state "in-place divide" as InPlace
    state "padded/cropped fallback" as Fallback
    state "AWQ rows below 640" as Native
    state "AWQ rows 640 and up" as Dense
    [*] --> Loaded
    Loaded --> Divisible: tensor / row / block divides evenly
    Loaded --> NonDivisible: block shape not divisible
    Divisible --> InPlace
    NonDivisible --> Fallback
    InPlace --> [*]
    Fallback --> [*]
    [*] --> Native: rows < 640
    [*] --> Dense: rows >= 640 and fp32_accum
    Native --> [*]
    Dense --> [*]
Loading
Situation Before After
tensor / row scale out-of-place divide allocates a second full-size FP32 buffer in-place divide on the FP32 dequant buffer
block scale, divisible two repeat_interleave allocations plus division output 4-D view(...).div_() with a broadcast scale
block scale, non-divisible full expansion could not broadcast last scale row/column replicated, then cropped to the weight shape
AWQ rows < 640 native split-K native split-K (unchanged)
AWQ rows >= 640, fp32_accum=True native split-K dequant + FP32-output GEMM
AWQ rows > 1024, fp32_accum=False dequant + FP16-output matmul dequant + FP32-output GEMM (historical dispatch preserved)
AWQ CPU unit test n/a _awq_fp32_output_mm uses an FP32-input torch.mm because out_dtype is CUDA-only

4. Change inventory

Area File Purpose
FP8 dequant gptqmodel/nn_modules/qlinear/fp8.py in-place division for tensor/row/divisible block; padded/cropped fallback
FP8 tests tests/test_fp8_dequant_inplace.py bitwise identity, FP64 oracle, no-expansion property, non-divisible fallback
AWQ dispatch gptqmodel/nn_modules/qlinear/gemm_awq.py 640-row threshold, FP32-output GEMM, FP32 bias
AWQ tests tests/test_awq_fp32_output.py dispatch, FP32 bias rounding, FP64 accuracy vs native
AWQ tests tests/test_awq_rank_and_empty.py threshold expectations updated to 640 / fp32_accum opt-out
Benchmark tests/benchmark/benchmark_issue_3984.py reproducible µs / peak-MiB table for the issue shapes
Docs docs/fp8-dequant-scaling.md, README.md reference page + link

5. Tests / guarantees

Test Invariant
test_fp8_native_dequant_is_bitwise_identical_to_old_formulation in-place divide equals the old FP32 formulation with torch.equal
test_fp8_native_dequant_matches_fp64_oracle FP64 reference from the dequantized weight, rtol=1e-6
test_fp8_native_dequant_does_not_expand_the_scale divisible shapes never call _expanded_scale_inv
test_fp8_block_dequant_crops_non_divisible_* non-divisible rows/columns crop or pad correctly
test_fp8_native_dequant_cuda_is_bitwise_identical same guarantee on the CUDA/HIP device path
test_640_rows_uses_fp32_output_mm 640 rows selects dequant + FP32 GEMM
test_639_rows_keeps_the_native_kernel below 640 stays native
test_640_rows_adds_bias_in_fp32_before_the_cast bias is added before the single FP16 cast
test_awq_fp32_output_matches_fp64_oracle_and_native_accuracy FP64 reference; FP32-output error <= native

6. Validation

Hardware: NVIDIA RTX 4090 GPU-d458708b-c760-bbf1-08fb-8b8803a2157d (pinned by UUID; RTX 5090 never selected) and AMD R9700 HIP_VISIBLE_DEVICES=2. nvidia-smi --query-compute-apps was checked before every timing run; the 4090 was free.

Accuracy (CUDA, rows=640, 256->128, group 32, fp16)

FP64 reference built from awq_dequantize_weights output:

path abs rms relative rms max relative
FP32-output dequant + mm 1.0686e-02 2.08e-04 1.28e-03
native split-K 1.5124e-02 2.95e-04 2.01e-02

FP32 output is accuracy-neutral or better at every measured shape.

Benchmarks, CUDA 4090 (µs, transient peak MiB)

FP8 dequant (weight-only, fp16 output):

scale shape old us old peak new us new peak
row 4096x4096 354.8 160 285.7 128
row 4096x14336 1474.3 560 1473.5 448
block 4096x4096 496.1 224 286.6 128
block 4096x14336 1972.2 784 1470.0 448

AWQ GEMM (native -> dequant + FP32 mm):

shape M=640 M=768 M=1024 M=2048
4096x4096 256.9 -> 218.6 (1.18x) 337.4 -> 233.3 (1.45x) 473.7 -> 274.2 (1.73x) 926.4 -> 529.4 (1.75x)
4096x14336 1080.7 -> 887.7 (1.22x) 1181.4 -> 1004.0 (1.18x) 1705.4 -> 1014.1 (1.68x) 3057.2 -> 1618.4 (1.89x)
14336x4096 756.2 -> 670.4 (1.13x) 755.7 -> 752.7 (1.00x) 1006.4 -> 935.5 (1.08x) 2031.1 -> 1661.3 (1.22x)

M=1 and M=64 stay on the native kernel by dispatch; their raw dequant+mm timings are 123.8/109.6 us for 4096x4096, 460.0/524.3 us for 4096x14336, and 412.2/391.2 us for 14336x4096, versus native 23.5/37.9, 27.6/89.9, and 37.9/84.9 us. Transient peak also drops, e.g. 4096x14336 M=1024 476 -> 196 MiB and M=2048 952 -> 280 MiB; 4096x4096 M=1024 136 -> 56 MiB.

Benchmarks, ROCm R9700 (FP8 only; AWQ GEMM is CUDA-only)

scale shape old us old peak new us new peak
row 4096x4096 676.3 160 618.6 128
row 4096x14336 2701.5 560 2919.6 448
block 4096x4096 909.1 224 628.8 128
block 4096x14336 3653.2 784 2939.6 448

Test commands and results

# New regression tests, CUDA
CUDA_VISIBLE_DEVICES=GPU-d458708b-... PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python \
  -m pytest -q tests/test_fp8_dequant_inplace.py tests/test_awq_fp32_output.py
  -> 22 passed

# New regression tests, ROCm
HIP_VISIBLE_DEVICES=2 PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python \
  -m pytest -q tests/test_fp8_dequant_inplace.py tests/test_awq_fp32_output.py
  -> 21 passed, 1 skipped (native AWQ oracle is CUDA-only)

# Affected existing suites, CUDA
  test_fp8_kernel.py test_fp8.py test_fp8_bf16_scale_reload.py test_fp8_bias_range.py
  test_fp8_nonfinite_source.py test_fp8_packed_weight_lifecycle.py
  test_model_dequant_fp8.py test_rocm_fp8_gating.py test_awq_fp16_matmul_heuristic.py
  test_awq_rank_and_empty.py test_awq_fp32_output.py test_fp8_dequant_inplace.py
  test_awq_native_eval_autograd.py
  -> 396 passed, 62 skipped, 4 failed

# Affected existing suites, ROCm
  -> 390 passed, 69 skipped, 3 failed

# Lint
ruff check gptqmodel docs/eora tests setup.py
  -> All checks passed!

The 4 CUDA failures are pre-existing on origin/main d18e2ba1 (3x test_fp8_nonfinite_source.py expecting a missing _apply_optional_smoother, 1x test_model_dequant_fp8.py needing bitsandbytes); the ROCm run reproduces the same 3 _apply_optional_smoother failures. None touch the changed paths.

Fail-before / pass-after

  1. FP8 implementation property. Reverting fp8.py to origin/main and running tests/test_fp8_dequant_inplace.py: 6 failed, 9 passed (the three _expanded_scale_inv-must-not-run cases and the three non-divisible crop/pad cases). Restored: 18 passed.
  2. AWQ dispatch and FP32 bias. Reverting gemm_awq.py to origin/main and running tests/test_awq_fp32_output.py tests/test_awq_rank_and_empty.py: 5 failed, 53 passed (640-row dispatch, FP32 bias rounding, and three threshold cases). Restored: 22 passed for the new files.
  3. FP64 accuracy. Before the change the 640-row path is the native kernel (rel_rms=2.95e-04, max_rel=2.01e-02); after it is the FP32-output path (rel_rms=2.08e-04, max_rel=1.28e-03).

7. Reviewer notes

  • torch.mm(..., out_dtype=torch.float32) is a CUDA-only overload in this PyTorch build. _awq_fp32_output_mm uses it on CUDA and falls back to an FP32-input torch.mm on CPU so the unit tests run without a GPU; production AwqGEMMLinear is CUDA-only.
  • The 640-row crossover is shape-dependent on torch 2.16: 14336x4096 is break-even at M=768 (1.00x) and wins at M=1024+; the other two shapes win from M=640. The issue's 640 threshold is implemented as requested and the measured table is reported above.
  • FP8 row on 4096x14336 is memory-bound on the FP32 buffer, so its time is essentially unchanged (1474 -> 1474 us); the peak drops 560 -> 448 MiB. The block path improves both.
  • Peak MiB is max_memory_allocated - baseline around one call, so it includes the final transposed output tensor.
  • No dense weight or scale is retained on the module; the FP32 dequant buffer and the dequantized AWQ weight are local to the call.

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

Labels

None yet

Projects

None yet

1 participant