Skip to content

Conversation

hebiao064
Copy link
Collaborator

@hebiao064 hebiao064 commented Jul 29, 2025

File Changes

  • /workspace/sglang/sgl-kernel/benchmark/bench_per_token_quant_fp8.py
  • /workspace/sglang/sgl-kernel/csrc/gemm/per_token_quant_fp8.cu

All other compilation file changes will be reverted, I did it to accelerate my development process

Motivation

First of all, I updated the benchmark to include torch's quant implementation, which surfaced that both vllm and sglang quant kernel is not very accurate as torch, but I do feel it its acceptable, since vllm and sglang both delivered similar quantization result.

Secondly, I modified the kernel to allow hidden dim like 1368 which will fail due to #8460, not it's solved.

E2E Test

TP 4:

Accuracy: 0.940
Invalid: 0.000
Latency: 17.850 s
Output throughput: 1098.818 token/s

TP 8: (before this pr, TP8 will fail)
Need to use USE_VLLM_CUTLASS_W8A8_FP8_KERNEL since our cutlass fp8 doesn't support
RuntimeError: mat_a must be multiple of 16 bytes for memory alignment

Accuracy: 0.945
Invalid: 0.000
Latency: 15.905 s
Output throughput: 1248.437 token/s

Before this PR:

Hidden Dim 1368: RuntimeError: Hidden dimension must be divisible by 16, but got 1368

=== Comparison for hidden_dim=2048 ===
Scale differences:
  Torch vs VLLM:   0.00000127
  Torch vs SGLang: 0.00000049
  VLLM vs SGLang:  0.00000092
Output differences:
  Torch vs VLLM:   0.10605328
  Torch vs SGLang: 0.10605328
  VLLM vs SGLang:  0.00000000
Matches (rtol=0.001, atol=1e-05):
  Torch vs VLLM:   ❌
  Torch vs SGLang: ❌
  VLLM vs SGLang:  ✅

=== Comparison for hidden_dim=4096 ===
Scale differences:
  Torch vs VLLM:   0.00000080
  Torch vs SGLang: 0.00000052
  VLLM vs SGLang:  0.00000031
Output differences:
  Torch vs VLLM:   0.11732540
  Torch vs SGLang: 0.11732540
  VLLM vs SGLang:  0.00000000
Matches (rtol=0.001, atol=1e-05):
  Torch vs VLLM:   ❌
  Torch vs SGLang: ❌
  VLLM vs SGLang:  ✅

============================================================
Starting performance benchmark...
per-token-dynamic-quant-fp8-performance:
    batch_size  seq_len  hidden_dim  Torch Reference         VLLM   SGL Kernel
0         16.0     64.0      2048.0        49.376000    15.776001    15.776001
1         16.0     64.0      4096.0        67.648001    20.864001    19.264000
2         16.0    128.0      2048.0        62.080000    21.952000    20.576000
3         16.0    128.0      4096.0        95.551997    32.800000    27.807999
4         16.0    256.0      2048.0        90.559997    34.752000    28.448001
5         16.0    256.0      4096.0       182.239994    58.176000    49.120001
6         16.0    512.0      2048.0       173.536003    63.327998    50.783999
7         16.0    512.0      4096.0       316.960007   104.128003   109.632000
8         16.0   1024.0      2048.0       323.103994   114.239998    99.296004
9         16.0   1024.0      4096.0       599.776030   195.999995   210.784003
10        16.0   2048.0      2048.0       606.271982   215.616003   189.903997
11        16.0   2048.0      4096.0      1152.944028   377.472013   404.320002
12        16.0   4096.0      2048.0      1166.960001   416.608006   376.704007
13        16.0   4096.0      4096.0      2266.863942   746.799976   797.439992
14        32.0     64.0      2048.0        61.951999    21.952000    19.552000
15        32.0     64.0      4096.0        95.232002    32.768000    28.704001
16        32.0    128.0      2048.0        89.631997    34.655999    26.880000
17        32.0    128.0      4096.0       181.456000    58.208000    49.984001
18        32.0    256.0      2048.0       174.000002    63.183997    49.632002
19        32.0    256.0      4096.0       316.383988   103.840001   109.600000
20        32.0    512.0      2048.0       322.576001   113.920003    99.296004
21        32.0    512.0      4096.0       598.847985   195.519999   210.304007
22        32.0   1024.0      2048.0       604.704022   215.839997   189.344004
23        32.0   1024.0      4096.0      1153.232038   377.568007   406.623989
24        32.0   2048.0      2048.0      1167.199969   416.575998   378.176004
25        32.0   2048.0      4096.0      2267.087936   745.280027   797.536016
26        32.0   4096.0      2048.0      2296.047926   823.328018   756.735981
27        32.0   4096.0      4096.0      4488.671780  1476.575971  1577.455997
28        64.0     64.0      2048.0        89.711998    34.784000    27.392000
29        64.0     64.0      4096.0       182.287998    58.143999    48.767999
30        64.0    128.0      2048.0       174.112007    63.327998    50.655998
31        64.0    128.0      4096.0       315.647990   103.840001   109.600000
32        64.0    256.0      2048.0       322.495997   113.920003    99.264003
33        64.0    256.0      4096.0       598.752022   195.519999   210.304007
34        64.0    512.0      2048.0       604.896009   215.519994   188.960001
35        64.0    512.0      4096.0      1153.232038   377.535999   404.864013
36        64.0   1024.0      2048.0      1167.583942   416.927993   375.616014
37        64.0   1024.0      4096.0      2267.311931   746.815979   797.695994
38        64.0   2048.0      2048.0      2296.800017   824.687988   756.352007
39        64.0   2048.0      4096.0      4489.823818  1477.920055  1577.536047
40        64.0   4096.0      2048.0      4549.664021  1634.064019  1504.271984
41        64.0   4096.0      4096.0      8928.223610  2939.167976  3138.047934
42       128.0     64.0      2048.0       173.184007    63.167997    49.408000
43       128.0     64.0      4096.0       315.647990   103.904001   108.704001
44       128.0    128.0      2048.0       322.528005   113.920003   100.383997
45       128.0    128.0      4096.0       598.768026   195.552006   211.776003
46       128.0    256.0      2048.0       605.632007   215.552002   191.264004
47       128.0    256.0      4096.0      1154.207945   377.696007   405.008003
48       128.0    512.0      2048.0      1167.423964   416.864008   376.255989
49       128.0    512.0      4096.0      2267.567992   746.847987   797.504008
50       128.0   1024.0      2048.0      2296.687961   824.959993   756.640017
51       128.0   1024.0      4096.0      4490.496159  1477.823973  1577.520013
52       128.0   2048.0      2048.0      4549.248219  1633.967996  1503.872037
53       128.0   2048.0      4096.0      8930.080414  2940.608025  3139.199972
54       128.0   4096.0      2048.0      9051.616192  3251.071930  2991.216063
55       128.0   4096.0      4096.0     17895.647049  5863.103867  6262.495995

After the PR

=== Comparison for hidden_dim=1368 ===
Scale differences:
  Torch vs VLLM:   0.00000178
  Torch vs SGLang: 0.00000048
  VLLM vs SGLang:  0.00000151
Output differences:
  Torch vs VLLM:   0.09933983
  Torch vs SGLang: 0.09933983
  VLLM vs SGLang:  0.00000000
Matches (rtol=0.01, atol=1e-05):
  Torch vs VLLM:   ❌
  Torch vs SGLang: ❌
  VLLM vs SGLang:  ✅

=== Comparison for hidden_dim=2048 ===
Scale differences:
  Torch vs VLLM:   0.00000128
  Torch vs SGLang: 0.00000049
  VLLM vs SGLang:  0.00000093
Output differences:
  Torch vs VLLM:   0.10555448
  Torch vs SGLang: 0.10555448
  VLLM vs SGLang:  0.00000000
Matches (rtol=0.001, atol=1e-05):
  Torch vs VLLM:   ❌
  Torch vs SGLang: ❌
  VLLM vs SGLang:  ✅

=== Comparison for hidden_dim=4096 ===
Scale differences:
  Torch vs VLLM:   0.00000079
  Torch vs SGLang: 0.00000052
  VLLM vs SGLang:  0.00000029
Output differences:
  Torch vs VLLM:   0.11747192
  Torch vs SGLang: 0.11747192
  VLLM vs SGLang:  0.00000000
Matches (rtol=0.001, atol=1e-05):
  Torch vs VLLM:   ❌
  Torch vs SGLang: ❌
  VLLM vs SGLang:  ✅

============================================================
Starting performance benchmark...
per-token-dynamic-quant-fp8-performance:
    batch_size  seq_len  hidden_dim  Torch Reference         VLLM   SGL Kernel
0         16.0     64.0      1368.0        44.383999    15.744001    15.328000
1         16.0     64.0      2048.0        51.456001    16.096000    14.976000
2         16.0     64.0      4096.0        68.544000    20.992000    20.191999
3         16.0    128.0      1368.0        53.440001    21.120001    19.296000
4         16.0    128.0      2048.0        63.135996    22.016000    19.471999
5         16.0    128.0      4096.0        97.888000    33.216000    29.023999
6         16.0    256.0      1368.0        69.983996    32.832000    35.872001
7         16.0    256.0      2048.0        91.328003    35.135999    28.640000
8         16.0    256.0      4096.0       183.487996    58.336001    50.112002
9         16.0    512.0      1368.0       119.680002    60.192000    63.135996
10        16.0    512.0      2048.0       175.392002    63.840002    50.880000
11        16.0    512.0      4096.0       317.216009   104.383998   109.792002
12        16.0   1024.0      1368.0       230.816007   127.967998   116.159998
13        16.0   1024.0      2048.0       323.103994   114.464000   100.639999
14        16.0   1024.0      4096.0       599.712014   195.391998   210.848004
15        16.0   2048.0      1368.0       422.656000   240.927994   214.880005
16        16.0   2048.0      2048.0       605.632007   214.944005   189.824000
17        16.0   2048.0      4096.0      1154.160023   378.352001   406.208009
18        16.0   4096.0      1368.0       804.159999   467.359990   410.640001
19        16.0   4096.0      2048.0      1169.535995   417.535990   378.048003
20        16.0   4096.0      4096.0      2268.192053   747.135997   797.472000
21        32.0     64.0      1368.0        53.408001    21.120001    17.952001
22        32.0     64.0      2048.0        62.912002    21.984000    20.479999
23        32.0     64.0      4096.0        97.631998    33.119999    29.247999
24        32.0    128.0      1368.0        69.920003    32.736000    34.784000
25        32.0    128.0      2048.0        91.072001    35.039999    28.896000
26        32.0    128.0      4096.0       182.239994    58.336001    50.528001
27        32.0    256.0      1368.0       118.752003    59.904002    62.752001
28        32.0    256.0      2048.0       174.255997    63.712001    50.175998
29        32.0    256.0      4096.0       316.783994   104.064003   109.760001
30        32.0    512.0      1368.0       229.984000   127.616003   116.351999
31        32.0    512.0      2048.0       322.111994   114.143997   100.383997
32        32.0    512.0      4096.0       598.111987   195.424005   210.559994
33        32.0   1024.0      1368.0       423.359990   240.704000   214.368001
34        32.0   1024.0      2048.0       604.896009   215.072006   190.144002
35        32.0   1024.0      4096.0      1155.247986   378.288001   404.960006
36        32.0   2048.0      1368.0       803.680003   467.456013   410.912007
37        32.0   2048.0      2048.0      1168.640018   417.584002   377.983987
38        32.0   2048.0      4096.0      2268.015981   747.135997   797.407985
39        32.0   4096.0      1368.0      1564.447999   921.216011   802.448004
40        32.0   4096.0      2048.0      2297.312021   823.808014   756.479979
41        32.0   4096.0      4096.0      4490.816116  1476.063967  1575.871944
42        64.0     64.0      1368.0        69.311999    32.800000    35.039999
43        64.0     64.0      2048.0        91.855999    35.039999    28.640000
44        64.0     64.0      4096.0       183.264002    58.304001    50.560001
45        64.0    128.0      1368.0       119.023997    59.904002    61.696000
46        64.0    128.0      2048.0       174.303994    63.584000    49.984001
47        64.0    128.0      4096.0       317.375988   104.064003   109.952003
48        64.0    256.0      1368.0       229.791999   127.680004   116.031997
49        64.0    256.0      2048.0       322.704002   114.239998    99.679999
50        64.0    256.0      4096.0       599.135995   195.360005   210.623994
51        64.0    512.0      1368.0       423.424006   240.927994   214.688003
52        64.0    512.0      2048.0       605.184019   214.975998   189.488001
53        64.0    512.0      4096.0      1154.495955   378.271997   405.936003
54        64.0   1024.0      1368.0       804.416001   467.296004   410.463989
55        64.0   1024.0      2048.0      1169.407964   417.376012   378.143996
56        64.0   1024.0      4096.0      2267.567992   745.664001   797.215998
57        64.0   2048.0      1368.0      1564.880013   921.184003   803.551972
58        64.0   2048.0      2048.0      2296.671987   824.576020   756.336004
59        64.0   2048.0      4096.0      4490.367889  1477.280021  1576.000035
60        64.0   4096.0      1368.0      3079.776049  1826.272011  1586.272001
61        64.0   4096.0      2048.0      4547.872066  1632.048011  1503.136039
62        64.0   4096.0      4096.0      8930.239677  2939.935923  3138.751984
63       128.0     64.0      1368.0       119.808003    59.935998    61.983999
64       128.0     64.0      2048.0       175.136000    63.648000    49.984001
65       128.0     64.0      4096.0       317.472011   104.032002   109.984003
66       128.0    128.0      1368.0       229.984000   127.680004   116.047997
67       128.0    128.0      2048.0       322.704002   114.207998   100.383997
68       128.0    128.0      4096.0       598.367989   195.360005   210.848004
69       128.0    256.0      1368.0       422.656000   240.672007   214.688003
70       128.0    256.0      2048.0       605.664015   215.136006   190.047994
71       128.0    256.0      4096.0      1154.495955   378.095999   405.984014
72       128.0    512.0      1368.0       804.048002   467.103988   410.111994
73       128.0    512.0      2048.0      1169.216037   417.311996   377.279997
74       128.0    512.0      4096.0      2267.488003   746.976018   797.248006
75       128.0   1024.0      1368.0      1565.472007   921.248019   803.248018
76       128.0   1024.0      2048.0      2296.815991   823.935986   755.807996
77       128.0   1024.0      4096.0      4489.888191  1476.960003  1575.984001
78       128.0   2048.0      1368.0      3080.512047  1826.416016  1584.064007
79       128.0   2048.0      2048.0      4548.096180  1632.528007  1503.167987
80       128.0   2048.0      4096.0      8930.784225  2940.479994  3138.720036
81       128.0   4096.0      1368.0      6115.839958  3640.095949  3151.936054
82       128.0   4096.0      2048.0      9051.023960  3252.608061  2991.535902
83       128.0   4096.0      4096.0     17893.791199  5863.535881  6265.535831

To be added

Modifications

Accuracy Test

Benchmark & Profiling

Checklist

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @hebiao064, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request aims to enhance the per_token_quant_fp8 CUDA kernel by extending its compatibility to hidden dimensions divisible by 8, rather than being strictly limited to those divisible by 16. It introduces a new 8-element vectorized kernel path and dynamically selects the appropriate kernel based on the input tensor's dimensions. To ensure correctness and facilitate development, a pure PyTorch reference implementation has been added to the benchmarking suite, allowing for direct verification against the optimized CUDA kernels. The build system has also been temporarily streamlined to focus exclusively on this kernel, indicating an ongoing development effort.

Highlights

  • Expanded per_token_quant_fp8 Support: The per_token_quant_fp8 CUDA kernel now supports input tensors where the hidden dimension is divisible by 8, not just 16. This is achieved by introducing a new 8-element vectorized kernel path and dynamically selecting the appropriate kernel based on the hidden_dim.
  • Enhanced Benchmarking and Verification: A pure PyTorch reference implementation for per-token FP8 quantization has been added to the benchmark script. This allows for direct correctness comparison and more comprehensive performance analysis against the VLLM and SGLang kernel implementations across various hidden_dim values.
  • Streamlined Development Environment: The build configuration (CMakeLists.txt) and Python bindings (common_extension.cc, __init__.py) have been temporarily stripped down to focus solely on the per_token_quant_fp8 kernel. This facilitates isolated development, debugging, and benchmarking of this specific feature.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This PR adds support for hidden_dim values that are multiples of 8 in the per_token_quant_fp8 kernel. The changes include updating the CUDA kernel, modifying build files, and extending the benchmark. Critical issues related to commented-out code in build files and Python packages need to be resolved. A potential correctness issue in the benchmark reference implementation and opportunities to improve the CUDA code's portability and maintainability were also identified.

@hebiao064 hebiao064 changed the title [Not Ready for Review] fix per token hidden 16 [Not Ready for Review] fix per token cuda kernel hidden dim cannot divide by 16 Jul 29, 2025
@hebiao064 hebiao064 marked this pull request as ready for review July 29, 2025 21:47
@hebiao064 hebiao064 changed the title [Not Ready for Review] fix per token cuda kernel hidden dim cannot divide by 16 [Not Ready for merge] fix per token cuda kernel hidden dim cannot divide by 16 Jul 29, 2025
@BBuf
Copy link
Collaborator

BBuf commented Jul 30, 2025

Clean code and add modify sgl-kernel test_per_token_quant_fp8.py.

Copy link
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@hebiao064 hebiao064 changed the title [Not Ready for merge] fix per token cuda kernel hidden dim cannot divide by 16 fix per token cuda kernel hidden dim cannot divide by 16 Jul 31, 2025
Copy link
Collaborator

@BBuf BBuf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@hebiao064
Copy link
Collaborator Author

Need to use USE_VLLM_CUTLASS_W8A8_FP8_KERNEL since our cutlass fp8 doesn't support
RuntimeError: mat_a must be multiple of 16 bytes for memory alignment
cc @BBuf

@hebiao064 hebiao064 added the ready-to-merge The PR is ready to merge after the CI is green. label Aug 1, 2025
@hebiao064 hebiao064 merged commit db7343c into main Aug 1, 2025
50 of 55 checks passed
@hebiao064 hebiao064 deleted the bhe/fix_per_token_hidden_16 branch August 1, 2025 16:27
@hebiao064
Copy link
Collaborator Author

@hebiao064
Copy link
Collaborator Author

USE_VLLM_CUTLASS_W8A8_FP8_KERNEL

Fixed by #9093

narutolhy pushed a commit to narutolhy/sglang that referenced this pull request Aug 17, 2025
narutolhy pushed a commit to narutolhy/sglang that referenced this pull request Aug 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-to-merge The PR is ready to merge after the CI is green.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants