mirror of
https://github.com/k2-fsa/icefall.git
synced 2025-08-09 01:52:41 +00:00
* support streaming in conformer * Add more documents * support streaming on pruned_transducer_stateless2; add delay penalty; fixes for decode states * Minor fixes * streaming for pruned_transducer_stateless4 * Fix conv cache error, support async streaming decoding * Fix style * Fix style * Fix style * Add torch.jit.export * mask the initial cache * Cutting off invalid frames of encoder_embed output * fix relative positional encoding in streaming decoding for compution saving * Minor fixes * Minor fixes * Minor fixes * Minor fixes * Minor fixes * Fix jit export for torch 1.6 * Minor fixes for streaming decoding * Minor fixes on decode stream * move model parameters to train.py * make states in forward streaming optional * update pretrain to support streaming model * update results.md * update tensorboard and pre-models * fix typo * Fix tests * remove unused arguments * add streaming decoding ci * Minor fix * Minor fix * disable right context by default
127 lines
4.1 KiB
Python
127 lines
4.1 KiB
Python
# Copyright 2022 Xiaomi Corp. (authors: Wei Kang)
|
|
#
|
|
# See ../../../../LICENSE for clarification regarding multiple authors
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import math
|
|
from typing import List, Optional, Tuple
|
|
|
|
import k2
|
|
import torch
|
|
|
|
from icefall.utils import AttributeDict
|
|
|
|
|
|
class DecodeStream(object):
|
|
def __init__(
|
|
self,
|
|
params: AttributeDict,
|
|
initial_states: List[torch.Tensor],
|
|
decoding_graph: Optional[k2.Fsa] = None,
|
|
device: torch.device = torch.device("cpu"),
|
|
) -> None:
|
|
"""
|
|
Args:
|
|
initial_states:
|
|
Initial decode states of the model, e.g. the return value of
|
|
`get_init_state` in conformer.py
|
|
decoding_graph:
|
|
Decoding graph used for decoding, may be a TrivialGraph or a HLG.
|
|
Used only when decoding_method is fast_beam_search.
|
|
device:
|
|
The device to run this stream.
|
|
"""
|
|
if decoding_graph is not None:
|
|
assert device == decoding_graph.device
|
|
|
|
self.params = params
|
|
self.LOG_EPS = math.log(1e-10)
|
|
|
|
self.states = initial_states
|
|
|
|
# It contains a 2-D tensors representing the feature frames.
|
|
self.features: torch.Tensor = None
|
|
|
|
self.num_frames: int = 0
|
|
# how many frames have been processed. (before subsampling).
|
|
# we only modify this value in `func:get_feature_frames`.
|
|
self.num_processed_frames: int = 0
|
|
|
|
self._done: bool = False
|
|
|
|
# The transcript of current utterance.
|
|
self.ground_truth: str = ""
|
|
|
|
# The decoding result (partial or final) of current utterance.
|
|
self.hyp: List = []
|
|
|
|
# how many frames have been processed, after subsampling (i.e. a
|
|
# cumulative sum of the second return value of
|
|
# encoder.streaming_forward
|
|
self.done_frames: int = 0
|
|
|
|
self.pad_length = (
|
|
params.right_context + 2
|
|
) * params.subsampling_factor + 3
|
|
|
|
if params.decoding_method == "greedy_search":
|
|
self.hyp = [params.blank_id] * params.context_size
|
|
elif params.decoding_method == "fast_beam_search":
|
|
# The rnnt_decoding_stream for fast_beam_search.
|
|
self.rnnt_decoding_stream: k2.RnntDecodingStream = (
|
|
k2.RnntDecodingStream(decoding_graph)
|
|
)
|
|
else:
|
|
assert (
|
|
False
|
|
), f"Decoding method :{params.decoding_method} do not support."
|
|
|
|
@property
|
|
def done(self) -> bool:
|
|
"""Return True if all the features are processed."""
|
|
return self._done
|
|
|
|
def set_features(
|
|
self,
|
|
features: torch.Tensor,
|
|
) -> None:
|
|
"""Set features tensor of current utterance."""
|
|
assert features.dim() == 2, features.dim()
|
|
self.features = torch.nn.functional.pad(
|
|
features,
|
|
(0, 0, 0, self.pad_length),
|
|
mode="constant",
|
|
value=self.LOG_EPS,
|
|
)
|
|
self.num_frames = self.features.size(0)
|
|
|
|
def get_feature_frames(self, chunk_size: int) -> Tuple[torch.Tensor, int]:
|
|
"""Consume chunk_size frames of features"""
|
|
chunk_length = chunk_size + self.pad_length
|
|
|
|
ret_length = min(
|
|
self.num_frames - self.num_processed_frames, chunk_length
|
|
)
|
|
|
|
ret_features = self.features[
|
|
self.num_processed_frames : self.num_processed_frames # noqa
|
|
+ ret_length
|
|
]
|
|
|
|
self.num_processed_frames += chunk_size
|
|
if self.num_processed_frames >= self.num_frames:
|
|
self._done = True
|
|
|
|
return ret_features, ret_length
|