-
Notifications
You must be signed in to change notification settings - Fork 49
Feat/pretransposed states #36
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
Closed
higgsboson1710
wants to merge
7
commits into
inclusionAI:main
from
higgsboson1710:feat/pretransposed-states
+50
−24
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cecba88
opt: implement pretransposed state layout (BHVK) for lightning attention
higgsboson1710 9a84996
test: update test suite for BHVK state layout
higgsboson1710 6a856d5
Apply suggestion from @gemini-code-assist[bot]
higgsboson1710 22a34a0
Apply suggestion from @gemini-code-assist[bot]
higgsboson1710 cc85ab6
Apply suggestion from @gemini-code-assist[bot]
higgsboson1710 0eb8ebb
Apply suggestion from @gemini-code-assist[bot]
higgsboson1710 f297510
Refactor tensor initialization by removing transpose
higgsboson1710 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 |
|---|---|---|
|
|
@@ -410,10 +410,9 @@ def __call__( | |
| # and the parameter is eliminated at compile time via const_expr guards. | ||
| # For varlen: state pool is [pool_size, H, D, D]. We use B (=N) as the | ||
| # pool dimension — strides are correct regardless of actual pool_size. | ||
| fstate_layout = cute.make_layout( | ||
| (D, D, (H, B)), | ||
| stride=(1, D, (D * D, D * D * H)), | ||
| ) | ||
| fstate_layout = cute.make_layout((D, D, (H, B)), | ||
| stride=(D, 1, (D * D, D * D * H)), | ||
| ) | ||
| if cutlass.const_expr(self.has_initial_state): | ||
| initial_state = cute.make_tensor(initial_state_in.iterator, fstate_layout) | ||
| else: | ||
|
|
@@ -1703,8 +1702,8 @@ def kernel( | |
| # -------------- Initial State Loading (h0) ---------------- | ||
| if cutlass.const_expr(self.has_initial_state): | ||
| gState_h0 = initial_state[None, None, (hidx, state_idx)] | ||
| gRow_h0 = cute.make_tensor(gState_h0.iterator + local_tidx, cute.make_layout(_D, stride=_D)) | ||
| cute.autovec_copy(gRow_h0, init_flat) | ||
| gCol_h0 = cute.make_tensor(gState_h0.iterator + local_tidx * _D, cute.make_layout(_D, stride=1)) | ||
| cute.autovec_copy(gCol_h0, init_flat) | ||
|
|
||
| # Store raw h0 as BF16 to kv16 TMEM for SQ MMA at idx=0 | ||
| tmem_store_rAccKVAsBF16.store(tTR_rKV.load().to(self.io_dtype)) | ||
|
|
@@ -1877,10 +1876,10 @@ def kernel( | |
| gState_ht = initial_state[None, None, (hidx, state_idx)] | ||
| else: | ||
| gState_ht = final_state[None, None, (hidx, state_idx)] | ||
| gRow_ht = cute.make_tensor(gState_ht.iterator + local_tidx, cute.make_layout(_D, stride=_D)) | ||
|
|
||
|
|
||
| gCol_ht = cute.make_tensor(gState_ht.iterator + local_tidx * _D, cute.make_layout(_D, stride=1)) | ||
| out_flat = cute.make_tensor(tTR_rKV.iterator, layout=cute.make_layout(_D)) | ||
| cute.autovec_copy(out_flat, gRow_ht) | ||
| cute.autovec_copy(out_flat, gCol_ht) | ||
|
Collaborator
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. We need to track the performance change here. Could you share the results of bench_lightning_attn.py |
||
|
|
||
| # Advance k_stage_offset by number of chunks in this WU | ||
| # so next WU's k_stage_idx stays in sync with the K pipeline. | ||
|
|
@@ -2895,7 +2894,7 @@ def lightning_attn_fwd( | |
| V: (B, S, H, D) bf16 value | ||
| decay: (H,) f32 per-head decay coefficients | ||
| scale: attention scale factor (default: 1.0) | ||
| initial_state: (B, H, D, D) f32 initial state or None | ||
| initial_state: (B, H, D, D) f32 initial state in BHVK layout or None | ||
| output_final_state: whether to output final state | ||
| chunk_size: chunk size (default: 64) | ||
|
|
||
|
|
@@ -3111,7 +3110,7 @@ def lightning_attn_fwd_varlen( | |
| decay: (H,) f32 per-head decay coefficients | ||
| cu_seqlens: (N+1,) int32 cumulative sequence lengths | ||
| scale: attention scale factor (default: 1.0) | ||
| state_pool: (pool_size, H, D, D) f32 state pool, or None | ||
| state_pool: (pool_size, H, D, D) f32 state pool in BHVK layout, or None | ||
| If None, a zero state pool is allocated with pool_size=N. | ||
| States are updated in-place (INPLACE_UPDATE). | ||
| initial_state_indices: (N,) int32 indices into state_pool per sequence. | ||
|
|
||
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
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.