MCPcopy Create free account
hub / github.com/modelscope/FunASR / forward

Method forward

funasr/models/transformer/search.py:330–401  ·  view source on GitHub ↗

Perform beam search. Args: x (torch.Tensor): Encoded speech feature (T, D) maxlenratio (float): Input length ratio to obtain max output length. If maxlenratio=0.0 (default), it uses a end-detect function to automatically find maximum h

(
        self, x: torch.Tensor, maxlenratio: float = 0.0, minlenratio: float = 0.0
    )

Source from the content-addressed store, hash-verified

328 return best_hyps
329
330 def forward(
331 self, x: torch.Tensor, maxlenratio: float = 0.0, minlenratio: float = 0.0
332 ) -> List[Hypothesis]:
333 """Perform beam search.
334
335 Args:
336 x (torch.Tensor): Encoded speech feature (T, D)
337 maxlenratio (float): Input length ratio to obtain max output length.
338 If maxlenratio=0.0 (default), it uses a end-detect function
339 to automatically find maximum hypothesis lengths
340 If maxlenratio<0.0, its absolute value is interpreted
341 as a constant max output length.
342 minlenratio (float): Input length ratio to obtain min output length.
343
344 Returns:
345 list[Hypothesis]: N-best decoding results
346
347 """
348 # set length bounds
349 if maxlenratio == 0:
350 maxlen = x.shape[0]
351 elif maxlenratio < 0:
352 maxlen = -1 * int(maxlenratio)
353 else:
354 maxlen = max(1, int(maxlenratio * x.size(0)))
355 minlen = int(minlenratio * x.size(0))
356 logging.info("decoder input length: " + str(x.shape[0]))
357 logging.info("max output length: " + str(maxlen))
358 logging.info("min output length: " + str(minlen))
359
360 # main loop of prefix search
361 running_hyps = self.init_hyp(x)
362 ended_hyps = []
363 for i in range(maxlen):
364 logging.debug("position " + str(i))
365 best = self.search(running_hyps, x)
366 # post process of one iteration
367 running_hyps = self.post_process(i, maxlen, maxlenratio, best, ended_hyps)
368 # end detection
369 if maxlenratio == 0.0 and end_detect([h.asdict() for h in ended_hyps], i):
370 logging.info(f"end detected at {i}")
371 break
372 if len(running_hyps) == 0:
373 logging.info("no hypothesis. Finish decoding.")
374 break
375 else:
376 logging.debug(f"remained hypotheses: {len(running_hyps)}")
377
378 nbest_hyps = sorted(ended_hyps, key=lambda x: x.score, reverse=True)
379 # check the number of hypotheses reaching to eos
380 if len(nbest_hyps) == 0:
381 logging.warning(
382 "there is no N-best results, perform recognition " "again with smaller minlenratio."
383 )
384 return (
385 []
386 if minlenratio < 0.1
387 else self.forward(x, maxlenratio, max(0.0, minlenratio - 0.1))

Callers

nothing calls this directly

Calls 5

init_hypMethod · 0.95
searchMethod · 0.95
post_processMethod · 0.95
end_detectFunction · 0.90
asdictMethod · 0.45

Tested by

no test coverage detected