Skip to content

feat: support flashinfer kernel autotune - #12306

Merged
Kangyan-Zhou merged 10 commits into
sgl-project:mainfrom
elvischenv:elvischenv/support-flashinfer-autotune
Nov 29, 2025
Merged

Kangyan-Zhou merged 10 commits into
sgl-project:mainfrom
elvischenv:elvischenv/support-flashinfer-autotune

Conversation

@elvischenv

@elvischenv elvischenv commented Oct 29, 2025 •

Copy link
Copy Markdown
Contributor

Motivation

Flashinfer MoE requires autotune to select a most performant kernel. This should be done before cudagraph captures.

Modifications

This PR added a kernel warmup stage before the cudagraph captures. In the warmup stage, it will run a dummy forward for the model under the Flashinfer autotune context.

Accuracy Tests

lm_eval --model local-completions --tasks gsm8k --model_args model=openai/gpt-oss-120b,base_url=http://127.0.0.1:18000/v1/completions,max_retries=3,tokenized_requests=False,timeout=1200,max_gen_toks=2048,max_length=8192 --batch_size 2048 --trust_remote_code --limit 0.5

PR:

|Tasks|Version|     Filter     |n-shot|  Metric   |   |Value |   |Stderr|
|-----|------:|----------------|-----:|-----------|---|-----:|---|-----:|
|gsm8k|      3|flexible-extract|     5|exact_match|↑  |0.8394|±  |0.0143|
|     |       |strict-match    |     5|exact_match|↑  |0.6318|±  |0.0188|

main:

|Tasks|Version|     Filter     |n-shot|  Metric   |   |Value |   |Stderr|
|-----|------:|----------------|-----:|-----------|---|-----:|---|-----:|
|gsm8k|      3|flexible-extract|     5|exact_match|↑  |0.8394|±  |0.0143|
|     |       |strict-match    |     5|exact_match|↑  |0.6318|±  |0.0188|

Benchmarking and Profiling

python3 -m sglang.bench_serving --model openai/gpt-oss-120b --host 127.0.0.1 --port 18000 --backend sglang-oai --dataset-name random --random-range-ratio 1 --random-input-len 1024 --random-output-len 1024 --max-concurrency 512 --num-prompts 2560

PR(16% perf improvement):

============ Serving Benchmark Result ============
Backend:                                 sglang-oai
Traffic request rate:                    inf
Max request concurrency:                 512
Successful requests:                     2560
Benchmark duration (s):                  138.33
Total input tokens:                      2621440
Total input text tokens:                 2621440
Total input vision tokens:               0
Total generated tokens:                  2621440
Total generated tokens (retokenized):    2539762
Request throughput (req/s):              18.51
Input token throughput (tok/s):          18950.11
Output token throughput (tok/s):         18950.11
Total token throughput (tok/s):          37900.23
Concurrency:                             510.27
----------------End-to-End Latency----------------
Mean E2E Latency (ms):                   27573.10
Median E2E Latency (ms):                 27586.49
---------------Time to First Token----------------
Mean TTFT (ms):                          2658.05
Median TTFT (ms):                        2622.65
P99 TTFT (ms):                           4843.49
---------------Inter-Token Latency----------------
Mean ITL (ms):                           24.43
Median ITL (ms):                         21.66
P95 ITL (ms):                            24.23
P99 ITL (ms):                            179.70
Max ITL (ms):                            4049.93
==================================================

main:

============ Serving Benchmark Result ============
Backend:                                 sglang-oai
Traffic request rate:                    inf
Max request concurrency:                 512
Successful requests:                     2560
Benchmark duration (s):                  161.57
Total input tokens:                      2621440
Total input text tokens:                 2621440
Total input vision tokens:               0
Total generated tokens:                  2621440
Total generated tokens (retokenized):    2539762
Request throughput (req/s):              15.84
Input token throughput (tok/s):          16225.25
Output token throughput (tok/s):         16225.25
Total token throughput (tok/s):          32450.49
Concurrency:                             510.02
----------------End-to-End Latency----------------
Mean E2E Latency (ms):                   32188.29
Median E2E Latency (ms):                 32219.19
---------------Time to First Token----------------
Mean TTFT (ms):                          2632.67
Median TTFT (ms):                        2608.20
P99 TTFT (ms):                           4823.79
---------------Inter-Token Latency----------------
Mean ITL (ms):                           28.98
Median ITL (ms):                         26.49
P95 ITL (ms):                            28.89
P99 ITL (ms):                            154.62
Max ITL (ms):                            4009.70
==================================================

Checklist

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

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

@b8zhong b8zhong added the run-ci CI: run the baseline test suite on this PR label Oct 29, 2025
@FlamingoPg

Copy link
Copy Markdown
Collaborator

Great work! May I ask how long a single tuning run takes now? Is there a switch to control whether the kernel is tuned?

@elvischenv

Copy link
Copy Markdown
Contributor Author

@FlamingoPg

May I ask how long a single tuning run takes now?

For B200+gpt-oss-120b, it takes about 1 min from my local test:

[2025-10-29 02:42:01] Running FlashInfer autotune...
2025-10-29 02:42:01,952 - INFO - autotuner.py:256 - flashinfer.jit: [Autotuner]: Autotuning process starts ...
2025-10-29 02:42:57,165 - INFO - autotuner.py:262 - flashinfer.jit: [Autotuner]: Autotuning process ends
[2025-10-29 02:42:57] FlashInfer autotune completed.

Is there a switch to control whether the kernel is tuned?

The current design from flashinfer is just running one dummy forward iteration on max batch size with flashinfer context, if there is any flashinfer apis called in the iteration(e.g. trtllm_fp4_block_scale_moe) then flashinfer will do autotune for these apis to find the best kernel.

@elvischenv
elvischenv force-pushed the elvischenv/support-flashinfer-autotune branch 8 times, most recently from 3e541ac to a9c50aa Compare November 5, 2025 07:03
@nvpohanh

nvpohanh commented Nov 5, 2025

Copy link
Copy Markdown
Collaborator

@FlamingoPg Could you review this PR? Thanks!

@FlamingoPg FlamingoPg self-assigned this Nov 6, 2025
@FlamingoPg

Copy link
Copy Markdown
Collaborator

@FlamingoPg Could you review this PR? Thanks!

Looks fine, let's wait for the CI

@nvpohanh

nvpohanh commented Nov 7, 2025

Copy link
Copy Markdown
Collaborator

@FlamingoPg There are quite a few CI failures. Are all of them caused by this PR? Or are some of them known failing issues?

@elvischenv
elvischenv force-pushed the elvischenv/support-flashinfer-autotune branch from a9c50aa to 5c666ea Compare November 7, 2025 06:27
@elvischenv
elvischenv requested a review from Fridge003 as a code owner November 7, 2025 06:27
@FlamingoPg

Copy link
Copy Markdown
Collaborator

I will help you rerun failed jobs

@elvischenv

Copy link
Copy Markdown
Contributor Author

Hi @FlamingoPg, It seems that CI failures are not related to my PR. Could you help confirm? Thanks!

@Qiaolin-Yu Qiaolin-Yu self-assigned this Nov 22, 2025
@elvischenv
elvischenv force-pushed the elvischenv/support-flashinfer-autotune branch from edd223e to 817866b Compare November 25, 2025 08:34
@Qiaolin-Yu

Copy link
Copy Markdown
Collaborator

Waiting for the CI to pass.

@elvischenv

Copy link
Copy Markdown
Contributor Author

Hi @Qiaolin-Yu @FlamingoPg @Fridge003, could you help us to merge this PR? The CI failures are all unrelated. Thanks!

@elvischenv
elvischenv force-pushed the elvischenv/support-flashinfer-autotune branch from 67bab45 to 852fc37 Compare November 26, 2025 15:13
@Qiaolin-Yu

Copy link
Copy Markdown
Collaborator

Hi @Qiaolin-Yu @FlamingoPg @Fridge003, could you help us to merge this PR? The CI failures are all unrelated. Thanks!

@Kangyan-Zhou will help to merge this after all Nvidia CI pass.

@nvpohanh

Copy link
Copy Markdown
Collaborator

Test failures:

  • models/test_qwen3_next_models.py: accuracy is 0.0
  • test_gpt_oss_4gpu.py: Looks like infra issue: FileNotFoundError: No such file or directory: /hf_home/hub/models--openai--gpt-oss-120b/snapshots/b5c939de8f754692c1647ca79fbf85e8c1e70f8a/model-00000-of-00014.safetensors
  • test_deepseek_v3_fp4_4gpu.py: accuracy is 0.0 AssertionError: np.float64(0.0) not greater than 0.94
  • quant/test_w4a8_deepseek_v3.py: timeout with ImportError: DeepEP is not installed. Please install DeepEP package from https://github.com/deepseek-ai/deepep.. Looks like infra issue?
  • ep/test_deepep_large.py: timeout after 1200 secs. ~10 mins are spent in:
Capturing batches (bs=256 avail_mem=17.16 GB):   0%|          | 0/1 [00:26<?, ?it/s]
[2025-11-27 06:51:25 DP0 TP0 EP0] Registering 0 cuda graph addresses
E[2025-11-27 06:59:56] INFO utils.py:148: Note: detected 112 virtual cores but NumExpr set to maximum of 64, check "NUMEXPR_MAX_THREADS" environment variable.
[2025-11-27 06:59:56] INFO utils.py:151: Note: NumExpr detected 112 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 16.

and timeout at:

Capturing batches (bs=64 avail_mem=15.67 GB):   0%|          | 0/1 [00:26<?, ?it/s]

why does capturing cuda graphs take that long?

  • test_quantization.py: Mixtral-8x7B-Instruct-v0.1-AWQ-INT4 has slightly lower accuracy (0.612) than target 0.62

@Kangyan-Zhou

Kangyan-Zhou commented Nov 28, 2025 •

Copy link
Copy Markdown
Collaborator

Test failures:

  • models/test_qwen3_next_models.py: accuracy is 0.0
  • test_gpt_oss_4gpu.py: Looks like infra issue: FileNotFoundError: No such file or directory: /hf_home/hub/models--openai--gpt-oss-120b/snapshots/b5c939de8f754692c1647ca79fbf85e8c1e70f8a/model-00000-of-00014.safetensors
  • test_deepseek_v3_fp4_4gpu.py: accuracy is 0.0 AssertionError: np.float64(0.0) not greater than 0.94
  • quant/test_w4a8_deepseek_v3.py: timeout with ImportError: DeepEP is not installed. Please install DeepEP package from https://github.com/deepseek-ai/deepep.. Looks like infra issue?
  • ep/test_deepep_large.py: timeout after 1200 secs. ~10 mins are spent in:
Capturing batches (bs=256 avail_mem=17.16 GB):   0%|          | 0/1 [00:26<?, ?it/s]
[2025-11-27 06:51:25 DP0 TP0 EP0] Registering 0 cuda graph addresses
E[2025-11-27 06:59:56] INFO utils.py:148: Note: detected 112 virtual cores but NumExpr set to maximum of 64, check "NUMEXPR_MAX_THREADS" environment variable.
[2025-11-27 06:59:56] INFO utils.py:151: Note: NumExpr detected 112 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 16.

and timeout at:

Capturing batches (bs=64 avail_mem=15.67 GB):   0%|          | 0/1 [00:26<?, ?it/s]

why does capturing cuda graphs take that long?

  • test_quantization.py: Mixtral-8x7B-Instruct-v0.1-AWQ-INT4 has slightly lower accuracy (0.612) than target 0.62

We've fixed a few test failure issue on main due to a number of issues recently. Sry for the delay, retrying now

@Kangyan-Zhou
Kangyan-Zhou merged commit 848ee57 into sgl-project:main Nov 29, 2025
@elvischenv
elvischenv deleted the elvischenv/support-flashinfer-autotune branch December 4, 2025 15:18
0826joyce pushed a commit to 0826joyce/sglang-perf-opt that referenced this pull request May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

high priority run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants