Source code for openspeech.encoders.conformer_encoder

# MIT License
#
# Copyright (c) 2021 Soohwan Kim and Sangchun Ha and Soyoung Cho
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import torch
import torch.nn as nn
from typing import Tuple

from openspeech.encoders.openspeech_encoder import OpenspeechEncoder
from openspeech.modules import Conv2dSubsampling, Linear, ConformerBlock, Transpose


[docs]class ConformerEncoder(OpenspeechEncoder): r""" Transformer models are good at capturing content-based global interactions, while CNNs exploit local features effectively. Conformer achieves the best of both worlds by studying how to combine convolution neural networks and transformers to model both local and global dependencies of an audio sequence in a parameter-efficient way. Args: num_classes (int): Number of classification input_dim (int, optional): Dimension of input vector encoder_dim (int, optional): Dimension of conformer encoders num_layers (int, optional): Number of conformer blocks num_attention_heads (int, optional): Number of attention heads feed_forward_expansion_factor (int, optional): Expansion factor of feed forward module conv_expansion_factor (int, optional): Expansion factor of conformer convolution module feed_forward_dropout_p (float, optional): Probability of feed forward module dropout attention_dropout_p (float, optional): Probability of attention module dropout conv_dropout_p (float, optional): Probability of conformer convolution module dropout conv_kernel_size (int or tuple, optional): Size of the convolving kernel half_step_residual (bool): Flag indication whether to use half step residual or not joint_ctc_attention (bool, optional): flag indication joint ctc attention or not Inputs: inputs, input_lengths - **inputs** (batch, time, dim): Tensor containing input vector - **input_lengths** (batch): list of sequence input lengths Returns: outputs, output_lengths - **outputs** (batch, out_channels, time): Tensor produces by conformer encoders. - **output_lengths** (batch): list of sequence output lengths Reference: Anmol Gulati et al: Conformer: Convolution-augmented Transformer for Speech Recognition https://arxiv.org/abs/2005.08100 """ def __init__( self, num_classes: int, input_dim: int = 80, encoder_dim: int = 512, num_layers: int = 17, num_attention_heads: int = 8, feed_forward_expansion_factor: int = 4, conv_expansion_factor: int = 2, input_dropout_p: float = 0.1, feed_forward_dropout_p: float = 0.1, attention_dropout_p: float = 0.1, conv_dropout_p: float = 0.1, conv_kernel_size: int = 31, half_step_residual: bool = True, joint_ctc_attention: bool = True, ) -> None: super(ConformerEncoder, self).__init__() self.joint_ctc_attention = joint_ctc_attention self.conv_subsample = Conv2dSubsampling(input_dim, in_channels=1, out_channels=encoder_dim) self.input_projection = nn.Sequential( Linear(self.conv_subsample.get_output_dim(), encoder_dim), nn.Dropout(p=input_dropout_p), ) self.layers = nn.ModuleList([ ConformerBlock( encoder_dim=encoder_dim, num_attention_heads=num_attention_heads, feed_forward_expansion_factor=feed_forward_expansion_factor, conv_expansion_factor=conv_expansion_factor, feed_forward_dropout_p=feed_forward_dropout_p, attention_dropout_p=attention_dropout_p, conv_dropout_p=conv_dropout_p, conv_kernel_size=conv_kernel_size, half_step_residual=half_step_residual, ) for _ in range(num_layers) ]) if self.joint_ctc_attention: self.fc = nn.Sequential( Transpose(shape=(1, 2)), nn.Dropout(feed_forward_dropout_p), Linear(encoder_dim, num_classes, bias=False), )
[docs] def forward( self, inputs: torch.Tensor, input_lengths: torch.Tensor, ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: r""" Forward propagate a `inputs` for encoders training. Args: inputs (torch.FloatTensor): A input sequence passed to encoders. Typically for inputs this will be a padded `FloatTensor` of size ``(batch, seq_length, dimension)``. input_lengths (torch.LongTensor): The length of input tensor. ``(batch)`` Returns: (Tensor, Tensor, Tensor) * outputs: A output sequence of encoders. `FloatTensor` of size ``(batch, seq_length, dimension)`` * encoder_logits: Log probability of encoders outputs will be passed to CTC Loss. If joint_ctc_attention is False, return None. * output_lengths: The length of encoders outputs. ``(batch)`` """ encoder_logits = None outputs, output_lengths = self.conv_subsample(inputs, input_lengths) outputs = self.input_projection(outputs) for layer in self.layers: outputs = layer(outputs) if self.joint_ctc_attention: encoder_logits = self.fc(outputs.transpose(1, 2)).log_softmax(dim=2) return outputs, encoder_logits, output_lengths