47 #include <sphinxbase/ckd_alloc.h>
48 #include <sphinxbase/listelem_alloc.h>
49 #include <sphinxbase/err.h>
56 #define __CHAN_DUMP__ 0
58 #define chan_v_eval(chan) hmm_dump_vit_eval(&(chan)->hmm, stderr)
60 #define chan_v_eval(chan) hmm_vit_eval(&(chan)->hmm)
73 ngs->n_expand_words = 0;
74 n_words = ps_search_n_words(ngs);
75 bitvec_clear_all(ngs->expand_word_flag, ps_search_n_words(ngs));
76 for (i = 0; i < n_words; ++i) {
77 if (!ngram_model_set_known_wid(ngs->
lmset,
78 dict_basewid(ps_search_dict(ngs),i)))
81 ngs->expand_word_list[ngs->n_expand_words] = i;
82 bitvec_set(ngs->expand_word_flag, i);
83 ngs->n_expand_words++;
85 E_INFO(
"Utterance vocabulary contains %d words\n", ngs->n_expand_words);
86 ngs->expand_word_list[ngs->n_expand_words] = -1;
93 dict_t *dict = ps_search_dict(ngs);
94 int n_words = ps_search_n_words(ngs);
100 for (w = 0; w < n_words; w++) {
101 if (dict_is_single_phone(dict, w))
108 for (w = 0; w < n_words; w++) {
109 if (!dict_is_single_phone(dict, w))
116 bin_mdef_pid2ssid(ps_search_acmod(ngs)->mdef,
118 bin_mdef_pid2tmatid(ps_search_acmod(ngs)->mdef,
131 int n_words = ps_search_n_words(ngs);
133 for (i = w = 0; w < n_words; ++w) {
134 if (!dict_is_single_phone(ps_search_dict(ngs), w))
149 n_words = ps_search_n_words(ngs);
151 ngs->expand_word_flag = bitvec_alloc(n_words);
152 ngs->expand_word_list = ckd_calloc(n_words + 1,
sizeof(*ngs->expand_word_list));
154 ngs->min_ef_width = cmd_ln_int32_r(ps_search_config(ngs),
"-fwdflatefwid");
155 ngs->max_sf_win = cmd_ln_int32_r(ps_search_config(ngs),
"-fwdflatsfwin");
156 E_INFO(
"fwdflat: min_ef_width = %d, max_sf_win = %d\n",
157 ngs->min_ef_width, ngs->max_sf_win);
162 ngram_fwdflat_expand_all(ngs);
164 ngram_fwdflat_allocate_1ph(ngs);
171 double n_speech = (double)ngs->n_tot_frame
172 / cmd_ln_int32_r(ps_search_config(ngs),
"-frate");
174 E_INFO(
"TOTAL fwdflat %.2f CPU %.3f xRT\n",
175 ngs->fwdflat_perf.t_tot_cpu,
176 ngs->fwdflat_perf.t_tot_cpu / n_speech);
177 E_INFO(
"TOTAL fwdflat %.2f wall %.3f xRT\n",
178 ngs->fwdflat_perf.t_tot_elapsed,
179 ngs->fwdflat_perf.t_tot_elapsed / n_speech);
183 ngram_fwdflat_free_1ph(ngs);
186 bitvec_free(ngs->expand_word_flag);
187 ckd_free(ngs->expand_word_list);
198 ckd_free(ngs->expand_word_list);
199 bitvec_free(ngs->expand_word_flag);
200 n_words = ps_search_n_words(ngs);
202 ngs->expand_word_flag = bitvec_alloc(n_words);
203 ngs->expand_word_list = ckd_calloc(n_words + 1,
sizeof(*ngs->expand_word_list));
208 ngram_fwdflat_free_1ph(ngs);
214 ngram_fwdflat_expand_all(ngs);
216 ngram_fwdflat_allocate_1ph(ngs);
229 int32 i, f, sf, ef, wid, nwd;
238 dict = ps_search_dict(ngs);
244 for (i = 0, bp = ngs->bp_table; i < ngs->bpidx; i++, bp++) {
245 sf = (bp->
bp < 0) ? 0 : ngs->bp_table[bp->
bp].
frame + 1;
251 if (!ngram_model_set_known_wid(ngs->
lmset,
252 dict_basewid(ps_search_dict(ngs), wid)))
266 node->
fef = node->
lef = ef;
274 for (f = 0; f < ngs->
n_frame; f++) {
276 for (node = ngs->
frm_wordlist[f]; node; node = nextnode) {
277 nextnode = node->
next;
279 if ((node->
lef - node->
fef < ngs->min_ef_width) ||
281 ((node->
wid == ps_search_finish_wid(ngs)) && (node->
lef < ngs->
n_frame - 1))) {
285 prevnode->
next = nextnode;
295 bitvec_clear_all(ngs->
word_active, ps_search_n_words(ngs));
296 for (f = 0; f < ngs->
n_frame; f++) {
305 E_INFO(
"Utterance vocabulary contains %d words\n", nwd);
320 dict = ps_search_dict(ngs);
321 d2p = ps_search_dict2pid(ngs);
328 if (dict_is_single_phone(dict, wid))
337 rhmm->
ci2phone = dict_second_phone(dict, wid);
338 rhmm->
ciphone = dict_first_phone(dict, wid);
341 bin_mdef_pid2ssid(ps_search_acmod(ngs)->mdef, rhmm->
ciphone),
342 bin_mdef_pid2tmatid(ps_search_acmod(ngs)->mdef, rhmm->
ciphone));
346 for (p = 1; p < dict_pronlen(dict, wid) - 1; p++) {
349 hmm->info.
rc_id = (p == dict_pronlen(dict, wid) - 1) ? 0 : -1;
353 bin_mdef_pid2tmatid(ps_search_acmod(ngs)->mdef, hmm->
ciphone));
382 ptmr_reset(&ngs->fwdflat_perf);
383 ptmr_start(&ngs->fwdflat_perf);
384 build_fwdflat_wordlist(ngs);
385 build_fwdflat_chan(ngs);
390 for (i = 0; i < ps_search_n_words(ngs); i++)
391 ngs->word_lat_idx[i] = NO_BP;
398 hmm_clear(&rhmm->
hmm);
403 hmm_enter(&rhmm->
hmm, 0, NO_BP, 0);
408 ngs->renormalized = FALSE;
410 for (i = 0; i < ps_search_n_words(ngs); i++)
411 ngs->last_ltrans[i].sf = -1;
416 ngs->
st.n_fwdflat_chan = 0;
417 ngs->
st.n_fwdflat_words = 0;
418 ngs->
st.n_fwdflat_word_transition = 0;
419 ngs->
st.n_senone_active_utt = 0;
435 for (w = *(awl++); i > 0; --i, w = *(awl++)) {
437 if (hmm_frame(&rhmm->
hmm) == frame_idx) {
441 for (hmm = rhmm->
next; hmm; hmm = hmm->
next) {
442 if (hmm_frame(&hmm->
hmm) == frame_idx) {
452 int32 i, w, bestscore;
461 ngs->
st.n_fwdflat_words += i;
464 for (w = *(awl++); i > 0; --i, w = *(awl++)) {
466 if (hmm_frame(&rhmm->
hmm) == frame_idx) {
467 int32 score = chan_v_eval(rhmm);
468 if ((score
BETTER_THAN bestscore) && (w != ps_search_finish_wid(ngs)))
470 ngs->
st.n_fwdflat_chan++;
473 for (hmm = rhmm->
next; hmm; hmm = hmm->
next) {
474 if (hmm_frame(&hmm->
hmm) == frame_idx) {
475 int32 score = chan_v_eval(hmm);
478 ngs->
st.n_fwdflat_chan++;
489 int32 i, cf, nf, w, pip, newscore, thresh, wordthresh;
498 bitvec_clear_all(ngs->
word_active, ps_search_n_words(ngs));
501 wordthresh = ngs->
best_score + ngs->fwdflatwbeam;
503 E_DEBUG(3,(
"frame %d thresh %d wordthresh %d\n", frame_idx, thresh, wordthresh));
506 for (w = *(awl++); i > 0; --i, w = *(awl++)) {
509 if (hmm_frame(&rhmm->
hmm) == cf
511 hmm_frame(&rhmm->
hmm) = nf;
515 newscore = hmm_out_score(&rhmm->
hmm);
517 assert(!dict_is_single_phone(ps_search_dict(ngs), w));
523 if (hmm->info.
rc_id >= 0) {
524 for (; hmm; hmm = hmm->
next) {
525 if ((hmm_frame(&hmm->
hmm) < cf)
527 hmm_enter(&hmm->
hmm, newscore,
528 hmm_out_history(&rhmm->
hmm), nf);
534 if ((hmm_frame(&hmm->
hmm) < cf)
536 hmm_enter(&hmm->
hmm, newscore,
537 hmm_out_history(&rhmm->
hmm), nf);
543 assert(dict_is_single_phone(ps_search_dict(ngs), w));
551 hmm_out_history(&rhmm->
hmm), 0);
557 for (hmm = rhmm->
next; hmm; hmm = hmm->
next) {
558 if (hmm_frame(&hmm->
hmm) >= cf) {
561 hmm_frame(&hmm->
hmm) = nf;
564 newscore = hmm_out_score(&hmm->
hmm);
566 if (hmm->info.
rc_id < 0) {
571 if (nexthmm->info.
rc_id >= 0) {
572 for (; nexthmm; nexthmm = nexthmm->
next) {
573 if ((hmm_frame(&nexthmm->
hmm) < cf)
575 hmm_in_score(&nexthmm->
hmm))) {
576 hmm_enter(&nexthmm->
hmm,
578 hmm_out_history(&hmm->
hmm),
585 if ((hmm_frame(&nexthmm->
hmm) < cf)
587 hmm_in_score(&nexthmm->
hmm))) {
588 hmm_enter(&nexthmm->
hmm, newscore,
589 hmm_out_history(&hmm->
hmm), nf);
598 hmm_out_history(&hmm->
hmm),
604 else if (hmm_frame(&hmm->
hmm) != nf) {
605 hmm_clear_scores(&hmm->
hmm);
619 ngs->
st.n_fwdflat_word_transition += ngs->n_expand_words;
630 bitvec_clear_all(ngs->expand_word_flag, ps_search_n_words(ngs));
631 ngs->n_expand_words = 0;
633 for (f = sf; f < ef; f++) {
635 if (!bitvec_is_set(ngs->expand_word_flag, node->
wid)) {
636 ngs->expand_word_list[ngs->n_expand_words++] = node->
wid;
637 bitvec_set(ngs->expand_word_flag, node->
wid);
641 ngs->expand_word_list[ngs->n_expand_words] = -1;
642 ngs->
st.n_fwdflat_word_transition += ngs->n_expand_words;
648 int32 cf, nf, b, thresh, pip, i, w, newscore;
649 int32 best_silrc_score = 0, best_silrc_bp = 0;
655 dict_t *dict = ps_search_dict(ngs);
663 lwf = ngs->fwdflat_fwdtree_lw_ratio;
667 get_expand_wordlist(ngs, cf, ngs->max_sf_win);
670 for (b = ngs->bp_table_idx[cf]; b < ngs->bpidx; b++) {
674 bp = ngs->bp_table + b;
675 ngs->word_lat_idx[bp->
wid] = NO_BP;
677 if (bp->
wid == ps_search_finish_wid(ngs))
683 rcss = ngs->bscore_stack + bp->
s_idx;
690 for (i = 0; ngs->expand_word_list[i] >= 0; i++) {
693 w = ngs->expand_word_list[i];
698 newscore = rcss[rssid->
cimap[dict_first_phone(dict, w)]];
700 newscore = bp->
score;
705 * (ngram_tg_score(ngs->
lmset,
706 dict_basewid(dict, w),
715 if ((hmm_frame(&rhmm->
hmm) < cf)
717 hmm_enter(&rhmm->
hmm, newscore, b, nf);
720 hmm_mpx_ssid(&rhmm->
hmm, 0) =
722 dict_last_phone(dict, bp->
wid));
723 assert(IS_S3SSID(hmm_mpx_ssid(&rhmm->
hmm, 0)));
724 E_DEBUG(6,(
"ssid %d(%d,%d) = %d\n",
726 hmm_mpx_ssid(&rhmm->
hmm, 0)));
734 silscore = rcss[rssid->
cimap[ps_search_acmod(ngs)->mdef->sil]];
736 silscore = bp->
score;
738 best_silrc_score = silscore;
744 newscore = best_silrc_score + ngs->silpen + pip;
746 w = ps_search_silence_wid(ngs);
748 if ((hmm_frame(&rhmm->
hmm) < cf)
750 hmm_enter(&rhmm->
hmm, newscore,
756 newscore = best_silrc_score + ngs->fillpen + pip;
758 for (w = ps_search_silence_wid(ngs) + 1; w < ps_search_n_words(ngs); w++) {
763 if ((hmm_frame(&rhmm->
hmm) < cf)
765 hmm_enter(&rhmm->
hmm, newscore,
775 for (w = *(awl++); i > 0; --i, w = *(awl++)) {
777 if (hmm_frame(&rhmm->
hmm) == cf) {
778 hmm_clear_scores(&rhmm->
hmm);
784 fwdflat_renormalize_scores(
ngram_search_t *ngs,
int frame_idx, int32 norm)
788 int32 i, cf, w, *awl;
795 for (w = *(awl++); i > 0; --i, w = *(awl++)) {
797 if (hmm_frame(&rhmm->
hmm) == cf) {
798 hmm_normalize(&rhmm->
hmm, norm);
800 for (hmm = rhmm->
next; hmm; hmm = hmm->
next) {
801 if (hmm_frame(&hmm->
hmm) == cf) {
802 hmm_normalize(&hmm->
hmm, norm);
807 ngs->renormalized = TRUE;
818 if (!ps_search_acmod(ngs)->compallsen)
819 compute_fwdflat_sen_active(ngs, frame_idx);
822 senscr =
acmod_score(ps_search_acmod(ngs), &frame_idx);
823 ngs->
st.n_senone_active_utt += ps_search_acmod(ngs)->n_senone_active;
834 E_INFO(
"Renormalizing Scores at frame %d, best score %d\n",
836 fwdflat_renormalize_scores(ngs, frame_idx, ngs->
best_score);
843 fwdflat_eval_chan(ngs, frame_idx);
845 fwdflat_prune_chan(ngs, frame_idx);
847 fwdflat_word_transition(ngs, frame_idx);
858 for (i = ps_search_start_wid(ngs); i < ps_search_n_words(ngs); i++) {
884 for (f = 0; f < ngs->
n_frame; f++) {
904 if (dict_is_single_phone(ps_search_dict(ngs),wid))
926 destroy_fwdflat_chan(ngs);
927 destroy_fwdflat_wordlist(ngs);
928 bitvec_clear_all(ngs->
word_active, ps_search_n_words(ngs));
931 cf = ps_search_acmod(ngs)->output_frame;
935 ptmr_stop(&ngs->fwdflat_perf);
938 double n_speech = (double)(cf + 1)
939 / cmd_ln_int32_r(ps_search_config(ngs),
"-frate");
940 E_INFO(
"%8d words recognized (%d/fr)\n",
941 ngs->bpidx, (ngs->bpidx + (cf >> 1)) / (cf + 1));
942 E_INFO(
"%8d senones evaluated (%d/fr)\n", ngs->
st.n_senone_active_utt,
943 (ngs->
st.n_senone_active_utt + (cf >> 1)) / (cf + 1));
944 E_INFO(
"%8d channels searched (%d/fr)\n",
945 ngs->
st.n_fwdflat_chan, ngs->
st.n_fwdflat_chan / (cf + 1));
946 E_INFO(
"%8d words searched (%d/fr)\n",
947 ngs->
st.n_fwdflat_words, ngs->
st.n_fwdflat_words / (cf + 1));
948 E_INFO(
"%8d word transitions (%d/fr)\n",
949 ngs->
st.n_fwdflat_word_transition,
950 ngs->
st.n_fwdflat_word_transition / (cf + 1));
951 E_INFO(
"fwdflat %.2f CPU %.3f xRT\n",
952 ngs->fwdflat_perf.t_cpu,
953 ngs->fwdflat_perf.t_cpu / n_speech);
954 E_INFO(
"fwdflat %.2f wall %.3f xRT\n",
955 ngs->fwdflat_perf.t_elapsed,
956 ngs->fwdflat_perf.t_elapsed / n_speech);