Initial commit: FunASR Speech Recognition Toolkit
Update API Documentation / build-api-docs (push) Has been cancelled
Update API Documentation / build-api-docs (push) Has been cancelled
Add complete FunASR codebase including models, runtime, and documentation.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
|
||||
from funasr.register import tables
|
||||
|
||||
|
||||
@tables.register("adaptor_classes", "Linear")
|
||||
class Linear(nn.Module):
|
||||
def __init__(self, downsample_rate, encoder_dim, llm_dim, ffn_dim: int = 2048, **kwargs):
|
||||
"""Initialize Linear.
|
||||
|
||||
Args:
|
||||
downsample_rate: TODO.
|
||||
encoder_dim: Size/dimension parameter.
|
||||
llm_dim: Size/dimension parameter.
|
||||
ffn_dim: Size/dimension parameter.
|
||||
**kwargs: Additional keyword arguments.
|
||||
"""
|
||||
super().__init__()
|
||||
self.k = downsample_rate
|
||||
self.encoder_dim = encoder_dim
|
||||
self.llm_dim = llm_dim
|
||||
self.linear1 = nn.Linear(self.encoder_dim * self.k, ffn_dim)
|
||||
self.relu = nn.ReLU()
|
||||
self.linear2 = nn.Linear(ffn_dim, self.llm_dim)
|
||||
|
||||
def forward(self, x):
|
||||
"""Forward pass for training.
|
||||
|
||||
Args:
|
||||
x: TODO.
|
||||
"""
|
||||
batch_size, seq_len, dim = x.size()
|
||||
num_frames_to_discard = seq_len % self.k
|
||||
if num_frames_to_discard > 0:
|
||||
x = x[:, :-num_frames_to_discard, :]
|
||||
seq_len = x.size(1)
|
||||
|
||||
x = x.contiguous()
|
||||
x = x.view(batch_size, seq_len // self.k, dim * self.k)
|
||||
x = self.linear1(x)
|
||||
x = self.relu(x)
|
||||
x = self.linear2(x)
|
||||
return x
|
||||
Reference in New Issue
Block a user