vithist.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 1999-2004 Carnegie Mellon University. All rights
4  * reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * This work was supported in part by funding from the Defense Advanced
19  * Research Projects Agency and the National Science Foundation of the
20  * United States of America, and the CMU Sphinx Speech Consortium.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * ====================================================================
35  *
36  */
37 /*
38  * vithist.h -- Viterbi history
39  *
40  * **********************************************
41  * CMU ARPA Speech Project
42  *
43  * Copyright (c) 1999 Carnegie Mellon University.
44  * ALL RIGHTS RESERVED.
45  * **********************************************
46  *
47  * HISTORY
48  * $Log$
49  * Revision 1.1 2006/04/05 20:27:30 dhdfu
50  * A Great Reorganzation of header files and executables
51  *
52  * Revision 1.12 2006/02/23 16:56:13 arthchan2003
53  * Merged from the branch SPHINX3_5_2_RCI_IRII_BRANCH
54  * 1, Split latticehist_t from flat_fwd.c to here.
55  * 2, Introduced vithist_entry_cp. This is much better than the direct
56  * copy we have been using. (Which could cause memory problem easily)
57  *
58  * Revision 1.11.4.9 2006/01/16 18:11:39 arthchan2003
59  * 1, Important Bug fixes, a local pointer is used when realloc is needed. This causes invalid writing of the memory, 2, Acoustic scores of the last segment in IBM lattice generation couldn't be found in the past. Now, this could be handled by the optional acoustic scores in the node of lattice. Application code is not yet checked-in
60  *
61  * Revision 1.11.4.8 2005/11/17 06:46:02 arthchan2003
62  * 3 changes. 1, Code was added for full triphone implementation, not yet working. 2, Senone scale is removed from vithist table. This was a bug introduced during some fixes in CALO.
63  *
64  * Revision 1.11.4.7 2005/10/17 04:58:30 arthchan2003
65  * vithist.c is the true source of memory leaks in the past for full cwtp expansion. There are two changes made to avoid this happen, 1, instead of using ve->rc_info as the indicator whether something should be done, used a flag bFullExpand to control it. 2, avoid doing direct C-struct copy (like *ve = *tve), it becomes the reason of why memory are leaked and why the code goes wrong.
66  *
67  * Revision 1.11.4.6 2005/10/07 20:05:05 arthchan2003
68  * When rescoring in full triphone expansion, the code should use the score for the word end with corret right context.
69  *
70  * Revision 1.11.4.5 2005/09/26 06:37:33 arthchan2003
71  * Before anyone get hurt, quickly change back to using SINGLE_RC_HISTORY.
72  *
73  * Revision 1.11.4.4 2005/09/25 19:23:55 arthchan2003
74  * 1, Added arguments for turning on/off LTS rules. 2, Added arguments for turning on/off composite triphones. 3, Moved dict2pid deallocation back to dict2pid. 4, Tidying up the clean up code.
75  *
76  * Revision 1.11.4.3 2005/09/11 03:00:15 arthchan2003
77  * All lattice-related functions are not incorporated into vithist. So-called "lattice" is essentially the predecessor of vithist_t and fsg_history_t. Later when vithist_t support by right context score and history. It should replace both of them.
78  *
79  * Revision 1.11.4.2 2005/07/26 02:20:39 arthchan2003
80  * merged hyp_t with srch_hyp_t.
81  *
82  * Revision 1.11.4.1 2005/07/04 07:25:22 arthchan2003
83  * Added vithist_entry_display and vh_lmstate_display in vithist.
84  *
85  * Revision 1.11 2005/06/22 02:47:35 arthchan2003
86  * 1, Added reporting flag for vithist_init. 2, Added a flag to allow using words other than silence to be the last word for backtracing. 3, Fixed doxygen documentation. 4, Add keyword.
87  *
88  * Revision 1.10 2005/06/16 04:59:10 archan
89  * Sphinx3 to s3.generic, a gentle-refactored version of Dave's change in senone scale.
90  *
91  * Revision 1.9 2005/06/13 04:02:59 archan
92  * Fixed most doxygen-style documentation under libs3decoder.
93  *
94  * Revision 1.8 2005/05/26 00:46:59 archan
95  * Added functionalities that such that <sil> will not be inserted at the end of the utterance.
96  *
97  * Revision 1.7 2005/04/25 23:53:35 archan
98  * 1, Some minor modification of vithist_t, vithist_rescore can now support optional LM rescoring, vithist also has its own reporting routine. A new argument -lmrescore is also added in decode and livepretend. This can switch on and off the rescoring procedure. 2, I am reaching the final difficulty of mode 5 implementation. That is, to implement an algorithm which dynamically decide which tree copies should be entered. However, stuffs like score propagation in the leave nodes and non-leaves nodes are already done. 3, As briefly mentioned in 2, implementation of rescoring , which used to happened at leave nodes are now separated. The current implementation is not the most clever one. Wish I have time to change it before check-in to the canonical.
99  *
100  * Revision 1.6 2005/04/21 23:50:26 archan
101  * Some more refactoring on the how reporting of structures inside kbcore_t is done, it is now 50% nice. Also added class-based LM test case into test-decode.sh.in. At this moment, everything in search mode 5 is already done. It is time to test the idea whether the search can really be used.
102  *
103  * Revision 1.5 2005/04/20 03:46:30 archan
104  * factor dag header writer into vithist.[ch], do the corresponding change for lm_t
105  *
106  * Revision 1.4 2005/03/30 01:22:47 archan
107  * Fixed mistakes in last updates. Add
108  *
109  *
110  * 20.Apr.2001 RAH (rhoughton@mediasite.com, ricky.houghton@cs.cmu.edu)
111  * Added vithist_free() to free allocated memory
112  *
113  * 30-Sep-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
114  * Added vithist_entry_t.ascr.
115  *
116  * 13-Aug-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
117  * Added maxwpf handling.
118  *
119  * 24-May-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
120  * Started.
121  */
122 
123 
124 #ifndef _S3_VITHIST_H_
125 #define _S3_VITHIST_H_
126 
127 #include <stdio.h>
128 
129 #include <s3types.h>
130 #include <cmd_ln.h>
131 #include <logmath.h>
132 #include <glist.h>
133 #include "kbcore.h"
134 #include "search.h"
135 #include "dict.h"
136 #include "lm.h"
137 #include "fillpen.h"
138 
139 #include "dag.h"
140 #include "ctxt_table.h"
141 
150 #ifdef __cplusplus
151 extern "C" {
152 #endif
153 #if 0
154 } /* Fool Emacs into not indenting things. */
155 #endif
156 
162 typedef union vh_lmstate_u {
163  struct {
165  } lm3g;
166 } vh_lmstate_t;
167 
168 
169 typedef struct backpointer_s {
170  int32 score;
171  int32 pred;
172 } backpointer_t;
173 
177 typedef struct {
181  s3frmid_t sf, ef;
182  int32 ascr;
183  int32 lscr;
184  int16 type;
185  int16 valid;
187  int32 n_rc;
189 
191 #define vithist_entry_wid(ve) ((ve)->wid)
192 
194 #define vithist_entry_sf(ve) ((ve)->sf)
195 
197 #define vithist_entry_ef(ve) ((ve)->ef)
198 
200 #define vithist_entry_ascr(ve) ((ve)->ascr)
201 
203 #define vithist_entry_lscr(ve) ((ve)->lscr)
204 
206 #define vithist_entry_score(ve) ((ve)->path.score)
207 #define vithist_entry_pred(ve) ((ve)->path.pred)
208 #define vithist_entry_valid(ve) ((ve)->valid)
209 
210 
222 typedef struct {
223  int32 state;
224  int32 vhid;
226  glist_t children;
228 } vh_lms2vh_t;
229 
230 
238 typedef struct {
240  int32 *frame_start;
242  int32 n_entry;
243  int32 n_frm;
244  int32 n_ci;
245  int32 bghist;
248  int32 wbeam;
250  int32 *bestscore;
251  int32 *bestvh;
254  glist_t lwidlist;
255 } vithist_t;
256 
257 
258 #define VITHIST_BLKSIZE 16384 /* (1 << 14) */
259 #define VITHIST_MAXBLKS 256
260 #define VITHIST_ID2BLK(i) ((i) >> 14)
261 #define VITHIST_ID2BLKOFFSET(i) ((i) & 0x00003fff) /* 14 LSB */
262 
266 #define vithist_id2entry(vh,id) ((vh)->entry[VITHIST_ID2BLK(id)] + VITHIST_ID2BLKOFFSET(id))
267 
269 #define vithist_n_entry(vh) ((vh)->n_entry)
270 
272 #define vithist_bestscore(vh) ((vh)->bestscore)
273 
275 #define vithist_bestvh(vh) ((vh)->bestvh)
276 
278 #define vithist_lms2vh_root(vh,w) ((vh)->lms2vh_root[w])
279 
281 #define vithist_lwidlist(vh) ((vh)->lwidlist)
282 
284 #define vithist_first_entry(vh,f) ((vh)->frame_start[f])
285 
287 #define vithist_last_entry(vh,f) ((vh)->frame_start[f+1] - 1)
288 
289 
297  int32 wbeam,
298  int32 bghist,
299  int32 report
300  );
301 
302 
307 int32 vithist_utt_begin (vithist_t *vh,
308  kbcore_t *kbc
309  );
310 
311 
317 int32 vithist_utt_end (vithist_t *vh,
318  kbcore_t *kbc
319  );
320 
321 
327  kbcore_t *kbc
328  );
329 
330 /* Invoked at the end of each utterance to clear up and deallocate space */
331 void vithist_utt_reset (vithist_t *vh
332  );
333 
334 
339 glist_t vithist_backtrace (vithist_t *vh,
340  int32 id,
341  dict_t *dict
342  );
343 
344 
349 void vithist_enter(vithist_t * vh,
350  kbcore_t * kbc,
351  vithist_entry_t * tve,
352  int32 comp_rc
353  );
354 
362 void vithist_rescore (vithist_t *vh,
363  kbcore_t *kbc,
364  s3wid_t wid,
365  int32 ef,
366  int32 score,
367  int32 pred,
368  int32 type,
369  int32 rc
370  );
371 
372 
375  int32 frm,
376  FILE *fp,
378  kbcore_t *kbc
380  );
381 
386 void vithist_prune (vithist_t *vh,
387  dict_t *dict,
388  int32 frm,
389  int32 maxwpf,
390  int32 maxhist,
391  int32 beam
392  );
393 
397 void vithist_dump (vithist_t *vh,
398  int32 frm,
400  kbcore_t *kbc,
401  FILE *fp
402  );
403 
407 dag_t *vithist_dag_build(vithist_t * vh, glist_t hyp, dict_t * dict, int32 endid,
408  cmd_ln_t *config, logmath_t *logmath);
409 
413 int32 vithist_dag_write(vithist_t *vithist,
414  const char *filename,
415  dag_t * dag,
416  lm_t * lm,
417  dict_t * dict);
418 
423 void vithist_free(vithist_t *vh
424  );
425 
426 
431 void vithist_report(vithist_t *vh
432  );
433 
439  dict_t *dict
440  );
441 
446  dict_t* dict
447  );
448 
449 
459 typedef struct lattice_s {
464  int32 ascr;
465  int32 lscr;
467  int32 score;
468  int32 *rcscore;
470 } lattice_t;
471 
472 
473 #define LAT_ALLOC_INCR 32768
474 
475 #define LATID2SF(hist,l) (IS_S3LATID(hist->lattice[l].history) ? \
476  hist->lattice[hist->lattice[l].history].frm + 1 : 0)
477 
489 /* FIXME, 1, Either replace all latticehist_t by vithist_t or
490  2, there is no need to always put dict_t, ctxt_table_t , lm_t and fillpen_t as arguments
491 */
492 
493 typedef struct {
494 
498  int32 lat_alloc;
499  int32 n_lat_entry;
500  int32 n_cand;
505  int32 n_frms_alloc;
506  int32 n_frm;
508 } latticehist_t;
509 
510 
511 #define latticehist_n_cand(hist) ((hist)->n_cand)
512 #define latticehist_lat_alloc(hist) ((hist)->lat_alloc)
513 #define latticehist_n_lat_entry(hist) ((hist)->n_lat_entry)
514 
515 
520 latticehist_t *latticehist_init(int32 init_alloc_size,
521  int32 num_frames
522  );
523 
528  );
529 
530 /*
531  * Reset a lattice history table
532  */
533 
535  );
536 
540 void latticehist_dump (latticehist_t *lathist,
541  FILE *fp,
542  dict_t *dict,
543  ctxt_table_t *ct,
544  int32 dumpRC
545  );
546 
550 void lattice_entry (latticehist_t *lathist,
551  s3wid_t w,
552  int32 f,
553  int32 score,
554  s3latid_t history,
555  int32 rc,
556  ctxt_table_t *ct,
557  dict_t *dict
558  );
559 
560 
561 void two_word_history (latticehist_t *lathist,
562  s3latid_t l, s3wid_t *w0, s3wid_t *w1, dict_t *dict);
563 
569 int32 lat_pscr_rc (latticehist_t *lathist,
570  s3latid_t l,
571  s3wid_t w_rc,
572  ctxt_table_t *ct,
573  dict_t *dict
574  );
575 
581  s3latid_t l,
582  s3wid_t w_rc,
583  ctxt_table_t *ct,
584  dict_t *dict
585  );
586 
587 int32 lat_seg_lscr (latticehist_t *lathist,
588  s3latid_t l,
589  lm_t *lm,
590  dict_t *dict,
591  ctxt_table_t *ct,
592  fillpen_t *fillpen,
593  int32 isCand);
594 
595 
600 void lat_seg_ascr_lscr (latticehist_t *lathist,
601  s3latid_t l,
602  s3wid_t w_rc,
603  int32 *ascr,
604  int32 *lscr,
605  lm_t *lm,
606  dict_t *dict,
607  ctxt_table_t *ct,
609  );
610 
611 
616  dict_t* dict,
617  int32 curfrm,
618  char* uttid
619  );
620 
625  s3latid_t l,
626  s3wid_t w_rc,
627  srch_hyp_t **hyp,
628  lm_t *lm,
629  dict_t *dict,
630  ctxt_table_t *ct,
632  );
633 
646 dag_t * latticehist_dag_build(latticehist_t * vh, glist_t hyp, dict_t * dict,
647  lm_t *lm, ctxt_table_t *ctxt, fillpen_t *fpen,
648  int32 endid, cmd_ln_t *config, logmath_t *logmath);
649 
653 int32 latticehist_dag_write (latticehist_t *lathist,
654  const char *filename,
655  dag_t *dag,
656  lm_t *lm,
657  dict_t *dict,
658  ctxt_table_t *ct,
660  );
661 
662 #if 0
663 { /* Stop indent from complaining */
664 #endif
665 #ifdef __cplusplus
666 }
667 #endif
668 
669 #endif