-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathforward.py
1281 lines (1141 loc) · 49.7 KB
/
forward.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import math
from typing import Any, Dict, List, Optional, Tuple, Union
from dataclasses import dataclass, field
import heapq
import numpy as np
import torch
from torch.nn.utils.rnn import pad_sequence
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor, Wav2Vec2ProcessorWithLM
from speechbrain.pretrained import EncoderDecoderASR
from speechbrain.decoders.ctc import CTCPrefixScorer
import nemo.collections.asr as nemo_asr
from nemo.collections.asr.parts.utils import rnnt_utils
from nemo.collections.common.parts.rnn import label_collate
from pyctcdecode.alphabet import BPE_TOKEN
from pyctcdecode.constants import DEFAULT_HOTWORD_WEIGHT, DEFAULT_MIN_TOKEN_LOGP, DEFAULT_PRUNE_BEAMS, DEFAULT_PRUNE_LOGP, MIN_TOKEN_CLIP_P
from pyctcdecode.language_model import HotwordScorer
import kenlm
from utils import log_softmax
# for ctc-based models and conformers
Frames = Tuple[int, int]
WordFrames = Tuple[str, Frames]
LMBeam = Tuple[str, str, str, Optional[str], List[Frames], Frames, float, float]
LMState = Optional[Union["kenlm.State", List["kenlm.State"]]]
OutputBeam = Tuple[str, LMState, List[WordFrames], float, float]
OutputBeamMPSafe = Tuple[str, List[WordFrames], float, float]
NULL_FRAMES: Frames = (-1, -1) # placeholder that gets replaced with positive integer frame indices
@dataclass
class Hypothesis: # for transducers
score: float
y_sequence: Union[List[int], torch.Tensor]
text: Optional[str] = None
dec_out: Optional[List[torch.Tensor]] = None
dec_state: Optional[Union[List[List[torch.Tensor]], List[torch.Tensor]]] = None
timestep: Union[List[int], torch.Tensor] = field(default_factory=list)
alignments: Optional[Union[List[int], List[List[int]]]] = None
length: Union[int, torch.Tensor] = 0
y: List[torch.tensor] = None
lm_state: Optional[Union[Dict[str, Any], List[Any]]] = None
lm_scores: Optional[torch.Tensor] = None
tokens: Optional[Union[List[int], torch.Tensor]] = None
last_token: Optional[torch.Tensor] = None
token_list: List = field(default_factory=list)
@torch.no_grad()
def transcribe_batch(args, model, processor, wavs, lens):
transcription = []
if isinstance(model, Wav2Vec2ForCTC):
for wav, len in zip(wavs, lens):
wav = wav[:len].unsqueeze(0)
outputs = model(wav).logits
predicted_ids = torch.argmax(outputs, dim=-1)
if isinstance(processor, Wav2Vec2Processor): # greedy decoding
text = processor.batch_decode(predicted_ids)[0]
elif isinstance(processor, Wav2Vec2ProcessorWithLM): # beam search decoding with external language model
text = processor.decode(
outputs.squeeze(0).detach().cpu().numpy(),
beam_width=args.beam_width,
).text
transcription.append(text)
elif isinstance(model, EncoderDecoderASR):
for wav, len in zip(wavs, lens):
wav = wav[:len].unsqueeze(0)
text = model.transcribe_batch(wav, wav_lens=torch.ones(1).to(args.device))[0]
transcription.append(text[0])
elif isinstance(model, nemo_asr.models.EncDecCTCModelBPE):
for wav, len in zip(wavs, lens):
wav = wav[:len].unsqueeze(0)
len = len.unsqueeze(0)
processed_signal, processed_signal_length = model.preprocessor(
input_signal=wav.to(args.device), length=len.to(args.device),
)
encoder_output, encoder_length = model.encoder(audio_signal=processed_signal, length=processed_signal_length)
log_probs = model.decoder(encoder_output=encoder_output)
greedy_predictions = log_probs.argmax(dim=-1, keepdim=False)
text = model._wer.ctc_decoder_predictions_tensor(greedy_predictions, predictions_len=encoder_length, return_hypotheses=False)[0].upper()
transcription.append(text)
elif isinstance(model, nemo_asr.models.EncDecRNNTBPEModel):
for wav, len in zip(wavs, lens):
wav = wav[:len].unsqueeze(0)
len = len.unsqueeze(0)
encoded_feature, encoded_len = model(input_signal=wav, input_signal_length=len)
best_hyp_texts, _ = model.decoding.rnnt_decoder_predictions_tensor(
encoder_output=encoded_feature, encoded_lengths=encoded_len, return_hypotheses=False
)
text = [best_hyp_text.upper() for best_hyp_text in best_hyp_texts][0]
transcription.append(text)
return transcription
def forward_batch(args, model, processor, wavs, lens, labels=None):
if isinstance(model, Wav2Vec2ForCTC):
outputs = forward_ctc_or_conformer(args, model, processor, wavs, lens, labels)
elif isinstance(model, EncoderDecoderASR):
model.mods.decoder.dec.train()
# model.mods.decoder.lm_weight = args.lm_coef
outputs = forward_attn(args, model, wavs, lens, labels)
elif isinstance(model, nemo_asr.models.EncDecCTCModelBPE):
outputs = forward_ctc_or_conformer(args, model, processor, wavs, lens, labels)
elif isinstance(model, nemo_asr.models.EncDecRNNTBPEModel):
outputs = forward_trans(args, model, wavs, lens, labels)
return outputs
def forward_ctc_or_conformer(args, model, processor, wavs, lens, labels):
if isinstance(model, Wav2Vec2ForCTC):
logits = model(wavs).logits
elif isinstance(model, nemo_asr.models.EncDecCTCModelBPE):
processed_signal, processed_signal_length = model.preprocessor(
input_signal=wavs.to(args.device), length=lens.to(args.device),
)
encoder_output, _ = model.encoder(audio_signal=processed_signal, length=processed_signal_length)
logits = model.decoder(encoder_output=encoder_output)
if labels == None or not args.lm_coef:
return logits
else:
lm_logits = forward_ctc_or_conformer_with_labels(
args,
processor.decoder if isinstance(model, Wav2Vec2ForCTC) else processor,
np.clip(log_softmax(logits.squeeze(0).detach().cpu().numpy(), axis=1),
np.log(MIN_TOKEN_CLIP_P), 0),
labels,
hotword_scorer=HotwordScorer.build_scorer(None, weight=DEFAULT_HOTWORD_WEIGHT),
lm_start_state=None,
).unsqueeze(0)
return logits + args.lm_coef * lm_logits
def forward_ctc_or_conformer_with_labels(
args,
model,
logits,
labels,
hotword_scorer,
lm_start_state,
):
def _merge_tokens(token_1: str, token_2: str) -> str:
"""Fast, whitespace safe merging of tokens."""
if len(token_2) == 0:
text = token_1
elif len(token_1) == 0:
text = token_2
else:
text = token_1 + " " + token_2
return text
def get_new_beams(
model,
beams,
idx_list,
frame_idx,
logit_col,
):
new_beams = []
# bpe we can also have trailing word boundaries ▁⁇▁ so we may need to remember breaks
force_next_break = False
for idx_char in idx_list:
p_char = logit_col[idx_char]
char = model._idx2vocab[idx_char]
for (
text,
next_word,
word_part,
last_char,
text_frames,
part_frames,
logit_score,
idx_history,
lm_logits,
) in beams:
if char == "" or last_char == char:
if char == "":
new_end_frame = part_frames[0]
else:
new_end_frame = frame_idx + 1
new_part_frames = (
part_frames if char == "" else (part_frames[0], new_end_frame)
)
new_beams.append(
(
text,
next_word,
word_part,
char,
text_frames,
new_part_frames,
logit_score + p_char,
idx_history + [idx_char],
lm_logits,
)
)
# if bpe and leading space char
elif model._is_bpe and (char[:1] == BPE_TOKEN or force_next_break):
force_next_break = False
# some tokens are bounded on both sides like ▁⁇▁
clean_char = char
if char[:1] == BPE_TOKEN:
clean_char = clean_char[1:]
if char[-1:] == BPE_TOKEN:
clean_char = clean_char[:-1]
force_next_break = True
new_frame_list = (
text_frames if word_part == "" else text_frames + [part_frames]
)
new_beams.append(
(
text,
word_part,
clean_char,
char,
new_frame_list,
(frame_idx, frame_idx + 1),
logit_score + p_char,
idx_history + [idx_char],
lm_logits,
)
)
# if not bpe and space char
elif not model._is_bpe and char == " ":
new_frame_list = (
text_frames if word_part == "" else text_frames + [part_frames]
)
new_beams.append(
(
text,
word_part,
"",
char,
new_frame_list,
NULL_FRAMES,
logit_score + p_char,
idx_history + [idx_char],
lm_logits,
)
)
# general update of continuing token without space
else:
new_part_frames = (
(frame_idx, frame_idx + 1)
if part_frames[0] < 0
else (part_frames[0], frame_idx + 1)
)
new_beams.append(
(
text,
next_word,
word_part + char,
char,
text_frames,
new_part_frames,
logit_score + p_char,
idx_history + [idx_char],
lm_logits,
)
)
return new_beams
def get_lm_beams(
model,
beams,
hotword_scorer,
cached_lm_scores,
cached_partial_token_scores,
is_eos,
):
lm_score_list = np.zeros(len(beams))
language_model = model._language_model
new_beams = []
for text, next_word, word_part, last_char, frame_list, frames, logit_score, idx_history, lm_logits in beams:
new_text = _merge_tokens(text, next_word)
if new_text not in cached_lm_scores:
_, prev_raw_lm_score, start_state = cached_lm_scores[text]
score, end_state = language_model.score(start_state, next_word, is_last_word=is_eos)
raw_lm_score = prev_raw_lm_score + score
lm_hw_score = raw_lm_score + hotword_scorer.score(new_text)
cached_lm_scores[new_text] = (lm_hw_score, raw_lm_score, end_state)
lm_score, _, _ = cached_lm_scores[new_text]
if len(word_part) > 0:
if word_part not in cached_partial_token_scores:
# if prefix available in hotword trie use that, otherwise default to char trie
if word_part in hotword_scorer:
cached_partial_token_scores[word_part] = hotword_scorer.score_partial_token(
word_part
)
else:
cached_partial_token_scores[word_part] = language_model.score_partial_token(
word_part
)
lm_score += cached_partial_token_scores[word_part]
new_beams.append(
(
new_text,
"",
word_part,
last_char,
frame_list,
frames,
logit_score,
logit_score + lm_score,
idx_history,
lm_logits,
)
)
lm_score_list[model._vocab2idx[last_char]] = lm_score
new_beams_with_lm_logits = []
for text, next_word, word_part, last_char, frame_list, frames, logit_score, combined_score, idx_history, lm_logits in new_beams:
new_beams_with_lm_logits.append(
(
text,
next_word,
word_part,
last_char,
frame_list,
frames,
logit_score,
combined_score,
idx_history,
lm_logits + [lm_score_list],
)
)
return new_beams_with_lm_logits
language_model = model._language_model
if lm_start_state is None and language_model is not None:
cached_lm_scores: Dict[str, Tuple[float, float, LMState]] = {
"": (0.0, 0.0, language_model.get_start_state())
}
else:
cached_lm_scores = {"": (0.0, 0.0, lm_start_state)}
cached_p_lm_scores: Dict[str, float] = {}
if not hasattr(model, '_vocab2idx'):
model._vocab2idx = {vocab: idx for idx, vocab in model._idx2vocab.items()}
beams = [("", "", "", None, [], NULL_FRAMES, 0.0, [], [])] # start with single beam to expand on
for frame_idx, logit_col in enumerate(logits):
idx_list = list(range(0, logit_col.shape[-1]))
new_beams = get_new_beams(
model,
beams,
idx_list,
frame_idx,
logit_col,
)
scored_beams = get_lm_beams(
model,
new_beams,
hotword_scorer,
cached_lm_scores,
cached_p_lm_scores,
is_eos=False,
)
beams = [scored_beams[labels[frame_idx]][:-3] + scored_beams[labels[frame_idx]][-2:]]
return torch.tensor(np.array(beams[0][-1])).to(args.device)
def forward_attn(args, model, wavs, lens, labels):
def decoder_forward_step(model, inp_tokens, memory, enc_states, enc_lens):
"""Performs a step in the implemented beamsearcher."""
hs, c = memory
e = model.emb(inp_tokens)
dec_out, hs, c, w = model.dec.forward_step(
e, hs, c, enc_states, enc_lens
)
log_probs = model.softmax(model.fc(dec_out) / model.temperature)
if model.dec.attn_type == "multiheadlocation":
w = torch.mean(w, dim=1)
return log_probs, (hs, c), w
logits = []
enc_states = model.encode_batch(wavs, lens)
enc_lens = torch.tensor([enc_states.shape[1]]).to(args.device)
device = enc_states.device
batch_size = enc_states.shape[0]
memory = model.mods.decoder.reset_mem(batch_size, device=device)
inp_tokens = (enc_states.new_zeros(batch_size).fill_(model.mods.decoder.bos_index).long())
max_decode_steps = int(enc_states.shape[1] * model.mods.decoder.max_decode_ratio)
for decode_step in range(max_decode_steps):
log_probs, memory, _ = decoder_forward_step(
model.mods.decoder, inp_tokens, memory, enc_states, enc_lens
)
logits.append(log_probs)
# teacher-forcing using beam search
if labels != None:
inp_tokens = torch.tensor([labels[decode_step]]).to(log_probs.device)
else:
inp_tokens = log_probs.argmax(dim=-1)
logits = torch.stack(logits, dim=1).to(args.device)
return logits
def forward_trans(args, model, wavs, lens, labels):
logits = []
encoder_output, encoded_lengths = model(input_signal=wavs, input_signal_length=lens)
encoder_output = encoder_output.transpose(1, 2)
logitlen = encoded_lengths
inseq = encoder_output # [B, T, D]
x, out_len, device = inseq, logitlen, inseq.device
batchsize = x.shape[0]
hypotheses = [rnnt_utils.Hypothesis(score=0.0, y_sequence=[], timestep=[], dec_state=None) for _ in range(batchsize)]
hidden = None
if model.decoding.decoding.preserve_alignments:
for hyp in hypotheses:
hyp.alignments = [[]]
last_label = torch.full([batchsize, 1], fill_value=model.decoding.decoding._blank_index, dtype=torch.long, device=device)
blank_mask = torch.full([batchsize], fill_value=0, dtype=torch.bool, device=device)
max_out_len = out_len.max()
for time_idx in range(max_out_len):
f = x.narrow(dim=1, start=time_idx, length=1) # [B, 1, D]
not_blank = True
symbols_added = 0
blank_mask.mul_(False)
blank_mask = time_idx >= out_len
while not_blank and (model.decoding.decoding.max_symbols is None or symbols_added < model.decoding.decoding.max_symbols):
if time_idx == 0 and symbols_added == 0 and hidden is None:
in_label = model.decoding.decoding._SOS
else:
in_label = last_label
if isinstance(in_label, torch.Tensor) and in_label.dtype != torch.long:
in_label = in_label.long()
g, hidden_prime = model.decoding.decoding.decoder.predict(None, hidden, False, batchsize)
else:
if in_label == model.decoding.decoding._SOS:
g, hidden_prime = model.decoding.decoding.decoder.predict(None, hidden, False, batchsize)
else:
in_label = label_collate([[in_label.cpu()]])
g, hidden_prime = model.decoding.decoding.decoder.predict(in_label, hidden, False, batchsize)
logp = model.decoding.decoding.joint.joint(f, g)
if not logp.is_cuda:
logp = logp.log_softmax(dim=len(logp.shape) - 1)
logp = logp[:, 0, 0, :]
if logp.dtype != torch.float32:
logp = logp.float()
# teacher-forcing using beam search
if labels != None:
label_idx = len(logits)
label = labels[label_idx] if label_idx < len(labels) else model.decoding.decoding._blank_index
v, k = logp[:, label], torch.tensor([label for _ in range(logp.shape[0])]).to(logp.device)
else:
v, k = logp.max(1)
del g
logits.append(logp)
k_is_blank = k == model.decoding.decoding._blank_index
blank_mask.bitwise_or_(k_is_blank)
del k_is_blank
if model.decoding.decoding.preserve_alignments:
logp_vals = logp.to('cpu')
logp_ids = logp_vals.max(1)[1]
for batch_idx in range(batchsize):
if time_idx < out_len[batch_idx]:
hypotheses[batch_idx].alignments[-2].append(
(logp_vals[batch_idx], logp_ids[batch_idx])
)
del logp_vals
if blank_mask.all():
not_blank = False
if model.decoding.decoding.preserve_alignments:
for batch_idx in range(batchsize):
if len(hypotheses[batch_idx].alignments[-2]) > 0:
hypotheses[batch_idx].alignments.append([]) # blank buffer for next timestep
else:
blank_indices = (blank_mask == 1).nonzero(as_tuple=False)
if hidden is not None:
hidden_prime = model.decoding.decoding.decoder.batch_copy_states(hidden_prime, hidden, blank_indices)
elif len(blank_indices) > 0 and hidden is None:
hidden_prime = model.decoding.decoding.decoder.batch_copy_states(hidden_prime, None, blank_indices, value=0.0)
k[blank_indices] = last_label[blank_indices, 0]
last_label = k.clone().view(-1, 1)
hidden = hidden_prime
for kidx, ki in enumerate(k):
if blank_mask[kidx] == 0:
hypotheses[kidx].y_sequence.append(ki)
hypotheses[kidx].timestep.append(time_idx)
hypotheses[kidx].score += float(v[kidx])
symbols_added += 1
logits = torch.stack(logits, dim=1)[:, :max_out_len, :]
return logits
def get_logits_and_pseudo_labels(args, model, processor, wavs, lens):
if args.decoding_method == "greedy_search" or args.beam_width == 1: # greedy search
logits = forward_batch(args, model, processor, wavs, lens)
pseudo_labels = [torch.argmax(logits, dim=-1).squeeze(0)]
else: # beam search
encoder_output, encoder_length = encode_batch(args, model, wavs, lens)
pseudo_labels = decode_batch(args, model, processor, encoder_output, encoder_length)
logits = forward_batch(args, model, processor, wavs, lens, labels=pseudo_labels[0])
return logits, pseudo_labels
@torch.no_grad()
def encode_batch(args, model, wavs, lens):
if isinstance(model, Wav2Vec2ForCTC):
logits = model(wavs).logits
logitlen = torch.tensor([logits.shape[1]]).to(logits.device)
outputs = logits, logitlen
elif isinstance(model, EncoderDecoderASR):
enc_states = model.encode_batch(wavs, lens)
enc_lens = torch.tensor([enc_states.shape[1]]).to(args.device)
outputs = enc_states, enc_lens
elif isinstance(model, nemo_asr.models.EncDecCTCModelBPE):
processed_signal, processed_signal_length = model.preprocessor(
input_signal=wavs.to(args.device), length=lens.to(args.device),
)
encoder_output, encoder_length = model.encoder(
audio_signal=processed_signal, length=processed_signal_length
)
outputs = encoder_output, encoder_length
elif isinstance(model, nemo_asr.models.EncDecRNNTBPEModel):
enc_states, enc_lens = model(input_signal=wavs, input_signal_length=lens)
enc_states = enc_states.transpose(1, 2)
outputs = enc_states, enc_lens
return outputs
@torch.no_grad()
def decode_batch(args, model, processor, encoder_output, encoder_length):
beam_width = args.beam_width if args.decoding_method == "beam_search" else 1
if isinstance(model, Wav2Vec2ForCTC):
pseudo_labels = decode_ctc_or_conformer(
processor.decoder,
logits=np.clip(log_softmax(encoder_output.squeeze(0).detach().cpu().numpy(), axis=1), np.log(MIN_TOKEN_CLIP_P), 0),
beam_width=beam_width,
beam_prune_logp=DEFAULT_PRUNE_LOGP,
token_min_logp=DEFAULT_MIN_TOKEN_LOGP,
prune_history=DEFAULT_PRUNE_BEAMS,
hotword_scorer=HotwordScorer.build_scorer(None, weight=DEFAULT_HOTWORD_WEIGHT),
lm_start_state=None,
)
elif isinstance(model, EncoderDecoderASR):
model.mods.decoder.topk = beam_width
pseudo_labels = decode_attn(
model.mods.decoder,
encoder_output,
torch.ones(1).to(encoder_output.device),
beam_width,
)
elif isinstance(model, nemo_asr.models.EncDecCTCModelBPE):
logits = model.decoder(encoder_output=encoder_output)
pseudo_labels = decode_ctc_or_conformer(
processor,
logits=np.clip(log_softmax(logits.squeeze(0).detach().cpu().numpy(), axis=1), np.log(MIN_TOKEN_CLIP_P), 0),
beam_width=beam_width,
beam_prune_logp=DEFAULT_PRUNE_LOGP,
token_min_logp=DEFAULT_MIN_TOKEN_LOGP,
prune_history=DEFAULT_PRUNE_BEAMS,
hotword_scorer=HotwordScorer.build_scorer(None, weight=DEFAULT_HOTWORD_WEIGHT),
lm_start_state=None,
)
elif isinstance(model, nemo_asr.models.EncDecRNNTBPEModel):
processor.beam_size = beam_width
pseudo_labels = decode_trans(processor, encoder_output, encoder_length)
return pseudo_labels
def decode_ctc_or_conformer(
model,
logits,
beam_width,
beam_prune_logp,
token_min_logp,
prune_history,
hotword_scorer,
lm_start_state,
):
def _merge_beams(beams):
"""Merge beams with same prefix together."""
beam_dict = {}
for text, next_word, word_part, last_char, text_frames, part_frames, logit_score, idx_history in beams:
new_text = _merge_tokens(text, next_word)
hash_idx = (new_text, word_part, last_char)
if hash_idx not in beam_dict:
beam_dict[hash_idx] = (
text,
next_word,
word_part,
last_char,
text_frames,
part_frames,
logit_score,
idx_history
)
else:
beam_dict[hash_idx] = (
text,
next_word,
word_part,
last_char,
text_frames,
part_frames,
_sum_log_scores(beam_dict[hash_idx][-2], logit_score),
idx_history
)
return list(beam_dict.values())
def _sort_and_trim_beams(beams, beam_width: int):
"""Take top N beams by score."""
return heapq.nlargest(beam_width, beams, key=lambda x: x[-2])
def _merge_tokens(token_1: str, token_2: str) -> str:
"""Fast, whitespace safe merging of tokens."""
if len(token_2) == 0:
text = token_1
elif len(token_1) == 0:
text = token_2
else:
text = token_1 + " " + token_2
return text
def _sum_log_scores(s1: float, s2: float) -> float:
"""Sum log odds in a numerically stable way."""
# this is slightly faster than using max
if s1 >= s2:
log_sum = s1 + math.log(1 + math.exp(s2 - s1))
else:
log_sum = s2 + math.log(1 + math.exp(s1 - s2))
return log_sum
def get_new_beams(
model,
beams,
idx_list,
frame_idx,
logit_col,
):
new_beams = []
# bpe we can also have trailing word boundaries ▁⁇▁ so we may need to remember breaks
force_next_break = False
for idx_char in idx_list:
p_char = logit_col[idx_char]
char = model._idx2vocab[idx_char]
for (
text,
next_word,
word_part,
last_char,
text_frames,
part_frames,
logit_score,
idx_history,
) in beams:
if char == "" or last_char == char:
if char == "":
new_end_frame = part_frames[0]
else:
new_end_frame = frame_idx + 1
new_part_frames = (
part_frames if char == "" else (part_frames[0], new_end_frame)
)
new_beams.append(
(
text,
next_word,
word_part,
char,
text_frames,
new_part_frames,
logit_score + p_char,
idx_history + [idx_char],
)
)
# if bpe and leading space char
elif model._is_bpe and (char[:1] == BPE_TOKEN or force_next_break):
force_next_break = False
# some tokens are bounded on both sides like ▁⁇▁
clean_char = char
if char[:1] == BPE_TOKEN:
clean_char = clean_char[1:]
if char[-1:] == BPE_TOKEN:
clean_char = clean_char[:-1]
force_next_break = True
new_frame_list = (
text_frames if word_part == "" else text_frames + [part_frames]
)
new_beams.append(
(
text,
word_part,
clean_char,
char,
new_frame_list,
(frame_idx, frame_idx + 1),
logit_score + p_char,
idx_history + [idx_char],
)
)
# if not bpe and space char
elif not model._is_bpe and char == " ":
new_frame_list = (
text_frames if word_part == "" else text_frames + [part_frames]
)
new_beams.append(
(
text,
word_part,
"",
char,
new_frame_list,
NULL_FRAMES,
logit_score + p_char,
idx_history + [idx_char],
)
)
# general update of continuing token without space
else:
new_part_frames = (
(frame_idx, frame_idx + 1)
if part_frames[0] < 0
else (part_frames[0], frame_idx + 1)
)
new_beams.append(
(
text,
next_word,
word_part + char,
char,
text_frames,
new_part_frames,
logit_score + p_char,
idx_history + [idx_char],
)
)
new_beams = _merge_beams(new_beams)
return new_beams
def get_lm_beams(
model,
beams,
hotword_scorer: HotwordScorer,
cached_lm_scores: Dict[str, Tuple[float, float, LMState]],
cached_partial_token_scores: Dict[str, float],
is_eos: bool = False,
) -> List[LMBeam]:
"""Update score by averaging logit_score and lm_score."""
# get language model and see if exists
language_model = model._language_model
# if no language model available then return raw score + hotwords as lm score
if language_model is None:
new_beams = []
for text, next_word, word_part, last_char, frame_list, frames, logit_score, idx_history in beams:
new_text = _merge_tokens(text, next_word)
# note that usually this gets scaled with alpha
lm_hw_score = (
logit_score
+ hotword_scorer.score(new_text)
+ hotword_scorer.score_partial_token(word_part)
)
new_beams.append(
(
new_text,
"",
word_part,
last_char,
frame_list,
frames,
logit_score,
lm_hw_score,
idx_history
)
)
return new_beams
new_beams = []
for text, next_word, word_part, last_char, frame_list, frames, logit_score, idx_history in beams:
new_text = _merge_tokens(text, next_word)
if new_text not in cached_lm_scores:
_, prev_raw_lm_score, start_state = cached_lm_scores[text]
score, end_state = language_model.score(start_state, next_word, is_last_word=is_eos)
raw_lm_score = prev_raw_lm_score + score
lm_hw_score = raw_lm_score + hotword_scorer.score(new_text)
cached_lm_scores[new_text] = (lm_hw_score, raw_lm_score, end_state)
lm_score, _, _ = cached_lm_scores[new_text]
if len(word_part) > 0:
if word_part not in cached_partial_token_scores:
# if prefix available in hotword trie use that, otherwise default to char trie
if word_part in hotword_scorer:
cached_partial_token_scores[word_part] = hotword_scorer.score_partial_token(
word_part
)
else:
cached_partial_token_scores[word_part] = language_model.score_partial_token(
word_part
)
lm_score += cached_partial_token_scores[word_part]
new_beams.append(
(
new_text,
"",
word_part,
last_char,
frame_list,
frames,
logit_score,
logit_score + lm_score,
idx_history,
)
)
return new_beams
language_model = model._language_model
if lm_start_state is None and language_model is not None:
cached_lm_scores: Dict[str, Tuple[float, float, LMState]] = {
"": (0.0, 0.0, language_model.get_start_state())
}
else:
cached_lm_scores = {"": (0.0, 0.0, lm_start_state)}
cached_p_lm_scores: Dict[str, float] = {}
# start with single beam to expand on
beams = [("", "", "", None, [], NULL_FRAMES, 0.0, [])]
for frame_idx, logit_col in enumerate(logits):
max_idx = logit_col.argmax()
idx_list = set(np.where(logit_col >= token_min_logp)[0]) | {max_idx}
new_beams = get_new_beams(
model,
beams,
idx_list,
frame_idx,
logit_col,
)
# lm scoring and beam pruning
scored_beams = get_lm_beams(
model,
new_beams,
hotword_scorer,
cached_lm_scores,
cached_p_lm_scores,
)
# remove beam outliers
max_score = max([b[-2] for b in scored_beams])
scored_beams = [b for b in scored_beams if b[-2] >= max_score + beam_prune_logp]
trimmed_beams = _sort_and_trim_beams(scored_beams, beam_width)
beams = [b[:-2] + (b[-1], ) for b in trimmed_beams]
new_beams = []
for text, _, word_part, _, frame_list, frames, logit_score, idx_history in beams:
new_token_times = frame_list if word_part == "" else frame_list + [frames]
new_beams.append((text, word_part, "", None, new_token_times, (-1, -1), logit_score, idx_history))
new_beams = _merge_beams(new_beams)
scored_beams = get_lm_beams(
model,
new_beams,
hotword_scorer,
cached_lm_scores,
cached_p_lm_scores,
is_eos=True,
)
scored_beams = [b[:-2] + (b[-1], ) for b in scored_beams]
scored_beams = _merge_beams(scored_beams)
# remove beam outliers
max_score = max([b[-2] for b in beams])
scored_beams = [b for b in beams if b[-2] >= max_score + beam_prune_logp]
trimmed_beams = _sort_and_trim_beams(scored_beams, beam_width)
# remove unnecessary information from beams
output_beams = [
torch.tensor(idx_history)
for _, _, _, _, _, _, _, idx_history in trimmed_beams
]
return output_beams
def decode_attn(model, enc_states, wav_len, beam_width):
def inflate_tensor(tensor, times, dim):
return torch.repeat_interleave(tensor, times, dim=dim)
def mask_by_condition(tensor, cond, fill_value):
tensor = torch.where(
cond, tensor, torch.Tensor([fill_value]).to(tensor.device)
)
return tensor
def forward_step(model, inp_tokens, memory, enc_states, enc_lens):
"""Performs a step in the implemented beamsearcher."""
hs, c = memory
e = model.emb(inp_tokens)
dec_out, hs, c, w = model.dec.forward_step(
e, hs, c, enc_states, enc_lens
)
log_probs = model.softmax(model.fc(dec_out) / model.temperature)
if model.dec.attn_type == "multiheadlocation":
w = torch.mean(w, dim=1)
return log_probs, (hs, c), w
def lm_forward_step(model, inp_tokens, memory):
"""Applies a step to the LM during beamsearch."""
with torch.no_grad():
logits, hs = model.lm(inp_tokens, hx=memory)
log_probs = model.log_softmax(logits / model.temperature_lm)
return log_probs, hs
def ctc_forward_step(model, x):
"""Applies a ctc step during bramsearch."""
logits = model.ctc_fc(x)
log_probs = model.softmax(logits)
return log_probs
enc_lens = torch.round(enc_states.shape[1] * wav_len).int()
device = enc_states.device
batch_size = enc_states.shape[0]
memory = model.reset_mem(batch_size * beam_width, device=device)
if model.lm_weight > 0:
lm_memory = model.reset_lm_mem(batch_size * beam_width, device)
if model.ctc_weight > 0:
# (batch_size * beam_size, L, vocab_size)
ctc_outputs = ctc_forward_step(model, enc_states)
ctc_scorer = CTCPrefixScorer(
ctc_outputs,
enc_lens,
batch_size,
beam_width,
model.blank_index,
model.eos_index,
model.ctc_window_size,
)
ctc_memory = None
# Inflate the enc_states and enc_len by beam_size times
enc_states = inflate_tensor(enc_states, times=beam_width, dim=0)
enc_lens = inflate_tensor(enc_lens, times=beam_width, dim=0)
# Using bos as the first input
inp_tokens = (
torch.zeros(batch_size * beam_width, device=device)
.fill_(model.bos_index)
.long()
)
# The first index of each sentence.
model.beam_offset = (
torch.arange(batch_size, device=device) * beam_width
)
# initialize sequence scores variables.
sequence_scores = torch.empty(
batch_size * beam_width, device=device
)
sequence_scores.fill_(float("-inf"))
# keep only the first to make sure no redundancy.
sequence_scores.index_fill_(0, model.beam_offset, 0.0)
# keep the hypothesis that reaches eos and their corresponding score and log_probs.
hyps_and_scores = [[] for _ in range(batch_size)]
# keep the sequences that still not reaches eos.
alived_seq = torch.empty(
batch_size * beam_width, 0, device=device
).long()
# Keep the log-probabilities of alived sequences.
alived_log_probs = torch.empty(
batch_size * beam_width, 0, device=device
)
min_decode_steps = int(enc_states.shape[1] * model.min_decode_ratio)
max_decode_steps = int(enc_states.shape[1] * model.max_decode_ratio)
# Initialize the previous attention peak to zero
# This variable will be used when using_max_attn_shift=True
prev_attn_peak = torch.zeros(batch_size * beam_width, device=device)