Sukhpal Singh commited on
Added nemo stream patch
Browse files- nemo-v2-streaming-punjabi.patch +134 -0
nemo-v2-streaming-punjabi.patch
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
diff --git a/examples/asr/asr_cache_aware_streaming/speech_to_text_cache_aware_streaming_infer.py b/examples/asr/asr_cache_aware_streaming/speech_to_text_cache_aware_streaming_infer.py
|
| 2 |
+
index 7726c2b27..981c099ef 100644
|
| 3 |
+
--- a/examples/asr/asr_cache_aware_streaming/speech_to_text_cache_aware_streaming_infer.py
|
| 4 |
+
+++ b/examples/asr/asr_cache_aware_streaming/speech_to_text_cache_aware_streaming_infer.py
|
| 5 |
+
@@ -120,9 +120,19 @@ def calc_drop_extra_pre_encoded(asr_model, step_num, pad_and_drop_preencoded):
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def perform_streaming(
|
| 9 |
+
- asr_model, streaming_buffer, compare_vs_offline=False, debug_mode=False, pad_and_drop_preencoded=False
|
| 10 |
+
+ asr_model,
|
| 11 |
+
+ streaming_buffer,
|
| 12 |
+
+ compare_vs_offline=False,
|
| 13 |
+
+ debug_mode=False,
|
| 14 |
+
+ pad_and_drop_preencoded=False,
|
| 15 |
+
+ target_lang=None,
|
| 16 |
+
):
|
| 17 |
+
batch_size = len(streaming_buffer.streams_length)
|
| 18 |
+
+
|
| 19 |
+
+ lang_ids = None
|
| 20 |
+
+ if target_lang is not None:
|
| 21 |
+
+ lang_ids = [target_lang] * batch_size
|
| 22 |
+
+
|
| 23 |
+
if compare_vs_offline:
|
| 24 |
+
# would pass the whole audio at once through the model like offline mode in order to compare the results with the stremaing mode
|
| 25 |
+
# the output of the model in the offline and streaming mode should be exactly the same
|
| 26 |
+
@@ -141,6 +151,7 @@ def perform_streaming(
|
| 27 |
+
processed_signal=processed_signal,
|
| 28 |
+
processed_signal_length=processed_signal_length,
|
| 29 |
+
return_transcription=True,
|
| 30 |
+
+ lang_ids=lang_ids,
|
| 31 |
+
)
|
| 32 |
+
final_offline_tran = extract_transcriptions(transcribed_texts)
|
| 33 |
+
logging.info(f" Final offline transcriptions: {final_offline_tran}")
|
| 34 |
+
@@ -181,6 +192,7 @@ def perform_streaming(
|
| 35 |
+
asr_model, step_num, pad_and_drop_preencoded
|
| 36 |
+
),
|
| 37 |
+
return_transcription=True,
|
| 38 |
+
+ lang_ids=lang_ids,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
if debug_mode:
|
| 42 |
+
@@ -276,6 +288,13 @@ def main():
|
| 43 |
+
help="Selects the decoder for Hybrid ASR models which has both the CTC and RNNT decoder. Supported decoders are ['ctc', 'rnnt']",
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
+ parser.add_argument(
|
| 47 |
+
+ "--target_lang",
|
| 48 |
+
+ type=str,
|
| 49 |
+
+ default=None,
|
| 50 |
+
+ help="Language ID for multilingual models, e.g. pa",
|
| 51 |
+
+ )
|
| 52 |
+
+
|
| 53 |
+
parser.add_argument(
|
| 54 |
+
"--att_context_size",
|
| 55 |
+
type=str,
|
| 56 |
+
@@ -376,7 +395,9 @@ def main():
|
| 57 |
+
asr_model=asr_model,
|
| 58 |
+
streaming_buffer=streaming_buffer,
|
| 59 |
+
compare_vs_offline=args.compare_vs_offline,
|
| 60 |
+
+ debug_mode=args.debug_mode,
|
| 61 |
+
pad_and_drop_preencoded=args.pad_and_drop_preencoded,
|
| 62 |
+
+ target_lang=args.target_lang,
|
| 63 |
+
)
|
| 64 |
+
else:
|
| 65 |
+
# stream audio files in a manifest file in batched mode
|
| 66 |
+
@@ -409,6 +430,7 @@ def main():
|
| 67 |
+
compare_vs_offline=args.compare_vs_offline,
|
| 68 |
+
debug_mode=args.debug_mode,
|
| 69 |
+
pad_and_drop_preencoded=args.pad_and_drop_preencoded,
|
| 70 |
+
+ target_lang=args.target_lang,
|
| 71 |
+
)
|
| 72 |
+
all_streaming_tran.extend(streaming_tran)
|
| 73 |
+
if args.compare_vs_offline:
|
| 74 |
+
diff --git a/nemo/collections/asr/models/hybrid_rnnt_ctc_models.py b/nemo/collections/asr/models/hybrid_rnnt_ctc_models.py
|
| 75 |
+
index 3e9f21e04..29c97e02a 100644
|
| 76 |
+
--- a/nemo/collections/asr/models/hybrid_rnnt_ctc_models.py
|
| 77 |
+
+++ b/nemo/collections/asr/models/hybrid_rnnt_ctc_models.py
|
| 78 |
+
@@ -163,12 +163,34 @@ class EncDecHybridRNNTCTCModel(EncDecRNNTModel, ASRBPEMixin, InterCTCMixin):
|
| 79 |
+
self.ctc_decoder.unfreeze()
|
| 80 |
+
|
| 81 |
+
def _transcribe_forward(self, batch: Any, trcfg: TranscribeConfig):
|
| 82 |
+
-
|
| 83 |
+
+
|
| 84 |
+
+ # AI4Bharat multilingual GPU transcribe fix:
|
| 85 |
+
+ # transcribe() dataloader yields CPU audio tensors even when model is CUDA.
|
| 86 |
+
+ device = next(self.parameters()).device
|
| 87 |
+
+
|
| 88 |
+
+ if isinstance(batch, tuple):
|
| 89 |
+
+ batch = (
|
| 90 |
+
+ batch[0].to(device, non_blocking=True),
|
| 91 |
+
+ batch[1].to(device, non_blocking=True),
|
| 92 |
+
+ *batch[2:],
|
| 93 |
+
+ )
|
| 94 |
+
+ elif isinstance(batch, list):
|
| 95 |
+
+ batch = list(batch)
|
| 96 |
+
+ batch[0] = batch[0].to(device, non_blocking=True)
|
| 97 |
+
+ batch[1] = batch[1].to(device, non_blocking=True)
|
| 98 |
+
+ else:
|
| 99 |
+
+ raise TypeError(f"Unexpected transcribe batch type: {type(batch)}")
|
| 100 |
+
+
|
| 101 |
+
if self.cur_decoder == "rnnt":
|
| 102 |
+
return super()._transcribe_forward(batch, trcfg)
|
| 103 |
+
|
| 104 |
+
# CTC Path
|
| 105 |
+
- encoded, encoded_len = self.forward(input_signal=batch[0], input_signal_length=batch[1])
|
| 106 |
+
+ input_signal = batch[0]
|
| 107 |
+
+ input_signal_length = batch[1]
|
| 108 |
+
+ encoded, encoded_len = self.forward(
|
| 109 |
+
+ input_signal=input_signal,
|
| 110 |
+
+ input_signal_length=input_signal_length,
|
| 111 |
+
+ )
|
| 112 |
+
if "multisoftmax" not in self.cfg.decoder:
|
| 113 |
+
language_ids = None
|
| 114 |
+
else:
|
| 115 |
+
diff --git a/nemo/collections/asr/parts/mixins/mixins.py b/nemo/collections/asr/parts/mixins/mixins.py
|
| 116 |
+
index 57dc95da8..9256e5444 100644
|
| 117 |
+
--- a/nemo/collections/asr/parts/mixins/mixins.py
|
| 118 |
+
+++ b/nemo/collections/asr/parts/mixins/mixins.py
|
| 119 |
+
@@ -584,6 +584,7 @@ class ASRModuleMixin(ASRAdapterModelMixin):
|
| 120 |
+
drop_extra_pre_encoded: int = None,
|
| 121 |
+
return_transcription: bool = True,
|
| 122 |
+
return_log_probs: bool = False,
|
| 123 |
+
+ lang_ids: List[str] = None, # AI4Bharat multilingual streaming
|
| 124 |
+
):
|
| 125 |
+
"""
|
| 126 |
+
It simulates a forward step with caching for streaming purposes.
|
| 127 |
+
@@ -689,6 +690,7 @@ class ASRModuleMixin(ASRAdapterModelMixin):
|
| 128 |
+
encoded_lengths=encoded_len,
|
| 129 |
+
return_hypotheses=True,
|
| 130 |
+
partial_hypotheses=previous_hypotheses,
|
| 131 |
+
+ lang_ids=lang_ids,
|
| 132 |
+
)
|
| 133 |
+
greedy_predictions = [hyp.y_sequence for hyp in best_hyp]
|
| 134 |
+
|