-
Notifications
You must be signed in to change notification settings - Fork 5k
Refactor tuning block wise kernel and opt Qwen/Qwen3-VL-32B-Instruct-FP8 #14141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6f48bbd
add moe_wna16_marlin_gemm_v2
BBuf eeea208
Revert "add moe_wna16_marlin_gemm_v2"
BBuf 83b3a76
Merge branch 'main' of github.com:sgl-project/sglang
BBuf 51d1a9e
upd
BBuf b21d244
upd
BBuf 93d07c3
upd
BBuf 4bb5508
revert
BBuf 37bdafe
Merge branch 'main' into refactor_tuning_block_wise_kernel
BBuf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # W8A8 Block-wise Quantization Kernel Tuning | ||
|
|
||
| Auto-tune Triton FP8/INT8 block-wise quantization kernels for optimal performance. | ||
|
|
||
| ## When to Use Triton FP8 Block-wise Quantization Kernel vs DeepGEMM | ||
|
|
||
| **Use Triton FP8 Block-wise Quantization Kernel when:** | ||
| - Output dtype is NOT `bfloat16` (e.g., `float16`, `float32`) | ||
| - DeepGEMM is disabled (environment variable `SGLANG_ENABLE_JIT_DEEPGEMM=0`) | ||
| - Running on GPUs with compute capability < SM90 (DeepGEMM requires SM90+) | ||
| - You need cross-platform compatibility (Triton works on both NVIDIA and AMD GPUs) | ||
|
|
||
| **Use DeepGEMM when:** | ||
| - Output dtype is `bfloat16` AND DeepGEMM is enabled | ||
| - Running on NVIDIA GPUs with compute capability >= SM90 (e.g., H100, H200) | ||
| - Need maximum performance for production workloads (DeepGEMM is highly optimized for Hopper architecture) | ||
|
|
||
| **Note:** DeepGEMM requires CUDA compute capability >= 9.0 (SM90+). It is specifically optimized for NVIDIA Hopper GPUs (H100/H200). | ||
|
|
||
| The kernel selection logic in SGLang automatically chooses DeepGEMM when conditions are met (see `w8a8_block_fp8_matmul` function in `fp8_kernel.py`), otherwise falls back to Triton implementation. | ||
|
|
||
| ## Quick Start | ||
|
|
||
| **Default (DeepSeek-V3):** | ||
| ```bash | ||
| python benchmark/kernels/quantization/tuning_block_wise_kernel.py --tp-size 8 | ||
| ``` | ||
|
|
||
| **Custom Model (specify N and K):** | ||
| ```bash | ||
| python benchmark/kernels/quantization/tuning_block_wise_kernel.py --N 5120 --K 25600 | ||
| ``` | ||
|
|
||
| ## Parameters | ||
|
|
||
| - `--N`, `--K`: Weight matrix dimensions (N=output_dim, K=input_dim). If not specified, uses `--tp-size` for DeepSeek-V3 | ||
| - `--tp-size`: Tensor parallelism size for DeepSeek-V3 (default: 8) | ||
| - `--input-type`: `fp8` or `int8` (default: fp8) | ||
| - `--block-n`, `--block-k`: Block quantization granularity (default: 128) | ||
| - `--batch-size`: Test single batch size (optional) | ||
|
|
||
| ## How to Calculate N and K | ||
|
|
||
| For a linear layer `y = xW^T` where `x` is (M, K) and `W` is (N, K): | ||
| - **N**: Output features (weight matrix output dimension) | ||
| - **K**: Input features (weight matrix input dimension) | ||
|
|
||
| **Example: Qwen3-VL-32B** (hidden_size=5120, intermediate_size=25600, num_heads=64, num_kv_heads=8, head_dim=128) and TP=1 | ||
| ```bash | ||
| # QKV projection: Q(8192) + K(1024) + V(1024) = 10240 | ||
| python benchmark/kernels/quantization/tuning_block_wise_kernel.py --N 10240 --K 5120 | ||
|
|
||
| # MLP gate+up (SwiGLU): 2 * intermediate_size = 51200 | ||
| python benchmark/kernels/quantization/tuning_block_wise_kernel.py --N 51200 --K 5120 | ||
|
|
||
| # MLP down projection | ||
| python benchmark/kernels/quantization/tuning_block_wise_kernel.py --N 5120 --K 25600 | ||
|
|
||
| # O projection (if separate from QKV) | ||
| python benchmark/kernels/quantization/tuning_block_wise_kernel.py --N 5120 --K 8192 | ||
| ``` | ||
|
|
||
| If TP=8: | ||
|
|
||
| ```bash | ||
| # QKV projection: Q(8192) + K(1024) + V(1024) = 10240 / TP=8 | ||
| python benchmark/kernels/quantization/tuning_block_wise_kernel.py --N 1280 --K 5120 | ||
|
|
||
| # MLP gate+up (SwiGLU): 2 * intermediate_size = 51200 / TP=8 | ||
| python benchmark/kernels/quantization/tuning_block_wise_kernel.py --N 6400 --K 5120 | ||
|
|
||
| # MLP down projection | ||
| python benchmark/kernels/quantization/tuning_block_wise_kernel.py --N 5120 --K 3200 | ||
|
|
||
| # O projection (if separate from QKV) | ||
| python benchmark/kernels/quantization/tuning_block_wise_kernel.py --N 5120 --K 1024 | ||
| ``` | ||
|
|
||
| ## Output | ||
|
|
||
| Generates JSON config files saved to `python/sglang/srt/layers/quantization/configs/`: | ||
| ``` | ||
| N={N},K={K},device_name={DEVICE},dtype=fp8_w8a8,block_shape=[128,128].json | ||
| ``` | ||
|
|
||
| Config maps batch size to optimal kernel parameters: | ||
| ```json | ||
| { | ||
| "1": {"BLOCK_SIZE_M": 16, "BLOCK_SIZE_N": 64, "BLOCK_SIZE_K": 128, ...}, | ||
| "2048": {"BLOCK_SIZE_M": 128, "BLOCK_SIZE_N": 128, "BLOCK_SIZE_K": 128, ...} | ||
| } | ||
| ``` | ||
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
26 changes: 26 additions & 0 deletions
26
.../configs/N=1280,K=5120,device_name=NVIDIA_B200,dtype=fp8_w8a8,block_shape=[128, 128].json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "2048": { | ||
| "BLOCK_SIZE_M": 64, | ||
| "BLOCK_SIZE_N": 128, | ||
| "BLOCK_SIZE_K": 128, | ||
| "GROUP_SIZE_M": 1, | ||
| "num_warps": 4, | ||
| "num_stages": 4 | ||
| }, | ||
| "3072": { | ||
| "BLOCK_SIZE_M": 64, | ||
| "BLOCK_SIZE_N": 128, | ||
| "BLOCK_SIZE_K": 128, | ||
| "GROUP_SIZE_M": 64, | ||
| "num_warps": 4, | ||
| "num_stages": 4 | ||
| }, | ||
| "4096": { | ||
| "BLOCK_SIZE_M": 64, | ||
| "BLOCK_SIZE_N": 128, | ||
| "BLOCK_SIZE_K": 128, | ||
| "GROUP_SIZE_M": 64, | ||
| "num_warps": 4, | ||
| "num_stages": 3 | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
.../configs/N=5120,K=1024,device_name=NVIDIA_B200,dtype=fp8_w8a8,block_shape=[128, 128].json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "2048": { | ||
| "BLOCK_SIZE_M": 64, | ||
| "BLOCK_SIZE_N": 128, | ||
| "BLOCK_SIZE_K": 128, | ||
| "GROUP_SIZE_M": 32, | ||
| "num_warps": 4, | ||
| "num_stages": 3 | ||
| }, | ||
| "3072": { | ||
| "BLOCK_SIZE_M": 64, | ||
| "BLOCK_SIZE_N": 128, | ||
| "BLOCK_SIZE_K": 128, | ||
| "GROUP_SIZE_M": 32, | ||
| "num_warps": 4, | ||
| "num_stages": 2 | ||
| }, | ||
| "4096": { | ||
| "BLOCK_SIZE_M": 64, | ||
| "BLOCK_SIZE_N": 128, | ||
| "BLOCK_SIZE_K": 128, | ||
| "GROUP_SIZE_M": 32, | ||
| "num_warps": 4, | ||
| "num_stages": 2 | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
.../configs/N=5120,K=3200,device_name=NVIDIA_B200,dtype=fp8_w8a8,block_shape=[128, 128].json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "2048": { | ||
| "BLOCK_SIZE_M": 64, | ||
| "BLOCK_SIZE_N": 128, | ||
| "BLOCK_SIZE_K": 128, | ||
| "GROUP_SIZE_M": 16, | ||
| "num_warps": 4, | ||
| "num_stages": 2 | ||
| }, | ||
| "3072": { | ||
| "BLOCK_SIZE_M": 64, | ||
| "BLOCK_SIZE_N": 128, | ||
| "BLOCK_SIZE_K": 128, | ||
| "GROUP_SIZE_M": 32, | ||
| "num_warps": 4, | ||
| "num_stages": 4 | ||
| }, | ||
| "4096": { | ||
| "BLOCK_SIZE_M": 64, | ||
| "BLOCK_SIZE_N": 128, | ||
| "BLOCK_SIZE_K": 128, | ||
| "GROUP_SIZE_M": 32, | ||
| "num_warps": 4, | ||
| "num_stages": 4 | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
.../configs/N=6400,K=5120,device_name=NVIDIA_B200,dtype=fp8_w8a8,block_shape=[128, 128].json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "2048": { | ||
| "BLOCK_SIZE_M": 64, | ||
| "BLOCK_SIZE_N": 128, | ||
| "BLOCK_SIZE_K": 128, | ||
| "GROUP_SIZE_M": 32, | ||
| "num_warps": 4, | ||
| "num_stages": 3 | ||
| }, | ||
| "3072": { | ||
| "BLOCK_SIZE_M": 64, | ||
| "BLOCK_SIZE_N": 128, | ||
| "BLOCK_SIZE_K": 128, | ||
| "GROUP_SIZE_M": 16, | ||
| "num_warps": 4, | ||
| "num_stages": 3 | ||
| }, | ||
| "4096": { | ||
| "BLOCK_SIZE_M": 64, | ||
| "BLOCK_SIZE_N": 128, | ||
| "BLOCK_SIZE_K": 128, | ||
| "GROUP_SIZE_M": 16, | ||
| "num_warps": 4, | ||
| "num_stages": 3 | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # W8A8 Block FP8 Kernel Configurations | ||
|
|
||
| This directory contains optimized kernel configurations for the W8A8 block FP8 matrix multiplication kernel. | ||
|
|
||
| ## Configuration File Format | ||
|
|
||
| Configuration files are named using the following pattern: | ||
| ``` | ||
| N={N},K={K},device_name={DEVICE_NAME},dtype=fp8_w8a8,block_shape=[{BLOCK_N},{BLOCK_K}].json | ||
| ``` | ||
|
|
||
| Where: | ||
| - `N`: Output dimension (number of columns in weight matrix) | ||
| - `K`: Input dimension (number of columns in activation matrix) | ||
| - `DEVICE_NAME`: GPU device name with spaces replaced by underscores (e.g., `NVIDIA_H100_80GB_HBM3`) | ||
| - `BLOCK_N`, `BLOCK_K`: Block quantization granularity (typically `[128,128]`) |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When do we need to use Triton FP8 block-wise quantization kernel instead of DeepGEMM?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replying to here .