-
Notifications
You must be signed in to change notification settings - Fork 0
fix(specialized_clm): contiguous q/k/v for flash backward under torch.compile on ROCm #116
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -60,9 +60,13 @@ def forward( | |
| h = self.attn_norm(x) | ||
| q = self.wq(h).view(B, T, self.n_heads, self.head_dim).transpose(1, 2) | ||
| k = self.wk(h).view(B, T, self.n_heads, self.head_dim).transpose(1, 2) | ||
| v = self.wv(h).view(B, T, self.n_heads, self.head_dim).transpose(1, 2) | ||
| q = _apply_rope(q, rope_cos, rope_sin) | ||
| k = _apply_rope(k, rope_cos, rope_sin) | ||
| v = self.wv(h).view(B, T, self.n_heads, self.head_dim).transpose(1, 2).contiguous() | ||
| # Force contiguous q/k/v before SDPA so flash attention's backward | ||
| # gets the strides it expects under torch.compile on ROCm — same | ||
| # fix as the main model's Attention.forward (pawn/model.py). Without | ||
| # it the inductor flash-backward kernel hits a stride mismatch. | ||
| q = _apply_rope(q, rope_cos, rope_sin).contiguous() | ||
| k = _apply_rope(k, rope_cos, rope_sin).contiguous() | ||
| attn_out = F.scaled_dot_product_attention(q, k, v, is_causal=True) | ||
|
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. Pre-existing gap (not introduced by this PR, but visible here): |
||
| attn_out = attn_out.transpose(1, 2).contiguous().view(B, T, -1) | ||
| x = x + self.wo(attn_out) | ||
|
|
||
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.
Minor: the comment says "Force contiguous q/k/v" but
vis already on the line before it, so the explanation arrives after the first call it covers. Moving the comment block above all three makes the intent clearer and matches how you'd normally read it: