From c875d7c1c15c56bc5764f5c2447f0a15be8ee5b0 Mon Sep 17 00:00:00 2001 From: dohe0342 Date: Thu, 2 Feb 2023 13:48:52 +0900 Subject: [PATCH] from local --- .../ASR/conformer_ctc/.transformer.py.swp | Bin 49152 -> 61440 bytes egs/aishell/ASR/conformer_ctc/transformer.py | 124 ++++++++++++++++++ 2 files changed, 124 insertions(+) diff --git a/egs/aishell/ASR/conformer_ctc/.transformer.py.swp b/egs/aishell/ASR/conformer_ctc/.transformer.py.swp index a4911e7486070c7e7eee125ddf425609e944c93e..89e1f19e85894de52db9ae2209a5d070870f315e 100644 GIT binary patch delta 6321 zcmbtYYit}>6~2z02hGEdlIGEr=z2@!*~%F|>Qs%919p;D6^fgJQx&D|WOn!N?!>b* z>pQbfOjS*gXp;&8v=hmtQE5X0sI(x!NJx}NL50%(K>TT?sz|^eP$NN#5K=*?@SQue zv$M9>iPe!l&wA$GbI(2ZobQ}7XU?>r`BVFjT>k_29^w1-euk@yvA=)fo&4LQT-}X@WWY_llH_`xHW6_O0-WL6cZ){Wid?>#EcES4%@$;MF`{(Ds*0r|t zhrHwH#vOQcg@$AOhvqj|jkd`vs~9^5tOh!O|KK$ZxeR;&Tm!xbTmsa^IpgsqoV;5Q z(y+~0A&0r?aLe(0)5#0&7P(&*;U&2<$nv~xdLGxLn66n;6tcRKFe*#lFjGIr4u5(= zEBk)c8}92f_nqs*r^v3O$O$`!0!%vesf<|~0Y)5p0LKF-DIs>tI^IION0tcnJs75N>AQJ>SsBnp)l19)QZsEh57by{a4<-t z7wg5fl-!h9=(zBOl91&q(mc^7Xy|U{eQU<_E!Q!muxkd~yM%kPlNJY?R;PuCWmF3m zMr1Lw*ihs<213Nqjp?Zw!vmQcqLM=x;<#6$-(o>QO42fFF4i)!V8eh7>B=`F&$y0y zLDO+ADPcuwEi#^3-*UJwP0OKVNLjMsZ-FAB^cmI%ez;|o%cd-_g!8CHAB-X=J^aHh zT(>uBiyc~HpI}=eC(EmsAnEw(4X0)I11WZ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%ED1d7KfnSp-M7Uf)gE~MVbw$K_SR#i%!Z(x>> encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8) + >>> transformer_encoder = nn.TransformerEncoder(encoder_layer, num_layers=6) + >>> src = torch.rand(10, 32, 512) + >>> out = transformer_encoder(src) + """ + __constants__ = ['norm'] + + def __init__(self, encoder_layer, num_layers, norm=None, enable_nested_tensor=True, mask_check=True): + super(TransformerEncoder, self).__init__() + self.layers = _get_clones(encoder_layer, num_layers) + self.num_layers = num_layers + self.norm = norm + self.enable_nested_tensor = enable_nested_tensor + self.mask_check = mask_check + +[docs] def forward(self, src: Tensor, mask: Optional[Tensor] = None, src_key_padding_mask: Optional[Tensor] = None) -> Tensor: + r"""Pass the input through the encoder layers in turn. + + Args: + src: the sequence to the encoder (required). + mask: the mask for the src sequence (optional). + src_key_padding_mask: the mask for the src keys per batch (optional). + + Shape: + see the docs in Transformer class. + """ + if src_key_padding_mask is not None: + _skpm_dtype = src_key_padding_mask.dtype + if _skpm_dtype != torch.bool and not torch.is_floating_point(src_key_padding_mask): + raise AssertionError( + "only bool and floating types of key_padding_mask are supported") + output = src + convert_to_nested = False + first_layer = self.layers[0] + src_key_padding_mask_for_layers = src_key_padding_mask + why_not_sparsity_fast_path = '' + str_first_layer = "self.layers[0]" + if not isinstance(first_layer, torch.nn.TransformerEncoderLayer): + why_not_sparsity_fast_path = f"{str_first_layer} was not TransformerEncoderLayer" + elif first_layer.norm_first : + why_not_sparsity_fast_path = f"{str_first_layer}.norm_first was True" + elif first_layer.training: + why_not_sparsity_fast_path = f"{str_first_layer} was in training mode" + elif not first_layer.self_attn.batch_first: + why_not_sparsity_fast_path = f" {str_first_layer}.self_attn.batch_first was not True" + elif not first_layer.self_attn._qkv_same_embed_dim: + why_not_sparsity_fast_path = f"{str_first_layer}.self_attn._qkv_same_embed_dim was not True" + elif not first_layer.activation_relu_or_gelu: + why_not_sparsity_fast_path = f" {str_first_layer}.activation_relu_or_gelu was not True" + elif not (first_layer.norm1.eps == first_layer.norm2.eps) : + why_not_sparsity_fast_path = f"{str_first_layer}.norm1.eps was not equal to {str_first_layer}.norm2.eps" + elif not src.dim() == 3: + why_not_sparsity_fast_path = f"input not batched; expected src.dim() of 3 but got {src.dim()}" + elif not self.enable_nested_tensor: + why_not_sparsity_fast_path = "enable_nested_tensor was not True" + elif src_key_padding_mask is None: + why_not_sparsity_fast_path = "src_key_padding_mask was None" + elif (((not hasattr(self, "mask_check")) or self.mask_check) + and not torch._nested_tensor_from_mask_left_aligned(src, src_key_padding_mask.logical_not())): + why_not_sparsity_fast_path = "mask_check enabled, and src and src_key_padding_mask was not left aligned" + elif output.is_nested: + why_not_sparsity_fast_path = "NestedTensor input is not supported" + elif mask is not None: + why_not_sparsity_fast_path = "src_key_padding_mask and mask were both supplied" + elif first_layer.self_attn.num_heads % 2 == 1: + why_not_sparsity_fast_path = "num_head is odd" + elif torch.is_autocast_enabled(): + why_not_sparsity_fast_path = "autocast is enabled" + + if not why_not_sparsity_fast_path: + tensor_args = ( + src, + first_layer.self_attn.in_proj_weight, + first_layer.self_attn.in_proj_bias, + first_layer.self_attn.out_proj.weight, + first_layer.self_attn.out_proj.bias, + first_layer.norm1.weight, + first_layer.norm1.bias, + first_layer.norm2.weight, + first_layer.norm2.bias, + first_layer.linear1.weight, + first_layer.linear1.bias, + first_layer.linear2.weight, + first_layer.linear2.bias, + ) + + if torch.overrides.has_torch_function(tensor_args): + why_not_sparsity_fast_path = "some Tensor argument has_torch_function" + elif not (src.is_cuda or 'cpu' in str(src.device)): + why_not_sparsity_fast_path = "src is neither CUDA nor CPU" + elif torch.is_grad_enabled() and any(x.requires_grad for x in tensor_args): + why_not_sparsity_fast_path = ("grad is enabled and at least one of query or the " + "input/output projection weights or biases requires_grad") + + if (not why_not_sparsity_fast_path) and (src_key_padding_mask is not None): + convert_to_nested = True + output = torch._nested_tensor_from_mask(output, src_key_padding_mask.logical_not(), mask_check=False) + src_key_padding_mask_for_layers = None + + for mod in self.layers: + output = mod(output, src_mask=mask, src_key_padding_mask=src_key_padding_mask_for_layers) + + if convert_to_nested: + output = output.to_padded_tensor(0.) + + if self.norm is not None: + output = self.norm(output) + + return output + + class TransformerEncoderLayer(nn.Module): """ Modified from torch.nn.TransformerEncoderLayer.