Repository navigation
perf(qlinear): avoid full-size FP8 scales and lower AWQ GEMM threshold (#3984) - #3996
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3984
TL;DR
Two measured performance paths from the issue:
N x KFP32 scale on every forward. It divides the FP32 dequant buffer in place fortensor,row, and divisibleblockscales; non-divisible block shapes keep a padded/cropped expansion fallback. Output is bitwise identical for divisible shapes.torch.mmfromrows >= 640(whenfp32_accumis 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 fromREADME.md.1. The problem
Old FP8 flow: every forward expanded the scale with two
repeat_interleavecalls 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;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;Before/after mechanisms:
origin/maind18e2ba1)fp8.py:466-467tworepeat_interleavecalls;fp8.py:517weight / expandedfp8.py:520deq.view(nb, br, kb, bc).div_(scale_inv[:, None, :, None])fp8.py:463returnsscale_inv.view(-1, 1);fp8.py:517out-of-place dividefp8.py:534deq.div_(scale_inv.view(-1, 1))fp8.py:453_expanded_scale_invpads the last scale row/column and cropsgemm_awq.py:84rows > 1024gemm_awq.py:101rows > 1024 or (rows >= 640 and fp32_accum)gemm_awq.py:88torch.matmul(..., weight.to(x.dtype))gemm_awq.py:40_awq_fp32_output_mmreturns FP32; bias added in FP32, cast once2. 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;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;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 --> [*]repeat_interleaveallocations plus division outputview(...).div_()with a broadcast scalerows < 640rows >= 640,fp32_accum=Truerows > 1024,fp32_accum=False_awq_fp32_output_mmuses an FP32-inputtorch.mmbecauseout_dtypeis CUDA-only4. Change inventory
gptqmodel/nn_modules/qlinear/fp8.pytests/test_fp8_dequant_inplace.pygptqmodel/nn_modules/qlinear/gemm_awq.pytests/test_awq_fp32_output.pytests/test_awq_rank_and_empty.pytests/benchmark/benchmark_issue_3984.pydocs/fp8-dequant-scaling.md,README.md5. Tests / guarantees
test_fp8_native_dequant_is_bitwise_identical_to_old_formulationtorch.equaltest_fp8_native_dequant_matches_fp64_oraclertol=1e-6test_fp8_native_dequant_does_not_expand_the_scale_expanded_scale_invtest_fp8_block_dequant_crops_non_divisible_*test_fp8_native_dequant_cuda_is_bitwise_identicaltest_640_rows_uses_fp32_output_mmtest_639_rows_keeps_the_native_kerneltest_640_rows_adds_bias_in_fp32_before_the_casttest_awq_fp32_output_matches_fp64_oracle_and_native_accuracy6. Validation
Hardware: NVIDIA RTX 4090
GPU-d458708b-c760-bbf1-08fb-8b8803a2157d(pinned by UUID; RTX 5090 never selected) and AMD R9700HIP_VISIBLE_DEVICES=2.nvidia-smi --query-compute-appswas checked before every timing run; the 4090 was free.Accuracy (CUDA, rows=640, 256->128, group 32, fp16)
FP64 reference built from
awq_dequantize_weightsoutput:FP32 output is accuracy-neutral or better at every measured shape.
Benchmarks, CUDA 4090 (µs, transient peak MiB)
FP8 dequant (weight-only, fp16 output):
AWQ GEMM (native -> dequant + FP32 mm):
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)
Test commands and results
The 4 CUDA failures are pre-existing on
origin/maind18e2ba1(3xtest_fp8_nonfinite_source.pyexpecting a missing_apply_optional_smoother, 1xtest_model_dequant_fp8.pyneedingbitsandbytes); the ROCm run reproduces the same 3_apply_optional_smootherfailures. None touch the changed paths.Fail-before / pass-after
fp8.pytoorigin/mainand runningtests/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.gemm_awq.pytoorigin/mainand runningtests/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 passedfor the new files.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_mmuses it on CUDA and falls back to an FP32-inputtorch.mmon CPU so the unit tests run without a GPU; productionAwqGEMMLinearis CUDA-only.14336x4096is 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.rowon4096x14336is 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.max_memory_allocated - baselinearound one call, so it includes the final transposed output tensor.