Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
1126 lines (1007 loc) · 46.1 KB
/
Copy pathmain.cpp
File metadata and controls
1126 lines (1007 loc) · 46.1 KB
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
#include "parakeet/parakeet.hpp"
#include <axiom/io/numpy.hpp>
#include <axiom/io/safetensors.hpp>
#include <axiom/system.hpp>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <string>
static void print_usage(const char *prog) {
std::cerr
<< "Usage: " << prog
<< " <model.safetensors> <audio.wav> [audio2.wav ...] [options]\n"
<< "\nModel types:\n"
<< " --model TYPE Model type (default: tdt-ctc-110m)\n"
<< " Types: tdt-ctc-110m, tdt-600m, rnnt-600m,\n"
<< " eou-120m, nemotron-600m, sortformer,\n"
<< " diarized\n"
<< "\nDecoder options:\n"
<< " --ctc Use CTC greedy decoder (default: TDT)\n"
<< " --tdt Use TDT decoder\n"
<< " --ctc-beam Use CTC beam search decoder\n"
<< " --tdt-beam Use TDT beam search decoder\n"
<< " --beam-width N Beam width for beam search (default: 8)\n"
<< " --lm PATH ARPA language model for beam search\n"
<< " --lm-weight N LM interpolation weight (default: 0.5)\n"
<< "\nPhrase boost:\n"
<< " --boost PHRASE Boost a phrase (repeatable)\n"
<< " --boost-score N Boost score (default: 5.0)\n"
<< "\nOther options:\n"
<< " --vocab PATH SentencePiece vocab file for detokenization\n"
<< " --sortformer-weights PATH Sortformer weights (for diarized "
"mode)\n"
<< " --features PATH Load pre-computed features from .npy file\n"
<< " --gpu Run on Metal GPU\n"
<< " --fp16 Use half-precision (fp16) inference\n"
<< " --timestamps Show word-level timestamps\n"
<< " --streaming Use streaming mode (eou/nemotron models)\n"
<< " --latency N Right context frames for nemotron (0/1/6/13)\n"
<< " --vad PATH Enable Silero VAD with given weights file\n"
<< " --vad-threshold F VAD speech threshold (default: 0.5)\n"
<< "\nBatch mode:\n"
<< " Multiple audio files can be specified for batch inference.\n"
<< " Supported for tdt-ctc-110m and tdt-600m models.\n"
<< std::endl;
}
// ─── TDT-CTC 110M mode (original) ───────────────────────────────────────────
static int run_tdt_ctc_110m(const std::string &weights_path,
const std::string &audio_path,
const std::string &vocab_path,
const std::string &features_path, bool use_ctc,
bool use_gpu, bool use_fp16, bool show_timestamps,
const std::vector<std::string> &boost_phrases = {},
float boost_score = 5.0f, bool use_ctc_beam = false,
int beam_width = 8, const std::string &lm_path = "",
float lm_weight = 0.5f, bool use_tdt_beam = false,
const std::string &vad_weights_path = {},
float vad_threshold = 0.5f) {
using namespace parakeet;
using Clock = std::chrono::high_resolution_clock;
std::cout << "Loading model: tdt-ctc-110m" << std::endl;
TDTCTCConfig cfg = make_110m_config();
ParakeetTDTCTC model(cfg);
auto weights = axiom::io::safetensors::load(weights_path);
model.load_state_dict(weights, "", false);
std::cout << "Model loaded (" << weights.size() << " tensors)" << std::endl;
if (use_fp16) {
model.to(axiom::DType::Float16);
std::cout << "Model cast to fp16" << std::endl;
}
if (use_gpu) {
auto t = Clock::now();
model.to(axiom::Device::GPU);
auto dt = std::chrono::duration_cast<std::chrono::milliseconds>(
Clock::now() - t)
.count();
std::cout << "Model moved to GPU (" << dt << " ms)" << std::endl;
}
Tokenizer tokenizer;
if (!vocab_path.empty()) {
tokenizer.load(vocab_path);
std::cout << "Vocab loaded (" << tokenizer.vocab_size() << " tokens)"
<< std::endl;
}
auto audio = read_audio(audio_path);
std::cout << "Audio: " << audio.original_sample_rate << "Hz"
<< (audio.original_sample_rate != audio.sample_rate
? " -> " + std::to_string(audio.sample_rate) + "Hz"
: "")
<< ", " << audio.num_samples << " samples, " << std::fixed
<< std::setprecision(1) << audio.duration << "s" << std::endl;
// VAD preprocessing
axiom::Tensor audio_samples = audio.samples;
std::unique_ptr<parakeet::audio::TimestampRemapper> remapper;
if (!vad_weights_path.empty()) {
parakeet::audio::SileroVAD silero_vad(vad_weights_path);
parakeet::audio::VADConfig vad_cfg;
vad_cfg.threshold = vad_threshold;
vad_cfg.neg_threshold = vad_threshold - 0.15f;
auto segments =
silero_vad.detect(audio.samples, audio.sample_rate, vad_cfg);
if (!segments.empty()) {
float speech_dur = 0;
for (const auto &s : segments)
speech_dur +=
static_cast<float>(s.end_sample - s.start_sample) /
audio.sample_rate;
std::cout << "VAD: " << segments.size() << " speech segments, "
<< std::fixed << std::setprecision(1) << speech_dur
<< "s speech / " << audio.duration << "s total ("
<< static_cast<int>(100 *
(1 - speech_dur / audio.duration))
<< "% filtered)" << std::endl;
audio_samples =
parakeet::audio::collect_speech(audio.samples, segments);
if (show_timestamps) {
remapper = std::make_unique<parakeet::audio::TimestampRemapper>(
segments);
}
}
}
auto t0 = Clock::now();
axiom::Tensor features;
if (!features_path.empty()) {
features = axiom::io::numpy::load(features_path);
} else {
features = preprocess_audio(audio_samples);
}
auto t1 = Clock::now();
std::cout << "Preprocessing: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0)
.count()
<< " ms" << std::endl;
if (use_fp16)
features = features.half();
if (use_gpu)
features = features.gpu();
t0 = Clock::now();
auto encoder_out = model.encoder()(features);
t1 = Clock::now();
std::cout << "Encoder: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0)
.count()
<< " ms" << std::endl;
// Build phrase boost trie if needed
ContextTrie trie;
bool use_boost = !boost_phrases.empty() && tokenizer.loaded();
if (use_boost) {
trie.build(boost_phrases, tokenizer);
std::cout << "Phrase boost: " << boost_phrases.size() << " phrases, "
<< trie.size() << " trie nodes" << std::endl;
}
// Load LM if beam search with LM requested
ArpaLM arpa_lm;
if ((use_ctc_beam || use_tdt_beam) && !lm_path.empty()) {
arpa_lm.load(lm_path);
std::cout << "LM loaded: " << arpa_lm.size() << " n-grams, order "
<< arpa_lm.order() << std::endl;
}
t0 = Clock::now();
std::vector<std::vector<int>> token_ids;
std::vector<std::vector<TimestampedToken>> timestamped_tokens;
if (use_tdt_beam) {
TDTBeamSearchOptions tdt_bs_opts;
tdt_bs_opts.beam_width = beam_width;
tdt_bs_opts.lm = arpa_lm.loaded() ? &arpa_lm : nullptr;
tdt_bs_opts.lm_weight = lm_weight;
tdt_bs_opts.pieces = tokenizer.loaded() ? &tokenizer.pieces() : nullptr;
if (show_timestamps) {
timestamped_tokens = tdt_beam_decode_with_timestamps(
model, encoder_out, cfg.durations, tdt_bs_opts);
token_ids.resize(timestamped_tokens.size());
for (size_t b = 0; b < timestamped_tokens.size(); ++b)
for (const auto &t : timestamped_tokens[b])
token_ids[b].push_back(t.token_id);
} else {
token_ids =
tdt_beam_decode(model, encoder_out, cfg.durations, tdt_bs_opts);
}
std::cout << "Decoder: TDT beam (width=" << beam_width
<< (arpa_lm.loaded() ? ", +LM" : "") << ")" << std::endl;
} else if (use_ctc_beam) {
auto log_probs = model.ctc_decoder()(encoder_out);
auto cpu_lp = log_probs.cpu();
BeamSearchOptions bs_opts;
bs_opts.beam_width = beam_width;
bs_opts.lm = arpa_lm.loaded() ? &arpa_lm : nullptr;
bs_opts.lm_weight = lm_weight;
bs_opts.pieces = tokenizer.loaded() ? &tokenizer.pieces() : nullptr;
if (show_timestamps) {
timestamped_tokens =
ctc_beam_decode_with_timestamps(cpu_lp, bs_opts);
token_ids.resize(timestamped_tokens.size());
for (size_t b = 0; b < timestamped_tokens.size(); ++b)
for (const auto &t : timestamped_tokens[b])
token_ids[b].push_back(t.token_id);
} else {
token_ids = ctc_beam_decode(cpu_lp, bs_opts);
}
std::cout << "Decoder: CTC beam (width=" << beam_width
<< (arpa_lm.loaded() ? ", +LM" : "") << ")" << std::endl;
} else if (use_ctc) {
auto log_probs = model.ctc_decoder()(encoder_out);
auto cpu_lp = log_probs.cpu();
if (show_timestamps) {
timestamped_tokens =
use_boost ? ctc_greedy_decode_with_timestamps_boosted(
cpu_lp, trie, boost_score)
: ctc_greedy_decode_with_timestamps(cpu_lp);
token_ids.resize(timestamped_tokens.size());
for (size_t b = 0; b < timestamped_tokens.size(); ++b)
for (const auto &t : timestamped_tokens[b])
token_ids[b].push_back(t.token_id);
} else {
token_ids =
use_boost ? ctc_greedy_decode_boosted(cpu_lp, trie, boost_score)
: ctc_greedy_decode(cpu_lp);
}
std::cout << "Decoder: CTC" << (use_boost ? " (boosted)" : "")
<< std::endl;
} else {
if (show_timestamps) {
timestamped_tokens =
use_boost
? tdt_greedy_decode_with_timestamps_boosted(
model, encoder_out, cfg.durations, trie, boost_score)
: tdt_greedy_decode_with_timestamps(model, encoder_out,
cfg.durations);
token_ids.resize(timestamped_tokens.size());
for (size_t b = 0; b < timestamped_tokens.size(); ++b)
for (const auto &t : timestamped_tokens[b])
token_ids[b].push_back(t.token_id);
} else {
token_ids =
use_boost
? tdt_greedy_decode_boosted(
model, encoder_out, cfg.durations, trie, boost_score)
: tdt_greedy_decode(model, encoder_out, cfg.durations);
}
std::cout << "Decoder: TDT" << (use_boost ? " (boosted)" : "")
<< std::endl;
}
t1 = Clock::now();
std::cout << "Decode: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0)
.count()
<< " ms" << std::endl;
// Remap timestamps if VAD was used
if (remapper && show_timestamps) {
for (auto &ts_vec : timestamped_tokens) {
ts_vec = remapper->remap_tokens(ts_vec);
}
}
for (size_t b = 0; b < token_ids.size(); ++b) {
const auto &tokens = token_ids[b];
std::cout << "\n--- Transcription (" << tokens.size() << " tokens) ---"
<< std::endl;
if (tokenizer.loaded()) {
std::cout << tokenizer.decode(tokens) << std::endl;
} else {
for (size_t i = 0; i < tokens.size(); ++i) {
if (i > 0)
std::cout << " ";
std::cout << tokens[i];
}
std::cout << std::endl;
}
if (show_timestamps && b < timestamped_tokens.size() &&
tokenizer.loaded()) {
auto words =
group_timestamps(timestamped_tokens[b], tokenizer.pieces());
std::cout << "\n--- Word Timestamps ---" << std::endl;
for (const auto &w : words) {
std::cout << " [" << std::fixed << std::setprecision(2)
<< w.start << "s - " << w.end << "s] ("
<< w.confidence << ") " << w.word << std::endl;
}
}
}
return 0;
}
// ─── TDT-CTC 110M batch mode ────────────────────────────────────────────────
static int run_tdt_ctc_110m_batch(
const std::string &weights_path,
const std::vector<std::string> &audio_paths, const std::string &vocab_path,
bool use_ctc, bool use_gpu, bool use_fp16, bool show_timestamps,
const std::vector<std::string> &boost_phrases = {},
float boost_score = 5.0f, const std::string &vad_weights_path = {},
float vad_threshold = 0.5f) {
using namespace parakeet;
using Clock = std::chrono::high_resolution_clock;
std::cout << "Loading model: tdt-ctc-110m (batch mode, "
<< audio_paths.size() << " files)" << std::endl;
Transcriber transcriber(weights_path, vocab_path);
if (use_fp16)
transcriber.to_half();
if (use_gpu)
transcriber.to_gpu();
if (!vad_weights_path.empty()) {
transcriber.enable_vad(vad_weights_path);
std::cout << "VAD enabled (threshold=" << vad_threshold << ")"
<< std::endl;
}
TranscribeOptions opts;
opts.decoder = use_ctc ? Decoder::CTC : Decoder::TDT;
opts.timestamps = show_timestamps;
opts.boost_phrases = boost_phrases;
opts.boost_score = boost_score;
opts.use_vad = !vad_weights_path.empty();
auto t0 = Clock::now();
auto results = transcriber.transcribe_batch(audio_paths, opts);
auto t1 = Clock::now();
auto total_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count();
std::cout << "Batch transcription: " << total_ms << " ms ("
<< audio_paths.size() << " files)" << std::endl;
for (size_t i = 0; i < results.size(); ++i) {
std::cout << "\n--- [" << (i + 1) << "/" << results.size() << "] "
<< audio_paths[i] << " (" << results[i].token_ids.size()
<< " tokens) ---" << std::endl;
std::cout << results[i].text << std::endl;
if (show_timestamps && !results[i].word_timestamps.empty()) {
std::cout << "\n--- Word Timestamps ---" << std::endl;
for (const auto &w : results[i].word_timestamps) {
std::cout << " [" << std::fixed << std::setprecision(2)
<< w.start << "s - " << w.end << "s] ("
<< w.confidence << ") " << w.word << std::endl;
}
}
}
return 0;
}
// ─── TDT 600M mode ──────────────────────────────────────────────────────────
static int run_tdt_600m(
const std::string &weights_path, const std::string &audio_path,
const std::string &vocab_path, bool use_gpu, bool use_fp16,
bool show_timestamps, const std::vector<std::string> &boost_phrases = {},
float boost_score = 5.0f, bool use_tdt_beam = false, int beam_width = 4,
const std::string &lm_path = "", float lm_weight = 0.5f,
const std::string &vad_weights_path = {}, float vad_threshold = 0.5f) {
using namespace parakeet;
using Clock = std::chrono::high_resolution_clock;
std::cout << "Loading model: tdt-600m" << std::endl;
TDTConfig cfg = make_tdt_600m_config();
ParakeetTDT model(cfg);
auto weights = axiom::io::safetensors::load(weights_path);
model.load_state_dict(weights, "", false);
std::cout << "Model loaded (" << weights.size() << " tensors)" << std::endl;
if (use_fp16) {
model.to(axiom::DType::Float16);
std::cout << "Model cast to fp16" << std::endl;
}
if (use_gpu) {
model.to(axiom::Device::GPU);
std::cout << "Model moved to GPU" << std::endl;
}
Tokenizer tokenizer;
if (!vocab_path.empty()) {
tokenizer.load(vocab_path);
}
auto audio = read_audio(audio_path);
// VAD preprocessing
axiom::Tensor audio_samples = audio.samples;
std::unique_ptr<parakeet::audio::TimestampRemapper> remapper;
if (!vad_weights_path.empty()) {
parakeet::audio::SileroVAD silero_vad(vad_weights_path);
parakeet::audio::VADConfig vad_cfg;
vad_cfg.threshold = vad_threshold;
vad_cfg.neg_threshold = vad_threshold - 0.15f;
auto segments =
silero_vad.detect(audio.samples, audio.sample_rate, vad_cfg);
if (!segments.empty()) {
float speech_dur = 0;
for (const auto &s : segments)
speech_dur +=
static_cast<float>(s.end_sample - s.start_sample) /
audio.sample_rate;
std::cout << "VAD: " << segments.size() << " speech segments, "
<< std::fixed << std::setprecision(1) << speech_dur
<< "s speech / " << audio.duration << "s total ("
<< static_cast<int>(100 *
(1 - speech_dur / audio.duration))
<< "% filtered)" << std::endl;
audio_samples =
parakeet::audio::collect_speech(audio.samples, segments);
if (show_timestamps) {
remapper = std::make_unique<parakeet::audio::TimestampRemapper>(
segments);
}
}
}
AudioConfig audio_cfg;
audio_cfg.n_mels = cfg.encoder.mel_bins;
auto features = preprocess_audio(audio_samples, audio_cfg);
if (use_fp16)
features = features.half();
if (use_gpu)
features = features.gpu();
auto t0 = Clock::now();
auto encoder_out = model.encoder()(features);
auto t1 = Clock::now();
std::cout << "Encoder: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0)
.count()
<< " ms" << std::endl;
// Build phrase boost trie if needed
ContextTrie trie;
bool use_boost = !boost_phrases.empty() && tokenizer.loaded();
if (use_boost) {
trie.build(boost_phrases, tokenizer);
std::cout << "Phrase boost: " << boost_phrases.size() << " phrases, "
<< trie.size() << " trie nodes" << std::endl;
}
// Load LM if beam search with LM requested
ArpaLM arpa_lm;
if (use_tdt_beam && !lm_path.empty()) {
arpa_lm.load(lm_path);
std::cout << "LM loaded: " << arpa_lm.size() << " n-grams, order "
<< arpa_lm.order() << std::endl;
}
std::vector<std::vector<int>> token_ids;
std::vector<std::vector<TimestampedToken>> timestamped_tokens;
int blank_id = cfg.prediction.vocab_size - 1;
if (use_tdt_beam) {
TDTBeamSearchOptions tdt_bs_opts;
tdt_bs_opts.beam_width = beam_width;
tdt_bs_opts.blank_id = blank_id;
tdt_bs_opts.lm = arpa_lm.loaded() ? &arpa_lm : nullptr;
tdt_bs_opts.lm_weight = lm_weight;
tdt_bs_opts.pieces = tokenizer.loaded() ? &tokenizer.pieces() : nullptr;
if (show_timestamps) {
timestamped_tokens = tdt_beam_decode_with_timestamps(
model, encoder_out, cfg.durations, tdt_bs_opts);
token_ids.resize(timestamped_tokens.size());
for (size_t b = 0; b < timestamped_tokens.size(); ++b)
for (const auto &t : timestamped_tokens[b])
token_ids[b].push_back(t.token_id);
} else {
token_ids =
tdt_beam_decode(model, encoder_out, cfg.durations, tdt_bs_opts);
}
std::cout << "Decoder: TDT beam (width=" << beam_width
<< (arpa_lm.loaded() ? ", +LM" : "") << ")" << std::endl;
} else if (show_timestamps) {
timestamped_tokens =
use_boost ? tdt_greedy_decode_with_timestamps_boosted(
model, encoder_out, cfg.durations, trie,
boost_score, blank_id)
: tdt_greedy_decode_with_timestamps(
model, encoder_out, cfg.durations, blank_id);
token_ids.resize(timestamped_tokens.size());
for (size_t b = 0; b < timestamped_tokens.size(); ++b)
for (const auto &t : timestamped_tokens[b])
token_ids[b].push_back(t.token_id);
} else {
token_ids = use_boost ? tdt_greedy_decode_boosted(model, encoder_out,
cfg.durations, trie,
boost_score, blank_id)
: tdt_greedy_decode(model, encoder_out,
cfg.durations, blank_id);
}
// Remap timestamps if VAD was used
if (remapper && show_timestamps) {
for (auto &ts_vec : timestamped_tokens) {
ts_vec = remapper->remap_tokens(ts_vec);
}
}
for (size_t b = 0; b < token_ids.size(); ++b) {
std::cout << "\n--- Transcription (" << token_ids[b].size()
<< " tokens) ---" << std::endl;
if (tokenizer.loaded()) {
std::cout << tokenizer.decode(token_ids[b]) << std::endl;
}
if (show_timestamps && b < timestamped_tokens.size() &&
tokenizer.loaded()) {
auto words =
group_timestamps(timestamped_tokens[b], tokenizer.pieces());
std::cout << "\n--- Word Timestamps ---" << std::endl;
for (const auto &w : words) {
std::cout << " [" << std::fixed << std::setprecision(2)
<< w.start << "s - " << w.end << "s] ("
<< w.confidence << ") " << w.word << std::endl;
}
}
}
return 0;
}
// ─── TDT 600M batch mode ────────────────────────────────────────────────────
static int run_tdt_600m_batch(
const std::string &weights_path,
const std::vector<std::string> &audio_paths, const std::string &vocab_path,
bool use_gpu, bool use_fp16, bool show_timestamps,
const std::vector<std::string> &boost_phrases = {},
float boost_score = 5.0f, const std::string &vad_weights_path = {},
float vad_threshold = 0.5f) {
using namespace parakeet;
using Clock = std::chrono::high_resolution_clock;
std::cout << "Loading model: tdt-600m (batch mode, " << audio_paths.size()
<< " files)" << std::endl;
TDTTranscriber transcriber(weights_path, vocab_path);
if (use_fp16)
transcriber.to_half();
if (use_gpu)
transcriber.to_gpu();
if (!vad_weights_path.empty()) {
transcriber.enable_vad(vad_weights_path);
std::cout << "VAD enabled (threshold=" << vad_threshold << ")"
<< std::endl;
}
TranscribeOptions opts;
opts.decoder = Decoder::TDT;
opts.timestamps = show_timestamps;
opts.boost_phrases = boost_phrases;
opts.boost_score = boost_score;
opts.use_vad = !vad_weights_path.empty();
auto t0 = Clock::now();
auto results = transcriber.transcribe_batch(audio_paths, opts);
auto t1 = Clock::now();
auto total_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count();
std::cout << "Batch transcription: " << total_ms << " ms ("
<< audio_paths.size() << " files)" << std::endl;
for (size_t i = 0; i < results.size(); ++i) {
std::cout << "\n--- [" << (i + 1) << "/" << results.size() << "] "
<< audio_paths[i] << " (" << results[i].token_ids.size()
<< " tokens) ---" << std::endl;
std::cout << results[i].text << std::endl;
if (show_timestamps && !results[i].word_timestamps.empty()) {
std::cout << "\n--- Word Timestamps ---" << std::endl;
for (const auto &w : results[i].word_timestamps) {
std::cout << " [" << std::fixed << std::setprecision(2)
<< w.start << "s - " << w.end << "s] ("
<< w.confidence << ") " << w.word << std::endl;
}
}
}
return 0;
}
// ─── RNNT 600M mode ─────────────────────────────────────────────────────────
static int run_rnnt_600m(const std::string &weights_path,
const std::string &audio_path,
const std::string &vocab_path, bool use_gpu,
bool use_fp16, bool show_timestamps) {
using namespace parakeet;
using Clock = std::chrono::high_resolution_clock;
std::cout << "Loading model: rnnt-600m" << std::endl;
RNNTConfig cfg = make_rnnt_600m_config();
ParakeetRNNT model(cfg);
auto weights = axiom::io::safetensors::load(weights_path);
model.load_state_dict(weights, "", false);
std::cout << "Model loaded (" << weights.size() << " tensors)" << std::endl;
if (use_fp16) {
model.to(axiom::DType::Float16);
std::cout << "Model cast to fp16" << std::endl;
}
if (use_gpu) {
model.to(axiom::Device::GPU);
std::cout << "Model moved to GPU" << std::endl;
}
Tokenizer tokenizer;
if (!vocab_path.empty()) {
tokenizer.load(vocab_path);
}
auto audio = read_audio(audio_path);
auto features = preprocess_audio(audio.samples);
if (use_fp16)
features = features.half();
if (use_gpu)
features = features.gpu();
auto t0 = Clock::now();
auto encoder_out = model.encoder()(features);
auto t1 = Clock::now();
std::cout << "Encoder: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0)
.count()
<< " ms" << std::endl;
int blank_id = cfg.prediction.vocab_size - 1;
std::vector<std::vector<int>> token_ids;
std::vector<std::vector<TimestampedToken>> timestamped_tokens;
t0 = Clock::now();
if (show_timestamps) {
timestamped_tokens =
rnnt_greedy_decode_with_timestamps(model, encoder_out, blank_id);
token_ids.resize(timestamped_tokens.size());
for (size_t b = 0; b < timestamped_tokens.size(); ++b)
for (const auto &t : timestamped_tokens[b])
token_ids[b].push_back(t.token_id);
} else {
token_ids = rnnt_greedy_decode(model, encoder_out, blank_id);
}
t1 = Clock::now();
std::cout << "Decoder: RNNT ("
<< std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0)
.count()
<< " ms)" << std::endl;
for (size_t b = 0; b < token_ids.size(); ++b) {
std::cout << "\n--- Transcription (" << token_ids[b].size()
<< " tokens) ---" << std::endl;
if (tokenizer.loaded()) {
std::cout << tokenizer.decode(token_ids[b]) << std::endl;
}
if (show_timestamps && b < timestamped_tokens.size() &&
tokenizer.loaded()) {
auto words =
group_timestamps(timestamped_tokens[b], tokenizer.pieces());
std::cout << "\n--- Word Timestamps ---" << std::endl;
for (const auto &w : words) {
std::cout << " [" << std::fixed << std::setprecision(2)
<< w.start << "s - " << w.end << "s] ("
<< w.confidence << ") " << w.word << std::endl;
}
}
}
return 0;
}
// ─── EOU Streaming mode ──────────────────────────────────────────────────────
static int run_eou_streaming(const std::string &weights_path,
const std::string &audio_path,
const std::string &vocab_path, bool use_gpu,
bool use_fp16, bool show_timestamps) {
using namespace parakeet;
using Clock = std::chrono::high_resolution_clock;
std::cout << "Loading model: eou-120m (streaming)" << std::endl;
StreamingTranscriber transcriber(weights_path, vocab_path,
make_eou_120m_config());
if (use_fp16)
transcriber.to_half();
if (use_gpu)
transcriber.to_gpu();
auto audio = read_audio(audio_path);
std::cout << "Audio: " << audio.original_sample_rate << "Hz"
<< (audio.original_sample_rate != audio.sample_rate
? " -> " + std::to_string(audio.sample_rate) + "Hz"
: "")
<< ", " << audio.num_samples << " samples, " << std::fixed
<< std::setprecision(1) << audio.duration << "s" << std::endl;
// Simulate streaming: process in ~160ms chunks (2560 samples at 16kHz)
constexpr int CHUNK_SAMPLES = 2560;
auto samples = audio.samples.ascontiguousarray();
int total = static_cast<int>(samples.shape()[0]);
const float *data = samples.typed_data<float>();
std::string full_text;
auto t0 = Clock::now();
for (int offset = 0; offset < total; offset += CHUNK_SAMPLES) {
int chunk_len = std::min(CHUNK_SAMPLES, total - offset);
auto chunk = axiom::Tensor::from_data(
data + offset, axiom::Shape{static_cast<size_t>(chunk_len)}, true);
auto text = transcriber.transcribe_chunk(chunk);
if (!text.empty()) {
std::cout << text << std::flush;
full_text += text;
}
}
auto t1 = Clock::now();
std::cout << "\n\n--- Full Transcription ---" << std::endl;
std::cout << transcriber.get_text() << std::endl;
std::cout << "Total time: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0)
.count()
<< " ms" << std::endl;
if (show_timestamps && transcriber.tokenizer().loaded()) {
auto words = group_timestamps(transcriber.get_timestamped_tokens(),
transcriber.tokenizer().pieces());
std::cout << "\n--- Word Timestamps ---" << std::endl;
for (const auto &w : words) {
std::cout << " [" << std::fixed << std::setprecision(2) << w.start
<< "s - " << w.end << "s] (" << w.confidence << ") "
<< w.word << std::endl;
}
}
return 0;
}
// ─── Nemotron Streaming mode ─────────────────────────────────────────────────
static int run_nemotron_streaming(const std::string &weights_path,
const std::string &audio_path,
const std::string &vocab_path, bool use_gpu,
bool use_fp16, bool show_timestamps,
int latency_frames) {
using namespace parakeet;
using Clock = std::chrono::high_resolution_clock;
std::cout << "Loading model: nemotron-600m (streaming, latency="
<< latency_frames << " frames)" << std::endl;
NemotronTranscriber transcriber(weights_path, vocab_path,
make_nemotron_600m_config(latency_frames));
if (use_fp16)
transcriber.to_half();
if (use_gpu)
transcriber.to_gpu();
auto audio = read_audio(audio_path);
constexpr int CHUNK_SAMPLES = 2560;
auto samples = audio.samples.ascontiguousarray();
int total = static_cast<int>(samples.shape()[0]);
const float *data = samples.typed_data<float>();
auto t0 = Clock::now();
for (int offset = 0; offset < total; offset += CHUNK_SAMPLES) {
int chunk_len = std::min(CHUNK_SAMPLES, total - offset);
auto chunk = axiom::Tensor::from_data(
data + offset, axiom::Shape{static_cast<size_t>(chunk_len)}, true);
auto text = transcriber.transcribe_chunk(chunk);
if (!text.empty()) {
std::cout << text << std::flush;
}
}
auto t1 = Clock::now();
std::cout << "\n\n--- Full Transcription ---" << std::endl;
std::cout << transcriber.get_text() << std::endl;
std::cout << "Total time: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0)
.count()
<< " ms" << std::endl;
if (show_timestamps && transcriber.tokenizer().loaded()) {
auto words = group_timestamps(transcriber.get_timestamped_tokens(),
transcriber.tokenizer().pieces());
std::cout << "\n--- Word Timestamps ---" << std::endl;
for (const auto &w : words) {
std::cout << " [" << std::fixed << std::setprecision(2) << w.start
<< "s - " << w.end << "s] (" << w.confidence << ") "
<< w.word << std::endl;
}
}
return 0;
}
// ─── Sortformer Diarization mode ─────────────────────────────────────────────
static int run_sortformer(const std::string &weights_path,
const std::string &audio_path, bool use_gpu,
bool use_fp16) {
using namespace parakeet;
using Clock = std::chrono::high_resolution_clock;
std::cout << "Loading model: sortformer" << std::endl;
SortformerConfig cfg = make_sortformer_117m_config();
Sortformer model(cfg);
auto weights = axiom::io::safetensors::load(weights_path);
model.load_state_dict(weights, "", false);
if (use_fp16) {
model.to(axiom::DType::Float16);
std::cout << "Model cast to fp16" << std::endl;
}
if (use_gpu) {
model.to(axiom::Device::GPU);
}
auto audio = read_audio(audio_path);
AudioConfig audio_cfg;
audio_cfg.n_mels = cfg.nest_encoder.mel_bins;
audio_cfg.normalize = false; // sortformer uses normalize: NA
auto features = preprocess_audio(audio.samples, audio_cfg);
if (use_fp16)
features = features.half();
if (use_gpu)
features = features.gpu();
auto t0 = Clock::now();
auto segments = model.diarize(features);
auto t1 = Clock::now();
std::cout << "Diarization: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0)
.count()
<< " ms" << std::endl;
std::cout << "\n--- Speaker Segments (" << segments.size()
<< " segments) ---" << std::endl;
for (const auto &seg : segments) {
std::cout << " Speaker " << seg.speaker_id << ": [" << std::fixed
<< std::setprecision(2) << seg.start << "s - " << seg.end
<< "s]" << std::endl;
}
return 0;
}
// ─── Diarized Transcription mode ─────────────────────────────────────────────
static int run_diarized(const std::string &weights_path,
const std::string &audio_path,
const std::string &vocab_path,
const std::string &sortformer_weights_path,
bool use_ctc, bool use_gpu, bool use_fp16,
const std::string &vad_weights_path = {}) {
using namespace parakeet;
using Clock = std::chrono::high_resolution_clock;
if (sortformer_weights_path.empty()) {
std::cerr << "Error: --sortformer-weights required for diarized mode"
<< std::endl;
return 1;
}
if (vocab_path.empty()) {
std::cerr << "Error: --vocab required for diarized mode" << std::endl;
return 1;
}
std::cout << "Loading diarized transcriber (ASR + Sortformer)" << std::endl;
auto t0 = Clock::now();
DiarizedTranscriber dt(weights_path, sortformer_weights_path, vocab_path);
auto t1 = Clock::now();
std::cout << "Models loaded ("
<< std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0)
.count()
<< " ms)" << std::endl;
if (use_fp16) {
dt.to_half();
std::cout << "Models cast to fp16" << std::endl;
}
if (use_gpu) {
t0 = Clock::now();
dt.to_gpu();
t1 = Clock::now();
std::cout << "Models moved to GPU ("
<< std::chrono::duration_cast<std::chrono::milliseconds>(t1 -
t0)
.count()
<< " ms)" << std::endl;
}
if (!vad_weights_path.empty()) {
dt.enable_vad(vad_weights_path);
std::cout << "VAD enabled" << std::endl;
}
auto audio = read_audio(audio_path);
std::cout << "Audio: " << audio.original_sample_rate << "Hz"
<< (audio.original_sample_rate != audio.sample_rate
? " -> " + std::to_string(audio.sample_rate) + "Hz"
: "")
<< ", " << audio.num_samples << " samples, " << std::fixed
<< std::setprecision(1) << audio.duration << "s" << std::endl;
Decoder decoder = use_ctc ? Decoder::CTC : Decoder::TDT;
t0 = Clock::now();
auto result = dt.transcribe(audio.samples, decoder);
t1 = Clock::now();
std::cout << "Transcription + diarization: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0)
.count()
<< " ms" << std::endl;
// Print speaker-grouped output: merge consecutive runs of same speaker
std::cout << "\n--- Diarized Transcription ---" << std::endl;
for (size_t i = 0; i < result.words.size();) {
int spk = result.words[i].speaker_id;
float run_start = result.words[i].start;
std::string run_text = result.words[i].word;
size_t j = i + 1;
while (j < result.words.size() && result.words[j].speaker_id == spk) {
run_text += " " + result.words[j].word;
++j;
}
float run_end = result.words[j - 1].end;
if (spk >= 0) {
std::cout << "Speaker " << spk;
} else {
std::cout << "Unknown";
}
std::cout << " [" << std::fixed << std::setprecision(2) << run_start
<< "s - " << run_end << "s]: " << run_text << std::endl;
i = j;
}
// Print detailed word list
std::cout << "\n--- Diarized Words ---" << std::endl;
for (const auto &w : result.words) {
std::cout << " [" << std::fixed << std::setprecision(2) << w.start
<< "s - " << w.end << "s] (spk=" << w.speaker_id
<< ", conf=" << w.confidence << ") " << w.word << std::endl;
}
// Print speaker segments
std::cout << "\n--- Speaker Segments (" << result.segments.size()
<< " segments) ---" << std::endl;
for (const auto &seg : result.segments) {
std::cout << " Speaker " << seg.speaker_id << ": [" << std::fixed
<< std::setprecision(2) << seg.start << "s - " << seg.end
<< "s]" << std::endl;
}
return 0;
}
// ─── Main ────────────────────────────────────────────────────────────────────
int main(int argc, char *argv[]) {
if (argc < 3) {
print_usage(argv[0]);
return 1;
}
try {
std::string weights_path = argv[1];
std::string model_type = "tdt-ctc-110m";
bool use_ctc = false;
bool use_ctc_beam = false;
bool use_tdt_beam = false;
bool use_gpu = false;
bool use_fp16 = false;
bool show_timestamps = false;
bool streaming = false;
int latency_frames = 0;
int beam_width = 8;
std::string lm_path;
float lm_weight = 0.5f;
std::string vocab_path;
std::string features_path;
std::string sortformer_weights_path;
std::vector<std::string> boost_phrases;
float boost_score = 5.0f;
std::string vad_weights_path;
float vad_threshold = 0.5f;