vithist.h

Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
00002 /* ====================================================================
00003  * Copyright (c) 1999-2004 Carnegie Mellon University.  All rights
00004  * reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer. 
00012  *
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in
00015  *    the documentation and/or other materials provided with the
00016  *    distribution.
00017  *
00018  * This work was supported in part by funding from the Defense Advanced 
00019  * Research Projects Agency and the National Science Foundation of the 
00020  * United States of America, and the CMU Sphinx Speech Consortium.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
00023  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
00024  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00025  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
00026  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00028  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
00029  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
00030  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00031  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00032  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * ====================================================================
00035  *
00036  */
00037 /*
00038  * vithist.h -- Viterbi history
00039  * 
00040  * **********************************************
00041  * CMU ARPA Speech Project
00042  *
00043  * Copyright (c) 1999 Carnegie Mellon University.
00044  * ALL RIGHTS RESERVED.
00045  * **********************************************
00046  * 
00047  * HISTORY
00048  * $Log$
00049  * Revision 1.1  2006/04/05  20:27:30  dhdfu
00050  * A Great Reorganzation of header files and executables
00051  * 
00052  * Revision 1.12  2006/02/23 16:56:13  arthchan2003
00053  * Merged from the branch SPHINX3_5_2_RCI_IRII_BRANCH
00054  * 1, Split latticehist_t from flat_fwd.c to  here.
00055  * 2, Introduced vithist_entry_cp.  This is much better than the direct
00056  * copy we have been using. (Which could cause memory problem easily)
00057  *
00058  * Revision 1.11.4.9  2006/01/16 18:11:39  arthchan2003
00059  * 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
00060  *
00061  * Revision 1.11.4.8  2005/11/17 06:46:02  arthchan2003
00062  * 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.
00063  *
00064  * Revision 1.11.4.7  2005/10/17 04:58:30  arthchan2003
00065  * 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.
00066  *
00067  * Revision 1.11.4.6  2005/10/07 20:05:05  arthchan2003
00068  * When rescoring in full triphone expansion, the code should use the score for the word end with corret right context.
00069  *
00070  * Revision 1.11.4.5  2005/09/26 06:37:33  arthchan2003
00071  * Before anyone get hurt, quickly change back to using SINGLE_RC_HISTORY.
00072  *
00073  * Revision 1.11.4.4  2005/09/25 19:23:55  arthchan2003
00074  * 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.
00075  *
00076  * Revision 1.11.4.3  2005/09/11 03:00:15  arthchan2003
00077  * 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.
00078  *
00079  * Revision 1.11.4.2  2005/07/26 02:20:39  arthchan2003
00080  * merged hyp_t with srch_hyp_t.
00081  *
00082  * Revision 1.11.4.1  2005/07/04 07:25:22  arthchan2003
00083  * Added vithist_entry_display and vh_lmstate_display in vithist.
00084  *
00085  * Revision 1.11  2005/06/22 02:47:35  arthchan2003
00086  * 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.
00087  *
00088  * Revision 1.10  2005/06/16 04:59:10  archan
00089  * Sphinx3 to s3.generic, a gentle-refactored version of Dave's change in senone scale.
00090  *
00091  * Revision 1.9  2005/06/13 04:02:59  archan
00092  * Fixed most doxygen-style documentation under libs3decoder.
00093  *
00094  * Revision 1.8  2005/05/26 00:46:59  archan
00095  * Added functionalities that such that <sil> will not be inserted at the end of the utterance.
00096  *
00097  * Revision 1.7  2005/04/25 23:53:35  archan
00098  * 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.
00099  *
00100  * Revision 1.6  2005/04/21 23:50:26  archan
00101  * 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.
00102  *
00103  * Revision 1.5  2005/04/20 03:46:30  archan
00104  * factor dag header writer into vithist.[ch], do the corresponding change for lm_t
00105  *
00106  * Revision 1.4  2005/03/30 01:22:47  archan
00107  * Fixed mistakes in last updates. Add
00108  *
00109  * 
00110  * 20.Apr.2001  RAH (rhoughton@mediasite.com, ricky.houghton@cs.cmu.edu)
00111  *              Added vithist_free() to free allocated memory
00112  * 
00113  * 30-Sep-1999  M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
00114  *              Added vithist_entry_t.ascr.
00115  * 
00116  * 13-Aug-1999  M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
00117  *              Added maxwpf handling.
00118  * 
00119  * 24-May-1999  M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
00120  *              Started.
00121  */
00122 
00123 
00124 #ifndef _S3_VITHIST_H_
00125 #define _S3_VITHIST_H_
00126 
00127 #include <stdio.h>
00128 
00129 #include <s3types.h>
00130 #include <cmd_ln.h>
00131 #include <logmath.h>
00132 #include <glist.h>
00133 #include "kbcore.h"
00134 #include "search.h"
00135 #include "dict.h"
00136 #include "lm.h"
00137 #include "fillpen.h"
00138 
00139 #include "dag.h"  
00140 #include "ctxt_table.h"
00141 
00150 #ifdef __cplusplus
00151 extern "C" {
00152 #endif
00153 #if 0
00154 } /* Fool Emacs into not indenting things. */
00155 #endif
00156 
00162 typedef union vh_lmstate_u {
00163     struct {
00164         s3lmwid32_t lwid[2];    
00165     } lm3g;
00166 } vh_lmstate_t;
00167 
00168 
00169 typedef struct backpointer_s {
00170     int32 score;
00171     int32 pred;
00172 } backpointer_t;
00173 
00177 typedef struct {
00178     backpointer_t path;         
00179     vh_lmstate_t lmstate;       
00180     s3wid_t wid;                
00181     s3frmid_t sf, ef;           
00182     int32 ascr;                 
00183     int32 lscr;                 
00184     int16 type;                 
00185     int16 valid;                
00186     backpointer_t *rc;          
00187     int32 n_rc;                 
00188 } vithist_entry_t;
00189 
00191 #define vithist_entry_wid(ve)   ((ve)->wid)
00192 
00194 #define vithist_entry_sf(ve)    ((ve)->sf)
00195 
00197 #define vithist_entry_ef(ve)    ((ve)->ef)
00198 
00200 #define vithist_entry_ascr(ve)  ((ve)->ascr)
00201 
00203 #define vithist_entry_lscr(ve)  ((ve)->lscr)
00204 
00206 #define vithist_entry_score(ve) ((ve)->path.score)
00207 #define vithist_entry_pred(ve)  ((ve)->path.pred)
00208 #define vithist_entry_valid(ve) ((ve)->valid)
00209 
00210 
00222 typedef struct {                
00223     int32 state;                
00224     int32 vhid;                 
00225     vithist_entry_t *ve;        
00226     glist_t children;           
00228 } vh_lms2vh_t;
00229 
00230 
00238 typedef struct {
00239     vithist_entry_t **entry;    
00240     int32 *frame_start;         
00242     int32 n_entry;              
00243     int32 n_frm;                
00244     int32 n_ci;                 
00245     int32 bghist;               
00248     int32 wbeam;                
00250     int32 *bestscore;           
00251     int32 *bestvh;              
00253     vh_lms2vh_t **lms2vh_root;  
00254     glist_t lwidlist;           
00255 } vithist_t;
00256 
00257 
00258 #define VITHIST_BLKSIZE         16384   /* (1 << 14) */
00259 #define VITHIST_MAXBLKS         256
00260 #define VITHIST_ID2BLK(i)       ((i) >> 14)
00261 #define VITHIST_ID2BLKOFFSET(i) ((i) & 0x00003fff)      /* 14 LSB */
00262 
00266 #define vithist_id2entry(vh,id) ((vh)->entry[VITHIST_ID2BLK(id)] + VITHIST_ID2BLKOFFSET(id))
00267 
00269 #define vithist_n_entry(vh)             ((vh)->n_entry)
00270 
00272 #define vithist_bestscore(vh)           ((vh)->bestscore)
00273 
00275 #define vithist_bestvh(vh)              ((vh)->bestvh)
00276 
00278 #define vithist_lms2vh_root(vh,w)       ((vh)->lms2vh_root[w])
00279 
00281 #define vithist_lwidlist(vh)            ((vh)->lwidlist)
00282 
00284 #define vithist_first_entry(vh,f)       ((vh)->frame_start[f])
00285 
00287 #define vithist_last_entry(vh,f)        ((vh)->frame_start[f+1] - 1)
00288 
00289 
00296 vithist_t *vithist_init (kbcore_t *kbc,  
00297                          int32 wbeam,    
00298                          int32 bghist,   
00299                          int32 report    
00300     );
00301 
00302 
00307 int32 vithist_utt_begin (vithist_t *vh, 
00308                          kbcore_t *kbc  
00309     );
00310 
00311 
00317 int32 vithist_utt_end (vithist_t *vh, 
00318                        kbcore_t *kbc  
00319     );
00320 
00321 
00326 int32 vithist_partialutt_end (vithist_t *vh, 
00327                               kbcore_t *kbc  
00328     );
00329 
00330 /* Invoked at the end of each utterance to clear up and deallocate space */
00331 void vithist_utt_reset (vithist_t *vh  
00332     );
00333 
00334 
00339 glist_t vithist_backtrace (vithist_t *vh,       
00340                            int32 id,            
00341                            dict_t *dict         
00342     );
00343 
00344 
00349 void vithist_enter(vithist_t * vh,              
00350                    kbcore_t * kbc,              
00351                    vithist_entry_t * tve,       
00352                    int32 comp_rc                
00353     );
00354 
00362 void vithist_rescore (vithist_t *vh,    
00363                       kbcore_t *kbc,    
00364                       s3wid_t wid,      
00365                       int32 ef,         
00366                       int32 score,      
00367                       int32 pred,       
00368                       int32 type,       
00369                       int32 rc          
00370     );
00371 
00372 
00374 void vithist_frame_windup (vithist_t *vh,       
00375                            int32 frm,           
00376                            FILE *fp,            
00378                            kbcore_t *kbc        
00380     );
00381 
00386 void vithist_prune (vithist_t *vh,      
00387                     dict_t *dict,       
00388                     int32 frm,          
00389                     int32 maxwpf,       
00390                     int32 maxhist,      
00391                     int32 beam  
00392     );
00393 
00397 void vithist_dump (vithist_t *vh,     
00398                    int32 frm,           
00400                    kbcore_t *kbc,       
00401                    FILE *fp             
00402     );
00403 
00407 dag_t *vithist_dag_build(vithist_t * vh, glist_t hyp, dict_t * dict, int32 endid,
00408                          cmd_ln_t *config, logmath_t *logmath);
00409 
00413 int32 vithist_dag_write(vithist_t *vithist,
00414                         const char *filename,
00415                         dag_t * dag,
00416                         lm_t * lm,
00417                         dict_t * dict);
00418 
00423 void vithist_free(vithist_t *vh         
00424     );
00425 
00426 
00431 void vithist_report(vithist_t *vh       
00432     );
00433 
00438 void vh_lmstate_display(vh_lmstate_t *vhl, 
00439                         dict_t *dict 
00440     );
00441 
00445 void vithist_entry_display(vithist_entry_t *ve, 
00446                            dict_t* dict  
00447     );
00448 
00449 
00459 typedef struct lattice_s {
00460     s3wid_t   wid;      
00461     s3frmid_t frm;      
00462     s3latid_t history;  
00464     int32 ascr;         
00465     int32 lscr;         
00467     int32     score;    
00468     int32    *rcscore;  
00469     dagnode_t *dagnode; 
00470 } lattice_t;
00471 
00472 
00473 #define LAT_ALLOC_INCR          32768
00474 
00475 #define LATID2SF(hist,l)        (IS_S3LATID(hist->lattice[l].history) ? \
00476                          hist->lattice[hist->lattice[l].history].frm + 1 : 0)
00477 
00489 /* FIXME, 1, Either replace all latticehist_t by vithist_t or 
00490    2, there is no need to always put dict_t, ctxt_table_t , lm_t and fillpen_t as arguments 
00491 */
00492 
00493 typedef struct {
00494 
00495     lattice_t *lattice;  
00496     s3latid_t *frm_latstart;    
00498     int32 lat_alloc;     
00499     int32 n_lat_entry;   
00500     int32 n_cand;        
00505     int32 n_frms_alloc;  
00506     int32 n_frm;         
00508 } latticehist_t;
00509 
00510 
00511 #define latticehist_n_cand(hist)                ((hist)->n_cand)
00512 #define latticehist_lat_alloc(hist)             ((hist)->lat_alloc)
00513 #define latticehist_n_lat_entry(hist)           ((hist)->n_lat_entry)
00514 
00515   
00520 latticehist_t *latticehist_init(int32 init_alloc_size, 
00521                                 int32 num_frames       
00522     );
00523 
00527 void latticehist_free(latticehist_t *lat 
00528     );
00529 
00530 /*
00531  * Reset a lattice history table 
00532  */
00533    
00534 void latticehist_reset(latticehist_t *lat 
00535     );
00536 
00540 void latticehist_dump (latticehist_t *lathist,  
00541                        FILE *fp,                
00542                        dict_t *dict,            
00543                        ctxt_table_t *ct,        
00544                        int32 dumpRC             
00545     );
00546 
00550 void lattice_entry (latticehist_t *lathist, 
00551                     s3wid_t w,              
00552                     int32 f,                
00553                     int32 score,            
00554                     s3latid_t history,      
00555                     int32 rc,                
00556                     ctxt_table_t *ct,       
00557                     dict_t *dict            
00558     );
00559 
00560 
00561 void two_word_history (latticehist_t *lathist, 
00562                        s3latid_t l, s3wid_t *w0, s3wid_t *w1, dict_t *dict);
00563 
00569 int32 lat_pscr_rc (latticehist_t *lathist, 
00570                    s3latid_t l,            
00571                    s3wid_t w_rc,           
00572                    ctxt_table_t *ct,       
00573                    dict_t *dict            
00574     );
00575 
00580 s3latid_t lat_pscr_rc_history (latticehist_t *lathist, 
00581                                s3latid_t l,  
00582                                s3wid_t w_rc,  
00583                                ctxt_table_t *ct,  
00584                                dict_t *dict     
00585     );
00586 
00587 int32 lat_seg_lscr (latticehist_t *lathist, 
00588                     s3latid_t l, 
00589                     lm_t *lm, 
00590                     dict_t *dict, 
00591                     ctxt_table_t *ct, 
00592                     fillpen_t *fillpen, 
00593                     int32 isCand);
00594 
00595 
00600 void lat_seg_ascr_lscr (latticehist_t *lathist, 
00601                         s3latid_t l,  
00602                         s3wid_t w_rc, 
00603                         int32 *ascr,  
00604                         int32 *lscr,  
00605                         lm_t *lm,     
00606                         dict_t *dict,  
00607                         ctxt_table_t *ct,  
00608                         fillpen_t *fillpen 
00609     );
00610 
00611 
00615 s3latid_t lat_final_entry ( latticehist_t* lathist, 
00616                             dict_t* dict, 
00617                             int32 curfrm, 
00618                             char* uttid 
00619     );
00620                               
00624 srch_hyp_t *lattice_backtrace (latticehist_t *lathist, 
00625                                s3latid_t l,   
00626                                s3wid_t w_rc,   
00627                                srch_hyp_t **hyp, 
00628                                lm_t *lm,       
00629                                dict_t *dict,   
00630                                ctxt_table_t *ct,  
00631                                fillpen_t *fillpen 
00632     );
00633 
00646 dag_t * latticehist_dag_build(latticehist_t * vh, glist_t hyp, dict_t * dict,
00647                               lm_t *lm, ctxt_table_t *ctxt, fillpen_t *fpen,
00648                               int32 endid, cmd_ln_t *config, logmath_t *logmath);
00649 
00653 int32 latticehist_dag_write (latticehist_t *lathist,  
00654                              const char *filename,
00655                              dag_t *dag,
00656                              lm_t *lm, 
00657                              dict_t *dict, 
00658                              ctxt_table_t *ct, 
00659                              fillpen_t *fillpen
00660     );
00661 
00662 #if 0
00663 { /* Stop indent from complaining */
00664 #endif
00665 #ifdef __cplusplus
00666 }
00667 #endif
00668 
00669 #endif

Generated on 7 Mar 2010 by  doxygen 1.6.1