| |
| |
|
|
| """Transformer encoder definition.""" |
|
|
| from typing import List, Optional, Tuple |
|
|
| import torch |
| import torch.nn as nn |
| from typeguard import typechecked |
|
|
| from espnet2.asr.ctc import CTC |
| from espnet2.asr.encoder.abs_encoder import AbsEncoder |
| from espnet.nets.pytorch_backend.nets_utils import make_pad_mask |
| from espnet2.asr.encoder.Spike_driven.Spike_driven_modules.Q_attention import * |
|
|
| from espnet.nets.pytorch_backend.transformer.embedding import PositionalEncoding |
| from espnet.nets.pytorch_backend.transformer.layer_norm import LayerNorm |
| |
| from espnet.nets.pytorch_backend.transformer.multi_layer_conv import ( |
| Conv1dLinear, |
| MultiLayeredConv1d, |
| ) |
| from espnet2.asr.encoder.Spike_driven.Spike_driven_modules.Q_positionwise_feed_forward import Q_PositionwiseFeedForward, Q_GLU |
| from espnet.nets.pytorch_backend.transformer.repeat import repeat |
| from espnet.nets.pytorch_backend.transformer.subsampling import ( |
| Conv1dSubsampling2, |
| Conv2dSubsampling, |
| Conv2dSubsampling1, |
| Conv2dSubsampling2, |
| Conv2dSubsampling6, |
| Conv2dSubsampling8, |
| TooShortUttError, |
| check_short_utt, |
| ) |
| from espnet2.asr.encoder.Spike_driven.Q_trick import MultiSpike |
|
|
| class Q_Transformer_EncoderLayer(nn.Module): |
| """Encoder layer module. |
| |
| Args: |
| size (int): Input dimension. |
| self_attn (torch.nn.Module): Self-attention module instance. |
| `MultiHeadedAttention` or `RelPositionMultiHeadedAttention` instance |
| can be used as the argument. |
| feed_forward (torch.nn.Module): Feed-forward module instance. |
| `PositionwiseFeedForward`, `MultiLayeredConv1d`, or `Conv1dLinear` instance |
| can be used as the argument. |
| dropout_rate (float): Dropout rate. |
| normalize_before (bool): Whether to use layer_norm before the first block. |
| concat_after (bool): Whether to concat attention layer's input and output. |
| if True, additional linear will be applied. |
| i.e. x -> x + linear(concat(x, att(x))) |
| if False, no additional linear will be applied. i.e. x -> x + att(x) |
| stochastic_depth_rate (float): Proability to skip this layer. |
| During training, the layer may skip residual computation and return input |
| as-is with given probability. |
| """ |
|
|
| def __init__( |
| self, |
| size, |
| self_attn, |
| feed_forward, |
| dropout_rate, |
| normalize_before=True, |
| concat_after=False, |
| stochastic_depth_rate=0.0, |
| ): |
| """Construct an EncoderLayer object.""" |
| super(Q_Transformer_EncoderLayer, self).__init__() |
| self.self_attn = self_attn |
| self.feed_forward = feed_forward |
| self.norm1 = LayerNorm(size) |
| self.norm2 = LayerNorm(size) |
| self.dropout = nn.Dropout(dropout_rate) |
| self.size = size |
| self.normalize_before = normalize_before |
| self.concat_after = concat_after |
| if self.concat_after: |
| self.concat_linear = nn.Linear(size + size, size) |
| self.stochastic_depth_rate = stochastic_depth_rate |
| self.ATT_sn = MultiSpike(size) |
| self.FFN_sn = MultiSpike(size) |
|
|
| def forward(self, x, mask, iiter=None, cache=None): |
| """Compute encoded features. |
| |
| Args: |
| x_input (torch.Tensor): Input tensor (#batch, time, size). |
| mask (torch.Tensor): Mask tensor for the input (#batch, 1, time). |
| cache (torch.Tensor): Cache tensor of the input (#batch, time - 1, size). |
| |
| Returns: |
| torch.Tensor: Output tensor (#batch, time, size). |
| torch.Tensor: Mask tensor (#batch, 1, time). |
| |
| """ |
| skip_layer = False |
| |
| |
| stoch_layer_coeff = 1.0 |
| if self.training and self.stochastic_depth_rate > 0: |
| skip_layer = torch.rand(1).item() < self.stochastic_depth_rate |
| stoch_layer_coeff = 1.0 / (1 - self.stochastic_depth_rate) |
|
|
| if skip_layer: |
| if cache is not None: |
| x = torch.cat([cache, x], dim=1) |
| return x, mask |
|
|
| residual = x |
| if self.normalize_before: |
| x = self.norm1(x) |
|
|
| if cache is None: |
| x_q = x |
| else: |
| assert cache.shape == (x.shape[0], x.shape[1] - 1, self.size) |
| x_q = x[:, -1:, :] |
| residual = residual[:, -1:, :] |
| mask = None if mask is None else mask[:, -1:, :] |
|
|
| x_q = self.ATT_sn(x_q, iiter) |
| x = self.ATT_sn(x, iiter) |
| if self.concat_after: |
| x_concat = torch.cat((x, self.self_attn(x_q, x, x, mask, iiter)), dim=-1) |
| x = residual + stoch_layer_coeff * self.concat_linear(x_concat) |
| else: |
| x = residual + stoch_layer_coeff * self.dropout( |
| self.self_attn(x_q, x, x, mask, iiter) |
| ) |
| if not self.normalize_before: |
| x = self.norm1(x) |
|
|
| residual = x |
| x = self.FFN_sn(x, iiter) |
| if self.normalize_before: |
| x = self.norm2(x) |
| x = residual + stoch_layer_coeff * self.dropout(self.feed_forward(x, iiter)) |
| if not self.normalize_before: |
| x = self.norm2(x) |
|
|
| if cache is not None: |
| x = torch.cat([cache, x], dim=1) |
|
|
| return x, mask |
|
|
|
|
| class Q_TransformerEncoder(AbsEncoder): |
| """Transformer encoder module. |
| |
| Args: |
| input_size: input dim |
| output_size: dimension of attention |
| attention_heads: the number of heads of multi head attention |
| linear_units: the number of units of position-wise feed forward |
| num_blocks: the number of decoder blocks |
| dropout_rate: dropout rate |
| attention_dropout_rate: dropout rate in attention |
| positional_dropout_rate: dropout rate after adding positional encoding |
| input_layer: input layer type |
| pos_enc_class: PositionalEncoding or ScaledPositionalEncoding |
| normalize_before: whether to use layer_norm before the first block |
| concat_after: whether to concat attention layer's input and output |
| if True, additional linear will be applied. |
| i.e. x -> x + linear(concat(x, att(x))) |
| if False, no additional linear will be applied. |
| i.e. x -> x + att(x) |
| positionwise_layer_type: linear of conv1d |
| positionwise_conv_kernel_size: kernel size of positionwise conv1d layer |
| padding_idx: padding_idx for input_layer=embed |
| """ |
|
|
| @typechecked |
| def __init__( |
| self, |
| input_size: int, |
| output_size: int = 256, |
| attention_heads: int = 4, |
| attention_layer_type: str = "selfattn", |
| linear_units: int = 2048, |
| num_blocks: int = 6, |
| dropout_rate: float = 0.1, |
| positional_dropout_rate: float = 0.1, |
| attention_dropout_rate: float = 0.0, |
| input_layer: Optional[str] = "conv2d", |
| pos_enc_class=PositionalEncoding, |
| normalize_before: bool = True, |
| concat_after: bool = False, |
| positionwise_layer_type: str = "FFN", |
| padding_idx: int = -1, |
| interctc_layer_idx: List[int] = [], |
| interctc_use_conditioning: bool = False, |
| layer_drop_rate: float = 0.0, |
| ): |
| super().__init__() |
| self._output_size = output_size |
|
|
| if input_layer == "linear": |
| self.embed = torch.nn.Sequential( |
| torch.nn.Linear(input_size, output_size), |
| torch.nn.LayerNorm(output_size), |
| torch.nn.Dropout(dropout_rate), |
| torch.nn.ReLU(), |
| pos_enc_class(output_size, positional_dropout_rate), |
| ) |
| elif input_layer == "conv1d2": |
| self.embed = Conv1dSubsampling2( |
| input_size, |
| output_size, |
| dropout_rate, |
| pos_enc_class(output_size, positional_dropout_rate), |
| ) |
| elif input_layer == "conv2d": |
| self.embed = Conv2dSubsampling(input_size, output_size, dropout_rate) |
| elif input_layer == "conv2d1": |
| self.embed = Conv2dSubsampling1(input_size, output_size, dropout_rate) |
| elif input_layer == "conv2d2": |
| self.embed = Conv2dSubsampling2(input_size, output_size, dropout_rate) |
| elif input_layer == "conv2d6": |
| self.embed = Conv2dSubsampling6(input_size, output_size, dropout_rate) |
| elif input_layer == "conv2d8": |
| self.embed = Conv2dSubsampling8(input_size, output_size, dropout_rate) |
| elif input_layer == "embed": |
| self.embed = torch.nn.Sequential( |
| torch.nn.Embedding(input_size, output_size, padding_idx=padding_idx), |
| pos_enc_class(output_size, positional_dropout_rate), |
| ) |
| elif input_layer is None: |
| if input_size == output_size: |
| self.embed = None |
| else: |
| self.embed = torch.nn.Linear(input_size, output_size) |
| else: |
| raise ValueError("unknown input_layer: " + input_layer) |
| self.normalize_before = normalize_before |
| if attention_layer_type == "selfattn": |
| encoder_selfattn_layer = Q_MultiHeadedAttention |
| encoder_selfattn_layer_args = ( |
| attention_heads, |
| output_size, |
| attention_dropout_rate |
| ) |
| elif attention_layer_type == "selfattn_woSoftMax": |
| encoder_selfattn_layer = Q_MultiHeadedAttention_woSoftMax |
| encoder_selfattn_layer_args = ( |
| attention_heads, |
| output_size, |
| attention_dropout_rate |
| ) |
| elif attention_layer_type == "HierDecayv2": |
| encoder_selfattn_layer = Q_MultiHeadedAttention_HierDecay |
| encoder_selfattn_layer_args = ( |
| attention_heads, |
| output_size, |
| attention_dropout_rate, |
| ) |
| elif attention_layer_type == "HierDecay_woSoftMax": |
| encoder_selfattn_layer = Q_MultiHeadedAttention_HierDecay_woSoftMax |
| encoder_selfattn_layer_args = ( |
| attention_heads, |
| output_size, |
| attention_dropout_rate, |
| ) |
| |
| else: |
| raise ValueError("unknown encoder_attn_layer: " + attention_layer_type) |
|
|
| positionwise_layer = Q_PositionwiseFeedForward |
| positionwise_layer_args = ( |
| output_size, |
| linear_units, |
| dropout_rate, |
| ) |
| |
|
|
| if "HierDecay" in attention_layer_type: |
| self.encoders = repeat( |
| num_blocks, |
| lambda lnum: Q_Transformer_EncoderLayer( |
| output_size, |
| encoder_selfattn_layer(*encoder_selfattn_layer_args, lnum), |
| positionwise_layer(*positionwise_layer_args), |
| dropout_rate, |
| normalize_before, |
| concat_after, |
| ), |
| layer_drop_rate, |
| ) |
| else: |
| self.encoders = repeat( |
| num_blocks, |
| lambda lnum: Q_Transformer_EncoderLayer( |
| output_size, |
| encoder_selfattn_layer(*encoder_selfattn_layer_args), |
| positionwise_layer(*positionwise_layer_args), |
| dropout_rate, |
| normalize_before, |
| concat_after, |
| ), |
| layer_drop_rate, |
| ) |
| |
| if self.normalize_before: |
| self.after_norm = LayerNorm(output_size) |
|
|
| self.interctc_layer_idx = interctc_layer_idx |
| if len(interctc_layer_idx) > 0: |
| assert 0 < min(interctc_layer_idx) and max(interctc_layer_idx) < num_blocks |
| self.interctc_use_conditioning = interctc_use_conditioning |
| self.conditioning_layer = None |
|
|
| def output_size(self) -> int: |
| return self._output_size |
|
|
| def forward( |
| self, |
| xs_pad: torch.Tensor, |
| ilens: torch.Tensor, |
| iiter: int = 0, |
| prev_states: torch.Tensor = None, |
| ctc: CTC = None, |
| return_all_hs: bool = False, |
| ) -> Tuple[torch.Tensor, torch.Tensor, Optional[torch.Tensor]]: |
| """Embed positions in tensor. |
| |
| Args: |
| xs_pad: input tensor (B, L, D) |
| ilens: input length (B) |
| prev_states: Not to be used now. |
| ctc (CTC): ctc module for intermediate CTC loss |
| return_all_hs (bool): whether to return all hidden states |
| |
| Returns: |
| position embedded tensor and mask |
| """ |
| masks = (~make_pad_mask(ilens)[:, None, :]).to(xs_pad.device) |
| |
| if self.embed is None: |
| xs_pad = xs_pad |
| elif ( |
| isinstance(self.embed, Conv2dSubsampling) |
| or isinstance(self.embed, Conv1dSubsampling2) |
| or isinstance(self.embed, Conv2dSubsampling1) |
| or isinstance(self.embed, Conv2dSubsampling2) |
| or isinstance(self.embed, Conv2dSubsampling6) |
| or isinstance(self.embed, Conv2dSubsampling8) |
| ): |
| short_status, limit_size = check_short_utt(self.embed, xs_pad.size(1)) |
| if short_status: |
| raise TooShortUttError( |
| f"has {xs_pad.size(1)} frames and is too short for subsampling " |
| + f"(it needs more than {limit_size} frames), return empty results", |
| xs_pad.size(1), |
| limit_size, |
| ) |
| xs_pad, masks = self.embed(xs_pad, masks) |
| else: |
| xs_pad = self.embed(xs_pad) |
| intermediate_outs = [] |
| if len(self.interctc_layer_idx) == 0: |
| for layer_idx, encoder_layer in enumerate(self.encoders): |
| xs_pad, masks = encoder_layer(xs_pad, masks, iiter) |
| if return_all_hs: |
| if isinstance(xs_pad, tuple): |
| intermediate_outs.append(xs_pad[0]) |
| else: |
| intermediate_outs.append(xs_pad) |
|
|
| |
| else: |
| for layer_idx, encoder_layer in enumerate(self.encoders): |
| xs_pad, masks = encoder_layer(xs_pad, masks, iiter) |
|
|
| if layer_idx + 1 in self.interctc_layer_idx: |
| encoder_out = xs_pad |
|
|
| |
| if self.normalize_before: |
| encoder_out = self.after_norm(encoder_out) |
|
|
| intermediate_outs.append((layer_idx + 1, encoder_out)) |
|
|
| if self.interctc_use_conditioning: |
| ctc_out = ctc.softmax(encoder_out) |
| xs_pad = xs_pad + self.conditioning_layer(ctc_out) |
|
|
| if self.normalize_before: |
| xs_pad = self.after_norm(xs_pad) |
|
|
| olens = masks.squeeze(1).sum(1) |
| |
| if len(intermediate_outs) > 0: |
| return (xs_pad, intermediate_outs), olens, None |
| |
| return xs_pad, olens, None |
|
|