Repository navigation
[Triton/Gluon] chunk_kimi_delta_attn: accept a non-fp32 KDA state #5249
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -541,8 +541,12 @@ def _flash_kda_seg_scan_kernel( | |
|
|
||
| if HAS_H0: | ||
| base = (i_n * H + i_h) * K * V + o_v[None, :] | ||
| b_h1 = tl.load(h0 + base + o_k1[:, None] * V, mask=m_v[None, :], other=0.0) | ||
| b_h2 = tl.load(h0 + base + o_k2[:, None] * V, mask=m_v[None, :], other=0.0) | ||
| b_h1 = tl.load(h0 + base + o_k1[:, None] * V, mask=m_v[None, :], other=0.0).to( | ||
| tl.float32 | ||
| ) | ||
| b_h2 = tl.load(h0 + base + o_k2[:, None] * V, mask=m_v[None, :], other=0.0).to( | ||
| tl.float32 | ||
| ) | ||
| else: | ||
| b_h1 = tl.zeros([64, BV], dtype=tl.float32) | ||
| b_h2 = tl.zeros([64, BV], dtype=tl.float32) | ||
|
|
@@ -831,13 +835,14 @@ def flash_kda_fwd( | |
| if h0 is not None: | ||
| if state_v_first: | ||
| h0 = h0.transpose(-1, -2) | ||
| h0 = h0.to(torch.float32).contiguous() | ||
| h0 = h0.contiguous() | ||
|
XiaobingSuper marked this conversation as resolved.
Resolved
|
||
|
|
||
| o = torch.empty_like(v) | ||
| final_state = None | ||
| if output_final_state: | ||
| shape = (N, H, V, K) if state_v_first else (N, H, K, V) | ||
| final_state = torch.empty(shape, dtype=torch.float32, device=dev) | ||
| state_dtype = h0.dtype if h0 is not None else torch.float32 | ||
| final_state = torch.empty(shape, dtype=state_dtype, device=dev) | ||
|
|
||
|
Comment on lines
855
to
859
Contributor
Author
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. The store was already correct (Triton implicitly converts to the pointer element type, and fp16/bf16 both round-trip in the numerics table above), but the surrounding |
||
| common = { | ||
| "ws_kd": ws_kd, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,7 +96,8 @@ def chunk_kimi_delta_attn( | |
| Scale factor for the attention scores. Default: `1 / sqrt(K)`. | ||
| initial_state (torch.Tensor, optional): | ||
| Initial state of shape `[N, HV, K, V]` (`[N, HV, V, K]` when | ||
| `state_v_first=True`) and dtype fp32, for `N` input sequences. For | ||
| `state_v_first=True`), for `N` input sequences. Any float dtype; the | ||
| recurrence accumulates in fp32 whatever the state is stored as. For | ||
| equal-length inputs `N` equals the batch size `B`. Default: `None`. | ||
| output_final_state (bool): | ||
| Whether to return the final state, same shape and dtype as | ||
|
Contributor
Author
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. Correct, and I missed it — the default pipeline allocates |
||
|
|
@@ -196,12 +197,6 @@ def chunk_kimi_delta_attn( | |
| f"of input sequences, i.e., {len(cu_seqlens) - 1} rather than " | ||
| f"{initial_state.shape[0]}." | ||
| ) | ||
| if initial_state is not None and initial_state.dtype != torch.float32: | ||
| raise ValueError( | ||
| f"`initial_state` must be fp32, got {initial_state.dtype}. The recurrence " | ||
| "accumulates in fp32 and the state is read back verbatim." | ||
| ) | ||
|
|
||
| if use_gate_in_kernel and A_log is None: | ||
|
XiaobingSuper marked this conversation as resolved.
Resolved
|
||
| raise ValueError("`A_log` must be provided when `use_gate_in_kernel=True`.") | ||
| if safe_gate and use_gate_in_kernel: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.