|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +import torch |
| 4 | +from wenet.firered.encoder_layer import FireRedConformerEncoderLayer |
| 5 | +from wenet.transformer.convolution import ConvolutionModule |
| 6 | +from wenet.transformer.encoder import BaseEncoder |
| 7 | +from wenet.utils.class_utils import (WENET_ACTIVATION_CLASSES, |
| 8 | + WENET_ATTENTION_CLASSES, |
| 9 | + WENET_MLP_CLASSES) |
| 10 | + |
| 11 | + |
| 12 | +class FireRedConformerEncoder(BaseEncoder): |
| 13 | + """Conformer encoder module.""" |
| 14 | + |
| 15 | + def __init__( |
| 16 | + self, |
| 17 | + input_size: int, |
| 18 | + output_size: int = 256, |
| 19 | + attention_heads: int = 4, |
| 20 | + linear_units: int = 2048, |
| 21 | + num_blocks: int = 6, |
| 22 | + dropout_rate: float = 0.1, |
| 23 | + positional_dropout_rate: float = 0.1, |
| 24 | + attention_dropout_rate: float = 0.0, |
| 25 | + input_layer: str = "conv2d", |
| 26 | + pos_enc_layer_type: str = "rel_pos", |
| 27 | + normalize_before: bool = True, |
| 28 | + static_chunk_size: int = 0, |
| 29 | + use_dynamic_chunk: bool = False, |
| 30 | + global_cmvn: torch.nn.Module = None, |
| 31 | + use_dynamic_left_chunk: bool = False, |
| 32 | + positionwise_conv_kernel_size: int = 1, |
| 33 | + macaron_style: bool = True, |
| 34 | + selfattention_layer_type: str = "rel_selfattn", |
| 35 | + activation_type: str = "swish", |
| 36 | + use_cnn_module: bool = True, |
| 37 | + cnn_module_kernel: int = 15, |
| 38 | + causal: bool = False, |
| 39 | + cnn_module_norm: str = "batch_norm", |
| 40 | + query_bias: bool = True, |
| 41 | + key_bias: bool = True, |
| 42 | + value_bias: bool = True, |
| 43 | + conv_bias: bool = True, |
| 44 | + gradient_checkpointing: bool = False, |
| 45 | + use_sdpa: bool = False, |
| 46 | + layer_norm_type: str = 'layer_norm', |
| 47 | + norm_eps: float = 1e-5, |
| 48 | + n_kv_head: Optional[int] = None, |
| 49 | + head_dim: Optional[int] = None, |
| 50 | + mlp_type: str = 'position_wise_feed_forward', |
| 51 | + mlp_bias: bool = True, |
| 52 | + n_expert: int = 8, |
| 53 | + n_expert_activated: int = 2, |
| 54 | + conv_norm_eps: float = 1e-5, |
| 55 | + conv_inner_factor: int = 2, |
| 56 | + final_norm: bool = True, |
| 57 | + ): |
| 58 | + """ConstruConformerEncoder |
| 59 | +
|
| 60 | + Args: |
| 61 | + input_size to use_dynamic_chunk, see in BaseEncoder |
| 62 | + positionwise_conv_kernel_size (int): Kernel size of positionwise |
| 63 | + conv1d layer. |
| 64 | + macaron_style (bool): Whether to use macaron style for |
| 65 | + positionwise layer. |
| 66 | + selfattention_layer_type (str): Encoder attention layer type, |
| 67 | + the parameter has no effect now, it's just for configure |
| 68 | + compatibility. |
| 69 | + activation_type (str): Encoder activation function type. |
| 70 | + use_cnn_module (bool): Whether to use convolution module. |
| 71 | + cnn_module_kernel (int): Kernel size of convolution module. |
| 72 | + causal (bool): whether to use causal convolution or not. |
| 73 | + key_bias: whether use bias in attention.linear_k, False for whisper models. |
| 74 | + """ |
| 75 | + super().__init__(input_size, output_size, attention_heads, |
| 76 | + linear_units, num_blocks, dropout_rate, |
| 77 | + positional_dropout_rate, attention_dropout_rate, |
| 78 | + input_layer, pos_enc_layer_type, normalize_before, |
| 79 | + static_chunk_size, use_dynamic_chunk, global_cmvn, |
| 80 | + use_dynamic_left_chunk, gradient_checkpointing, |
| 81 | + use_sdpa, layer_norm_type, norm_eps, final_norm) |
| 82 | + activation = WENET_ACTIVATION_CLASSES[activation_type]() |
| 83 | + |
| 84 | + # self-attention module definition |
| 85 | + encoder_selfattn_layer_args = ( |
| 86 | + attention_heads, |
| 87 | + output_size, |
| 88 | + attention_dropout_rate, |
| 89 | + query_bias, |
| 90 | + key_bias, |
| 91 | + value_bias, |
| 92 | + use_sdpa, |
| 93 | + n_kv_head, |
| 94 | + head_dim, |
| 95 | + ) |
| 96 | + # feed-forward module definition |
| 97 | + positionwise_layer_args = ( |
| 98 | + output_size, |
| 99 | + linear_units, |
| 100 | + dropout_rate, |
| 101 | + activation, |
| 102 | + mlp_bias, |
| 103 | + n_expert, |
| 104 | + n_expert_activated, |
| 105 | + ) |
| 106 | + # convolution module definition |
| 107 | + convolution_layer_args = (output_size, cnn_module_kernel, activation, |
| 108 | + cnn_module_norm, causal, conv_bias, |
| 109 | + conv_norm_eps, conv_inner_factor) |
| 110 | + |
| 111 | + mlp_class = WENET_MLP_CLASSES[mlp_type] |
| 112 | + |
| 113 | + self.encoders = torch.nn.ModuleList([ |
| 114 | + FireRedConformerEncoderLayer( |
| 115 | + output_size, |
| 116 | + WENET_ATTENTION_CLASSES[selfattention_layer_type]( |
| 117 | + *encoder_selfattn_layer_args), |
| 118 | + mlp_class(*positionwise_layer_args), |
| 119 | + mlp_class(*positionwise_layer_args) if macaron_style else None, |
| 120 | + ConvolutionModule( |
| 121 | + *convolution_layer_args) if use_cnn_module else None, |
| 122 | + dropout_rate, |
| 123 | + normalize_before, |
| 124 | + layer_norm_type=layer_norm_type, |
| 125 | + norm_eps=norm_eps, |
| 126 | + ) for _ in range(num_blocks) |
| 127 | + ]) |
0 commit comments