-
Notifications
You must be signed in to change notification settings - Fork 284
add fa3_mtp #1005
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
Open
WANDY666
wants to merge
16
commits into
main
Choose a base branch
from
fa3_mtp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add fa3_mtp #1005
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fcaab12
Dockerfile add install fa3_mtp
WANDY666 22e1ca6
add wrapper and benchmark script
WANDY666 e5608fa
add _token_gqa_decode_attention_mtp to ds2
WANDY666 fddc5c6
fix q_nope discontinuous issues
WANDY666 9028d74
[fix]q_nope shape
WANDY666 60bbd86
format
WANDY666 fc22294
format
WANDY666 d98579d
finish, pass test
WANDY666 d3bf481
Enriched the benchmark
WANDY666 594de73
Improved the testing script
WANDY666 5f9c857
Merge branch 'main' of https://github.com/ModelTC/lightllm into fa3_mtp
WANDY666 d70d188
Remove redundant initialization of b_q_seq_len
WANDY666 d45865c
custom flash_attn
WANDY666 075f65d
merge main
WANDY666 67a1c87
fix flops
WANDY666 24aa227
merge main
shihaobai 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
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
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
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
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,103 @@ | ||
| import torch | ||
| from typing import List, Optional, Tuple, Union | ||
| from lightllm.utils.log_utils import init_logger | ||
|
|
||
| logger = init_logger(__name__) | ||
|
|
||
|
|
||
| def get_contiguous(x): | ||
| return x.contiguous() if x is not None and x.stride(-1) != 1 else x | ||
|
|
||
|
|
||
| try: | ||
| import flash_attn_3._C # Registers operators with PyTorch | ||
|
|
||
| flash_attn_3_mtp = torch.ops.flash_attn_3 | ||
|
|
||
| def flash_attn_with_kvcache_mtp( | ||
| q, | ||
| k, | ||
| v, | ||
| k_new: Optional[torch.Tensor] = None, | ||
| v_new: Optional[torch.Tensor] = None, | ||
| q_v: Optional[torch.Tensor] = None, | ||
| cu_seqlens_q: Optional[torch.Tensor] = None, | ||
| cu_seqlens_k: Optional[torch.Tensor] = None, | ||
| cu_seqlens_k_new: Optional[torch.Tensor] = None, | ||
| seqused_q: Optional[torch.Tensor] = None, | ||
| seqused_k: Optional[torch.Tensor] = None, | ||
| max_seqlen_q: Optional[int] = None, | ||
| max_seqlen_k: Optional[int] = None, | ||
| page_table: Optional[torch.Tensor] = None, | ||
| cache_batch_idx: Optional[torch.Tensor] = None, | ||
| cache_leftpad: Optional[torch.Tensor] = None, | ||
| rotary_cos: Optional[torch.Tensor] = None, | ||
| rotary_sin: Optional[torch.Tensor] = None, | ||
| rotary_seqlens: Optional[torch.Tensor] = None, | ||
| q_descale: Optional[torch.Tensor] = None, | ||
| k_descale: Optional[torch.Tensor] = None, | ||
| v_descale: Optional[torch.Tensor] = None, | ||
| softmax_scale=None, | ||
| is_causal=False, | ||
| window_size=(-1, -1), | ||
| softcap=0.0, # 0.0 means deactivated | ||
| is_rotary_interleaved=True, | ||
| scheduler_metadata=None, | ||
| num_splits=0, | ||
| pack_gqa=None, | ||
| sm_margin=0, | ||
| mtp_step=0, | ||
| ): | ||
| assert k.stride(-1) == 1, "k must have contiguous last dimension" | ||
| assert v.stride(-1) == 1, "v must have contiguous last dimension" | ||
| if softmax_scale is None: | ||
| softmax_scale = (q.shape[-1] + (q_v.shape[-1] if q_v is not None else 0)) ** (-0.5) | ||
| seqused_k = get_contiguous(seqused_k) | ||
|
|
||
| q, k, k_new, v_new = [get_contiguous(x) for x in (q, k, k_new, v_new)] | ||
| v = v.contiguous() if v.stride(-1) != 1 and v.stride(-3) != 1 else v | ||
| cu_seqlens_q, cu_seqlens_k_new = [get_contiguous(x) for x in (cu_seqlens_q, cu_seqlens_k_new)] | ||
| page_table = get_contiguous(page_table) | ||
| out, softmax_lse, *rest = flash_attn_3_mtp.fwd( | ||
| q, | ||
| k, | ||
| v, | ||
| k_new, | ||
| v_new, | ||
| q_v, | ||
| None, # out | ||
| cu_seqlens_q, | ||
| None, # cu_seqlens_k | ||
| cu_seqlens_k_new, | ||
| None, # seqused_q | ||
| seqused_k, | ||
| max_seqlen_q, | ||
| None, # max_seqlen_k | ||
| page_table, | ||
| cache_batch_idx, | ||
| cache_leftpad, | ||
| rotary_cos, | ||
| rotary_sin, | ||
| rotary_seqlens, | ||
| q_descale, | ||
| k_descale, | ||
| v_descale, | ||
| softmax_scale, | ||
| is_causal, | ||
| window_size[0], | ||
| window_size[1], | ||
| 0, | ||
| softcap, | ||
| is_rotary_interleaved, | ||
| scheduler_metadata, | ||
| num_splits, | ||
| pack_gqa, | ||
| sm_margin, | ||
| mtp_step, | ||
| ) | ||
| return out | ||
|
|
||
| except: | ||
| flash_attn_3_mtp = None | ||
| flash_attn_with_kvcache_mtp = None | ||
| logger.warning("flash_attn_3._C is not available, please install flash-attention-3 package.") |
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
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 |
|---|---|---|
|
|
@@ -69,7 +69,7 @@ def __init__(self, kvargs): | |
| return | ||
|
|
||
| def _init_inferstate_cls(self): | ||
| if get_env_start_args().enable_fa3: | ||
| if get_env_start_args().enable_fa3 or get_env_start_args().enable_fa3_mtp: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| self.infer_state_class = Deepseek2FlashAttentionStateInfo | ||
| elif self.enable_flashinfer: | ||
| self.infer_state_class = Deepseek2FlashInferStateInfo | ||
|
|
||
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
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
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
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.
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.
The function
get_env_start_args()is called multiple times within thisif/elifchain (lines 103, 107, 111). To improve performance and readability, consider calling it once before this conditional block and storing the result in a local variable.