torchaudio 2.7.1__cp311-cp311-win_amd64.whl → 2.9.0__cp311-cp311-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of torchaudio might be problematic. Click here for more details.

Files changed (92) hide show
  1. torchaudio/__init__.py +184 -33
  2. torchaudio/_extension/__init__.py +1 -14
  3. torchaudio/_extension/utils.py +0 -47
  4. torchaudio/_internal/module_utils.py +68 -10
  5. torchaudio/_torchcodec.py +340 -0
  6. torchaudio/datasets/cmuarctic.py +1 -1
  7. torchaudio/datasets/utils.py +1 -1
  8. torchaudio/functional/__init__.py +6 -3
  9. torchaudio/functional/_alignment.py +1 -1
  10. torchaudio/functional/filtering.py +70 -55
  11. torchaudio/functional/functional.py +31 -61
  12. torchaudio/lib/_torchaudio.pyd +0 -0
  13. torchaudio/lib/libtorchaudio.pyd +0 -0
  14. torchaudio/models/decoder/__init__.py +19 -1
  15. torchaudio/models/decoder/_ctc_decoder.py +6 -6
  16. torchaudio/models/decoder/_cuda_ctc_decoder.py +1 -1
  17. torchaudio/models/squim/objective.py +2 -2
  18. torchaudio/pipelines/_source_separation_pipeline.py +1 -1
  19. torchaudio/pipelines/_squim_pipeline.py +2 -2
  20. torchaudio/pipelines/_tts/utils.py +3 -1
  21. torchaudio/pipelines/rnnt_pipeline.py +4 -4
  22. torchaudio/transforms/__init__.py +4 -1
  23. torchaudio/transforms/_transforms.py +4 -3
  24. torchaudio/utils/__init__.py +2 -9
  25. torchaudio/utils/download.py +1 -1
  26. torchaudio/version.py +2 -2
  27. {torchaudio-2.7.1.dist-info → torchaudio-2.9.0.dist-info}/METADATA +15 -7
  28. torchaudio-2.9.0.dist-info/RECORD +85 -0
  29. {torchaudio-2.7.1.dist-info → torchaudio-2.9.0.dist-info}/top_level.txt +0 -1
  30. torchaudio/_backend/__init__.py +0 -61
  31. torchaudio/_backend/backend.py +0 -53
  32. torchaudio/_backend/common.py +0 -52
  33. torchaudio/_backend/ffmpeg.py +0 -334
  34. torchaudio/_backend/soundfile.py +0 -54
  35. torchaudio/_backend/soundfile_backend.py +0 -457
  36. torchaudio/_backend/sox.py +0 -91
  37. torchaudio/_backend/utils.py +0 -317
  38. torchaudio/backend/__init__.py +0 -8
  39. torchaudio/backend/_no_backend.py +0 -25
  40. torchaudio/backend/_sox_io_backend.py +0 -294
  41. torchaudio/backend/common.py +0 -13
  42. torchaudio/backend/no_backend.py +0 -14
  43. torchaudio/backend/soundfile_backend.py +0 -14
  44. torchaudio/backend/sox_io_backend.py +0 -14
  45. torchaudio/io/__init__.py +0 -13
  46. torchaudio/io/_effector.py +0 -347
  47. torchaudio/io/_playback.py +0 -72
  48. torchaudio/kaldi_io.py +0 -144
  49. torchaudio/prototype/__init__.py +0 -0
  50. torchaudio/prototype/datasets/__init__.py +0 -4
  51. torchaudio/prototype/datasets/musan.py +0 -67
  52. torchaudio/prototype/functional/__init__.py +0 -26
  53. torchaudio/prototype/functional/_dsp.py +0 -433
  54. torchaudio/prototype/functional/_rir.py +0 -379
  55. torchaudio/prototype/functional/functional.py +0 -190
  56. torchaudio/prototype/models/__init__.py +0 -36
  57. torchaudio/prototype/models/_conformer_wav2vec2.py +0 -794
  58. torchaudio/prototype/models/_emformer_hubert.py +0 -333
  59. torchaudio/prototype/models/conv_emformer.py +0 -525
  60. torchaudio/prototype/models/hifi_gan.py +0 -336
  61. torchaudio/prototype/models/rnnt.py +0 -711
  62. torchaudio/prototype/models/rnnt_decoder.py +0 -399
  63. torchaudio/prototype/pipelines/__init__.py +0 -12
  64. torchaudio/prototype/pipelines/_vggish/__init__.py +0 -3
  65. torchaudio/prototype/pipelines/_vggish/_vggish_impl.py +0 -233
  66. torchaudio/prototype/pipelines/_vggish/_vggish_pipeline.py +0 -82
  67. torchaudio/prototype/pipelines/hifigan_pipeline.py +0 -228
  68. torchaudio/prototype/pipelines/rnnt_pipeline.py +0 -58
  69. torchaudio/prototype/transforms/__init__.py +0 -9
  70. torchaudio/prototype/transforms/_transforms.py +0 -456
  71. torchaudio/sox_effects/__init__.py +0 -10
  72. torchaudio/sox_effects/sox_effects.py +0 -272
  73. torchaudio/utils/ffmpeg_utils.py +0 -11
  74. torchaudio/utils/sox_utils.py +0 -99
  75. torchaudio-2.7.1.dist-info/RECORD +0 -144
  76. torio/__init__.py +0 -8
  77. torio/_extension/__init__.py +0 -13
  78. torio/_extension/utils.py +0 -147
  79. torio/io/__init__.py +0 -9
  80. torio/io/_streaming_media_decoder.py +0 -978
  81. torio/io/_streaming_media_encoder.py +0 -502
  82. torio/lib/__init__.py +0 -0
  83. torio/lib/_torio_ffmpeg4.pyd +0 -0
  84. torio/lib/_torio_ffmpeg5.pyd +0 -0
  85. torio/lib/_torio_ffmpeg6.pyd +0 -0
  86. torio/lib/libtorio_ffmpeg4.pyd +0 -0
  87. torio/lib/libtorio_ffmpeg5.pyd +0 -0
  88. torio/lib/libtorio_ffmpeg6.pyd +0 -0
  89. torio/utils/__init__.py +0 -4
  90. torio/utils/ffmpeg_utils.py +0 -247
  91. {torchaudio-2.7.1.dist-info → torchaudio-2.9.0.dist-info}/WHEEL +0 -0
  92. {torchaudio-2.7.1.dist-info → torchaudio-2.9.0.dist-info}/licenses/LICENSE +0 -0
@@ -1,794 +0,0 @@
1
- from typing import List, Optional, Tuple, Union
2
-
3
- import torch
4
- from torch import nn, Tensor
5
- from torch.nn import Module, ModuleList
6
- from torchaudio.models import Wav2Vec2Model
7
- from torchaudio.models.conformer import ConformerLayer
8
- from torchaudio.models.rnnt import _TimeReduction
9
- from torchaudio.models.wav2vec2 import components
10
-
11
-
12
- def _buffered_arange(max) -> Tensor:
13
- """Compute arange using a buffered tensor across function calls.
14
- Produces same result as torch.arange(end=max).
15
-
16
- Args:
17
- max (int): Ending value for arange.
18
- """
19
- if not hasattr(_buffered_arange, "buf"):
20
- _buffered_arange.buf = torch.LongTensor()
21
- if max > _buffered_arange.buf.numel():
22
- _buffered_arange.buf.resize_(max)
23
- torch.arange(max, out=_buffered_arange.buf)
24
- return _buffered_arange.buf[:max]
25
-
26
-
27
- def _sample_negatives(input: Tensor, num_negatives: int, cross_sample_negatives: int) -> Tuple[Tensor, Tensor]:
28
- """Sample negative examples from masked input.
29
-
30
- Args:
31
- input (Tensor): Tensor of dimension `(batch, frame, dim)`.
32
- num_negatives (int): Number of negative examples to sample.
33
- cross_sample_negatives (int): Number of negative examples to cross sample.
34
-
35
- Returns:
36
- (Tensor, Tensor):
37
- Tensor
38
- The negative samples.
39
- Tensor
40
- The indices of the negative samples.
41
- """
42
- if num_negatives == 0 and cross_sample_negatives == 0:
43
- return (
44
- torch.zeros(0).to(input.device, input.dtype),
45
- torch.zeros(0).to(input.device, input.dtype),
46
- )
47
-
48
- B, T, D = input.shape
49
- input = input.view(-1, D)
50
-
51
- cross_high = T * B
52
- high = T
53
-
54
- assert high > 1
55
-
56
- if num_negatives > 0:
57
- tszs = _buffered_arange(T).unsqueeze(-1).expand(-1, num_negatives).flatten()
58
-
59
- neg_idxs = torch.randint(low=0, high=high - 1, size=(B, num_negatives * T))
60
- neg_idxs[neg_idxs >= tszs] += 1
61
-
62
- if cross_sample_negatives > 0:
63
- tszs = _buffered_arange(T).unsqueeze(-1).expand(-1, cross_sample_negatives).flatten()
64
-
65
- cross_neg_idxs = torch.randint(low=0, high=cross_high - 1, size=(B, cross_sample_negatives * T))
66
- cross_neg_idxs[cross_neg_idxs >= tszs] += 1
67
-
68
- if num_negatives > 0:
69
- neg_idxs = neg_idxs + (torch.arange(B).unsqueeze(1) * high)
70
- else:
71
- neg_idxs = cross_neg_idxs
72
-
73
- if cross_sample_negatives > 0 and num_negatives > 0:
74
- neg_idxs = torch.cat([neg_idxs, cross_neg_idxs], dim=1)
75
-
76
- negs = input[neg_idxs.view(-1)]
77
- negs = negs.view(B, T, num_negatives + cross_sample_negatives, D).permute(2, 0, 1, 3) # NxBxCxT
78
-
79
- return negs, neg_idxs
80
-
81
-
82
- class NegativeSampler(Module):
83
- r"""Applies preprocessing to input and then computes negative sampling.
84
-
85
- Args:
86
- preprocessor (nn.Module): Transforms input tensor prior to negative sampling.
87
- num_negatives (int): Number of negative examples to sample.
88
- cross_sample_negatives (int): Number of negative examples to cross sample.
89
- """
90
-
91
- def __init__(
92
- self,
93
- preprocessor: Module,
94
- num_negatives: int,
95
- cross_sample_negatives: int,
96
- ):
97
- super().__init__()
98
- self.preprocessor = preprocessor
99
- self.num_negatives = num_negatives
100
- self.cross_sample_negatives = cross_sample_negatives
101
-
102
- def forward(self, input: Tensor) -> Tuple[Tensor, Tensor, Optional[Tensor]]:
103
- """
104
- Args:
105
- input (Tensor): Tensor of dimension `(B, T, D)`.
106
-
107
- Returns:
108
- (Tensor, Tensor, Optional[Tensor]):
109
- Tensor
110
- The input tensor after preprocessing, prior to being sampled.
111
- Tensor
112
- The negative samples.
113
- Tensor
114
- The indices of the negative samples.
115
- """
116
- preprocessed = self.preprocessor(input)
117
- negs, neg_idxs = _sample_negatives(preprocessed, self.num_negatives, self.cross_sample_negatives)
118
- return preprocessed, negs, neg_idxs
119
-
120
-
121
- class FeatureEncoder(Module):
122
- """Feature Encoder class, consisting of time reduction and linear layer.
123
-
124
- Args:
125
- stride (int): Number of frames to merge for the output frame.
126
- input_dim (int): Input dimension of the tensor.
127
- output_dim (int): Output dimension of the tensor.
128
- """
129
-
130
- def __init__(self, input_dim: int, output_dim: int, stride: int):
131
- super().__init__()
132
- self.time_reduction_layer = _TimeReduction(stride=stride)
133
- self.linear_layer = nn.Linear(input_dim * stride, output_dim)
134
-
135
- def forward(
136
- self,
137
- x: Tensor,
138
- lengths: Optional[Tensor],
139
- ) -> Tuple[Tensor, Optional[Tensor]]:
140
- """
141
- Args:
142
- x (Tensor): Feature Tensor representing log Mel Spectrogram output. shape ``(B, T, D)``.
143
- lengths (Tensor or None):
144
- Valid length of each input sample. shape: ``(B, )``.
145
-
146
- Returns:
147
- (Tensor, Optional[Tensor]):
148
- Tensor: output sequence after undergoing time reduction and linear projection.
149
- Shape ``(B, T // stride, D * stride).
150
- Optional[Tensor]: output lengths of shape ``(B,)`` if lengths parameter is provided,
151
- otherwise `None`.
152
- """
153
- if lengths is None:
154
- B, T, D = x.shape
155
- dummy_lengths = torch.full((B,), T)
156
- x, _ = self.time_reduction_layer(x, dummy_lengths)
157
- x = self.linear_layer(x)
158
- return x, None
159
-
160
- x, lengths = self.time_reduction_layer(x, lengths)
161
- x = self.linear_layer(x)
162
- return x, lengths
163
-
164
-
165
- class ConformerEncoder(Module):
166
- """Conformer Encoder class, consisting of feature projection and conformer modules.
167
-
168
- Args:
169
- feature_projection (nn.Module):
170
- Projects feature to encoder dimension.
171
- conformer (nn.ModuleList)
172
- List of Conformer layers.
173
- """
174
-
175
- def __init__(
176
- self,
177
- feature_projection: Module,
178
- conformer: ModuleList,
179
- ):
180
- super().__init__()
181
- self.feature_projection = feature_projection
182
- self.conformer = conformer
183
-
184
- def _preprocess(
185
- self,
186
- features: Tensor,
187
- lengths: Optional[Tensor] = None,
188
- ) -> Tuple[Tensor, Optional[Tensor]]:
189
- x = self.feature_projection(features)
190
- if lengths is not None:
191
- mask = components._get_padding_mask(x, lengths)
192
- else:
193
- mask = None
194
- return x, mask
195
-
196
- def _get_intermediate_outputs(
197
- self,
198
- x: Tensor,
199
- mask: Optional[Tensor] = None,
200
- num_layers: Optional[int] = None,
201
- ) -> List[Tensor]:
202
- if num_layers is not None:
203
- if not 0 < num_layers <= len(self.conformer):
204
- raise ValueError(f"`num_layers` must be between [1, {len(self.conformer)}]")
205
-
206
- ret: List[Tensor] = []
207
-
208
- x = x.transpose(0, 1)
209
- for layer in self.conformer:
210
- x = layer(x, mask)
211
- ret.append(x.transpose(0, 1))
212
- if num_layers is not None and len(ret) >= num_layers:
213
- return ret
214
- return ret
215
-
216
- def forward(
217
- self,
218
- features: Tensor,
219
- lengths: Optional[Tensor] = None,
220
- ) -> Tensor:
221
- """
222
- Args:
223
- features (Tensor): Tensor of features of shape ``(B, T, D)``.
224
- lengths (Tensor or None, optional): Valid length of each input sample. shape: ``(B, )``.
225
-
226
- Returns:
227
- Tensor: result after applying conformer encoder to features.
228
- """
229
- x, mask = self._preprocess(features, lengths)
230
- x = x.transpose(0, 1)
231
- for layer in self.conformer:
232
- x = layer(x, mask)
233
- return x.transpose(0, 1)
234
-
235
- def extract_features(
236
- self,
237
- features: Tensor,
238
- lengths: Optional[Tensor] = None,
239
- num_layers: Optional[int] = None,
240
- ) -> List[Tensor]:
241
- """Returns the list of outputs from the intermediate layers of conformer block in the encoder.
242
-
243
- Args:
244
- features (Tensor): Tensor of features of shape ``(B, T, D)``.
245
- lengths (Tensor or None, optional): Valid length of each input sample. shape: ``(B, )``.
246
-
247
- Returns:
248
- List[Tensor]:
249
- Features from requested layers. Each Tensor is of shape: `(batch, time frame, feature dimension)`.
250
- """
251
- x, masks = self._preprocess(features, lengths)
252
- return self._get_intermediate_outputs(x, mask=masks, num_layers=num_layers)
253
-
254
-
255
- class ConformerWav2Vec2PretrainModel(Module):
256
- """Conformer Wav2Vec2 pre-train model for training from scratch.
257
-
258
- Note:
259
- To build the model, please use one of the factory functions,
260
- :py:func:`conformer_wav2vec2_base` or :py:func:`conformer_wav2vec2_large`
261
-
262
- Args:
263
- wav2vec2 (nn.Module):
264
- Conformer based Wav2Vec2 model, including feature extractor and conformer encoder components.
265
- mask_generator (nn.Module):
266
- Mask generator that generates the mask for masked prediction during training.
267
- negative_sampler (nn.Module):
268
- Negative sampler to apply after masking.
269
-
270
- """
271
-
272
- def __init__(
273
- self,
274
- wav2vec2: Wav2Vec2Model,
275
- mask_generator: Module,
276
- negative_sampler: Module,
277
- ):
278
- super().__init__()
279
- self.wav2vec2 = wav2vec2
280
- self.mask_generator = mask_generator
281
- self.negative_sampler = negative_sampler
282
-
283
- def forward(
284
- self,
285
- features: Tensor,
286
- audio_lengths: Optional[Tensor] = None,
287
- ) -> Tuple[Tensor, Optional[Tensor], Tensor, Tensor]:
288
- """
289
- Args:
290
- features (Tensor):
291
- Tensor of audio features of shape `(batch, frame, dim)`.
292
- audio_lengths (Tensor or None, optional):
293
- Tensor of valid length of each valid auidio in the batch.
294
- shape: `(batch, )` (Default: ``None``)
295
-
296
- Returns:
297
- (Tensor, Optional[Tensor], Tensor, Tensor, Tensor, Tensor):
298
- Tensor
299
- The masked sequences of probability distribution of shape `(batch, frame dim)`.
300
- Tensor or None
301
- If ``lengths`` argument was provided, a Tensor of shape `(batch, )` representing
302
- valid length in time axis is returns.
303
- Tensor
304
- The mask indices.
305
- Tensor
306
- The targets, prior to negative sampling.
307
- Tensor
308
- The negative samples.
309
- Tensor
310
- The indices of the negative samples.
311
- """
312
- x, lengths = self.wav2vec2.feature_extractor(features, audio_lengths)
313
-
314
- if lengths is not None:
315
- padding_mask = components._get_padding_mask(x, lengths)
316
- else:
317
- padding_mask = None
318
-
319
- x = self.wav2vec2.encoder.feature_projection.layer_norm(x)
320
- x = self.wav2vec2.encoder.feature_projection.dropout(x)
321
-
322
- # Unmasked feature is used to generate positive and negative samples.
323
- unmasked_x = x.clone()
324
- # Apply masking to x before passing it to Conformer layers.
325
- x, mask_idxs = self.mask_generator(x, padding_mask)
326
- # Select the frames from masked indices for negative sampling.
327
- unmasked_x = unmasked_x[mask_idxs].view(x.shape[0], -1, x.shape[-1])
328
- targets, negs, neg_idxs = self.negative_sampler(unmasked_x)
329
-
330
- x = self.wav2vec2.encoder.feature_projection.projection(x)
331
- x = x.transpose(0, 1)
332
- for conformer_layer in self.wav2vec2.encoder.conformer:
333
- x = conformer_layer(x, padding_mask)
334
- x = x.transpose(0, 1)
335
-
336
- return x, lengths, mask_idxs, targets, negs, neg_idxs
337
-
338
-
339
- ################################################################################
340
- def _get_conformer_feature_extractor(
341
- input_dim: int,
342
- output_dim: int,
343
- stride: int,
344
- ) -> FeatureEncoder:
345
- """Construct Feature Extractor
346
-
347
- Args:
348
- input_dim (int): Input dimension of features.
349
- output_dim (int): Output dimension after feature extraction.
350
- stride (int): Stride used in Time Reduction layer of feature extractor.
351
-
352
- Returns:
353
- FeatureEncoder: The resulting feature extraction.
354
- """
355
- return FeatureEncoder(input_dim, output_dim, stride)
356
-
357
-
358
- def _get_conformer_encoder(
359
- in_features: int,
360
- embed_dim: int,
361
- dropout_input: float,
362
- num_layers: int,
363
- num_heads: int,
364
- ff_interm_features: int,
365
- dropout: float,
366
- depthwise_conv_kernel_size: Union[int, List[int]],
367
- convolution_first: bool,
368
- use_group_norm: bool,
369
- ) -> ConformerEncoder:
370
- """Construct Conformer Encoder
371
-
372
- Args:
373
- in_features (int): The number of input features.
374
- embed_dim (int): The dimension of the embedding in the feature projection.
375
- dropout_input (float): The dropout probability applied after the input feature
376
- is projected to ``embed_dim``.
377
- num_layers (int): Number of Conformer layers in the encoder.
378
- num_heads (int): Number of heads in each Conformer layer.
379
- ff_interm_features (int): Hidden layer dimension of the feedforward network in
380
- each Conformer layer.
381
- dropout (float): Dropout probability in each Conformer layer.
382
- depthwise_conv_kernel_size (int or List[int]): List of kernel sizes corresponding
383
- to each of the Conformer layers.If int is provided, all layers will have the
384
- same kernel size.
385
- convolution_first (bool): Whether to apply the convolution module ahead of the
386
- attention module in each Conformer layer.
387
- use_group_norm (bool): Whether to use ``GroupNorm`` rather than ``BatchNorm1d`` in
388
- the convolution module in each Conformer layer.
389
-
390
- Returns:
391
- ConformerEncoder:
392
- The resulting conformer encoder module.
393
- """
394
- feature_projection = components.FeatureProjection(in_features, embed_dim, dropout_input)
395
-
396
- if type(depthwise_conv_kernel_size) == int:
397
- depthwise_conv_kernel_size = [depthwise_conv_kernel_size] * num_layers
398
-
399
- assert len(depthwise_conv_kernel_size) == num_layers
400
-
401
- conformer_layers = []
402
- for l in range(num_layers):
403
- layer = ConformerLayer(
404
- input_dim=embed_dim,
405
- ffn_dim=ff_interm_features,
406
- num_attention_heads=num_heads,
407
- depthwise_conv_kernel_size=depthwise_conv_kernel_size[l],
408
- dropout=dropout,
409
- use_group_norm=use_group_norm,
410
- convolution_first=convolution_first,
411
- )
412
- conformer_layers.append(layer)
413
-
414
- return ConformerEncoder(feature_projection, ModuleList(conformer_layers))
415
-
416
-
417
- def _get_conformer_negativer_sampler(
418
- input_dim: int,
419
- output_dim: int,
420
- num_negatives: int,
421
- cross_sample_negatives: int,
422
- ) -> NegativeSampler:
423
- """Build custom NegativeSampler module, including linear layer and negative sampling.
424
-
425
- Args:
426
- input_dim (int): Dimension of input after feature extraction.
427
- output_dim (int): Dimension of embedding for use in negative sampling. Same as the
428
- embedding in the feature projection.
429
- num_negatives (int): Number of negatives to sample.
430
- cross_sample_negatives (int): Number of cross sampled negatives.
431
-
432
- Returns:
433
- NegativeSampler:
434
- The resulting negative sampler module.
435
- """
436
- preprocessor = nn.Linear(input_dim, output_dim)
437
- return NegativeSampler(preprocessor, num_negatives, cross_sample_negatives)
438
-
439
-
440
- def conformer_wav2vec2_model(
441
- extractor_input_dim: int,
442
- extractor_output_dim: int,
443
- extractor_stride: int,
444
- encoder_embed_dim: int,
445
- encoder_projection_dropout: float,
446
- encoder_num_layers: int,
447
- encoder_num_heads: int,
448
- encoder_ff_interm_features: int,
449
- encoder_depthwise_conv_kernel_size: Union[int, List[int]],
450
- encoder_dropout: float,
451
- encoder_convolution_first: bool,
452
- encoder_use_group_norm: bool,
453
- ) -> Wav2Vec2Model:
454
- """Build a custom Conformer Wav2Vec2Model
455
-
456
- Args:
457
- extractor_input_dim (int): Input dimension of the features.
458
- extractor_output_dim (int): Output dimension after feature extraction.
459
- extractor_stride (int): Stride used in time reduction layer of feature extraction.
460
- encoder_embed_dim (int): The dimension of the embedding in the feature projection.
461
- encoder_projection_dropout (float):
462
- The dropout probability applied after the input feature is projected to ``embed_dim``
463
- encoder_num_layers (int): Number of Conformer layers in the encoder.
464
- encoder_num_heads (int): Number of heads in each Conformer layer.
465
- encoder_ff_interm_features (int):
466
- Hidden layer dimension of the feedforward network in each Conformer layer.
467
- encoder_depthwise_conv_kernel_size (int or List[int]):
468
- List of kernel sizes corresponding to each of the Conformer layers.
469
- If int is provided, all layers will have the same kernel size.
470
- encoder_dropout (float): Dropout probability in each Conformer layer.
471
- encoder_convolution_first (bool):
472
- Whether to apply the convolution module ahead of the attention module
473
- in each Conformer layer.
474
- encoder_use_group_norm (bool):
475
- Whether to use ``GroupNorm`` rather than ``BatchNorm1d`` in the convolution
476
- module in each Conformer layer.
477
-
478
- Returns:
479
- Wav2Vec2Model:
480
- The resulting wav2vec2 model with a conformer encoder.
481
- """
482
- feature_extractor = _get_conformer_feature_extractor(
483
- extractor_input_dim,
484
- extractor_output_dim,
485
- extractor_stride,
486
- )
487
-
488
- encoder = _get_conformer_encoder(
489
- in_features=extractor_output_dim,
490
- embed_dim=encoder_embed_dim,
491
- dropout_input=encoder_projection_dropout,
492
- num_layers=encoder_num_layers,
493
- num_heads=encoder_num_heads,
494
- ff_interm_features=encoder_ff_interm_features,
495
- depthwise_conv_kernel_size=encoder_depthwise_conv_kernel_size,
496
- dropout=encoder_dropout,
497
- convolution_first=encoder_convolution_first,
498
- use_group_norm=encoder_use_group_norm,
499
- )
500
-
501
- return Wav2Vec2Model(feature_extractor, encoder)
502
-
503
-
504
- def conformer_wav2vec2_base(
505
- extractor_input_dim: int = 64,
506
- extractor_output_dim: int = 256,
507
- encoder_projection_dropout: float = 0.0,
508
- ) -> Wav2Vec2Model:
509
- """
510
- Build Conformer Wav2Vec2 Model with "small" architecture from
511
- *Conformer-Based Slef-Supervised Learning for Non-Speech Audio Tasks* :cite:`9746490`
512
-
513
- Args:
514
- extractor_input_dim (int, optional): Input dimension of feature extractor. (Default: 64)
515
- extractor_output_dim (int, optional): Output dimension of feature extractor. (Default: 256)
516
- encoder_projection_dropout (float, optional):
517
- Dropout probability applied after feature projection. (Default: 0.0)
518
-
519
- Returns:
520
- Wav2Vec2Model:
521
- The resulting wav2vec2 model with a conformer encoder and ``base`` configuration.
522
- """
523
- return conformer_wav2vec2_model(
524
- extractor_input_dim=extractor_input_dim,
525
- extractor_output_dim=extractor_output_dim,
526
- extractor_stride=4,
527
- encoder_embed_dim=256,
528
- encoder_projection_dropout=encoder_projection_dropout,
529
- encoder_num_layers=12,
530
- encoder_num_heads=8,
531
- encoder_ff_interm_features=1024,
532
- encoder_depthwise_conv_kernel_size=[31] + [15] * 11,
533
- encoder_dropout=0.1,
534
- encoder_convolution_first=True,
535
- encoder_use_group_norm=True,
536
- )
537
-
538
-
539
- def conformer_wav2vec2_pretrain_model(
540
- extractor_input_dim: int,
541
- extractor_output_dim: int,
542
- extractor_stride: int,
543
- encoder_embed_dim: int,
544
- encoder_projection_dropout: float,
545
- encoder_num_layers: int,
546
- encoder_num_heads: int,
547
- encoder_ff_interm_features: int,
548
- encoder_depthwise_conv_kernel_size: int,
549
- encoder_dropout: float,
550
- encoder_convolution_first: bool,
551
- encoder_use_group_norm: bool,
552
- mask_prob: float,
553
- mask_selection: str,
554
- mask_other: float,
555
- mask_length: int,
556
- no_mask_overlap: bool,
557
- mask_min_space: int,
558
- mask_channel_prob: float,
559
- mask_channel_selection: str,
560
- mask_channel_other: float,
561
- mask_channel_length: int,
562
- no_mask_channel_overlap: bool,
563
- mask_channel_min_space: int,
564
- num_negatives: int,
565
- cross_sample_negatives: int,
566
- ) -> ConformerWav2Vec2PretrainModel:
567
- """Build a custom Conformer Wav2Vec2 Model for pre-training
568
-
569
- Args:
570
- extractor_input_dim (int): Input dimension of the features.
571
- extractor_output_dim (int): Output dimension after feature extraction.
572
- extractor_stride (int):
573
- Stride used in time reduction layer of feature extraction.
574
- encoder_embed_dim (int):
575
- The dimension of the embedding in the feature projection.
576
- encoder_projection_dropout (float):
577
- The dropout probability applied after the input feature is projected to
578
- ``embed_dim``
579
- encoder_num_layers (int):
580
- Number of Conformer layers in the encoder.
581
- encoder_num_heads (int):
582
- Number of heads in each Conformer layer.
583
- encoder_ff_interm_features (int):
584
- Hidden layer dimension of the feedforward network in each Conformer layer.
585
- encoder_depthwise_conv_kernel_size (int or List[int]):
586
- List of kernel sizes corresponding to each of the Conformer layers.
587
- If int is provided, all layers will have the same kernel size.
588
- encoder_dropout (float):
589
- Dropout probability in each Conformer layer.
590
- encoder_convolution_first (bool):
591
- Whether to apply the convolution module ahead of the attention module
592
- in each Conformer layer.
593
- encoder_use_group_norm (bool):
594
- Whether to use ``GroupNorm`` rather than ``BatchNorm1d`` in the convolution
595
- module in each Conformer layer.
596
- mask_prob (float):
597
- Probability for each token to be chosen as start of the span to be masked.
598
- mask_selection (str)
599
- How to choose the mask length. Options: [``static``, ``uniform``, ``normal``, ``poisson``].
600
- mask_other (float):
601
- Secondary mask argument (used for more complex distributions).
602
- mask_length (int):
603
- The lengths of the mask.
604
- no_mask_overlap (bool):
605
- Whether to allow masks to overlap.
606
- mask_min_space (int):
607
- Minimum space between spans (if no overlap is enabled).
608
- mask_channel_prob: (float):
609
- The probability of replacing a feature with 0.
610
- mask_channel_selection (str):
611
- How to choose the mask length for channel masking.
612
- Options: [``static``, ``uniform``, ``normal``, ``poisson``].
613
- mask_channel_other (float):
614
- Secondary mask argument for channel masking (used for more complex distributions).
615
- mask_channel_length (int):
616
- Minimum space between spans (if no overlap is enabled) for channel masking.
617
- no_mask_channel_overlap (bool):
618
- Whether to allow channel masks to overlap.
619
- mask_channel_min_space (int):
620
- Minimum space between spans for channel masking (if no overlap is enabled).
621
- num_negatives (int):
622
- Number of negatives to sample.
623
- cross_sample_negatives (int):
624
- Number of cross sampled negatives.
625
-
626
- Returns:
627
- ConformerWav2Vec2PretrainModel:
628
- The resulting model.
629
- """
630
- wav2vec2 = conformer_wav2vec2_model(
631
- extractor_input_dim,
632
- extractor_output_dim,
633
- extractor_stride,
634
- encoder_embed_dim,
635
- encoder_projection_dropout,
636
- encoder_num_layers,
637
- encoder_num_heads,
638
- encoder_ff_interm_features,
639
- encoder_depthwise_conv_kernel_size,
640
- encoder_dropout,
641
- encoder_convolution_first,
642
- encoder_use_group_norm,
643
- )
644
-
645
- mask_generator = components.MaskGenerator(
646
- extractor_output_dim,
647
- mask_prob,
648
- mask_selection,
649
- mask_other,
650
- mask_length,
651
- no_mask_overlap,
652
- mask_min_space,
653
- mask_channel_prob,
654
- mask_channel_selection,
655
- mask_channel_other,
656
- mask_channel_length,
657
- no_mask_channel_overlap,
658
- mask_channel_min_space,
659
- )
660
-
661
- negative_sampler = _get_conformer_negativer_sampler(
662
- extractor_output_dim,
663
- encoder_embed_dim,
664
- num_negatives,
665
- cross_sample_negatives,
666
- )
667
-
668
- return ConformerWav2Vec2PretrainModel(
669
- wav2vec2=wav2vec2,
670
- mask_generator=mask_generator,
671
- negative_sampler=negative_sampler,
672
- )
673
-
674
-
675
- def conformer_wav2vec2_pretrain_base(
676
- extractor_input_dim: int = 64,
677
- extractor_output_dim: int = 256,
678
- encoder_projection_dropout: float = 0.0,
679
- mask_prob: float = 0.3,
680
- mask_length: int = 3,
681
- num_negatives: int = 100,
682
- cross_sample_negatives: int = 0,
683
- ) -> ConformerWav2Vec2PretrainModel:
684
- """Build Conformer Wav2Vec2 Model for pre-training with "small" architecture from
685
- *Conformer-Based Self-Supervised Learning for Non-Speech Audio Tasks* :cite:`9746490`
686
-
687
- Args:
688
- extractor_input_dim (int, optional): Input dimension of the features. (Default: 64)
689
- extractor_output_dim (int, optional): Output dimension after feature extraction. (Default: 256)
690
- encoder_projection_dropout (float, optional):
691
- The dropout probability applied after the input feature is projected to
692
- ``embed_dim``. (Default: 0.0)
693
- mask_prob (float, optional):
694
- Probability for each token to be chosen as start of the span to be masked. (Default: 0.3)
695
- mask_length (int, optional):
696
- The lengths of the mask. (Default: 3)
697
- num_negatives (int, optional):
698
- Number of sampled negatives. (Default: 0)
699
- cross_sample_negatives (int, optional):
700
- Number of cross sampled negatives. (Default: 0)
701
-
702
- Returns:
703
- ConformerWav2Vec2PretrainModel:
704
- The resulting model.
705
- """
706
- return conformer_wav2vec2_pretrain_model(
707
- extractor_input_dim=extractor_input_dim,
708
- extractor_output_dim=extractor_output_dim,
709
- extractor_stride=4,
710
- encoder_embed_dim=256,
711
- encoder_projection_dropout=encoder_projection_dropout,
712
- encoder_num_layers=12,
713
- encoder_num_heads=8,
714
- encoder_ff_interm_features=1024,
715
- encoder_depthwise_conv_kernel_size=[31] + [15] * 11,
716
- encoder_dropout=0.1,
717
- encoder_convolution_first=True,
718
- encoder_use_group_norm=True,
719
- mask_prob=mask_prob,
720
- mask_selection="static",
721
- mask_other=0.0,
722
- mask_length=mask_length,
723
- no_mask_overlap=False,
724
- mask_min_space=0,
725
- mask_channel_prob=0,
726
- mask_channel_selection="static",
727
- mask_channel_other=0,
728
- mask_channel_length=10,
729
- no_mask_channel_overlap=False,
730
- mask_channel_min_space=1,
731
- num_negatives=num_negatives,
732
- cross_sample_negatives=cross_sample_negatives,
733
- )
734
-
735
-
736
- def conformer_wav2vec2_pretrain_large(
737
- extractor_input_dim: int = 64,
738
- extractor_output_dim: int = 256,
739
- encoder_projection_dropout: float = 0.0,
740
- mask_prob: float = 0.3,
741
- mask_length: int = 3,
742
- num_negatives: int = 100,
743
- cross_sample_negatives: int = 0,
744
- ) -> ConformerWav2Vec2PretrainModel:
745
- """Build Conformer Wav2Vec2 Model for pre-training with "large" architecture from
746
- *Conformer-Based Slef-Supervised Learning for Non-Speech Audio Tasks* :cite:`9746490`
747
-
748
- Args:
749
- extractor_input_dim (int, optional): Input dimension of the features. (Default: 64)
750
- extractor_output_dim (int, optional): Output dimension after feature extraction. (Default: 256)
751
- encoder_projection_dropout (float, optional):
752
- The dropout probability applied after the input feature is projected to
753
- ``embed_dim``. (Default: 0.0)
754
- mask_prob (float, optional):
755
- Probability for each token to be chosen as start of the span to be masked. (Default: 0.3)
756
- mask_length (int, optional):
757
- The lengths of the mask. (Default: 3)
758
- num_negatives (int, optional):
759
- Number of sampled negatives. (Default: 0)
760
- cross_sample_negatives (int, optional):
761
- Number of cross sampled negatives. (Default: 0)
762
-
763
- Returns:
764
- ConformerWav2Vec2PretrainModel:
765
- The resulting model.
766
- """
767
- return conformer_wav2vec2_pretrain_model(
768
- extractor_input_dim=extractor_input_dim,
769
- extractor_output_dim=extractor_output_dim,
770
- extractor_stride=4,
771
- encoder_embed_dim=768,
772
- encoder_projection_dropout=encoder_projection_dropout,
773
- encoder_num_layers=12,
774
- encoder_num_heads=12,
775
- encoder_ff_interm_features=1024,
776
- encoder_depthwise_conv_kernel_size=[31] + [15] * 11,
777
- encoder_dropout=0.1,
778
- encoder_convolution_first=True,
779
- encoder_use_group_norm=True,
780
- mask_prob=mask_prob,
781
- mask_selection="static",
782
- mask_other=0.0,
783
- mask_length=mask_length,
784
- no_mask_overlap=False,
785
- mask_min_space=0,
786
- mask_channel_prob=0,
787
- mask_channel_selection="static",
788
- mask_channel_other=0,
789
- mask_channel_length=10,
790
- no_mask_channel_overlap=False,
791
- mask_channel_min_space=1,
792
- num_negatives=num_negatives,
793
- cross_sample_negatives=cross_sample_negatives,
794
- )