-
Notifications
You must be signed in to change notification settings - Fork 56
/
frontend.py
225 lines (188 loc) · 8.27 KB
/
frontend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
from typing import Optional, Tuple, final
from torch import Tensor
from torch.nn import Dropout
from fairseq2.models.feature_extractor import SequenceFeatureExtractor
from fairseq2.models.transformer import TransformerFrontend
from fairseq2.models.wav2vec2.masker import Wav2Vec2Masker
from fairseq2.nn import LayerNorm, Linear, PositionEncoder, StandardLayerNorm
from fairseq2.nn.incremental_state import IncrementalStateBag
from fairseq2.nn.padding import PaddingMask
from fairseq2.typing import DataType, Device, override
@final
class Wav2Vec2Frontend(TransformerFrontend):
"""Represents a Transformer encoder front-end as described in
:cite:t:`https://doi.org/10.48550/arxiv.2006.11477`."""
feature_dim: int
feature_extractor: Optional[SequenceFeatureExtractor]
post_extract_layer_norm: LayerNorm
model_dim_proj: Optional[Linear]
first_pass_dropout: Optional[Dropout]
pos_encoder: Optional[PositionEncoder]
layer_norm: Optional[LayerNorm]
dropout: Optional[Dropout]
def __init__(
self,
model_dim: int,
feature_dim: int,
feature_extractor: Optional[SequenceFeatureExtractor],
pos_encoder: Optional[PositionEncoder],
*,
first_pass_dropout_p: float = 0.0,
layer_norm: bool = False,
dropout_p: float = 0.0,
device: Optional[Device] = None,
dtype: Optional[DataType] = None,
) -> None:
"""
:param model_dim:
The dimensionality of the model.
:param feature_dim:
The dimensionality of extracted features.
:param feature_extractor:
The feature extractor. If ``None``, features are assumed to be
extracted externally before being fed to the model.
:param pos_encoder:
The position encoder.
:param first_pass_dropout_p:
The dropout probability on extracted features before masking and
positional encoding.
:param layer_norm:
If ``True``, applies Layer Normalization to extracted features
before dropout.
:param dropout_p:
The dropout probability on extracted features.
"""
super().__init__(model_dim)
self.feature_dim = feature_dim
if feature_extractor is not None:
if feature_dim != feature_extractor.feature_dim:
raise ValueError(
f"`feature_dim` of `feature_extractor` must be equal to `feature_dim` ({feature_dim}), but is {feature_extractor.feature_dim} instead."
)
self.feature_extractor = feature_extractor
else:
self.register_module("feature_extractor", None)
self.post_extract_layer_norm = StandardLayerNorm(
feature_dim, bias=True, device=device, dtype=dtype
)
if feature_dim != model_dim:
self.model_dim_proj = Linear(
feature_dim, model_dim, bias=True, device=device, dtype=dtype
)
else:
self.register_module("model_dim_proj", None)
if first_pass_dropout_p > 0.0:
self.first_pass_dropout = Dropout(first_pass_dropout_p)
else:
self.register_module("first_pass_dropout", None)
if pos_encoder is not None:
if pos_encoder.encoding_dim != model_dim:
raise ValueError(
f"`encoding_dim` of `pos_encoder` must be equal to `model_dim` ({model_dim}), but is {pos_encoder.encoding_dim} instead."
)
self.pos_encoder = pos_encoder
else:
self.register_module("pos_encoder", None)
if layer_norm:
self.layer_norm = StandardLayerNorm(
model_dim, bias=True, device=device, dtype=dtype
)
else:
self.register_module("layer_norm", None)
if dropout_p > 0.0:
self.dropout = Dropout(dropout_p)
else:
self.register_module("dropout", None)
@override
def forward(
self,
seqs: Tensor,
padding_mask: Optional[PaddingMask],
*,
state_bag: Optional[IncrementalStateBag] = None,
) -> Tuple[Tensor, Optional[PaddingMask]]:
if state_bag is not None:
raise ValueError(
"`Wav2Vec2Frontend` does not support incremental decoding."
)
seqs, padding_mask = self.extract_features(seqs, padding_mask)
seqs, padding_mask, _ = self.process_features(seqs, padding_mask)
return seqs, padding_mask
def extract_features(
self, seqs: Tensor, padding_mask: Optional[PaddingMask]
) -> Tuple[Tensor, Optional[PaddingMask]]:
"""Extract features from the specified sequences.
:param seqs:
The sequences from which to extract features. *Shape:*
:math:`(N,S,*)`, where :math:`N` is the batch size, :math:`S` is the
sequence length, and :math:`*` is any number of sequence-specific
dimensions including none.
:param padding_mask:
The padding mask of ``seqs``. *Shape:* :math:`(N,S)`, where :math:`N`
is the batch size and :math:`S` is the sequence length.
:returns:
- The extracted features. *Shape:* :math:`(N,S_{out},F)`, where
:math:`N` is the batch size, :math:`S_{out}` is the output
sequence length, and :math:`F` is the dimensionality of the
extracted features.
- The padding mask of the extracted features. *Shape:*
:math:`(N,S_{out})`, where :math:`N` is the batch size and
:math:`S_{out}` is the output sequence length.
"""
if self.feature_extractor is not None:
seqs, padding_mask = self.feature_extractor(seqs, padding_mask)
seqs = self.post_extract_layer_norm(seqs)
return seqs, padding_mask
def process_features(
self,
seqs: Tensor,
padding_mask: Optional[PaddingMask],
masker: Optional[Wav2Vec2Masker] = None,
) -> Tuple[Tensor, Optional[PaddingMask], Tensor]:
"""Process extracted features.
:param seqs:
The features to process. *Shape:* :math:`(N,S,F)`, where :math:`N`
is the batch size, :math:`S` is the sequence length, and :math:`F`
is the dimensionality of the features.
:param padding_mask:
The padding mask of ``seqs``. *Shape:* :math:`(N,S)`, where :math:`N`
is the batch size and :math:`S` is the sequence length.
:param masker:
If not ``None``, the features will be masked and the applied
temporal mask will be returned as the third element of the tuple.
:returns:
- The processed features to pass to the context network. *Shape:*
:math:`(N,S,M)`, where :math:`N` is the batch size, :math:`S` is
the sequence length, and :math:`M` is the dimensionality of the
model.
- The padding mask of the processed features. *Shape:* :math:`(N,S)`,
where :math:`N` is the batch size and :math:`S` is the output
sequence length.
- The temporal mask that has been applied to the processed features.
*Shape:* :math:`(N,S)`, where :math:`N` is the batch size and
:math`S` is the sequence length.
"""
if self.model_dim_proj is not None:
seqs = self.model_dim_proj(seqs)
if self.first_pass_dropout is not None:
seqs = self.first_pass_dropout(seqs)
if masker is not None:
seqs, temporal_mask = masker(seqs, padding_mask)
else:
temporal_mask = None
if self.pos_encoder is not None:
seqs = self.pos_encoder(seqs, padding_mask)
if self.layer_norm is not None:
seqs = self.layer_norm(seqs)
if self.dropout is not None:
seqs = self.dropout(seqs)
return seqs, padding_mask, temporal_mask
def extra_repr(self) -> str:
""":meta private:"""
s = super().extra_repr()
return f"{s}, feature_dim={self.feature_dim}"