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;
241 for (i = 0, bp = ngs->bp_table; i < ngs->bpidx; i++, bp++) {
242 sf = (bp->
bp < 0) ? 0 : ngs->bp_table[bp->
bp].
frame + 1;
248 if (!ngram_model_set_known_wid(ngs->
lmset,
249 dict_basewid(ps_search_dict(ngs), wid)))
263 node->
fef = node->
lef = ef;
271 for (f = 0; f < ngs->
n_frame; f++) {
273 for (node = ngs->
frm_wordlist[f]; node; node = nextnode) {
274 nextnode = node->
next;
276 if ((node->
lef - node->
fef < ngs->min_ef_width) ||
278 ((node->
wid == ps_search_finish_wid(ngs)) && (node->
lef < ngs->
n_frame - 1))) {
282 prevnode->
next = nextnode;
292 bitvec_clear_all(ngs->
word_active, ps_search_n_words(ngs));
293 for (f = 0; f < ngs->
n_frame; f++) {
302 E_INFO(
"Utterance vocabulary contains %d words\n", nwd);
317 dict = ps_search_dict(ngs);
318 d2p = ps_search_dict2pid(ngs);
325 if (dict_is_single_phone(dict, wid))
334 rhmm->
ci2phone = dict_second_phone(dict, wid);
335 rhmm->
ciphone = dict_first_phone(dict, wid);
338 bin_mdef_pid2ssid(ps_search_acmod(ngs)->mdef, rhmm->
ciphone),
339 bin_mdef_pid2tmatid(ps_search_acmod(ngs)->mdef, rhmm->
ciphone));
343 for (p = 1; p < dict_pronlen(dict, wid) - 1; p++) {
346 hmm->info.
rc_id = (p == dict_pronlen(dict, wid) - 1) ? 0 : -1;
350 bin_mdef_pid2tmatid(ps_search_acmod(ngs)->mdef, hmm->
ciphone));
379 ptmr_reset(&ngs->fwdflat_perf);
380 ptmr_start(&ngs->fwdflat_perf);
381 build_fwdflat_wordlist(ngs);
382 build_fwdflat_chan(ngs);
387 for (i = 0; i < ps_search_n_words(ngs); i++)
388 ngs->word_lat_idx[i] = NO_BP;
395 hmm_clear(&rhmm->
hmm);
400 hmm_enter(&rhmm->
hmm, 0, NO_BP, 0);
405 ngs->renormalized = FALSE;
407 for (i = 0; i < ps_search_n_words(ngs); i++)
408 ngs->last_ltrans[i].sf = -1;
413 ngs->
st.n_fwdflat_chan = 0;
414 ngs->
st.n_fwdflat_words = 0;
415 ngs->
st.n_fwdflat_word_transition = 0;
416 ngs->
st.n_senone_active_utt = 0;
432 for (w = *(awl++); i > 0; --i, w = *(awl++)) {
434 if (hmm_frame(&rhmm->
hmm) == frame_idx) {
438 for (hmm = rhmm->
next; hmm; hmm = hmm->
next) {
439 if (hmm_frame(&hmm->
hmm) == frame_idx) {
449 int32 i, w, bestscore;
458 ngs->
st.n_fwdflat_words += i;
461 for (w = *(awl++); i > 0; --i, w = *(awl++)) {
463 if (hmm_frame(&rhmm->
hmm) == frame_idx) {
464 int32 score = chan_v_eval(rhmm);
465 if ((score
BETTER_THAN bestscore) && (w != ps_search_finish_wid(ngs)))
467 ngs->
st.n_fwdflat_chan++;
470 for (hmm = rhmm->
next; hmm; hmm = hmm->
next) {
471 if (hmm_frame(&hmm->
hmm) == frame_idx) {
472 int32 score = chan_v_eval(hmm);
475 ngs->
st.n_fwdflat_chan++;
486 int32 i, cf, nf, w, pip, newscore, thresh, wordthresh;
495 bitvec_clear_all(ngs->
word_active, ps_search_n_words(ngs));
498 wordthresh = ngs->
best_score + ngs->fwdflatwbeam;
500 E_DEBUG(3,(
"frame %d thresh %d wordthresh %d\n", frame_idx, thresh, wordthresh));
503 for (w = *(awl++); i > 0; --i, w = *(awl++)) {
506 if (hmm_frame(&rhmm->
hmm) == cf
508 hmm_frame(&rhmm->
hmm) = nf;
512 newscore = hmm_out_score(&rhmm->
hmm);
514 assert(!dict_is_single_phone(ps_search_dict(ngs), w));
520 if (hmm->info.
rc_id >= 0) {
521 for (; hmm; hmm = hmm->
next) {
522 if ((hmm_frame(&hmm->
hmm) < cf)
524 hmm_enter(&hmm->
hmm, newscore,
525 hmm_out_history(&rhmm->
hmm), nf);
531 if ((hmm_frame(&hmm->
hmm) < cf)
533 hmm_enter(&hmm->
hmm, newscore,
534 hmm_out_history(&rhmm->
hmm), nf);
540 assert(dict_is_single_phone(ps_search_dict(ngs), w));
548 hmm_out_history(&rhmm->
hmm), 0);
554 for (hmm = rhmm->
next; hmm; hmm = hmm->
next) {
555 if (hmm_frame(&hmm->
hmm) >= cf) {
558 hmm_frame(&hmm->
hmm) = nf;
561 newscore = hmm_out_score(&hmm->
hmm);
563 if (hmm->info.
rc_id < 0) {
568 if (nexthmm->info.
rc_id >= 0) {
569 for (; nexthmm; nexthmm = nexthmm->
next) {
570 if ((hmm_frame(&nexthmm->
hmm) < cf)
572 hmm_in_score(&nexthmm->
hmm))) {
573 hmm_enter(&nexthmm->
hmm,
575 hmm_out_history(&hmm->
hmm),
582 if ((hmm_frame(&nexthmm->
hmm) < cf)
584 hmm_in_score(&nexthmm->
hmm))) {
585 hmm_enter(&nexthmm->
hmm, newscore,
586 hmm_out_history(&hmm->
hmm), nf);
595 hmm_out_history(&hmm->
hmm),
601 else if (hmm_frame(&hmm->
hmm) != nf) {
602 hmm_clear_scores(&hmm->
hmm);
616 ngs->
st.n_fwdflat_word_transition += ngs->n_expand_words;
627 bitvec_clear_all(ngs->expand_word_flag, ps_search_n_words(ngs));
628 ngs->n_expand_words = 0;
630 for (f = sf; f < ef; f++) {
632 if (!bitvec_is_set(ngs->expand_word_flag, node->
wid)) {
633 ngs->expand_word_list[ngs->n_expand_words++] = node->
wid;
634 bitvec_set(ngs->expand_word_flag, node->
wid);
638 ngs->expand_word_list[ngs->n_expand_words] = -1;
639 ngs->
st.n_fwdflat_word_transition += ngs->n_expand_words;
645 int32 cf, nf, b, thresh, pip, i, w, newscore;
646 int32 best_silrc_score = 0, best_silrc_bp = 0;
652 dict_t *dict = ps_search_dict(ngs);
660 lwf = ngs->fwdflat_fwdtree_lw_ratio;
664 get_expand_wordlist(ngs, cf, ngs->max_sf_win);
667 for (b = ngs->bp_table_idx[cf]; b < ngs->bpidx; b++) {
671 bp = ngs->bp_table + b;
672 ngs->word_lat_idx[bp->
wid] = NO_BP;
674 if (bp->
wid == ps_search_finish_wid(ngs))
680 rcss = ngs->bscore_stack + bp->
s_idx;
687 for (i = 0; ngs->expand_word_list[i] >= 0; i++) {
690 w = ngs->expand_word_list[i];
695 newscore = rcss[rssid->
cimap[dict_first_phone(dict, w)]];
697 newscore = bp->
score;
702 * (ngram_tg_score(ngs->
lmset,
703 dict_basewid(dict, w),
712 if ((hmm_frame(&rhmm->
hmm) < cf)
714 hmm_enter(&rhmm->
hmm, newscore, b, nf);
717 hmm_mpx_ssid(&rhmm->
hmm, 0) =
719 dict_last_phone(dict, bp->
wid));
720 assert(IS_S3SSID(hmm_mpx_ssid(&rhmm->
hmm, 0)));
721 E_DEBUG(6,(
"ssid %d(%d,%d) = %d\n",
723 hmm_mpx_ssid(&rhmm->
hmm, 0)));
731 silscore = rcss[rssid->
cimap[ps_search_acmod(ngs)->mdef->sil]];
733 silscore = bp->
score;
735 best_silrc_score = silscore;
741 newscore = best_silrc_score + ngs->silpen + pip;
743 w = ps_search_silence_wid(ngs);
745 if ((hmm_frame(&rhmm->
hmm) < cf)
747 hmm_enter(&rhmm->
hmm, newscore,
753 newscore = best_silrc_score + ngs->fillpen + pip;
755 for (w = ps_search_silence_wid(ngs) + 1; w < ps_search_n_words(ngs); w++) {
760 if ((hmm_frame(&rhmm->
hmm) < cf)
762 hmm_enter(&rhmm->
hmm, newscore,
772 for (w = *(awl++); i > 0; --i, w = *(awl++)) {
774 if (hmm_frame(&rhmm->
hmm) == cf) {
775 hmm_clear_scores(&rhmm->
hmm);
781 fwdflat_renormalize_scores(
ngram_search_t *ngs,
int frame_idx, int32 norm)
785 int32 i, cf, w, *awl;
792 for (w = *(awl++); i > 0; --i, w = *(awl++)) {
794 if (hmm_frame(&rhmm->
hmm) == cf) {
795 hmm_normalize(&rhmm->
hmm, norm);
797 for (hmm = rhmm->
next; hmm; hmm = hmm->
next) {
798 if (hmm_frame(&hmm->
hmm) == cf) {
799 hmm_normalize(&hmm->
hmm, norm);
804 ngs->renormalized = TRUE;
815 if (!ps_search_acmod(ngs)->compallsen)
816 compute_fwdflat_sen_active(ngs, frame_idx);
819 senscr =
acmod_score(ps_search_acmod(ngs), &frame_idx);
820 ngs->
st.n_senone_active_utt += ps_search_acmod(ngs)->n_senone_active;
831 E_INFO(
"Renormalizing Scores at frame %d, best score %d\n",
833 fwdflat_renormalize_scores(ngs, frame_idx, ngs->
best_score);
840 fwdflat_eval_chan(ngs, frame_idx);
842 fwdflat_prune_chan(ngs, frame_idx);
844 fwdflat_word_transition(ngs, frame_idx);
855 for (i = ps_search_start_wid(ngs); i < ps_search_n_words(ngs); i++) {
881 for (f = 0; f < ngs->
n_frame; f++) {
901 if (dict_is_single_phone(ps_search_dict(ngs),wid))
923 destroy_fwdflat_chan(ngs);
924 destroy_fwdflat_wordlist(ngs);
925 bitvec_clear_all(ngs->
word_active, ps_search_n_words(ngs));
928 cf = ps_search_acmod(ngs)->output_frame;
932 ptmr_stop(&ngs->fwdflat_perf);
935 double n_speech = (double)(cf + 1)
936 / cmd_ln_int32_r(ps_search_config(ngs),
"-frate");
937 E_INFO(
"%8d words recognized (%d/fr)\n",
938 ngs->bpidx, (ngs->bpidx + (cf >> 1)) / (cf + 1));
939 E_INFO(
"%8d senones evaluated (%d/fr)\n", ngs->
st.n_senone_active_utt,
940 (ngs->
st.n_senone_active_utt + (cf >> 1)) / (cf + 1));
941 E_INFO(
"%8d channels searched (%d/fr)\n",
942 ngs->
st.n_fwdflat_chan, ngs->
st.n_fwdflat_chan / (cf + 1));
943 E_INFO(
"%8d words searched (%d/fr)\n",
944 ngs->
st.n_fwdflat_words, ngs->
st.n_fwdflat_words / (cf + 1));
945 E_INFO(
"%8d word transitions (%d/fr)\n",
946 ngs->
st.n_fwdflat_word_transition,
947 ngs->
st.n_fwdflat_word_transition / (cf + 1));
948 E_INFO(
"fwdflat %.2f CPU %.3f xRT\n",
949 ngs->fwdflat_perf.t_cpu,
950 ngs->fwdflat_perf.t_cpu / n_speech);
951 E_INFO(
"fwdflat %.2f wall %.3f xRT\n",
952 ngs->fwdflat_perf.t_elapsed,
953 ngs->fwdflat_perf.t_elapsed / n_speech);