-
Notifications
You must be signed in to change notification settings - Fork 56
/
factory.py
213 lines (169 loc) · 6.18 KB
/
factory.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
# 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 dataclasses import dataclass, field
from typing import Final, Optional
from fairseq2.config_registry import ConfigRegistry
from fairseq2.models.w2vbert.model import W2VBertModel
from fairseq2.models.wav2vec2 import (
Wav2Vec2Builder,
Wav2Vec2Config,
Wav2Vec2EncoderBuilder,
Wav2Vec2EncoderConfig,
wav2vec2_encoder_arch,
)
from fairseq2.nn.transformer import TransformerNormOrder
from fairseq2.typing import DataType, Device
W2VBERT_FAMILY: Final = "w2vbert"
@wav2vec2_encoder_arch("bert_600m")
def _600m_encoder() -> Wav2Vec2EncoderConfig:
return Wav2Vec2EncoderConfig(
model_dim=1024,
max_seq_len=4096,
feature_dim=160,
use_fbank=True,
first_pass_dropout_p=0.0,
layer_norm_features=False,
feature_extractor_layer_descs=[],
feature_extractor_bias=False,
feature_extractor_layer_norm_convs=False,
feature_gradient_scale=0.0,
num_fbank_channels=80,
fbank_stride=2,
sample_fbank_every_k=1,
pos_encoder_type="relative",
pos_encoder_depth=0,
pos_conv_kernel_size=0,
num_pos_conv_groups=0,
use_conformer=True,
num_encoder_layers=24,
num_encoder_attn_heads=16,
ffn_inner_dim=4096,
dropout_p=0.0,
attn_dropout_p=0.0,
layer_drop_p=0.0,
norm_order=TransformerNormOrder.POST,
depthwise_conv_kernel_size=31,
)
@wav2vec2_encoder_arch("bert_300m")
def _300m_encoder() -> Wav2Vec2EncoderConfig:
config = _600m_encoder()
config.num_encoder_layers = 12
return config
@dataclass
class W2VBertConfig:
"""Holds the configuration of a w2v-BERT model.
The default values correspond to the base architecture as described in
:cite:t`https://doi.org/10.48550/arxiv.2108.06209`.
"""
w2v2_config: Wav2Vec2Config = field(
default_factory=lambda: Wav2Vec2Config(
encoder_config=_600m_encoder(),
final_dim=768,
final_proj_bias=True,
temporal_mask_span_len=10,
max_temporal_mask_prob=0.65,
min_num_temporal_mask_spans=2,
spatial_mask_span_len=10,
max_spatial_mask_prob=0.0,
min_num_spatial_mask_spans=2,
quantized_dim=1024,
num_codebooks=1,
num_codebook_entries=1024,
codebook_sampling_temperature=(2.0, 0.1, 0.999995),
num_distractors=100,
logit_temp=0.1,
)
)
"""The configuration of the wav2vec 2.0 model."""
num_bert_encoder_layers: int = 16
"""The number of encoder layers to use for masked prediction."""
num_target_codebooks: int = 1
"""The number of consecutive codebooks to use as masked prediction targets."""
w2vbert_archs = ConfigRegistry[W2VBertConfig]()
w2vbert_arch = w2vbert_archs.decorator
@w2vbert_arch("600m")
def _600m() -> W2VBertConfig:
return W2VBertConfig()
@w2vbert_arch("300m")
def _300m() -> W2VBertConfig:
config = _600m()
config.w2v2_config.encoder_config = _300m_encoder()
config.num_bert_encoder_layers = 8
return config
class W2VBertBuilder:
"""Builds modules of a w2v-BERT model as described in
:cite:t`https://doi.org/10.48550/arxiv.2108.06209`.
To tweak the architecture, you can derive from this class and override the
corresponding methods.
"""
_config: W2VBertConfig
_w2v2_builder: Wav2Vec2Builder
_device: Optional[Device]
_dtype: Optional[DataType]
def __init__(
self,
config: W2VBertConfig,
w2v2_builder: Wav2Vec2Builder,
*,
device: Optional[Device] = None,
dtype: Optional[DataType] = None,
) -> None:
"""
:param config:
The configuration.
:param w2v2_builder:
The wav2vec 2.0 builder.
:param device:
The device on which to initialize modules.
:param dtype:
The data type of module parameters and buffers.
"""
encoder_config = config.w2v2_config.encoder_config
if encoder_config.layer_drop_p != 0.0:
raise ValueError("w2v-BERT does not support LayerDrop.")
if config.num_bert_encoder_layers >= encoder_config.num_encoder_layers:
raise ValueError(
f"`config.num_bert_encoder_layers` must be less than `config.w2v2_config.encoder_config.num_encoder_layers` ({encoder_config.num_encoder_layers}), but is {config.num_bert_encoder_layers} instead."
)
if config.num_target_codebooks > config.w2v2_config.num_codebooks:
raise ValueError(
f"`config.num_target_codebooks` must be less than the number of codebooks ({config.w2v2_config.num_codebooks}), but is {config.num_target_codebooks} instead."
)
self._config = config
self._w2v2_builder = w2v2_builder
self._device, self._dtype = device, dtype
def build_model(self) -> W2VBertModel:
"""Build a model."""
w2v2_model = self._w2v2_builder.build_model()
return W2VBertModel(
w2v2_model,
self._config.num_bert_encoder_layers,
num_target_codebooks=self._config.num_target_codebooks,
device=self._device,
dtype=self._dtype,
)
def create_w2vbert_model(
config: W2VBertConfig,
*,
device: Optional[Device] = None,
dtype: Optional[DataType] = None,
) -> W2VBertModel:
"""Create a w2v-BERT model.
:param config:
The configuration.
:param device:
The device on which to initialize modules.
:param dtype:
The data type of module parameters and buffers.
"""
encoder_builder = Wav2Vec2EncoderBuilder(
config.w2v2_config.encoder_config, device=device, dtype=dtype
)
w2v2_builder = Wav2Vec2Builder(
config.w2v2_config, encoder_builder, device=device, dtype=dtype
)
builder = W2VBertBuilder(config, w2v2_builder, device=device, dtype=dtype)
return builder.build_model().set_family(W2VBERT_FAMILY)