Upload 3 files
Browse files- configuration_dasheng.py +70 -0
- feature_extraction_dasheng.py +171 -0
- modeling_dasheng.py +520 -0
configuration_dasheng.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2023-2024 Xiaomi Corporation and HuggingFace Inc. team. All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
""" Dasheng model configuration"""
|
| 16 |
+
|
| 17 |
+
from transformers import PretrainedConfig
|
| 18 |
+
|
| 19 |
+
DASHENG_PRETRAINED_CONFIG_ARCHIVE_MAP = {
|
| 20 |
+
"mispeech/dasheng-base": "https://huggingface.co/mispeech/dasheng-base/resolve/main/config.json",
|
| 21 |
+
"mispeech/dasheng-0.6B": "https://huggingface.co/mispeech/dasheng-0.6B/resolve/main/config.json",
|
| 22 |
+
"mispeech/dasheng-1.2B": "https://huggingface.co/mispeech/dasheng-1.2B/resolve/main/config.json",
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class DashengConfig(PretrainedConfig):
|
| 27 |
+
model_type = "dasheng"
|
| 28 |
+
|
| 29 |
+
def __init__(
|
| 30 |
+
self,
|
| 31 |
+
name: str = "dasheng-base",
|
| 32 |
+
loss: str = "BCELoss",
|
| 33 |
+
**kwargs,
|
| 34 |
+
):
|
| 35 |
+
r"""
|
| 36 |
+
Configuration class for the Dasheng model.
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
name (str, *optional*):
|
| 40 |
+
Can be "dasheng-base", "dasheng-0.6B", or "dasheng-1.2B". Default to "dasheng-base".
|
| 41 |
+
loss (str, *optional*):
|
| 42 |
+
Name of the loss function to use. Can be any loss in `nn.modules.loss`. Default to "BCELoss".
|
| 43 |
+
kwargs (dict, *optional*):
|
| 44 |
+
Additional keyword arguments, see `dasheng_model.modeling_dasheng.DashengFeatureExtractor` and `dasheng_model.modeling_dasheng.AudioTransformerMAE_Encoder` for more details.
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
super().__init__(**kwargs)
|
| 48 |
+
|
| 49 |
+
encoder_kwargs = dict(target_length=1008, patch_size=[64, 4], patch_stride=[64, 4])
|
| 50 |
+
|
| 51 |
+
if name == "dasheng-1.2B":
|
| 52 |
+
encoder_kwargs["embed_dim"] = 1536
|
| 53 |
+
encoder_kwargs["depth"] = 40
|
| 54 |
+
encoder_kwargs["num_heads"] = 24
|
| 55 |
+
elif name == "dasheng-0.6B":
|
| 56 |
+
encoder_kwargs["embed_dim"] = 1280
|
| 57 |
+
encoder_kwargs["depth"] = 32
|
| 58 |
+
encoder_kwargs["num_heads"] = 16
|
| 59 |
+
elif name == "dasheng-base":
|
| 60 |
+
encoder_kwargs["embed_dim"] = 768
|
| 61 |
+
encoder_kwargs["depth"] = 12
|
| 62 |
+
encoder_kwargs["num_heads"] = 12
|
| 63 |
+
else:
|
| 64 |
+
raise ValueError(f"Unrecognized model name: {name}")
|
| 65 |
+
self.name = name
|
| 66 |
+
|
| 67 |
+
encoder_kwargs.update((k, kwargs[k]) for k in set(kwargs).intersection(encoder_kwargs))
|
| 68 |
+
self.encoder_kwargs = {**encoder_kwargs, **kwargs}
|
| 69 |
+
|
| 70 |
+
self.loss = loss
|
feature_extraction_dasheng.py
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2023-2024 Xiaomi Corporation and HuggingFace Inc. team.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
"""
|
| 16 |
+
Feature extractor class for Dasheng.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
from typing import List, Optional, Union
|
| 20 |
+
|
| 21 |
+
import numpy as np
|
| 22 |
+
import torch
|
| 23 |
+
import torchaudio.transforms as audio_transforms
|
| 24 |
+
from transformers.feature_extraction_sequence_utils import SequenceFeatureExtractor
|
| 25 |
+
from transformers.feature_extraction_utils import BatchFeature
|
| 26 |
+
from transformers.utils import logging
|
| 27 |
+
|
| 28 |
+
logger = logging.get_logger(__name__)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class DashengFeatureExtractor(SequenceFeatureExtractor):
|
| 32 |
+
r"""
|
| 33 |
+
DashengFeatureExtractor extracts Mel spectrogram features from audio signals.
|
| 34 |
+
|
| 35 |
+
Args:
|
| 36 |
+
f_min (int, *optional*, defaults to 0): Minimum frequency for the Mel filterbank.
|
| 37 |
+
sampling_rate (int, *optional*, defaults to 16000):
|
| 38 |
+
Sampling rate of the input audio signal.
|
| 39 |
+
win_size (int, *optional*, defaults to 512): Window size for the STFT.
|
| 40 |
+
center (bool, *optional*, defaults to `True`):
|
| 41 |
+
Whether to pad the signal on both sides to center it.
|
| 42 |
+
n_fft (int, *optional*, defaults to 512): Number of FFT points for the STFT.
|
| 43 |
+
f_max (int, optional, *optional*): Maximum frequency for the Mel filterbank.
|
| 44 |
+
hop_size (int, *optional*, defaults to 160): Hop size for the STFT.
|
| 45 |
+
feature_size (int, *optional*, defaults to 64): Number of Mel bands to generate.
|
| 46 |
+
padding_value (float, *optional*, defaults to 0.0): Value for padding.
|
| 47 |
+
|
| 48 |
+
Returns:
|
| 49 |
+
BatchFeature: A BatchFeature object containing the extracted features.
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
def __init__(
|
| 53 |
+
self,
|
| 54 |
+
f_min: int = 0,
|
| 55 |
+
sampling_rate: int = 16000,
|
| 56 |
+
win_size: int = 512,
|
| 57 |
+
center: bool = True,
|
| 58 |
+
n_fft: int = 512,
|
| 59 |
+
f_max: Optional[int] = 8000,
|
| 60 |
+
hop_size: int = 160,
|
| 61 |
+
feature_size: int = 64,
|
| 62 |
+
padding_value: float = 0.0,
|
| 63 |
+
**kwargs,
|
| 64 |
+
):
|
| 65 |
+
super().__init__(
|
| 66 |
+
feature_size=feature_size,
|
| 67 |
+
sampling_rate=sampling_rate,
|
| 68 |
+
padding_value=padding_value,
|
| 69 |
+
**kwargs,
|
| 70 |
+
)
|
| 71 |
+
self.f_min = f_min
|
| 72 |
+
self.win_size = win_size
|
| 73 |
+
self.center = center
|
| 74 |
+
self.n_fft = n_fft
|
| 75 |
+
self.f_max = f_max
|
| 76 |
+
self.hop_size = hop_size
|
| 77 |
+
|
| 78 |
+
self.model_input_names = ["input_values"]
|
| 79 |
+
|
| 80 |
+
def __call__(
|
| 81 |
+
self,
|
| 82 |
+
x: Union[np.ndarray, torch.Tensor, List[np.ndarray], List[torch.Tensor]],
|
| 83 |
+
sampling_rate: Optional[int] = None,
|
| 84 |
+
max_length: Optional[int] = None,
|
| 85 |
+
truncation: bool = False,
|
| 86 |
+
return_tensors="pt",
|
| 87 |
+
) -> BatchFeature:
|
| 88 |
+
r"""
|
| 89 |
+
Extracts Mel spectrogram features from an audio signal tensor.
|
| 90 |
+
|
| 91 |
+
Args:
|
| 92 |
+
x: Input audio signal tensor.
|
| 93 |
+
sampling_rate (int, *optional*, defaults to `None`):
|
| 94 |
+
Sampling rate of the input audio signal.
|
| 95 |
+
max_length (int, *optional*, defaults to 16000):
|
| 96 |
+
Maximum length of the input audio signal.
|
| 97 |
+
truncation (bool, *optional*, defaults to `True`):
|
| 98 |
+
Whether to truncate the input signal to max_length.
|
| 99 |
+
return_tensors (str, *optional*, defaults to "pt"):
|
| 100 |
+
If set to "pt", the return type will be a PyTorch tensor.
|
| 101 |
+
|
| 102 |
+
Returns:
|
| 103 |
+
BatchFeature: A dictionary containing the extracted features.
|
| 104 |
+
"""
|
| 105 |
+
if sampling_rate is None:
|
| 106 |
+
sampling_rate = self.sampling_rate
|
| 107 |
+
|
| 108 |
+
if return_tensors != "pt":
|
| 109 |
+
raise NotImplementedError("Only return_tensors='pt' is currently supported.")
|
| 110 |
+
|
| 111 |
+
mel_spectrogram = audio_transforms.MelSpectrogram(
|
| 112 |
+
f_min=self.f_min,
|
| 113 |
+
sample_rate=sampling_rate,
|
| 114 |
+
win_length=self.win_size,
|
| 115 |
+
center=self.center,
|
| 116 |
+
n_fft=self.n_fft,
|
| 117 |
+
f_max=self.f_max,
|
| 118 |
+
hop_length=self.hop_size,
|
| 119 |
+
n_mels=self.feature_size,
|
| 120 |
+
)
|
| 121 |
+
amplitude_to_db = audio_transforms.AmplitudeToDB(top_db=120)
|
| 122 |
+
|
| 123 |
+
if isinstance(x, np.ndarray):
|
| 124 |
+
if x.ndim == 1:
|
| 125 |
+
x = x[np.newaxis, :]
|
| 126 |
+
if x.ndim != 2:
|
| 127 |
+
raise ValueError("np.ndarray input must be a 1D or 2D.")
|
| 128 |
+
x = torch.from_numpy(x)
|
| 129 |
+
elif isinstance(x, torch.Tensor):
|
| 130 |
+
if x.dim() == 1:
|
| 131 |
+
x = x.unsqueeze(0)
|
| 132 |
+
if x.dim() != 2:
|
| 133 |
+
raise ValueError("torch.Tensor input must be a 1D or 2D.")
|
| 134 |
+
elif isinstance(x, (list, tuple)):
|
| 135 |
+
longest_length = max(x_.shape[0] for x_ in x)
|
| 136 |
+
if not truncation and max_length is not None and max_length < longest_length:
|
| 137 |
+
max_length = longest_length
|
| 138 |
+
if max_length is None:
|
| 139 |
+
max_length = longest_length
|
| 140 |
+
|
| 141 |
+
if all(isinstance(x_, np.ndarray) for x_ in x):
|
| 142 |
+
if not all(x_.ndim == 1 for x_ in x):
|
| 143 |
+
raise ValueError("All np.ndarray in a list must be 1D.")
|
| 144 |
+
|
| 145 |
+
x_trim = [x_[:max_length] for x_ in x]
|
| 146 |
+
x_pad = [
|
| 147 |
+
np.pad(
|
| 148 |
+
x_,
|
| 149 |
+
(0, max_length - x_.shape[0]),
|
| 150 |
+
mode="constant",
|
| 151 |
+
constant_values=0,
|
| 152 |
+
)
|
| 153 |
+
for x_ in x_trim
|
| 154 |
+
]
|
| 155 |
+
x = torch.stack([torch.from_numpy(x_) for x_ in x_pad])
|
| 156 |
+
elif all(isinstance(x_, torch.Tensor) for x_ in x):
|
| 157 |
+
if not all(x_.dim() == 1 for x_ in x):
|
| 158 |
+
raise ValueError("All torch.Tensor in a list must be 1D.")
|
| 159 |
+
x_pad = [torch.nn.functional.pad(x_, (0, max_length - x_.shape[0]), value=0) for x_ in x]
|
| 160 |
+
x = torch.stack(x_pad)
|
| 161 |
+
else:
|
| 162 |
+
raise ValueError("Input list must be numpy arrays or PyTorch tensors.")
|
| 163 |
+
else:
|
| 164 |
+
raise ValueError(
|
| 165 |
+
"Input must be a numpy array, a list of numpy arrays, a PyTorch tensor, or a list of PyTorch tensor."
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
x = x.float()
|
| 169 |
+
x = mel_spectrogram(x)
|
| 170 |
+
x = amplitude_to_db(x)
|
| 171 |
+
return BatchFeature({"input_values": x})
|
modeling_dasheng.py
ADDED
|
@@ -0,0 +1,520 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2023-2024 Xiaomi Corporation and HuggingFace Inc. team. All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
""" PyTorch Dasheng (Deep Audio-Signal Holistic Embeddings) model."""
|
| 16 |
+
|
| 17 |
+
import collections
|
| 18 |
+
import math
|
| 19 |
+
from functools import partial
|
| 20 |
+
from typing import Any, Optional, Tuple
|
| 21 |
+
|
| 22 |
+
import torch
|
| 23 |
+
import torch.utils.checkpoint
|
| 24 |
+
from einops.layers.torch import Rearrange
|
| 25 |
+
from torch import nn
|
| 26 |
+
from transformers.modeling_outputs import SequenceClassifierOutput
|
| 27 |
+
from transformers.modeling_utils import PreTrainedModel
|
| 28 |
+
from transformers.utils import (
|
| 29 |
+
add_code_sample_docstrings,
|
| 30 |
+
add_start_docstrings,
|
| 31 |
+
add_start_docstrings_to_model_forward,
|
| 32 |
+
logging,
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
from .configuration_dasheng import DashengConfig
|
| 36 |
+
|
| 37 |
+
logger = logging.get_logger(__name__)
|
| 38 |
+
|
| 39 |
+
_CONFIG_FOR_DOC = "DashengConfig"
|
| 40 |
+
|
| 41 |
+
# Audio classification docstring
|
| 42 |
+
_SEQ_CLASS_CHECKPOINT = "mispeech/dasheng-base"
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
DASHENG_PRETRAINED_MODEL_ARCHIVE_LIST = [
|
| 46 |
+
"mispeech/dasheng-base",
|
| 47 |
+
"mispeech/dasheng-0.6B",
|
| 48 |
+
"mispeech/dasheng-1.2B",
|
| 49 |
+
# See all Dasheng models at https://huggingface.co/models?search=mispeech%2Fdasheng
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# The functions `trunc_normal_`, `_no_grad_trunc_normal_`, `drop_path` and the module `DropPath`` are taken from timm
|
| 54 |
+
def trunc_normal_(tensor, mean=0.0, std=1.0, a=-2.0, b=2.0):
|
| 55 |
+
return _no_grad_trunc_normal_(tensor, mean, std, a, b)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def _no_grad_trunc_normal_(tensor, mean, std, a, b):
|
| 59 |
+
# Cut & paste from PyTorch official master until it's in a few official releases - RW
|
| 60 |
+
# Method based on https://people.sc.fsu.edu/~jburkardt/presentations/truncated_normal.pdf
|
| 61 |
+
def norm_cdf(x):
|
| 62 |
+
# Computes standard normal cumulative distribution function
|
| 63 |
+
return (1.0 + math.erf(x / math.sqrt(2.0))) / 2.0
|
| 64 |
+
|
| 65 |
+
with torch.no_grad():
|
| 66 |
+
# Values are generated by using a truncated uniform distribution and
|
| 67 |
+
# then using the inverse CDF for the normal distribution.
|
| 68 |
+
# Get upper and lower cdf values
|
| 69 |
+
l = norm_cdf((a - mean) / std)
|
| 70 |
+
u = norm_cdf((b - mean) / std)
|
| 71 |
+
|
| 72 |
+
# Uniformly fill tensor with values from [l, u], then translate to
|
| 73 |
+
# [2l-1, 2u-1].
|
| 74 |
+
tensor.uniform_(2 * l - 1, 2 * u - 1)
|
| 75 |
+
|
| 76 |
+
# Use inverse cdf transform for normal distribution to get truncated
|
| 77 |
+
# standard normal
|
| 78 |
+
tensor.erfinv_()
|
| 79 |
+
|
| 80 |
+
# Transform to proper mean, std
|
| 81 |
+
tensor.mul_(std * math.sqrt(2.0))
|
| 82 |
+
tensor.add_(mean)
|
| 83 |
+
|
| 84 |
+
# Clamp to ensure it's in the proper range
|
| 85 |
+
tensor.clamp_(min=a, max=b)
|
| 86 |
+
return tensor
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def drop_path(x, drop_prob: float = 0.0, training: bool = False, scale_by_keep: bool = True):
|
| 90 |
+
"""Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
|
| 91 |
+
|
| 92 |
+
This is the same as the DropConnect impl I created for EfficientNet, etc networks, however,
|
| 93 |
+
the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper...
|
| 94 |
+
See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... I've opted for
|
| 95 |
+
changing the layer and argument names to 'drop path' rather than mix DropConnect as a layer name and use
|
| 96 |
+
'survival rate' as the argument.
|
| 97 |
+
|
| 98 |
+
"""
|
| 99 |
+
if drop_prob == 0.0 or not training:
|
| 100 |
+
return x
|
| 101 |
+
keep_prob = 1 - drop_prob
|
| 102 |
+
shape = (x.shape[0],) + (1,) * (x.ndim - 1) # work with diff dim tensors, not just 2D ConvNets
|
| 103 |
+
random_tensor = x.new_empty(shape).bernoulli_(keep_prob)
|
| 104 |
+
if keep_prob > 0.0 and scale_by_keep:
|
| 105 |
+
random_tensor.div_(keep_prob)
|
| 106 |
+
return x * random_tensor
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
class DropPath(nn.Module):
|
| 110 |
+
"""Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks)."""
|
| 111 |
+
|
| 112 |
+
def __init__(self, drop_prob: float = 0.0, scale_by_keep: bool = True):
|
| 113 |
+
super(DropPath, self).__init__()
|
| 114 |
+
self.drop_prob = drop_prob
|
| 115 |
+
self.scale_by_keep = scale_by_keep
|
| 116 |
+
|
| 117 |
+
def forward(self, x):
|
| 118 |
+
return drop_path(x, self.drop_prob, self.training, self.scale_by_keep)
|
| 119 |
+
|
| 120 |
+
def extra_repr(self):
|
| 121 |
+
return f"drop_prob={round(self.drop_prob,3):0.3f}"
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def to_2tuple(x: Any) -> Tuple[Any, Any]:
|
| 125 |
+
if isinstance(x, collections.abc.Iterable):
|
| 126 |
+
return x
|
| 127 |
+
return (x, x)
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
class AudioPatchEmbed(nn.Module):
|
| 131 |
+
def __init__(
|
| 132 |
+
self,
|
| 133 |
+
input_size=224,
|
| 134 |
+
patch_size=16,
|
| 135 |
+
patch_stride=16,
|
| 136 |
+
in_chans=1,
|
| 137 |
+
embed_dim=768,
|
| 138 |
+
norm_layer=None,
|
| 139 |
+
flatten=False,
|
| 140 |
+
):
|
| 141 |
+
super().__init__()
|
| 142 |
+
input_size = to_2tuple(input_size)
|
| 143 |
+
patch_size = to_2tuple(patch_size)
|
| 144 |
+
patch_stride = to_2tuple(patch_stride)
|
| 145 |
+
self.input_size = input_size
|
| 146 |
+
self.patch_size = patch_size
|
| 147 |
+
self.patch_stride = patch_stride
|
| 148 |
+
self.grid_size = (
|
| 149 |
+
input_size[0] // patch_stride[0],
|
| 150 |
+
input_size[1] // patch_stride[1],
|
| 151 |
+
)
|
| 152 |
+
self.num_patches = self.grid_size[0] * self.grid_size[1]
|
| 153 |
+
self.flatten = flatten
|
| 154 |
+
|
| 155 |
+
self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_stride)
|
| 156 |
+
self.norm = norm_layer(embed_dim) if norm_layer else nn.Identity()
|
| 157 |
+
|
| 158 |
+
def forward(self, x):
|
| 159 |
+
x = self.proj(x)
|
| 160 |
+
if self.flatten:
|
| 161 |
+
x = torch.permute(torch.flatten(x, 2, 3), (0, 2, 1)) # rearrange(x, "b c f t -> b (f t) c")
|
| 162 |
+
x = self.norm(x)
|
| 163 |
+
return x
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
class LayerScale(nn.Module):
|
| 167 |
+
def __init__(self, dim, init_values=1e-5, inplace=False):
|
| 168 |
+
super().__init__()
|
| 169 |
+
self.inplace = inplace
|
| 170 |
+
self.gamma = nn.Parameter(init_values * torch.ones(dim))
|
| 171 |
+
|
| 172 |
+
def forward(self, x):
|
| 173 |
+
return x.mul_(self.gamma) if self.inplace else x * self.gamma
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
class Mlp(nn.Module):
|
| 177 |
+
def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.0):
|
| 178 |
+
super().__init__()
|
| 179 |
+
out_features = out_features or in_features
|
| 180 |
+
hidden_features = hidden_features or in_features
|
| 181 |
+
self.fc1 = nn.Linear(in_features, hidden_features)
|
| 182 |
+
self.act = act_layer()
|
| 183 |
+
self.fc2 = nn.Linear(hidden_features, out_features)
|
| 184 |
+
self.drop = nn.Dropout(drop)
|
| 185 |
+
|
| 186 |
+
def forward(self, x):
|
| 187 |
+
x = self.fc1(x)
|
| 188 |
+
x = self.act(x)
|
| 189 |
+
x = self.drop(x)
|
| 190 |
+
x = self.fc2(x)
|
| 191 |
+
x = self.drop(x)
|
| 192 |
+
return x
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
class Attention(nn.Module):
|
| 196 |
+
def __init__(self, dim, num_heads=8, qkv_bias=False, attn_drop=0.0, proj_drop=0.0):
|
| 197 |
+
super().__init__()
|
| 198 |
+
assert dim % num_heads == 0, "dim should be divisible by num_heads"
|
| 199 |
+
self.num_heads = num_heads
|
| 200 |
+
head_dim = dim // num_heads
|
| 201 |
+
self.scale = head_dim**-0.5
|
| 202 |
+
|
| 203 |
+
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
|
| 204 |
+
self.attn_drop = nn.Dropout(attn_drop)
|
| 205 |
+
self.proj = nn.Linear(dim, dim)
|
| 206 |
+
self.proj_drop = nn.Dropout(proj_drop)
|
| 207 |
+
|
| 208 |
+
def forward(self, x):
|
| 209 |
+
B, N, C = x.shape
|
| 210 |
+
qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)
|
| 211 |
+
q, k, v = qkv.unbind(0) # make torchscript happy (cannot use tensor as tuple)
|
| 212 |
+
|
| 213 |
+
attn = (q @ k.transpose(-2, -1)) * self.scale
|
| 214 |
+
attn = attn.softmax(dim=-1)
|
| 215 |
+
attn = self.attn_drop(attn)
|
| 216 |
+
|
| 217 |
+
x = (attn @ v).transpose(1, 2).reshape(B, N, C)
|
| 218 |
+
x = self.proj(x)
|
| 219 |
+
x = self.proj_drop(x)
|
| 220 |
+
return x
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
class Block(nn.Module):
|
| 224 |
+
def __init__(
|
| 225 |
+
self,
|
| 226 |
+
dim,
|
| 227 |
+
num_heads,
|
| 228 |
+
mlp_ratio=4.0,
|
| 229 |
+
qkv_bias=False,
|
| 230 |
+
drop=0.0,
|
| 231 |
+
attn_drop=0.0,
|
| 232 |
+
init_values=None,
|
| 233 |
+
drop_path=0.0,
|
| 234 |
+
act_layer=nn.GELU,
|
| 235 |
+
norm_layer=nn.LayerNorm,
|
| 236 |
+
attention_type="Attention",
|
| 237 |
+
):
|
| 238 |
+
super().__init__()
|
| 239 |
+
self.norm1 = norm_layer(dim)
|
| 240 |
+
attn_type = globals()[attention_type]
|
| 241 |
+
self.attn = attn_type(dim, num_heads=num_heads, qkv_bias=qkv_bias, attn_drop=attn_drop, proj_drop=drop)
|
| 242 |
+
self.ls1 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity()
|
| 243 |
+
self.drop_path1 = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
|
| 244 |
+
|
| 245 |
+
self.norm2 = norm_layer(dim)
|
| 246 |
+
self.mlp = Mlp(in_features=dim, hidden_features=int(dim * mlp_ratio), act_layer=act_layer, drop=drop)
|
| 247 |
+
self.ls2 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity()
|
| 248 |
+
self.drop_path2 = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
|
| 249 |
+
|
| 250 |
+
def forward(self, x):
|
| 251 |
+
x = x + self.drop_path1(self.ls1(self.attn(self.norm1(x))))
|
| 252 |
+
x = x + self.drop_path2(self.ls2(self.mlp(self.norm2(x))))
|
| 253 |
+
return x
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
class AudioTransformerMAE_Encoder(nn.Module):
|
| 257 |
+
def __init__(
|
| 258 |
+
self,
|
| 259 |
+
patch_size=16,
|
| 260 |
+
patch_stride=16,
|
| 261 |
+
embed_dim=768,
|
| 262 |
+
depth=12,
|
| 263 |
+
num_heads=8,
|
| 264 |
+
mlp_ratio=4.0,
|
| 265 |
+
qkv_bias=True,
|
| 266 |
+
drop_rate=0.0,
|
| 267 |
+
attn_drop_rate=0.0,
|
| 268 |
+
drop_path_rate=0.0,
|
| 269 |
+
norm_layer=None,
|
| 270 |
+
act_layer=None,
|
| 271 |
+
init_values=None,
|
| 272 |
+
target_length=1012,
|
| 273 |
+
pooling="mean",
|
| 274 |
+
wavtransforms=None,
|
| 275 |
+
spectransforms=None,
|
| 276 |
+
time_patch_out: Optional[float] = None,
|
| 277 |
+
freq_patch_out: Optional[float] = None,
|
| 278 |
+
block_type="Block",
|
| 279 |
+
attention_type="Attention",
|
| 280 |
+
eval_avg="mean",
|
| 281 |
+
**kwargs,
|
| 282 |
+
):
|
| 283 |
+
super().__init__()
|
| 284 |
+
assert pooling in ("mean", "token", "logit")
|
| 285 |
+
self.pooling = pooling
|
| 286 |
+
self.embed_dim = embed_dim
|
| 287 |
+
self.patch_stride = patch_stride
|
| 288 |
+
self.patch_size = patch_size
|
| 289 |
+
self.n_mels = kwargs.get("n_mels", 64)
|
| 290 |
+
init_bn = kwargs.get("init_bn", True)
|
| 291 |
+
self.eval_avg = eval_avg
|
| 292 |
+
self.time_patch_out = time_patch_out
|
| 293 |
+
self.freq_patch_out = freq_patch_out
|
| 294 |
+
self.pad_last = kwargs.get("pad_last", True)
|
| 295 |
+
|
| 296 |
+
if init_bn:
|
| 297 |
+
self.init_bn = nn.Sequential(
|
| 298 |
+
Rearrange("b c f t -> b f c t"),
|
| 299 |
+
torch.nn.BatchNorm2d(self.n_mels, momentum=0.01),
|
| 300 |
+
Rearrange("b f c t -> b c f t"),
|
| 301 |
+
)
|
| 302 |
+
|
| 303 |
+
self.target_length = target_length
|
| 304 |
+
self.patch_embed = AudioPatchEmbed(
|
| 305 |
+
input_size=(self.n_mels, target_length),
|
| 306 |
+
embed_dim=self.embed_dim,
|
| 307 |
+
patch_size=self.patch_size,
|
| 308 |
+
flatten=False,
|
| 309 |
+
patch_stride=self.patch_stride,
|
| 310 |
+
)
|
| 311 |
+
self.spectransforms = nn.Sequential() if spectransforms is None else spectransforms
|
| 312 |
+
self.wavtransforms = nn.Sequential() if wavtransforms is None else wavtransforms
|
| 313 |
+
self.num_patches = self.patch_embed.num_patches
|
| 314 |
+
|
| 315 |
+
if pooling == "token":
|
| 316 |
+
self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim))
|
| 317 |
+
self.token_pos_embed = nn.Parameter(torch.randn(1, embed_dim) * 0.02)
|
| 318 |
+
|
| 319 |
+
self.time_pos_embed = nn.Parameter(torch.randn(1, embed_dim, 1, self.patch_embed.grid_size[1]) * 0.02)
|
| 320 |
+
self.freq_pos_embed = nn.Parameter(torch.randn(1, embed_dim, self.patch_embed.grid_size[0], 1) * 0.02)
|
| 321 |
+
|
| 322 |
+
norm_layer = norm_layer or partial(nn.LayerNorm, eps=1e-6)
|
| 323 |
+
act_layer = act_layer or nn.GELU
|
| 324 |
+
dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)] # stochastic depth decay rule
|
| 325 |
+
self.pos_drop = nn.Dropout(p=drop_rate)
|
| 326 |
+
block_function = globals()[block_type]
|
| 327 |
+
self.blocks = nn.Sequential(
|
| 328 |
+
*[
|
| 329 |
+
block_function(
|
| 330 |
+
dim=embed_dim,
|
| 331 |
+
num_heads=num_heads,
|
| 332 |
+
mlp_ratio=mlp_ratio,
|
| 333 |
+
qkv_bias=qkv_bias,
|
| 334 |
+
init_values=init_values,
|
| 335 |
+
drop=drop_rate,
|
| 336 |
+
attn_drop=attn_drop_rate,
|
| 337 |
+
drop_path=dpr[i],
|
| 338 |
+
norm_layer=norm_layer,
|
| 339 |
+
act_layer=act_layer,
|
| 340 |
+
attention_type=attention_type,
|
| 341 |
+
)
|
| 342 |
+
for i in range(depth)
|
| 343 |
+
]
|
| 344 |
+
)
|
| 345 |
+
self.norm = norm_layer(embed_dim)
|
| 346 |
+
if hasattr(self, "cls_token") and self.cls_token is not None:
|
| 347 |
+
nn.init.normal_(self.cls_token, std=1e-6)
|
| 348 |
+
|
| 349 |
+
@torch.jit.ignore
|
| 350 |
+
def no_weight_decay(self):
|
| 351 |
+
return {"time_pos_embed", "cls_token", "freq_pos_embed", "token_pos_embed"}
|
| 352 |
+
|
| 353 |
+
def forward_features(self, x):
|
| 354 |
+
x = self.patch_embed(x)
|
| 355 |
+
b, c, f, t = x.shape
|
| 356 |
+
x = x + self.time_pos_embed[:, :, :, :t]
|
| 357 |
+
x = x + self.freq_pos_embed[:, :, :, :] # Just for sin pos embed
|
| 358 |
+
x = torch.permute(torch.flatten(x, 2, 3), (0, 2, 1)) # rearrange(x, "b c f t -> b (f t) c")
|
| 359 |
+
if self.pooling == "token":
|
| 360 |
+
cls_token = self.cls_token.expand(x.shape[0], -1, -1)
|
| 361 |
+
cls_token = cls_token + self.token_pos_embed[:, :]
|
| 362 |
+
x = torch.cat((cls_token, x), dim=1)
|
| 363 |
+
x = self.pos_drop(x)
|
| 364 |
+
x = self.blocks(x)
|
| 365 |
+
x = self.norm(x)
|
| 366 |
+
return x
|
| 367 |
+
|
| 368 |
+
def forward(self, x):
|
| 369 |
+
x = self.init_bn(x) if self.init_bn is not None else x
|
| 370 |
+
# Remember starting position if we pad
|
| 371 |
+
padding_start = 0
|
| 372 |
+
if x.shape[-1] > self.target_length:
|
| 373 |
+
splits = x.split(self.target_length, -1)
|
| 374 |
+
|
| 375 |
+
if splits[-1].shape[-1] < self.target_length:
|
| 376 |
+
if self.pad_last:
|
| 377 |
+
pad = torch.zeros(*x.shape[:-1], self.target_length, device=x.device)
|
| 378 |
+
pad[..., : splits[-1].shape[-1]] = splits[-1]
|
| 379 |
+
padding_start = x.shape[-1] // self.patch_stride[-1]
|
| 380 |
+
splits = torch.stack((*splits[:-1], pad), dim=0)
|
| 381 |
+
else:
|
| 382 |
+
splits = torch.stack(splits[:-1], dim=0)
|
| 383 |
+
else:
|
| 384 |
+
splits = torch.stack(splits[:-1], dim=0)
|
| 385 |
+
n_splits = len(splits)
|
| 386 |
+
x = torch.flatten(splits, 0, 1) # spl b c f t-> (spl b) c f t
|
| 387 |
+
else:
|
| 388 |
+
n_splits = 1
|
| 389 |
+
|
| 390 |
+
x = self.forward_features(x)
|
| 391 |
+
x = torch.reshape(x, (x.shape[0] // n_splits, -1, x.shape[-1]))
|
| 392 |
+
if padding_start:
|
| 393 |
+
x = x[:,:padding_start, :]
|
| 394 |
+
return x
|
| 395 |
+
|
| 396 |
+
|
| 397 |
+
DASHENG_START_DOCSTRING = r"""
|
| 398 |
+
|
| 399 |
+
This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
|
| 400 |
+
library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
|
| 401 |
+
etc.)
|
| 402 |
+
|
| 403 |
+
This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
|
| 404 |
+
Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
|
| 405 |
+
and behavior.
|
| 406 |
+
|
| 407 |
+
Parameters:
|
| 408 |
+
config ([`DashengConfig`]): Model configuration class with all the parameters of the model.
|
| 409 |
+
Initializing with a config file does not load the weights associated with the model, only the
|
| 410 |
+
configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
|
| 411 |
+
"""
|
| 412 |
+
|
| 413 |
+
DASHENG_INPUTS_DOCSTRING = r"""
|
| 414 |
+
Args:
|
| 415 |
+
input_values (`torch.FloatTensor` of shape `(batch_size, n_mels, sequence_length)`):
|
| 416 |
+
The sequence of audio features extracted from the audio signal. Can be obtained from a raw audio waveform
|
| 417 |
+
using `~transformers.DashengFeatureExtractor.__call__`.
|
| 418 |
+
"""
|
| 419 |
+
|
| 420 |
+
|
| 421 |
+
class DashengPreTrainedModel(PreTrainedModel):
|
| 422 |
+
"""
|
| 423 |
+
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
|
| 424 |
+
models.
|
| 425 |
+
"""
|
| 426 |
+
|
| 427 |
+
config_class = DashengConfig
|
| 428 |
+
base_model_prefix = "dasheng"
|
| 429 |
+
main_input_name = "input_values"
|
| 430 |
+
supports_gradient_checkpointing = True
|
| 431 |
+
|
| 432 |
+
def _init_weights(self, module):
|
| 433 |
+
if isinstance(module, nn.Linear):
|
| 434 |
+
torch.nn.init.xavier_uniform_(module.weight)
|
| 435 |
+
if module.bias is not None:
|
| 436 |
+
nn.init.zeros_(module.bias)
|
| 437 |
+
elif isinstance(module, nn.LayerNorm):
|
| 438 |
+
nn.init.constant_(module.bias, 0)
|
| 439 |
+
nn.init.constant_(module.weight, 1.0)
|
| 440 |
+
|
| 441 |
+
|
| 442 |
+
@add_start_docstrings(
|
| 443 |
+
"The Dasheng Model transformer with an optional linear layer on top of the pooled output.",
|
| 444 |
+
DASHENG_START_DOCSTRING,
|
| 445 |
+
)
|
| 446 |
+
class DashengModel(DashengPreTrainedModel):
|
| 447 |
+
def __init__(self, config: DashengConfig, outputdim: Optional[int] = None) -> None:
|
| 448 |
+
r"""
|
| 449 |
+
Initializes the model.
|
| 450 |
+
|
| 451 |
+
Args:
|
| 452 |
+
config (DashengConfig): Configuration class for the model.
|
| 453 |
+
outputdim (int, optional): Dimension of the output layer. If None, the model returns the hidden states. Defaults to None.
|
| 454 |
+
"""
|
| 455 |
+
super().__init__(config)
|
| 456 |
+
self.config = config
|
| 457 |
+
self.name = config.name
|
| 458 |
+
|
| 459 |
+
self.encoder = AudioTransformerMAE_Encoder(**config.encoder_kwargs)
|
| 460 |
+
|
| 461 |
+
# Classifier head
|
| 462 |
+
if outputdim is not None:
|
| 463 |
+
self.outputlayer = nn.Sequential(
|
| 464 |
+
nn.LayerNorm(config.encoder_kwargs["embed_dim"]),
|
| 465 |
+
nn.Linear(config.encoder_kwargs["embed_dim"], outputdim),
|
| 466 |
+
)
|
| 467 |
+
else:
|
| 468 |
+
self.outputlayer = nn.Identity()
|
| 469 |
+
outputdim = config.encoder_kwargs["embed_dim"]
|
| 470 |
+
self.outputdim = outputdim
|
| 471 |
+
|
| 472 |
+
# Initialize weights and apply final processing
|
| 473 |
+
self.post_init()
|
| 474 |
+
|
| 475 |
+
def forward_head(self, x: torch.Tensor) -> torch.Tensor:
|
| 476 |
+
if self.encoder.pooling == "token":
|
| 477 |
+
x = x[:, 0]
|
| 478 |
+
return self.outputlayer(x).sigmoid()
|
| 479 |
+
elif self.encoder.pooling == "mean":
|
| 480 |
+
x = x.mean(1)
|
| 481 |
+
return self.outputlayer(x).sigmoid()
|
| 482 |
+
elif self.encoder.pooling == "logit":
|
| 483 |
+
x = x.mean(1)
|
| 484 |
+
return self.outputlayer(x)
|
| 485 |
+
else:
|
| 486 |
+
raise NotImplementedError(f"Pooling {self.encoder.pooling} not implemented.")
|
| 487 |
+
|
| 488 |
+
def freeze_encoder(self) -> None:
|
| 489 |
+
for param in self.encoder.parameters():
|
| 490 |
+
param.requires_grad = False
|
| 491 |
+
self._requires_grad = False
|
| 492 |
+
|
| 493 |
+
@add_start_docstrings_to_model_forward(DASHENG_INPUTS_DOCSTRING.format("batch_size, n_mels, sequence_length"))
|
| 494 |
+
@add_code_sample_docstrings(
|
| 495 |
+
checkpoint=_SEQ_CLASS_CHECKPOINT,
|
| 496 |
+
output_type=SequenceClassifierOutput,
|
| 497 |
+
config_class=_CONFIG_FOR_DOC,
|
| 498 |
+
modality="audio",
|
| 499 |
+
model_cls="DashengModel",
|
| 500 |
+
)
|
| 501 |
+
def forward(self, input_values: torch.Tensor, labels: Optional[torch.Tensor] = None) -> SequenceClassifierOutput:
|
| 502 |
+
"""
|
| 503 |
+
Runs a forward pass of the Dasheng model with audio features. The model returns logits and hidden states. If labels are provided, the model also returns the loss.
|
| 504 |
+
"""
|
| 505 |
+
x = torch.unsqueeze(input_values, 1)
|
| 506 |
+
last_hidden_states = self.encoder(x)
|
| 507 |
+
logits = self.forward_head(last_hidden_states)
|
| 508 |
+
|
| 509 |
+
if labels is not None:
|
| 510 |
+
try:
|
| 511 |
+
loss_fct = getattr(nn.modules.loss, self.config.loss)()
|
| 512 |
+
except AttributeError:
|
| 513 |
+
raise NotImplementedError(f"Loss {self.config.loss} not implemented.")
|
| 514 |
+
|
| 515 |
+
labels = nn.functional.one_hot(labels, num_classes=self.outputdim).float()
|
| 516 |
+
loss = loss_fct(logits, labels)
|
| 517 |
+
else:
|
| 518 |
+
loss = None
|
| 519 |
+
|
| 520 |
+
return SequenceClassifierOutput(logits=logits, loss=loss, hidden_states=last_hidden_states)
|