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

Method encode_chunk

funasr/models/sanm_kws_streaming/model.py:119–159  ·  view source on GitHub ↗

Encode chunk. Args: speech: Speech audio tensor, shape (batch, time). speech_lengths: Length of each speech sample. cache: State cache dict for streaming inference. **kwargs: Additional keyword arguments.

(
        self,
        speech: torch.Tensor,
        speech_lengths: torch.Tensor,
        cache: dict = None,
        **kwargs,
    )

Source from the content-addressed store, hash-verified

117 return loss, stats, weight
118
119 def encode_chunk(
120 self,
121 speech: torch.Tensor,
122 speech_lengths: torch.Tensor,
123 cache: dict = None,
124 **kwargs,
125 ) -> Tuple[torch.Tensor, torch.Tensor]:
126 """Encode chunk.
127
128 Args:
129 speech: Speech audio tensor, shape (batch, time).
130 speech_lengths: Length of each speech sample.
131 cache: State cache dict for streaming inference.
132 **kwargs: Additional keyword arguments.
133 """
134 if cache is None:
135 cache = {}
136 """Frontend + Encoder. Note that this method is used by asr_inference.py
137 Args:
138 speech: (Batch, Length, ...)
139 speech_lengths: (Batch, )
140 ind: int
141 """
142 with autocast(False):
143 # Data augmentation
144 if self.specaug is not None and self.training:
145 speech, speech_lengths = self.specaug(speech, speech_lengths)
146
147 # Normalization for feature: e.g. Global-CMVN, Utterance-CMVN
148 if self.normalize is not None:
149 speech, speech_lengths = self.normalize(speech, speech_lengths)
150
151 # Forward encoder
152 encoder_out, encoder_out_lens, _ = self.encoder.forward_chunk(
153 speech, speech_lengths, cache=cache["encoder"]
154 )
155
156 if isinstance(encoder_out, tuple):
157 encoder_out = encoder_out[0]
158
159 return encoder_out, torch.tensor([encoder_out.size(1)])
160
161 def init_cache(self, cache: dict = None, **kwargs):
162 """Init cache.

Callers 1

generate_chunkMethod · 0.95

Calls 3

autocastFunction · 0.90
normalizeMethod · 0.45
forward_chunkMethod · 0.45

Tested by

no test coverage detected