-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[transformer] support flash att by 'torch scaled dot attention' #2351
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c5ec7da
[transformer] support flash att by 'torch scaled dot attention'
Mddct 03068b8
pass ut on cpu
Mddct 0f967f8
pass ut on cpu
Mddct 6f1fb43
pass ut on cpu
Mddct 48c83e6
zero out pad mask att
Mddct 41cd68f
support attention mask bias in encocder
Mddct ee5aa9d
fix jit and unit test
Mddct 62c66cc
sdap in decoder and search
Mddct 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 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 was deleted.
Oops, something went wrong.
This file contains 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,111 @@ | ||
import torch | ||
import pytest | ||
from wenet.transformer.attention import MultiHeadedAttention | ||
from wenet.transformer.encoder_layer import TransformerEncoderLayer | ||
from wenet.transformer.positionwise_feed_forward import PositionwiseFeedForward | ||
from wenet.utils.class_utils import WENET_ACTIVATION_CLASSES | ||
|
||
from wenet.utils.mask import add_optional_chunk_mask, make_non_pad_mask | ||
|
||
|
||
@pytest.mark.parametrize("args", [ | ||
{ | ||
"n_feat": 256, | ||
"n_head": 4, | ||
"dropout_rate": 0.0 | ||
}, | ||
{ | ||
"n_feat": 512, | ||
"n_head": 8, | ||
"dropout_rate": 0.0 | ||
}, | ||
{ | ||
"n_feat": 1280, | ||
"n_head": 20, | ||
"dropout_rate": 0.0 | ||
}, | ||
{ | ||
"n_feat": 512, | ||
"n_head": 4, | ||
"dropout_rate": 0.0 | ||
}, | ||
]) | ||
def test_sdpa(args): | ||
torch.manual_seed(777) | ||
mha_module = MultiHeadedAttention(use_sdpa=False, **args) | ||
torch.manual_seed(777) | ||
mha_module_with_sdpa = MultiHeadedAttention(use_sdpa=True, **args) | ||
mha_module.eval() | ||
mha_module_with_sdpa.eval() | ||
|
||
q = torch.rand(10, 100, args['n_feat']) | ||
k = torch.rand(10, 100, args['n_feat']) | ||
v = torch.rand(10, 100, args['n_feat']) | ||
input_lens = torch.tensor([100, 90, 80, 79, 60, 51, 40, 30, 10, 5]) | ||
mask = make_non_pad_mask(input_lens).unsqueeze(1) | ||
att_mask = add_optional_chunk_mask(q, | ||
mask, | ||
use_dynamic_chunk=True, | ||
decoding_chunk_size=0, | ||
static_chunk_size=0, | ||
use_dynamic_left_chunk=True, | ||
num_decoding_left_chunks=-1) | ||
output, cache = mha_module(q, k, v, mask=att_mask) | ||
|
||
att_mask_bias = (1.0 - att_mask.float()) * torch.finfo(torch.float).min | ||
output_with_sdpa, cache_with_sdpa = mha_module_with_sdpa( | ||
q, k, v, mask=att_mask_bias) | ||
assert torch.allclose( | ||
output * mask.transpose(1, 2), | ||
output_with_sdpa * mask.transpose(1, 2), | ||
atol=9e-7, | ||
) | ||
assert torch.allclose(cache, cache_with_sdpa) | ||
|
||
n_blocks = 12 | ||
torch.manual_seed(777) | ||
mha_layers = [ | ||
TransformerEncoderLayer( | ||
args['n_feat'], | ||
MultiHeadedAttention(use_sdpa=False, **args), | ||
PositionwiseFeedForward( | ||
args['n_feat'], | ||
2048, | ||
0.0, | ||
WENET_ACTIVATION_CLASSES['swish'](), | ||
), | ||
0.0, | ||
normalize_before=True, | ||
) for _ in range(n_blocks) | ||
] | ||
|
||
torch.manual_seed(777) | ||
mha_layers_with_sdpa = [ | ||
TransformerEncoderLayer( | ||
args['n_feat'], | ||
MultiHeadedAttention(use_sdpa=True, **args), | ||
PositionwiseFeedForward( | ||
args['n_feat'], | ||
2048, | ||
0.0, | ||
WENET_ACTIVATION_CLASSES['swish'](), | ||
), | ||
0.0, | ||
normalize_before=True, | ||
) for _ in range(n_blocks) | ||
] | ||
|
||
for i in range(n_blocks): | ||
output, _, cache, _ = mha_layers[i](q, att_mask, None, mask) | ||
output_with_sdpa, _, cache_with_sdpa, _ = mha_layers_with_sdpa[i]( | ||
q, att_mask_bias, None, mask) | ||
|
||
assert torch.allclose( | ||
output * mask.transpose(1, 2), | ||
output_with_sdpa * mask.transpose(1, 2), | ||
atol=9e-7, | ||
rtol=9e-4, | ||
) | ||
# assert torch.allclose(cache, cache_with_sdpa) | ||
|
||
q = output |
This file contains 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 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 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 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.
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.
如果是bitransformer,这里访问use_sdpa属性还要再加一层module,model.decoder.left_decoder.use_sdpa