-
Notifications
You must be signed in to change notification settings - Fork 0
Implement ConformerFeedForwardV1 Part #6
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
25 commits
Select commit
Hold shift + click to select a range
a043765
add FFN
Judyxujj b52e172
black format
Judyxujj f38b67a
update
Judyxujj ed29b63
remove residual connection
Judyxujj 15c527a
remove newline
Judyxujj 6473775
black format
Judyxujj c84efc7
update based on comments
Judyxujj c3c3836
black format
Judyxujj 70a3b15
naming
Judyxujj 3b53088
remove LN & change name
Judyxujj a2a59b1
activation configurable
Judyxujj 7f499ed
black format
Judyxujj 17bf1b1
format
Judyxujj a9eeb96
Update i6_models/parts/conformer/feedforward.py
Judyxujj 16b7c72
update
Judyxujj 88d6d25
dropout
Judyxujj 88184cd
silu
Judyxujj 82f05de
update
Judyxujj eaedbfd
add torch in requirement
Judyxujj 291675d
format
Judyxujj 0e88668
format
Judyxujj c47a990
format
Judyxujj 14730f6
use config
Judyxujj 3f354cd
use functional variant
Judyxujj d37b28d
add activation default
Judyxujj 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,48 @@ | ||
from __future__ import annotations | ||
from dataclasses import dataclass | ||
from typing import Callable | ||
|
||
import torch | ||
from torch import nn | ||
|
||
from i6_models.config import ModelConfiguration | ||
|
||
|
||
@dataclass | ||
class ConformerPositionwiseFeedForwardV1Config(ModelConfiguration): | ||
input_dim: int | ||
"""input dimension""" | ||
hidden_dim: int | ||
"""hidden dimension (normally set to 4*input_dim as suggested by the paper)""" | ||
dropout: float | ||
curufinwe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""dropout probability""" | ||
activation: Callable[[torch.Tensor], torch.Tensor] = nn.functional.silu | ||
"""activation function""" | ||
|
||
|
||
class ConformerPositionwiseFeedForwardV1(nn.Module): | ||
""" | ||
Conformer feedforward module | ||
""" | ||
|
||
def __init__(self, cfg: ConformerPositionwiseFeedForwardV1Config): | ||
super().__init__() | ||
|
||
self.layer_norm = nn.LayerNorm(cfg.input_dim) | ||
self.linear_ff = nn.Linear(in_features=cfg.input_dim, out_features=cfg.hidden_dim, bias=True) | ||
self.activation = cfg.activation | ||
self.linear_out = nn.Linear(in_features=cfg.hidden_dim, out_features=cfg.input_dim, bias=True) | ||
self.dropout = cfg.dropout | ||
|
||
def forward(self, tensor: torch.Tensor) -> torch.Tensor: | ||
""" | ||
:param tensor: shape [B,T,F], F=input_dim | ||
:return: shape [B,T,F], F=input_dim | ||
""" | ||
michelwi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tensor = self.layer_norm(tensor) | ||
tensor = self.linear_ff(tensor) # [B,T,F] | ||
tensor = self.activation(tensor) # [B,T,F] | ||
tensor = nn.functional.dropout(tensor, p=self.dropout, training=self.training) # [B,T,F] | ||
tensor = self.linear_out(tensor) # [B,T,F] | ||
tensor = nn.functional.dropout(tensor, p=self.dropout, training=self.training) # [B,T,F] | ||
return tensor | ||
JackTemaki marked this conversation as resolved.
Show resolved
Hide resolved
Atticus1806 marked this conversation as resolved.
Show resolved
Hide resolved
|
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
typeguard | ||
typeguard | ||
torch |
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,24 @@ | ||
from itertools import product | ||
|
||
import torch | ||
from torch import nn | ||
|
||
from i6_models.parts.conformer.feedforward import ( | ||
ConformerPositionwiseFeedForwardV1, | ||
ConformerPositionwiseFeedForwardV1Config, | ||
) | ||
|
||
|
||
def test_ConformerPositionwiseFeedForwardV1(): | ||
def get_output_shape(input_shape, input_dim, hidden_dim, dropout, activation): | ||
x = torch.randn(input_shape) | ||
cfg = ConformerPositionwiseFeedForwardV1Config(input_dim, hidden_dim, dropout, activation) | ||
conf_ffn_part = ConformerPositionwiseFeedForwardV1(cfg) | ||
y = conf_ffn_part(x) | ||
return y.shape | ||
|
||
for input_dim, hidden_dim, dropout, activation in product( | ||
[10, 20], [100, 200], [0.1, 0.3], [nn.functional.silu, nn.functional.relu] | ||
): | ||
input_shape = (10, 100, input_dim) | ||
assert get_output_shape(input_shape, input_dim, hidden_dim, dropout, activation) == input_shape |
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.