forked from Project-MONAI/MONAI
-
Notifications
You must be signed in to change notification settings - Fork 0
SwinUNETR: fused attention + channels-last + bf16 for AMD MI300X inference #1
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
nilapate
wants to merge
3
commits into
amd-integration
Choose a base branch
from
nilapate/swin_unetr_opt
base: amd-integration
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -457,6 +457,7 @@ def __init__( | |
| qkv_bias: bool = False, | ||
| attn_drop: float = 0.0, | ||
| proj_drop: float = 0.0, | ||
| use_sdpa: bool | None = None, | ||
| ) -> None: | ||
| """ | ||
| Args: | ||
|
|
@@ -466,6 +467,8 @@ def __init__( | |
| qkv_bias: add a learnable bias to query, key, value. | ||
| attn_drop: attention dropout rate. | ||
| proj_drop: dropout rate of output. | ||
| use_sdpa: use fused scaled_dot_product_attention. Defaults to True on ROCm (AMD), | ||
| False elsewhere. Pass explicitly to override. | ||
| """ | ||
|
|
||
| super().__init__() | ||
|
|
@@ -523,31 +526,53 @@ def __init__( | |
| self.proj_drop = nn.Dropout(proj_drop) | ||
| trunc_normal_(self.relative_position_bias_table, std=0.02) | ||
| self.softmax = nn.Softmax(dim=-1) | ||
| if use_sdpa is None: | ||
| use_sdpa = torch.version.hip is not None and hasattr(F, "scaled_dot_product_attention") | ||
| self._use_sdpa: bool = use_sdpa | ||
|
|
||
| def forward(self, x, mask): | ||
| b, n, c = x.shape | ||
| qkv = self.qkv(x).reshape(b, n, 3, self.num_heads, c // self.num_heads).permute(2, 0, 3, 1, 4) | ||
| q, k, v = qkv[0], qkv[1], qkv[2] | ||
| q = q * self.scale | ||
| attn = q @ k.transpose(-2, -1) | ||
| relative_position_bias = self.relative_position_bias_table[ | ||
| self.relative_position_index.clone()[:n, :n].reshape(-1) # type: ignore[operator] | ||
| ].reshape(n, n, -1) | ||
| relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() | ||
| attn = attn + relative_position_bias.unsqueeze(0) | ||
|
|
||
| bias = ( | ||
| self.relative_position_bias_table[ | ||
| self.relative_position_index[:n, :n].reshape(-1) # type: ignore[operator] | ||
|
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. Did you drop the |
||
| ] | ||
| .reshape(n, n, -1) | ||
| .permute(2, 0, 1) | ||
| .contiguous() | ||
| .unsqueeze(0) | ||
| ) # (1, num_heads, n, n) | ||
|
|
||
| if self._use_sdpa: | ||
| out = self._attn_sdpa(q, k, v, bias, mask, b, n) | ||
| else: | ||
| out = self._attn_explicit(q, k, v, bias, mask, b, n) | ||
|
|
||
| return self.proj_drop(self.proj(out.transpose(1, 2).reshape(b, n, c))) | ||
|
|
||
| def _attn_explicit(self, q, k, v, bias, mask, b, n): | ||
| attn = q * self.scale @ k.transpose(-2, -1) + bias | ||
| if mask is not None: | ||
| nw = mask.shape[0] | ||
| attn = attn.view(b // nw, nw, self.num_heads, n, n) + mask.unsqueeze(1).unsqueeze(0) | ||
| attn = attn.view(-1, self.num_heads, n, n) | ||
| attn = self.softmax(attn) | ||
| else: | ||
| attn = self.softmax(attn) | ||
| return self.attn_drop(self.softmax(attn)).to(v.dtype) @ v | ||
|
|
||
| attn = self.attn_drop(attn).to(v.dtype) | ||
| x = (attn @ v).transpose(1, 2).reshape(b, n, c) | ||
| x = self.proj(x) | ||
| x = self.proj_drop(x) | ||
| return x | ||
| def _attn_sdpa(self, q, k, v, bias, mask, b, n): | ||
| if mask is not None: | ||
| nw = mask.shape[0] | ||
| attn_mask = ( | ||
| (bias.unsqueeze(0) + mask.view(1, nw, 1, n, n)) | ||
| .expand(b // nw, nw, self.num_heads, n, n) | ||
| .reshape(b, self.num_heads, n, n) | ||
| .to(q.dtype) | ||
| ) | ||
| else: | ||
| attn_mask = bias.to(q.dtype) | ||
| drop_p = self.attn_drop.p if self.training else 0.0 | ||
| return F.scaled_dot_product_attention(q, k, v, attn_mask=attn_mask, dropout_p=drop_p, scale=self.scale) | ||
|
|
||
|
|
||
| class SwinTransformerBlock(nn.Module): | ||
|
|
||
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.
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 changes don't suggest this is needed - can you double check?