huseinzol05
commited on
Commit
•
b9e9c01
1
Parent(s):
0eac7e3
Upload ConformerEncoder
Browse files- config.json +25 -0
- conformer.py +66 -0
- model.safetensors +3 -0
config.json
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "tiny/checkpoint-16600",
|
3 |
+
"architectures": [
|
4 |
+
"ConformerEncoder"
|
5 |
+
],
|
6 |
+
"auto_map": {
|
7 |
+
"AutoConfig": "conformer.ConformerConfig",
|
8 |
+
"AutoModel": "conformer.ConformerEncoder"
|
9 |
+
},
|
10 |
+
"conformer_depthwise_conv_kernel_size": 31,
|
11 |
+
"conformer_dropout": 0.1,
|
12 |
+
"conformer_ffn_dim": 576,
|
13 |
+
"conformer_input_dim": 144,
|
14 |
+
"conformer_num_heads": 4,
|
15 |
+
"conformer_num_layers": 8,
|
16 |
+
"ctc_loss_reduction": "mean",
|
17 |
+
"ctc_zero_infinity": true,
|
18 |
+
"input_dim": 80,
|
19 |
+
"model_type": "conformer",
|
20 |
+
"output_dim": 40,
|
21 |
+
"pad_token_id": 39,
|
22 |
+
"time_reduction_stride": 4,
|
23 |
+
"torch_dtype": "bfloat16",
|
24 |
+
"transformers_version": "4.35.2"
|
25 |
+
}
|
conformer.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from torchaudio.models import Conformer
|
2 |
+
from torchaudio.models.rnnt import _TimeReduction
|
3 |
+
from transformers import PretrainedConfig, PreTrainedModel
|
4 |
+
import torch
|
5 |
+
from torch import nn
|
6 |
+
from typing import List, Tuple, Optional
|
7 |
+
|
8 |
+
|
9 |
+
class ConformerConfig(PretrainedConfig):
|
10 |
+
model_type = 'conformer'
|
11 |
+
|
12 |
+
|
13 |
+
class ConformerEncoder(PreTrainedModel):
|
14 |
+
config_class = ConformerConfig
|
15 |
+
|
16 |
+
def __init__(
|
17 |
+
self,
|
18 |
+
config,
|
19 |
+
) -> None:
|
20 |
+
super().__init__(config)
|
21 |
+
self.time_reduction = _TimeReduction(config.time_reduction_stride)
|
22 |
+
self.input_linear = torch.nn.Linear(
|
23 |
+
config.input_dim * config.time_reduction_stride,
|
24 |
+
config.conformer_input_dim)
|
25 |
+
self.conformer = Conformer(
|
26 |
+
num_layers=config.conformer_num_layers,
|
27 |
+
input_dim=config.conformer_input_dim,
|
28 |
+
ffn_dim=config.conformer_ffn_dim,
|
29 |
+
num_heads=config.conformer_num_heads,
|
30 |
+
depthwise_conv_kernel_size=config.conformer_depthwise_conv_kernel_size,
|
31 |
+
dropout=config.conformer_dropout,
|
32 |
+
use_group_norm=True,
|
33 |
+
convolution_first=True,
|
34 |
+
)
|
35 |
+
self.output_linear = torch.nn.Linear(config.conformer_input_dim, config.output_dim)
|
36 |
+
|
37 |
+
def forward(self, inputs, lengths, labels=None):
|
38 |
+
time_reduction_out, time_reduction_lengths = self.time_reduction(inputs, lengths)
|
39 |
+
input_linear_out = self.input_linear(time_reduction_out)
|
40 |
+
x, input_lengths = self.conformer(input_linear_out, time_reduction_lengths)
|
41 |
+
logits = self.output_linear(x)
|
42 |
+
|
43 |
+
loss = None
|
44 |
+
if labels is not None:
|
45 |
+
labels_mask = labels >= 0
|
46 |
+
target_lengths = labels_mask.sum(-1)
|
47 |
+
flattened_targets = labels.masked_select(labels_mask)
|
48 |
+
log_probs = nn.functional.log_softmax(
|
49 |
+
logits,
|
50 |
+
dim=-1,
|
51 |
+
dtype=torch.float32
|
52 |
+
).transpose(0, 1)
|
53 |
+
|
54 |
+
with torch.backends.cudnn.flags(enabled=False):
|
55 |
+
loss = nn.functional.ctc_loss(
|
56 |
+
log_probs,
|
57 |
+
flattened_targets,
|
58 |
+
input_lengths,
|
59 |
+
target_lengths,
|
60 |
+
blank=self.config.pad_token_id,
|
61 |
+
reduction=self.config.ctc_loss_reduction,
|
62 |
+
zero_infinity=self.config.ctc_zero_infinity,
|
63 |
+
)
|
64 |
+
|
65 |
+
output = (logits, input_lengths)
|
66 |
+
return ((loss,) + output) if loss is not None else output
|
model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bafaee0f2a0d7ada1201d14c875e94b40d0c8560c86421dca23acfd31968da84
|
3 |
+
size 7905192
|