|
|
|
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
|
|
|
|
# Copyright 2019 Mobvoi Inc. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
# Modified from wenet(https://github.com/wenet-e2e/wenet)
|
|
|
|
"""Positonal Encoding Module."""
|
|
|
|
import math
|
|
|
|
from typing import Tuple
|
|
|
|
|
|
|
|
import paddle
|
|
|
|
from paddle import nn
|
|
|
|
|
|
|
|
from paddlespeech.s2t.utils.log import Log
|
|
|
|
|
E2E/Streaming Transformer/Conformer ASR (#578)
* add cmvn and label smoothing loss layer
* add layer for transformer
* add glu and conformer conv
* add torch compatiable hack, mask funcs
* not hack size since it exists
* add test; attention
* add attention, common utils, hack paddle
* add audio utils
* conformer batch padding mask bug fix #223
* fix typo, python infer fix rnn mem opt name error and batchnorm1d, will be available at 2.0.2
* fix ci
* fix ci
* add encoder
* refactor egs
* add decoder
* refactor ctc, add ctc align, refactor ckpt, add warmup lr scheduler, cmvn utils
* refactor docs
* add fix
* fix readme
* fix bugs, refactor collator, add pad_sequence, fix ckpt bugs
* fix docstring
* refactor data feed order
* add u2 model
* refactor cmvn, test
* add utils
* add u2 config
* fix bugs
* fix bugs
* fix autograd maybe has problem when using inplace operation
* refactor data, build vocab; add format data
* fix text featurizer
* refactor build vocab
* add fbank, refactor feature of speech
* refactor audio feat
* refactor data preprare
* refactor data
* model init from config
* add u2 bins
* flake8
* can train
* fix bugs, add coverage, add scripts
* test can run
* fix data
* speed perturb with sox
* add spec aug
* fix for train
* fix train logitc
* fix logger
* log valid loss, time dataset process
* using np for speed perturb, remove some debug log of grad clip
* fix logger
* fix build vocab
* fix logger name
* using module logger as default
* fix
* fix install
* reorder imports
* fix board logger
* fix logger
* kaldi fbank and mfcc
* fix cmvn and print prarams
* fix add_eos_sos and cmvn
* fix cmvn compute
* fix logger and cmvn
* fix subsampling, label smoothing loss, remove useless
* add notebook test
* fix log
* fix tb logger
* multi gpu valid
* fix log
* fix log
* fix config
* fix compute cmvn, need paddle 2.1
* add cmvn notebook
* fix layer tools
* fix compute cmvn
* add rtf
* fix decoding
* fix layer tools
* fix log, add avg script
* more avg and test info
* fix dataset pickle problem; using 2.1 paddle; num_workers can > 0; ckpt save in exp dir;fix setup.sh;
* add vimrc
* refactor tiny script, add transformer and stream conf
* spm demo; librisppech scripts and confs
* fix log
* add librispeech scripts
* refactor data pipe; fix conf; fix u2 default params
* fix bugs
* refactor aishell scripts
* fix test
* fix cmvn
* fix s0 scripts
* fix ds2 scripts and bugs
* fix dev & test dataset filter
* fix dataset filter
* filter dev
* fix ckpt path
* filter test, since librispeech will cause OOM, but all test wer will be worse, since mismatch train with test
* add comment
* add syllable doc
* fix ds2 configs
* add doc
* add pypinyin tools
* fix decoder using blank_id=0
* mmseg with pybind11
* format code
4 years ago
|
|
|
logger = Log(__name__).getlog()
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
"PositionalEncodingInterface", "NoPositionalEncoding", "PositionalEncoding",
|
|
|
|
"RelPositionalEncoding"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class PositionalEncodingInterface:
|
|
|
|
def forward(self, x: paddle.Tensor,
|
|
|
|
offset: int=0) -> Tuple[paddle.Tensor, paddle.Tensor]:
|
|
|
|
"""Compute positional encoding.
|
|
|
|
Args:
|
|
|
|
x (paddle.Tensor): Input tensor (batch, time, `*`).
|
|
|
|
Returns:
|
|
|
|
paddle.Tensor: Encoded tensor (batch, time, `*`).
|
|
|
|
paddle.Tensor: Positional embedding tensor (1, time, `*`).
|
|
|
|
"""
|
|
|
|
raise NotImplementedError("forward method is not implemented")
|
|
|
|
|
|
|
|
def position_encoding(self, offset: int, size: int) -> paddle.Tensor:
|
|
|
|
""" For getting encoding in a streaming fashion
|
|
|
|
Args:
|
|
|
|
offset (int): start offset
|
|
|
|
size (int): requried size of position encoding
|
|
|
|
Returns:
|
|
|
|
paddle.Tensor: Corresponding position encoding
|
|
|
|
"""
|
|
|
|
raise NotImplementedError("position_encoding method is not implemented")
|
|
|
|
|
|
|
|
|
|
|
|
class NoPositionalEncoding(nn.Layer, PositionalEncodingInterface):
|
|
|
|
def __init__(self,
|
|
|
|
d_model: int,
|
|
|
|
dropout_rate: float,
|
|
|
|
max_len: int=5000,
|
|
|
|
reverse: bool=False):
|
|
|
|
nn.Layer.__init__(self)
|
|
|
|
|
|
|
|
def forward(self, x: paddle.Tensor,
|
|
|
|
offset: int=0) -> Tuple[paddle.Tensor, paddle.Tensor]:
|
|
|
|
return x, None
|
|
|
|
|
|
|
|
def position_encoding(self, offset: int, size: int) -> paddle.Tensor:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
class PositionalEncoding(nn.Layer, PositionalEncodingInterface):
|
|
|
|
def __init__(self,
|
|
|
|
d_model: int,
|
|
|
|
dropout_rate: float,
|
|
|
|
max_len: int=5000,
|
|
|
|
reverse: bool=False):
|
|
|
|
"""Positional encoding.
|
|
|
|
PE(pos, 2i) = sin(pos/(10000^(2i/dmodel)))
|
|
|
|
PE(pos, 2i+1) = cos(pos/(10000^(2i/dmodel)))
|
|
|
|
Args:
|
|
|
|
d_model (int): embedding dim.
|
|
|
|
dropout_rate (float): dropout rate.
|
|
|
|
max_len (int, optional): maximum input length. Defaults to 5000.
|
|
|
|
reverse (bool, optional): Not used. Defaults to False.
|
|
|
|
"""
|
|
|
|
nn.Layer.__init__(self)
|
|
|
|
self.d_model = d_model
|
|
|
|
self.max_len = max_len
|
|
|
|
self.xscale = paddle.to_tensor(math.sqrt(self.d_model))
|
|
|
|
self.dropout = nn.Dropout(p=dropout_rate)
|
|
|
|
self.pe = paddle.zeros([1, self.max_len, self.d_model]) #[B=1,T,D]
|
|
|
|
|
|
|
|
position = paddle.arange(
|
E2E/Streaming Transformer/Conformer ASR (#578)
* add cmvn and label smoothing loss layer
* add layer for transformer
* add glu and conformer conv
* add torch compatiable hack, mask funcs
* not hack size since it exists
* add test; attention
* add attention, common utils, hack paddle
* add audio utils
* conformer batch padding mask bug fix #223
* fix typo, python infer fix rnn mem opt name error and batchnorm1d, will be available at 2.0.2
* fix ci
* fix ci
* add encoder
* refactor egs
* add decoder
* refactor ctc, add ctc align, refactor ckpt, add warmup lr scheduler, cmvn utils
* refactor docs
* add fix
* fix readme
* fix bugs, refactor collator, add pad_sequence, fix ckpt bugs
* fix docstring
* refactor data feed order
* add u2 model
* refactor cmvn, test
* add utils
* add u2 config
* fix bugs
* fix bugs
* fix autograd maybe has problem when using inplace operation
* refactor data, build vocab; add format data
* fix text featurizer
* refactor build vocab
* add fbank, refactor feature of speech
* refactor audio feat
* refactor data preprare
* refactor data
* model init from config
* add u2 bins
* flake8
* can train
* fix bugs, add coverage, add scripts
* test can run
* fix data
* speed perturb with sox
* add spec aug
* fix for train
* fix train logitc
* fix logger
* log valid loss, time dataset process
* using np for speed perturb, remove some debug log of grad clip
* fix logger
* fix build vocab
* fix logger name
* using module logger as default
* fix
* fix install
* reorder imports
* fix board logger
* fix logger
* kaldi fbank and mfcc
* fix cmvn and print prarams
* fix add_eos_sos and cmvn
* fix cmvn compute
* fix logger and cmvn
* fix subsampling, label smoothing loss, remove useless
* add notebook test
* fix log
* fix tb logger
* multi gpu valid
* fix log
* fix log
* fix config
* fix compute cmvn, need paddle 2.1
* add cmvn notebook
* fix layer tools
* fix compute cmvn
* add rtf
* fix decoding
* fix layer tools
* fix log, add avg script
* more avg and test info
* fix dataset pickle problem; using 2.1 paddle; num_workers can > 0; ckpt save in exp dir;fix setup.sh;
* add vimrc
* refactor tiny script, add transformer and stream conf
* spm demo; librisppech scripts and confs
* fix log
* add librispeech scripts
* refactor data pipe; fix conf; fix u2 default params
* fix bugs
* refactor aishell scripts
* fix test
* fix cmvn
* fix s0 scripts
* fix ds2 scripts and bugs
* fix dev & test dataset filter
* fix dataset filter
* filter dev
* fix ckpt path
* filter test, since librispeech will cause OOM, but all test wer will be worse, since mismatch train with test
* add comment
* add syllable doc
* fix ds2 configs
* add doc
* add pypinyin tools
* fix decoder using blank_id=0
* mmseg with pybind11
* format code
4 years ago
|
|
|
0, self.max_len, dtype=paddle.float32).unsqueeze(1) #[T, 1]
|
|
|
|
div_term = paddle.exp(
|
|
|
|
paddle.arange(0, self.d_model, 2, dtype=paddle.float32) *
|
|
|
|
-(math.log(10000.0) / self.d_model))
|
|
|
|
|
|
|
|
self.pe[:, :, 0::2] = paddle.sin(position * div_term)
|
|
|
|
self.pe[:, :, 1::2] = paddle.cos(position * div_term)
|
|
|
|
|
|
|
|
def forward(self, x: paddle.Tensor,
|
|
|
|
offset: int=0) -> Tuple[paddle.Tensor, paddle.Tensor]:
|
|
|
|
"""Add positional encoding.
|
|
|
|
Args:
|
|
|
|
x (paddle.Tensor): Input. Its shape is (batch, time, ...)
|
|
|
|
offset (int): position offset
|
|
|
|
Returns:
|
|
|
|
paddle.Tensor: Encoded tensor. Its shape is (batch, time, ...)
|
E2E/Streaming Transformer/Conformer ASR (#578)
* add cmvn and label smoothing loss layer
* add layer for transformer
* add glu and conformer conv
* add torch compatiable hack, mask funcs
* not hack size since it exists
* add test; attention
* add attention, common utils, hack paddle
* add audio utils
* conformer batch padding mask bug fix #223
* fix typo, python infer fix rnn mem opt name error and batchnorm1d, will be available at 2.0.2
* fix ci
* fix ci
* add encoder
* refactor egs
* add decoder
* refactor ctc, add ctc align, refactor ckpt, add warmup lr scheduler, cmvn utils
* refactor docs
* add fix
* fix readme
* fix bugs, refactor collator, add pad_sequence, fix ckpt bugs
* fix docstring
* refactor data feed order
* add u2 model
* refactor cmvn, test
* add utils
* add u2 config
* fix bugs
* fix bugs
* fix autograd maybe has problem when using inplace operation
* refactor data, build vocab; add format data
* fix text featurizer
* refactor build vocab
* add fbank, refactor feature of speech
* refactor audio feat
* refactor data preprare
* refactor data
* model init from config
* add u2 bins
* flake8
* can train
* fix bugs, add coverage, add scripts
* test can run
* fix data
* speed perturb with sox
* add spec aug
* fix for train
* fix train logitc
* fix logger
* log valid loss, time dataset process
* using np for speed perturb, remove some debug log of grad clip
* fix logger
* fix build vocab
* fix logger name
* using module logger as default
* fix
* fix install
* reorder imports
* fix board logger
* fix logger
* kaldi fbank and mfcc
* fix cmvn and print prarams
* fix add_eos_sos and cmvn
* fix cmvn compute
* fix logger and cmvn
* fix subsampling, label smoothing loss, remove useless
* add notebook test
* fix log
* fix tb logger
* multi gpu valid
* fix log
* fix log
* fix config
* fix compute cmvn, need paddle 2.1
* add cmvn notebook
* fix layer tools
* fix compute cmvn
* add rtf
* fix decoding
* fix layer tools
* fix log, add avg script
* more avg and test info
* fix dataset pickle problem; using 2.1 paddle; num_workers can > 0; ckpt save in exp dir;fix setup.sh;
* add vimrc
* refactor tiny script, add transformer and stream conf
* spm demo; librisppech scripts and confs
* fix log
* add librispeech scripts
* refactor data pipe; fix conf; fix u2 default params
* fix bugs
* refactor aishell scripts
* fix test
* fix cmvn
* fix s0 scripts
* fix ds2 scripts and bugs
* fix dev & test dataset filter
* fix dataset filter
* filter dev
* fix ckpt path
* filter test, since librispeech will cause OOM, but all test wer will be worse, since mismatch train with test
* add comment
* add syllable doc
* fix ds2 configs
* add doc
* add pypinyin tools
* fix decoder using blank_id=0
* mmseg with pybind11
* format code
4 years ago
|
|
|
paddle.Tensor: for compatibility to RelPositionalEncoding, (batch=1, time, ...)
|
|
|
|
"""
|
|
|
|
assert offset + x.shape[
|
|
|
|
1] < self.max_len, "offset: {} + x.shape[1]: {} is larger than the max_len: {}".format(
|
|
|
|
offset, x.shape[1], self.max_len)
|
|
|
|
pos_emb = self.pe[:, offset:offset + x.shape[1]]
|
|
|
|
x = x * self.xscale + pos_emb
|
|
|
|
return self.dropout(x), self.dropout(pos_emb)
|
|
|
|
|
|
|
|
def position_encoding(self, offset: int, size: int) -> paddle.Tensor:
|
|
|
|
""" For getting encoding in a streaming fashion
|
|
|
|
Attention!!!!!
|
|
|
|
we apply dropout only once at the whole utterance level in a none
|
|
|
|
streaming way, but will call this function several times with
|
|
|
|
increasing input size in a streaming scenario, so the dropout will
|
|
|
|
be applied several times.
|
|
|
|
Args:
|
|
|
|
offset (int): start offset
|
|
|
|
size (int): requried size of position encoding
|
|
|
|
Returns:
|
|
|
|
paddle.Tensor: Corresponding position encoding, #[1, T, D].
|
|
|
|
"""
|
|
|
|
assert offset + size < self.max_len
|
|
|
|
return self.dropout(self.pe[:, offset:offset + size])
|
|
|
|
|
|
|
|
|
|
|
|
class RelPositionalEncoding(PositionalEncoding):
|
|
|
|
"""Relative positional encoding module.
|
|
|
|
See : Appendix B in https://arxiv.org/abs/1901.02860
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, d_model: int, dropout_rate: float, max_len: int=5000):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
d_model (int): Embedding dimension.
|
|
|
|
dropout_rate (float): Dropout rate.
|
|
|
|
max_len (int, optional): [Maximum input length.]. Defaults to 5000.
|
|
|
|
"""
|
|
|
|
super().__init__(d_model, dropout_rate, max_len, reverse=True)
|
|
|
|
logger.info(f"max len: {max_len}")
|
|
|
|
|
|
|
|
def forward(self, x: paddle.Tensor,
|
|
|
|
offset: int=0) -> Tuple[paddle.Tensor, paddle.Tensor]:
|
|
|
|
"""Compute positional encoding.
|
|
|
|
Args:
|
|
|
|
x (paddle.Tensor): Input tensor (batch, time, `*`).
|
|
|
|
Returns:
|
|
|
|
paddle.Tensor: Encoded tensor (batch, time, `*`).
|
|
|
|
paddle.Tensor: Positional embedding tensor (1, time, `*`).
|
|
|
|
"""
|
|
|
|
assert offset + x.shape[
|
|
|
|
1] < self.max_len, "offset: {} + x.shape[1]: {} is larger than the max_len: {}".format(
|
|
|
|
offset, x.shape[1], self.max_len)
|
|
|
|
x = x * self.xscale
|
E2E/Streaming Transformer/Conformer ASR (#578)
* add cmvn and label smoothing loss layer
* add layer for transformer
* add glu and conformer conv
* add torch compatiable hack, mask funcs
* not hack size since it exists
* add test; attention
* add attention, common utils, hack paddle
* add audio utils
* conformer batch padding mask bug fix #223
* fix typo, python infer fix rnn mem opt name error and batchnorm1d, will be available at 2.0.2
* fix ci
* fix ci
* add encoder
* refactor egs
* add decoder
* refactor ctc, add ctc align, refactor ckpt, add warmup lr scheduler, cmvn utils
* refactor docs
* add fix
* fix readme
* fix bugs, refactor collator, add pad_sequence, fix ckpt bugs
* fix docstring
* refactor data feed order
* add u2 model
* refactor cmvn, test
* add utils
* add u2 config
* fix bugs
* fix bugs
* fix autograd maybe has problem when using inplace operation
* refactor data, build vocab; add format data
* fix text featurizer
* refactor build vocab
* add fbank, refactor feature of speech
* refactor audio feat
* refactor data preprare
* refactor data
* model init from config
* add u2 bins
* flake8
* can train
* fix bugs, add coverage, add scripts
* test can run
* fix data
* speed perturb with sox
* add spec aug
* fix for train
* fix train logitc
* fix logger
* log valid loss, time dataset process
* using np for speed perturb, remove some debug log of grad clip
* fix logger
* fix build vocab
* fix logger name
* using module logger as default
* fix
* fix install
* reorder imports
* fix board logger
* fix logger
* kaldi fbank and mfcc
* fix cmvn and print prarams
* fix add_eos_sos and cmvn
* fix cmvn compute
* fix logger and cmvn
* fix subsampling, label smoothing loss, remove useless
* add notebook test
* fix log
* fix tb logger
* multi gpu valid
* fix log
* fix log
* fix config
* fix compute cmvn, need paddle 2.1
* add cmvn notebook
* fix layer tools
* fix compute cmvn
* add rtf
* fix decoding
* fix layer tools
* fix log, add avg script
* more avg and test info
* fix dataset pickle problem; using 2.1 paddle; num_workers can > 0; ckpt save in exp dir;fix setup.sh;
* add vimrc
* refactor tiny script, add transformer and stream conf
* spm demo; librisppech scripts and confs
* fix log
* add librispeech scripts
* refactor data pipe; fix conf; fix u2 default params
* fix bugs
* refactor aishell scripts
* fix test
* fix cmvn
* fix s0 scripts
* fix ds2 scripts and bugs
* fix dev & test dataset filter
* fix dataset filter
* filter dev
* fix ckpt path
* filter test, since librispeech will cause OOM, but all test wer will be worse, since mismatch train with test
* add comment
* add syllable doc
* fix ds2 configs
* add doc
* add pypinyin tools
* fix decoder using blank_id=0
* mmseg with pybind11
* format code
4 years ago
|
|
|
pos_emb = self.pe[:, offset:offset + x.shape[1]]
|
|
|
|
return self.dropout(x), self.dropout(pos_emb)
|