[Performance] Qwen3-Next: optimize causal_conv1d_fn triton kernel - up to 9% faster#10552
Closed
byjiang1996 wants to merge 2 commits intosgl-project:mainfrom
Closed
[Performance] Qwen3-Next: optimize causal_conv1d_fn triton kernel - up to 9% faster#10552byjiang1996 wants to merge 2 commits intosgl-project:mainfrom
byjiang1996 wants to merge 2 commits intosgl-project:mainfrom
Conversation
4 tasks
Collaborator
|
4 tasks
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.
Motivation
Currently
causal_conv1d_fntriton kernel is used for extend & draft_extend. From the profile, it is inefficient - many CPU<>GPU copy sync and itsgridmethod is very slow (takes 139us to calculate the triton grid) while the real triton kernel launch itself only takes 17us.Modifications
Take this request as example:
Before
_causal_conv1d_fwd_kernelprocesses one BLOCK_M out of total 8 BLOCK_Mprogram_id(0) = imaps toblockm[i], maps to (m_list[i], offset_list[i]) chunk of tokensm_list = [0, 1, 1, 2, 3, 3, 3, 4]andoffset_list = [0, 0, 1, 0, 0, 1, 2, 0]):-- program_id(0) = 0
-- program_id(0) = 1
-- program_id(0) = 2
-- program_id(0) = 3
-- program_id(0) = 4
-- program_id(0) = 5
-- program_id(0) = 6
-- program_id(0) = 7
After: same processing logic as in triton_backend.py's extend_attention() method
_causal_conv1d_fwd_kernelprocesses one seq_len out of total 5 seq_lens_causal_conv1d_fwd_kernelprocesses one block of tokens belonging to that seq_lennum_of_blockm_s_per_seq_len = torch.tensor([1, 2, 1, 3, 1])):-- program_id(0) = 0, program_id(1) = 0
-- program_id(0) = 1, program_id(1) = 0
-- program_id(0) = 1, program_id(1) = 1
-- program_id(0) = 2, program_id(1) = 0
-- program_id(0) = 3, program_id(1) = 0
-- program_id(0) = 3, program_id(1) = 1
-- program_id(0) = 3, program_id(1) = 2
-- program_id(0) = 4, program_id(1) = 0
By doing so, we don't have to calculate m_list & offset_list which requires cpu<> gpu copy and expensive tensor/numpy operations. Instead, we can just leverage
forward_batch.extend_seq_len_cpuas it is in the triton kernel.In addition to this main change, there are some misc changes done by this PR such as removing unnecessary code/tensor and improve the
ifcondition in triton kernelBenchmarking and Profiling
causal_conv1d_fnruntimeBefore: p50=313us; avg=656us

After: p50=95us; avg=108us - 3X faster

GSM8k Accuracy
GSM8k Output token/s
Checklist