From 4ace5a95b052d338947fc88809a440ccd55cf6b4 Mon Sep 17 00:00:00 2001
From: 游雁 <zhifu.gzf@alibaba-inc.com>
Date: 星期四, 16 十一月 2023 16:39:52 +0800
Subject: [PATCH] funasr pages

---
 funasr/models/decoder/sanm_decoder.py |  141 ++++++++++++++---------------------------------
 1 files changed, 42 insertions(+), 99 deletions(-)

diff --git a/funasr/models/decoder/sanm_decoder.py b/funasr/models/decoder/sanm_decoder.py
index 3e4e554..ff35e46 100644
--- a/funasr/models/decoder/sanm_decoder.py
+++ b/funasr/models/decoder/sanm_decoder.py
@@ -105,48 +105,48 @@
 
         return x, tgt_mask, memory, memory_mask, cache
 
-    #def forward_chunk(self, tgt, tgt_mask, memory, memory_mask=None, cache=None):
-    #    """Compute decoded features.
+    def forward_one_step(self, tgt, tgt_mask, memory, memory_mask=None, cache=None):
+        """Compute decoded features.
 
-    #    Args:
-    #        tgt (torch.Tensor): Input tensor (#batch, maxlen_out, size).
-    #        tgt_mask (torch.Tensor): Mask for input tensor (#batch, maxlen_out).
-    #        memory (torch.Tensor): Encoded memory, float32 (#batch, maxlen_in, size).
-    #        memory_mask (torch.Tensor): Encoded memory mask (#batch, maxlen_in).
-    #        cache (List[torch.Tensor]): List of cached tensors.
-    #            Each tensor shape should be (#batch, maxlen_out - 1, size).
+        Args:
+            tgt (torch.Tensor): Input tensor (#batch, maxlen_out, size).
+            tgt_mask (torch.Tensor): Mask for input tensor (#batch, maxlen_out).
+            memory (torch.Tensor): Encoded memory, float32 (#batch, maxlen_in, size).
+            memory_mask (torch.Tensor): Encoded memory mask (#batch, maxlen_in).
+            cache (List[torch.Tensor]): List of cached tensors.
+                Each tensor shape should be (#batch, maxlen_out - 1, size).
 
-    #    Returns:
-    #        torch.Tensor: Output tensor(#batch, maxlen_out, size).
-    #        torch.Tensor: Mask for output tensor (#batch, maxlen_out).
-    #        torch.Tensor: Encoded memory (#batch, maxlen_in, size).
-    #        torch.Tensor: Encoded memory mask (#batch, maxlen_in).
+        Returns:
+            torch.Tensor: Output tensor(#batch, maxlen_out, size).
+            torch.Tensor: Mask for output tensor (#batch, maxlen_out).
+            torch.Tensor: Encoded memory (#batch, maxlen_in, size).
+            torch.Tensor: Encoded memory mask (#batch, maxlen_in).
 
-    #    """
-    #    # tgt = self.dropout(tgt)
-    #    residual = tgt
-    #    if self.normalize_before:
-    #        tgt = self.norm1(tgt)
-    #    tgt = self.feed_forward(tgt)
+        """
+        # tgt = self.dropout(tgt)
+        residual = tgt
+        if self.normalize_before:
+            tgt = self.norm1(tgt)
+        tgt = self.feed_forward(tgt)
 
-    #    x = tgt
-    #    if self.self_attn:
-    #        if self.normalize_before:
-    #            tgt = self.norm2(tgt)
-    #        if self.training:
-    #            cache = None
-    #        x, cache = self.self_attn(tgt, tgt_mask, cache=cache)
-    #        x = residual + self.dropout(x)
+        x = tgt
+        if self.self_attn:
+            if self.normalize_before:
+                tgt = self.norm2(tgt)
+            if self.training:
+                cache = None
+            x, cache = self.self_attn(tgt, tgt_mask, cache=cache)
+            x = residual + self.dropout(x)
 
-    #    if self.src_attn is not None:
-    #        residual = x
-    #        if self.normalize_before:
-    #            x = self.norm3(x)
+        if self.src_attn is not None:
+            residual = x
+            if self.normalize_before:
+                x = self.norm3(x)
 
-    #        x = residual + self.dropout(self.src_attn(x, memory, memory_mask))
+            x = residual + self.dropout(self.src_attn(x, memory, memory_mask))
 
 
-    #    return x, tgt_mask, memory, memory_mask, cache
+        return x, tgt_mask, memory, memory_mask, cache
 
     def forward_chunk(self, tgt, memory, fsmn_cache=None, opt_cache=None, chunk_size=None, look_back=0):
         """Compute decoded features.
@@ -438,7 +438,7 @@
         for i in range(self.att_layer_num):
             decoder = self.decoders[i]
             c = cache[i]
-            x, tgt_mask, memory, memory_mask, c_ret = decoder.forward_chunk(
+            x, tgt_mask, memory, memory_mask, c_ret = decoder.forward_one_step(
                 x, tgt_mask, memory, memory_mask, cache=c
             )
             new_cache.append(c_ret)
@@ -448,13 +448,13 @@
                 j = i + self.att_layer_num
                 decoder = self.decoders2[i]
                 c = cache[j]
-                x, tgt_mask, memory, memory_mask, c_ret = decoder.forward_chunk(
+                x, tgt_mask, memory, memory_mask, c_ret = decoder.forward_one_step(
                     x, tgt_mask, memory, memory_mask, cache=c
                 )
                 new_cache.append(c_ret)
 
         for decoder in self.decoders3:
-            x, tgt_mask, memory, memory_mask, _ = decoder.forward_chunk(
+            x, tgt_mask, memory, memory_mask, _ = decoder.forward_one_step(
                 x, tgt_mask, memory, None, cache=None
             )
 
@@ -878,6 +878,7 @@
         lora_rank: int = 8,
         lora_alpha: int = 16,
         lora_dropout: float = 0.1,
+        chunk_multiply_factor: tuple = (1,),
         tf2torch_tensor_name_prefix_torch: str = "decoder",
         tf2torch_tensor_name_prefix_tf: str = "seq2seq/decoder",
     ):
@@ -970,6 +971,7 @@
         )
         self.tf2torch_tensor_name_prefix_torch = tf2torch_tensor_name_prefix_torch
         self.tf2torch_tensor_name_prefix_tf = tf2torch_tensor_name_prefix_tf
+        self.chunk_multiply_factor = chunk_multiply_factor
 
     def forward(
         self,
@@ -1032,65 +1034,6 @@
             ys.unsqueeze(0), ys_mask, x.unsqueeze(0), cache=state
         )
         return logp.squeeze(0), state
-
-    #def forward_chunk(
-    #    self,
-    #    memory: torch.Tensor,
-    #    tgt: torch.Tensor,
-    #    cache: dict = None,
-    #) -> Tuple[torch.Tensor, torch.Tensor]:
-    #    """Forward decoder.
-
-    #    Args:
-    #        hs_pad: encoded memory, float32  (batch, maxlen_in, feat)
-    #        hlens: (batch)
-    #        ys_in_pad:
-    #            input token ids, int64 (batch, maxlen_out)
-    #            if input_layer == "embed"
-    #            input tensor (batch, maxlen_out, #mels) in the other cases
-    #        ys_in_lens: (batch)
-    #    Returns:
-    #        (tuple): tuple containing:
-
-    #        x: decoded token score before softmax (batch, maxlen_out, token)
-    #            if use_output_layer is True,
-    #        olens: (batch, )
-    #    """
-    #    x = tgt
-    #    if cache["decode_fsmn"] is None:
-    #        cache_layer_num = len(self.decoders)
-    #        if self.decoders2 is not None:
-    #            cache_layer_num += len(self.decoders2)
-    #        new_cache = [None] * cache_layer_num
-    #    else:
-    #        new_cache = cache["decode_fsmn"]
-    #    for i in range(self.att_layer_num):
-    #        decoder = self.decoders[i]
-    #        x, tgt_mask, memory, memory_mask, c_ret = decoder.forward_chunk(
-    #            x, None, memory, None, cache=new_cache[i]
-    #        )
-    #        new_cache[i] = c_ret
-
-    #    if self.num_blocks - self.att_layer_num > 1:
-    #        for i in range(self.num_blocks - self.att_layer_num):
-    #            j = i + self.att_layer_num
-    #            decoder = self.decoders2[i]
-    #            x, tgt_mask, memory, memory_mask, c_ret = decoder.forward_chunk(
-    #                x, None, memory, None, cache=new_cache[j]
-    #            )
-    #            new_cache[j] = c_ret
-
-    #    for decoder in self.decoders3:
-
-    #        x, tgt_mask, memory, memory_mask, _ = decoder.forward_chunk(
-    #            x, None, memory, None, cache=None
-    #        )
-    #    if self.normalize_before:
-    #        x = self.after_norm(x)
-    #    if self.output_layer is not None:
-    #        x = self.output_layer(x)
-    #    cache["decode_fsmn"] = new_cache
-    #    return x
 
     def forward_chunk(
         self,
@@ -1190,7 +1133,7 @@
         for i in range(self.att_layer_num):
             decoder = self.decoders[i]
             c = cache[i]
-            x, tgt_mask, memory, memory_mask, c_ret = decoder.forward_chunk(
+            x, tgt_mask, memory, memory_mask, c_ret = decoder.forward_one_step(
                 x, tgt_mask, memory, None, cache=c
             )
             new_cache.append(c_ret)
@@ -1200,14 +1143,14 @@
                 j = i + self.att_layer_num
                 decoder = self.decoders2[i]
                 c = cache[j]
-                x, tgt_mask, memory, memory_mask, c_ret = decoder.forward_chunk(
+                x, tgt_mask, memory, memory_mask, c_ret = decoder.forward_one_step(
                     x, tgt_mask, memory, None, cache=c
                 )
                 new_cache.append(c_ret)
 
         for decoder in self.decoders3:
 
-            x, tgt_mask, memory, memory_mask, _ = decoder.forward_chunk(
+            x, tgt_mask, memory, memory_mask, _ = decoder.forward_one_step(
                 x, tgt_mask, memory, None, cache=None
             )
 

--
Gitblit v1.9.1