parent
979bbd9dcb
commit
6d867f714d
@ -0,0 +1,98 @@
|
|||||||
|
############################################
|
||||||
|
# Network Architecture #
|
||||||
|
############################################
|
||||||
|
cmvn_file:
|
||||||
|
cmvn_file_type: "json"
|
||||||
|
# encoder related
|
||||||
|
encoder: conformer
|
||||||
|
encoder_conf:
|
||||||
|
encoder_dim: 256 # dimension of attention
|
||||||
|
output_size: 256 # dimension of output
|
||||||
|
attention_heads: 4
|
||||||
|
num_blocks: 12 # the number of encoder blocks
|
||||||
|
reduce_idx: 5
|
||||||
|
recover_idx: 11
|
||||||
|
feed_forward_expansion_factor: 4
|
||||||
|
input_dropout_rate: 0.1
|
||||||
|
feed_forward_dropout_rate: 0.1
|
||||||
|
attention_dropout_rate: 0.1
|
||||||
|
adaptive_scale: true
|
||||||
|
cnn_module_kernel: 31
|
||||||
|
normalize_before: false
|
||||||
|
activation_type: 'swish'
|
||||||
|
pos_enc_layer_type: 'rel_pos'
|
||||||
|
time_reduction_layer_type: 'conv2d'
|
||||||
|
causal: true
|
||||||
|
use_dynamic_chunk: true
|
||||||
|
use_dynamic_left_chunk: false
|
||||||
|
|
||||||
|
# decoder related
|
||||||
|
decoder: transformer
|
||||||
|
decoder_conf:
|
||||||
|
attention_heads: 4
|
||||||
|
linear_units: 2048
|
||||||
|
num_blocks: 6
|
||||||
|
dropout_rate: 0.1 # sublayer output dropout
|
||||||
|
positional_dropout_rate: 0.1
|
||||||
|
self_attention_dropout_rate: 0.0
|
||||||
|
src_attention_dropout_rate: 0.0
|
||||||
|
# hybrid CTC/attention
|
||||||
|
model_conf:
|
||||||
|
ctc_weight: 0.3
|
||||||
|
lsm_weight: 0.1 # label smoothing option
|
||||||
|
length_normalized_loss: false
|
||||||
|
init_type: 'kaiming_uniform' # !Warning: need to convergence
|
||||||
|
|
||||||
|
###########################################
|
||||||
|
# Data #
|
||||||
|
###########################################
|
||||||
|
|
||||||
|
train_manifest: data/manifest.train
|
||||||
|
dev_manifest: data/manifest.dev
|
||||||
|
test_manifest: data/manifest.test
|
||||||
|
|
||||||
|
|
||||||
|
###########################################
|
||||||
|
# Dataloader #
|
||||||
|
###########################################
|
||||||
|
|
||||||
|
vocab_filepath: data/lang_char/vocab.txt
|
||||||
|
spm_model_prefix: ''
|
||||||
|
unit_type: 'char'
|
||||||
|
preprocess_config: conf/preprocess.yaml
|
||||||
|
feat_dim: 80
|
||||||
|
stride_ms: 10.0
|
||||||
|
window_ms: 25.0
|
||||||
|
sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs
|
||||||
|
batch_size: 32
|
||||||
|
maxlen_in: 512 # if input length > maxlen-in, batchsize is automatically reduced
|
||||||
|
maxlen_out: 150 # if output length > maxlen-out, batchsize is automatically reduced
|
||||||
|
minibatches: 0 # for debug
|
||||||
|
batch_count: auto
|
||||||
|
batch_bins: 0
|
||||||
|
batch_frames_in: 0
|
||||||
|
batch_frames_out: 0
|
||||||
|
batch_frames_inout: 0
|
||||||
|
num_workers: 2
|
||||||
|
subsampling_factor: 1
|
||||||
|
num_encs: 1
|
||||||
|
|
||||||
|
###########################################
|
||||||
|
# Training #
|
||||||
|
###########################################
|
||||||
|
n_epoch: 240
|
||||||
|
accum_grad: 1
|
||||||
|
global_grad_clip: 5.0
|
||||||
|
dist_sampler: True
|
||||||
|
optim: adam
|
||||||
|
optim_conf:
|
||||||
|
lr: 0.001
|
||||||
|
weight_decay: 1.0e-6
|
||||||
|
scheduler: warmuplr
|
||||||
|
scheduler_conf:
|
||||||
|
warmup_steps: 25000
|
||||||
|
lr_decay: 1.0
|
||||||
|
log_interval: 100
|
||||||
|
checkpoint:
|
||||||
|
kbest_n: 50
|
||||||
|
latest_n: 5
|
@ -0,0 +1,93 @@
|
|||||||
|
############################################
|
||||||
|
# Network Architecture #
|
||||||
|
############################################
|
||||||
|
cmvn_file:
|
||||||
|
cmvn_file_type: "json"
|
||||||
|
# encoder related
|
||||||
|
encoder: squeezeformer
|
||||||
|
encoder_conf:
|
||||||
|
encoder_dim: 256 # dimension of attention
|
||||||
|
output_size: 256 # dimension of output
|
||||||
|
attention_heads: 4
|
||||||
|
num_blocks: 12 # the number of encoder blocks
|
||||||
|
reduce_idx: 5
|
||||||
|
recover_idx: 11
|
||||||
|
feed_forward_expansion_factor: 4
|
||||||
|
input_dropout_rate: 0.1
|
||||||
|
feed_forward_dropout_rate: 0.1
|
||||||
|
attention_dropout_rate: 0.1
|
||||||
|
adaptive_scale: true
|
||||||
|
cnn_module_kernel: 31
|
||||||
|
normalize_before: false
|
||||||
|
activation_type: 'swish'
|
||||||
|
pos_enc_layer_type: 'rel_pos'
|
||||||
|
time_reduction_layer_type: 'conv2d'
|
||||||
|
|
||||||
|
# decoder related
|
||||||
|
decoder: transformer
|
||||||
|
decoder_conf:
|
||||||
|
attention_heads: 4
|
||||||
|
linear_units: 2048
|
||||||
|
num_blocks: 6
|
||||||
|
dropout_rate: 0.1
|
||||||
|
positional_dropout_rate: 0.1
|
||||||
|
self_attention_dropout_rate: 0.0
|
||||||
|
src_attention_dropout_rate: 0.0
|
||||||
|
|
||||||
|
# hybrid CTC/attention
|
||||||
|
model_conf:
|
||||||
|
ctc_weight: 0.3
|
||||||
|
lsm_weight: 0.1 # label smoothing option
|
||||||
|
length_normalized_loss: false
|
||||||
|
init_type: 'kaiming_uniform' # !Warning: need to convergence
|
||||||
|
|
||||||
|
###########################################
|
||||||
|
# Data #
|
||||||
|
###########################################
|
||||||
|
train_manifest: data/manifest.train
|
||||||
|
dev_manifest: data/manifest.dev
|
||||||
|
test_manifest: data/manifest.test
|
||||||
|
|
||||||
|
###########################################
|
||||||
|
# Dataloader #
|
||||||
|
###########################################
|
||||||
|
vocab_filepath: data/lang_char/vocab.txt
|
||||||
|
spm_model_prefix: ''
|
||||||
|
unit_type: 'char'
|
||||||
|
preprocess_config: conf/preprocess.yaml
|
||||||
|
feat_dim: 80
|
||||||
|
stride_ms: 10.0
|
||||||
|
window_ms: 25.0
|
||||||
|
sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs
|
||||||
|
batch_size: 32
|
||||||
|
maxlen_in: 512 # if input length > maxlen-in, batchsize is automatically reduced
|
||||||
|
maxlen_out: 150 # if output length > maxlen-out, batchsize is automatically reduced
|
||||||
|
minibatches: 0 # for debug
|
||||||
|
batch_count: auto
|
||||||
|
batch_bins: 0
|
||||||
|
batch_frames_in: 0
|
||||||
|
batch_frames_out: 0
|
||||||
|
batch_frames_inout: 0
|
||||||
|
num_workers: 2
|
||||||
|
subsampling_factor: 1
|
||||||
|
num_encs: 1
|
||||||
|
|
||||||
|
###########################################
|
||||||
|
# Training #
|
||||||
|
###########################################
|
||||||
|
n_epoch: 150
|
||||||
|
accum_grad: 8
|
||||||
|
global_grad_clip: 5.0
|
||||||
|
dist_sampler: False
|
||||||
|
optim: adam
|
||||||
|
optim_conf:
|
||||||
|
lr: 0.002
|
||||||
|
weight_decay: 1.0e-6
|
||||||
|
scheduler: warmuplr
|
||||||
|
scheduler_conf:
|
||||||
|
warmup_steps: 25000
|
||||||
|
lr_decay: 1.0
|
||||||
|
log_interval: 100
|
||||||
|
checkpoint:
|
||||||
|
kbest_n: 50
|
||||||
|
latest_n: 5
|
@ -0,0 +1,56 @@
|
|||||||
|
from typing import Union, Optional
|
||||||
|
|
||||||
|
import paddle
|
||||||
|
import paddle.nn.functional as F
|
||||||
|
from paddle.nn.layer.conv import _ConvNd
|
||||||
|
|
||||||
|
__all__ = ['Conv2DValid']
|
||||||
|
|
||||||
|
|
||||||
|
class Conv2DValid(_ConvNd):
|
||||||
|
"""
|
||||||
|
Conv2d operator for VALID mode padding.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
in_channels: int,
|
||||||
|
out_channels: int,
|
||||||
|
kernel_size: int,
|
||||||
|
stride: int = 1,
|
||||||
|
padding: Union[str, int] = 0,
|
||||||
|
dilation: int = 1,
|
||||||
|
groups: int = 1,
|
||||||
|
padding_mode: str = 'zeros',
|
||||||
|
weight_attr=None,
|
||||||
|
bias_attr=None,
|
||||||
|
data_format="NCHW",
|
||||||
|
valid_trigx: bool = False,
|
||||||
|
valid_trigy: bool = False
|
||||||
|
) -> None:
|
||||||
|
super(Conv2DValid, self).__init__(in_channels,
|
||||||
|
out_channels,
|
||||||
|
kernel_size,
|
||||||
|
False,
|
||||||
|
2,
|
||||||
|
stride=stride,
|
||||||
|
padding=padding,
|
||||||
|
padding_mode=padding_mode,
|
||||||
|
dilation=dilation,
|
||||||
|
groups=groups,
|
||||||
|
weight_attr=weight_attr,
|
||||||
|
bias_attr=bias_attr,
|
||||||
|
data_format=data_format)
|
||||||
|
self.valid_trigx = valid_trigx
|
||||||
|
self.valid_trigy = valid_trigy
|
||||||
|
|
||||||
|
def _conv_forward(self, input: paddle.Tensor, weight: paddle.Tensor, bias: Optional[paddle.Tensor]):
|
||||||
|
validx, validy = 0, 0
|
||||||
|
if self.valid_trigx:
|
||||||
|
validx = (input.shape[-2] * (self._stride[-2] - 1) - 1 + self._kernel_size[-2]) // 2
|
||||||
|
if self.valid_trigy:
|
||||||
|
validy = (input.shape[-1] * (self._stride[-1] - 1) - 1 + self._kernel_size[-1]) // 2
|
||||||
|
return F.conv2d(input, weight, bias, self._stride, (validx, validy), self._dilation, self._groups)
|
||||||
|
|
||||||
|
def forward(self, input: paddle.Tensor) -> paddle.Tensor:
|
||||||
|
return self._conv_forward(input, self.weight, self.bias)
|
@ -0,0 +1,172 @@
|
|||||||
|
from typing import Tuple
|
||||||
|
|
||||||
|
import paddle
|
||||||
|
from paddle import nn
|
||||||
|
from paddle.nn import initializer as I
|
||||||
|
from typeguard import check_argument_types
|
||||||
|
|
||||||
|
__all__ = ['ConvolutionModule']
|
||||||
|
|
||||||
|
from paddlespeech.s2t import masked_fill
|
||||||
|
from paddlespeech.s2t.modules.align import Conv1D, BatchNorm1D, LayerNorm
|
||||||
|
|
||||||
|
|
||||||
|
class ConvolutionModule2(nn.Layer):
|
||||||
|
"""ConvolutionModule in Conformer model."""
|
||||||
|
|
||||||
|
def __init__(self,
|
||||||
|
channels: int,
|
||||||
|
kernel_size: int = 15,
|
||||||
|
activation: nn.Layer = nn.ReLU(),
|
||||||
|
norm: str = "batch_norm",
|
||||||
|
causal: bool = False,
|
||||||
|
bias: bool = True,
|
||||||
|
adaptive_scale: bool = False,
|
||||||
|
init_weights: bool = False):
|
||||||
|
"""Construct an ConvolutionModule object.
|
||||||
|
Args:
|
||||||
|
channels (int): The number of channels of conv layers.
|
||||||
|
kernel_size (int): Kernel size of conv layers.
|
||||||
|
causal (int): Whether use causal convolution or not
|
||||||
|
"""
|
||||||
|
assert check_argument_types()
|
||||||
|
super().__init__()
|
||||||
|
self.bias = bias
|
||||||
|
self.channels = channels
|
||||||
|
self.kernel_size = kernel_size
|
||||||
|
self.adaptive_scale = adaptive_scale
|
||||||
|
ada_scale = self.create_parameter([1, 1, channels], default_initializer=I.Constant(1.0))
|
||||||
|
self.add_parameter('ada_scale', ada_scale)
|
||||||
|
ada_bias = self.create_parameter([1, 1, channels], default_initializer=I.Constant(0.0))
|
||||||
|
self.add_parameter('ada_bias', ada_bias)
|
||||||
|
|
||||||
|
self.pointwise_conv1 = Conv1D(
|
||||||
|
channels,
|
||||||
|
2 * channels,
|
||||||
|
kernel_size=1,
|
||||||
|
stride=1,
|
||||||
|
padding=0,
|
||||||
|
bias_attr=None
|
||||||
|
if bias else False, # None for True, using bias as default config
|
||||||
|
)
|
||||||
|
|
||||||
|
# self.lorder is used to distinguish if it's a causal convolution,
|
||||||
|
# if self.lorder > 0: it's a causal convolution, the input will be
|
||||||
|
# padded with self.lorder frames on the left in forward.
|
||||||
|
# else: it's a symmetrical convolution
|
||||||
|
if causal:
|
||||||
|
padding = 0
|
||||||
|
self.lorder = kernel_size - 1
|
||||||
|
else:
|
||||||
|
# kernel_size should be an odd number for none causal convolution
|
||||||
|
assert (kernel_size - 1) % 2 == 0
|
||||||
|
padding = (kernel_size - 1) // 2
|
||||||
|
self.lorder = 0
|
||||||
|
self.depthwise_conv = Conv1D(
|
||||||
|
channels,
|
||||||
|
channels,
|
||||||
|
kernel_size,
|
||||||
|
stride=1,
|
||||||
|
padding=padding,
|
||||||
|
groups=channels,
|
||||||
|
bias_attr=None
|
||||||
|
if bias else False, # None for True, using bias as default config
|
||||||
|
)
|
||||||
|
|
||||||
|
assert norm in ['batch_norm', 'layer_norm']
|
||||||
|
if norm == "batch_norm":
|
||||||
|
self.use_layer_norm = False
|
||||||
|
self.norm = BatchNorm1D(channels)
|
||||||
|
else:
|
||||||
|
self.use_layer_norm = True
|
||||||
|
self.norm = LayerNorm(channels)
|
||||||
|
|
||||||
|
self.pointwise_conv2 = Conv1D(
|
||||||
|
channels,
|
||||||
|
channels,
|
||||||
|
kernel_size=1,
|
||||||
|
stride=1,
|
||||||
|
padding=0,
|
||||||
|
bias_attr=None
|
||||||
|
if bias else False, # None for True, using bias as default config
|
||||||
|
)
|
||||||
|
self.activation = activation
|
||||||
|
|
||||||
|
if init_weights:
|
||||||
|
self.init_weights()
|
||||||
|
|
||||||
|
def init_weights(self):
|
||||||
|
pw_max = self.channels ** -0.5
|
||||||
|
dw_max = self.kernel_size ** -0.5
|
||||||
|
self.pointwise_conv1._param_attr = paddle.nn.initializer.Uniform(low=-pw_max, high=pw_max)
|
||||||
|
if self.bias:
|
||||||
|
self.pointwise_conv1._bias_attr = paddle.nn.initializer.Uniform(low=-pw_max, high=pw_max)
|
||||||
|
self.depthwise_conv._param_attr = paddle.nn.initializer.Uniform(low=-dw_max, high=dw_max)
|
||||||
|
if self.bias:
|
||||||
|
self.depthwise_conv._bias_attr = paddle.nn.initializer.Uniform(low=-dw_max, high=dw_max)
|
||||||
|
self.pointwise_conv2._param_attr = paddle.nn.initializer.Uniform(low=-pw_max, high=pw_max)
|
||||||
|
if self.bias:
|
||||||
|
self.pointwise_conv2._bias_attr = paddle.nn.initializer.Uniform(low=-pw_max, high=pw_max)
|
||||||
|
|
||||||
|
def forward(
|
||||||
|
self,
|
||||||
|
x: paddle.Tensor,
|
||||||
|
mask_pad: paddle.Tensor = paddle.ones([0, 0, 0], dtype=paddle.bool),
|
||||||
|
cache: paddle.Tensor = paddle.zeros([0, 0, 0]),
|
||||||
|
) -> Tuple[paddle.Tensor, paddle.Tensor]:
|
||||||
|
"""Compute convolution module.
|
||||||
|
Args:
|
||||||
|
x (torch.Tensor): Input tensor (#batch, time, channels).
|
||||||
|
mask_pad (torch.Tensor): used for batch padding (#batch, 1, time),
|
||||||
|
(0, 0, 0) means fake mask.
|
||||||
|
cache (torch.Tensor): left context cache, it is only
|
||||||
|
used in causal convolution (#batch, channels, cache_t),
|
||||||
|
(0, 0, 0) meas fake cache.
|
||||||
|
Returns:
|
||||||
|
torch.Tensor: Output tensor (#batch, time, channels).
|
||||||
|
"""
|
||||||
|
if self.adaptive_scale:
|
||||||
|
x = self.ada_scale * x + self.ada_bias
|
||||||
|
|
||||||
|
# exchange the temporal dimension and the feature dimension
|
||||||
|
x = x.transpose([0, 2, 1]) # [B, C, T]
|
||||||
|
|
||||||
|
# mask batch padding
|
||||||
|
if mask_pad.shape[2] > 0: # time > 0
|
||||||
|
x = masked_fill(x, mask_pad, 0.0)
|
||||||
|
|
||||||
|
if self.lorder > 0:
|
||||||
|
if cache.shape[2] == 0: # cache_t == 0
|
||||||
|
x = nn.functional.pad(x, [self.lorder, 0], 'constant', 0.0, data_format='NCL')
|
||||||
|
else:
|
||||||
|
assert cache.shape[0] == x.shape[0] # B
|
||||||
|
assert cache.shape[1] == x.shape[1] # C
|
||||||
|
x = paddle.concat((cache, x), axis=2)
|
||||||
|
|
||||||
|
assert (x.shape[2] > self.lorder)
|
||||||
|
new_cache = x[:, :, -self.lorder:] # [B, C, T]
|
||||||
|
else:
|
||||||
|
# It's better we just return None if no cache is required,
|
||||||
|
# However, for JIT export, here we just fake one tensor instead of
|
||||||
|
# None.
|
||||||
|
new_cache = paddle.zeros([0, 0, 0], dtype=x.dtype)
|
||||||
|
|
||||||
|
# GLU mechanism
|
||||||
|
x = self.pointwise_conv1(x) # (batch, 2*channel, dim)
|
||||||
|
x = nn.functional.glu(x, axis=1) # (batch, channel, dim)
|
||||||
|
|
||||||
|
# 1D Depthwise Conv
|
||||||
|
x = self.depthwise_conv(x)
|
||||||
|
if self.use_layer_norm:
|
||||||
|
x = x.transpose([0, 2, 1]) # [B, T, C]
|
||||||
|
x = self.activation(self.norm(x))
|
||||||
|
if self.use_layer_norm:
|
||||||
|
x = x.transpose([0, 2, 1]) # [B, C, T]
|
||||||
|
x = self.pointwise_conv2(x)
|
||||||
|
|
||||||
|
# mask batch padding
|
||||||
|
if mask_pad.shape[2] > 0: # time > 0
|
||||||
|
x = masked_fill(x, mask_pad, 0.0)
|
||||||
|
|
||||||
|
x = x.transpose([0, 2, 1]) # [B, T, C]
|
||||||
|
return x, new_cache
|
Loading…
Reference in new issue