Repository: PaddlePaddle/PaddleSpeech

Issue:
PP-ASR超过15秒识别就越来越慢



Discussion:
原因是目前流式识别的 attention 是关注从开始到当前 chunk 的内容，而 attention 的时间复杂度是 n2d ，n 表示序列长度，所以随着时间推移速度就会变慢
这个我们会排期优化，您也可以看看代码，在合适的位置截断输入，因为可能目前的时刻并不太需要关注太久之前的信息

Resolving patch:
diff --git a/paddlespeech/cli/asr/infer.py b/paddlespeech/cli/asr/infer.py
--- a/paddlespeech/cli/asr/infer.py
+++ b/paddlespeech/cli/asr/infer.py
@@ -83,6 +83,12 @@ def __init__(self):
                 'attention_rescoring'
             ],
             help='only support transformer and conformer model')
+        self.parser.add_argument(
+            '--num_decoding_left_chunks',
+            '-num_left',
+            type=str,
+            default=-1,
+            help='only support transformer and conformer model')
         self.parser.add_argument(
             '--ckpt_path',
             type=str,
@@ -122,6 +128,7 @@ def _init_from_path(self,
                         sample_rate: int=16000,
                         cfg_path: Optional[os.PathLike]=None,
                         decode_method: str='attention_rescoring',
+                        num_decoding_left_chunks: int=-1,
                         ckpt_path: Optional[os.PathLike]=None):
         """
         Init model and other resources from a specific path.
@@ -129,6 +136,7 @@ def _init_from_path(self,
         logger.info("start to init the model")
         # default max_len: unit:second
         self.max_len = 50
+        assert num_decoding_left_chunks == -1 or num_decoding_left_chunks >= 0
         if hasattr(self, 'model'):
             logger.info('Model had been initialized.')
             return
@@ -179,6 +187,7 @@ def _init_from_path(self,
 
             elif "conformer" in model_type or "transformer" in model_type:
                 self.config.decode.decoding_method = decode_method
+                self.config.num_decoding_left_chunks = num_decoding_left_chunks
 
             else:
                 raise Exception("wrong type")
@@ -451,6 +460,7 @@ def __call__(self,
                  config: os.PathLike=None,
                  ckpt_path: os.PathLike=None,
                  decode_method: str='attention_rescoring',
+                 num_decoding_left_chunks: int=-1,
                  force_yes: bool=False,
                  rtf: bool=False,
                  device=paddle.get_device()):
@@ -460,7 +470,7 @@ def __call__(self,
         audio_file = os.path.abspath(audio_file)
         paddle.set_device(device)
         self._init_from_path(model, lang, sample_rate, config, decode_method,
-                             ckpt_path)
+                             num_decoding_left_chunks, ckpt_path)
         if not self._check(audio_file, sample_rate, force_yes):
             sys.exit(-1)
         if rtf:
diff --git a/paddlespeech/server/engine/asr/online/asr_engine.py b/paddlespeech/server/engine/asr/online/asr_engine.py
--- a/paddlespeech/server/engine/asr/online/asr_engine.py
+++ b/paddlespeech/server/engine/asr/online/asr_engine.py
@@ -703,6 +703,7 @@ def _init_from_path(self,
                         sample_rate: int=16000,
                         cfg_path: Optional[os.PathLike]=None,
                         decode_method: str='attention_rescoring',
+                        num_decoding_left_chunks: int=-1,
                         am_predictor_conf: dict=None):
         """
         Init model and other resources from a specific path.
@@ -788,7 +789,10 @@ def _init_from_path(self,
                 # update the decoding method
                 if decode_method:
                     self.config.decode.decoding_method = decode_method
-
+                # update num_decoding_left_chunks
+                if num_decoding_left_chunks:
+                    self.config.decode.num_decoding_left_chunks = num_decoding_left_chunks
+                    assert self.config.decode.num_decoding_left_chunks == -1 or self.config.decode.num_decoding_left_chunks >= 0, f"num_decoding_left_chunks should be -1 or >=0"
                 # we only support ctc_prefix_beam_search and attention_rescoring dedoding method
                 # Generally we set the decoding_method to attention_rescoring
                 if self.config.decode.decoding_method not in [
@@ -862,6 +866,7 @@ def init(self, config: dict) -> bool:
                 sample_rate=self.config.sample_rate,
                 cfg_path=self.config.cfg_path,
                 decode_method=self.config.decode_method,
+                num_decoding_left_chunks=self.config.num_decoding_left_chunks,
                 am_predictor_conf=self.config.am_predictor_conf):
             logger.error(
                 "Init the ASR server occurs error, please check the server configuration yaml"


Test patch: