-
Notifications
You must be signed in to change notification settings - Fork 0
add MHSA module #7
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a88eea4
add MHSA module
2387483
fix errors
38537da
add shape information
8deb4ca
use ModelConfiguration, add ConformerMHSAV1test
ce8ec49
fix config
4945818
reformat comments, remove padding from config, rename
d8e2ce9
remove dropout default values
b71c2c3
use torch.nn.functional.droput, value check in Configuration,more doc…
7a5ccec
change doc
d21132a
a small change in docstring
769f34b
Merge branch 'main' into add_conformer_mhsa_part
curufinwe e6b1052
black
10f3893
Update i6_models/parts/conformer/mhsa.py
curufinwe 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from __future__ import annotations | ||
from dataclasses import dataclass | ||
from typing import Optional, Callable | ||
import torch | ||
|
||
from i6_models.config import ModelConfiguration | ||
|
||
|
||
@dataclass | ||
class ConformerMHSAV1Config(ModelConfiguration): | ||
Atticus1806 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
input_dim: int | ||
"""input dim and total dimension for query/key and value projections, should be divisible by `num_att_heads`""" | ||
num_att_heads: int | ||
"""number of attention heads""" | ||
att_weights_dropout: float | ||
"""attention weights dropout""" | ||
dropout: float | ||
"""multi-headed self attention output dropout""" | ||
|
||
def __post_init__(self) -> None: | ||
super().__post_init__() | ||
assert self.input_dim % self.num_att_heads == 0, "input_dim must be divisible by num_att_heads" | ||
|
||
|
||
class ConformerMHSAV1(torch.nn.Module): | ||
""" | ||
Conformer multi-headed self-attention module | ||
""" | ||
|
||
def __init__(self, cfg: ConformerMHSAV1Config): | ||
|
||
super().__init__() | ||
|
||
self.layernorm = torch.nn.LayerNorm(cfg.input_dim) | ||
self.mhsa = torch.nn.MultiheadAttention( | ||
cfg.input_dim, cfg.num_att_heads, dropout=cfg.att_weights_dropout, batch_first=True | ||
) | ||
self.dropout = cfg.dropout | ||
|
||
def forward(self, input_tensor: torch.Tensor, key_padding_mask: Optional[torch.Tensor] = None) -> torch.Tensor: | ||
""" | ||
Apply layer norm and multi-head self attention and dropout | ||
:param Optional[torch.Tensor] key_padding_mask: could be a binary or float mask of shape (B, T) | ||
which will be applied/added to dot product, used to mask padded key positions out | ||
""" | ||
|
||
Atticus1806 marked this conversation as resolved.
Show resolved
Hide resolved
Atticus1806 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
output_tensor = self.layernorm(input_tensor) # [B,T,F] | ||
|
||
output_tensor, _ = self.mhsa( | ||
output_tensor, output_tensor, output_tensor, key_padding_mask=key_padding_mask, need_weights=False | ||
) # [B,T,F] | ||
output_tensor = torch.nn.functional.dropout(output_tensor, p=self.dropout, training=self.training) # [B,T,F] | ||
|
||
return output_tensor |
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.