PocketSphinx  5prealpha
hmm.c
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 
42 /* System headers. */
43 #include <assert.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <limits.h>
47 
48 /* SphinxBase headers. */
49 #include <sphinxbase/ckd_alloc.h>
50 #include <sphinxbase/err.h>
51 
52 /* Local headers. */
53 #include "hmm.h"
54 
56 hmm_context_init(int32 n_emit_state,
57  uint8 ** const *tp,
58  int16 const *senscore,
59  uint16 * const *sseq)
60 {
61  hmm_context_t *ctx;
62 
63  assert(n_emit_state > 0);
64  if (n_emit_state > HMM_MAX_NSTATE) {
65  E_ERROR("Number of emitting states must be <= %d\n", HMM_MAX_NSTATE);
66  return NULL;
67  }
68 
69  ctx = ckd_calloc(1, sizeof(*ctx));
70  ctx->n_emit_state = n_emit_state;
71  ctx->tp = tp;
72  ctx->senscore = senscore;
73  ctx->sseq = sseq;
74  ctx->st_sen_scr = ckd_calloc(n_emit_state, sizeof(*ctx->st_sen_scr));
75 
76  return ctx;
77 }
78 
79 void
81 {
82  if (ctx == NULL)
83  return;
84  ckd_free(ctx->st_sen_scr);
85  ckd_free(ctx);
86 }
87 
88 void
89 hmm_init(hmm_context_t *ctx, hmm_t *hmm, int mpx, int ssid, int tmatid)
90 {
91  hmm->ctx = ctx;
92  hmm->mpx = mpx;
93  hmm->n_emit_state = ctx->n_emit_state;
94  if (mpx) {
95  int i;
96  hmm->ssid = BAD_SSID;
97  hmm->senid[0] = ssid;
98  for (i = 1; i < hmm_n_emit_state(hmm); ++i) {
99  hmm->senid[i] = BAD_SSID;
100  }
101  }
102  else {
103  hmm->ssid = ssid;
104  memcpy(hmm->senid, ctx->sseq[ssid], hmm->n_emit_state * sizeof(*hmm->senid));
105  }
106  hmm->tmatid = tmatid;
107  hmm_clear(hmm);
108 }
109 
110 void
112 {
113 }
114 
115 void
117  FILE * fp)
118 {
119  int32 i;
120 
121  if (hmm_is_mpx(hmm)) {
122  fprintf(fp, "MPX ");
123  for (i = 0; i < hmm_n_emit_state(hmm); i++)
124  fprintf(fp, " %11d", hmm_senid(hmm, i));
125  fprintf(fp, " ( ");
126  for (i = 0; i < hmm_n_emit_state(hmm); i++)
127  fprintf(fp, "%d ", hmm_ssid(hmm, i));
128  fprintf(fp, ")\n");
129  }
130  else {
131  fprintf(fp, "SSID ");
132  for (i = 0; i < hmm_n_emit_state(hmm); i++)
133  fprintf(fp, " %11d", hmm_senid(hmm, i));
134  fprintf(fp, " (%d)\n", hmm_ssid(hmm, 0));
135  }
136 
137  if (hmm->ctx->senscore) {
138  fprintf(fp, "SENSCR");
139  for (i = 0; i < hmm_n_emit_state(hmm); i++)
140  fprintf(fp, " %11d", hmm_senscr(hmm, i));
141  fprintf(fp, "\n");
142  }
143 
144  fprintf(fp, "SCORES %11d", hmm_in_score(hmm));
145  for (i = 1; i < hmm_n_emit_state(hmm); i++)
146  fprintf(fp, " %11d", hmm_score(hmm, i));
147  fprintf(fp, " %11d", hmm_out_score(hmm));
148  fprintf(fp, "\n");
149 
150  fprintf(fp, "HISTID %11d", hmm_in_history(hmm));
151  for (i = 1; i < hmm_n_emit_state(hmm); i++)
152  fprintf(fp, " %11d", hmm_history(hmm, i));
153  fprintf(fp, " %11d", hmm_out_history(hmm));
154  fprintf(fp, "\n");
155 
156  if (hmm_in_score(hmm) > 0)
157  fprintf(fp,
158  "ALERT!! The input score %d is large than 0. Probably wrap around.\n",
159  hmm_in_score(hmm));
160  if (hmm_out_score(hmm) > 0)
161  fprintf(fp,
162  "ALERT!! The output score %d is large than 0. Probably wrap around\n.",
163  hmm_out_score(hmm));
164 
165  fflush(fp);
166 }
167 
168 
169 void
171 {
172  int32 i;
173 
174  hmm_in_score(h) = WORST_SCORE;
175  for (i = 1; i < hmm_n_emit_state(h); i++)
176  hmm_score(h, i) = WORST_SCORE;
177  hmm_out_score(h) = WORST_SCORE;
178 
179  h->bestscore = WORST_SCORE;
180 }
181 
182 void
184 {
185  int32 i;
186 
187  hmm_in_score(h) = WORST_SCORE;
188  hmm_in_history(h) = -1;
189  for (i = 1; i < hmm_n_emit_state(h); i++) {
190  hmm_score(h, i) = WORST_SCORE;
191  hmm_history(h, i) = -1;
192  }
193  hmm_out_score(h) = WORST_SCORE;
194  hmm_out_history(h) = -1;
195 
196  h->bestscore = WORST_SCORE;
197  h->frame = -1;
198 }
199 
200 void
201 hmm_enter(hmm_t *h, int32 score, int32 histid, int frame)
202 {
203  hmm_in_score(h) = score;
204  hmm_in_history(h) = histid;
205  hmm_frame(h) = frame;
206 }
207 
208 void
209 hmm_normalize(hmm_t *h, int32 bestscr)
210 {
211  int32 i;
212 
213  for (i = 0; i < hmm_n_emit_state(h); i++) {
214  if (hmm_score(h, i) BETTER_THAN WORST_SCORE)
215  hmm_score(h, i) -= bestscr;
216  }
217  if (hmm_out_score(h) BETTER_THAN WORST_SCORE)
218  hmm_out_score(h) -= bestscr;
219 }
220 
221 #define hmm_tprob_5st(i, j) (-tp[(i)*6+(j)])
222 #define nonmpx_senscr(i) (-senscore[sseq[i]])
223 
224 static int32
225 hmm_vit_eval_5st_lr(hmm_t * hmm)
226 {
227  int16 const *senscore = hmm->ctx->senscore;
228  uint8 const *tp = hmm->ctx->tp[hmm->tmatid][0];
229  uint16 const *sseq = hmm->senid;
230  int32 s5, s4, s3, s2, s1, s0, t2, t1, t0, bestScore;
231 
232  /* It was the best of scores, it was the worst of scores. */
233  bestScore = WORST_SCORE;
234 
235  /* Cache problem here! */
236  s4 = hmm_score(hmm, 4) + nonmpx_senscr(4);
237  s3 = hmm_score(hmm, 3) + nonmpx_senscr(3);
238  /* Transitions into non-emitting state 5 */
239  if (s3 BETTER_THAN WORST_SCORE) {
240  t1 = s4 + hmm_tprob_5st(4, 5);
241  t2 = s3 + hmm_tprob_5st(3, 5);
242  if (t1 BETTER_THAN t2) {
243  s5 = t1;
244  hmm_out_history(hmm) = hmm_history(hmm, 4);
245  } else {
246  s5 = t2;
247  hmm_out_history(hmm) = hmm_history(hmm, 3);
248  }
249  if (s5 WORSE_THAN WORST_SCORE) s5 = WORST_SCORE;
250  hmm_out_score(hmm) = s5;
251  bestScore = s5;
252  }
253 
254  s2 = hmm_score(hmm, 2) + nonmpx_senscr(2);
255  /* All transitions into state 4 */
256  if (s2 BETTER_THAN WORST_SCORE) {
257  t0 = s4 + hmm_tprob_5st(4, 4);
258  t1 = s3 + hmm_tprob_5st(3, 4);
259  t2 = s2 + hmm_tprob_5st(2, 4);
260  if (t0 BETTER_THAN t1) {
261  if (t2 BETTER_THAN t0) {
262  s4 = t2;
263  hmm_history(hmm, 4) = hmm_history(hmm, 2);
264  } else
265  s4 = t0;
266  } else {
267  if (t2 BETTER_THAN t1) {
268  s4 = t2;
269  hmm_history(hmm, 4) = hmm_history(hmm, 2);
270  } else {
271  s4 = t1;
272  hmm_history(hmm, 4) = hmm_history(hmm, 3);
273  }
274  }
275  if (s4 WORSE_THAN WORST_SCORE) s4 = WORST_SCORE;
276  if (s4 BETTER_THAN bestScore) bestScore = s4;
277  hmm_score(hmm, 4) = s4;
278  }
279 
280  s1 = hmm_score(hmm, 1) + nonmpx_senscr(1);
281  /* All transitions into state 3 */
282  if (s1 BETTER_THAN WORST_SCORE) {
283  t0 = s3 + hmm_tprob_5st(3, 3);
284  t1 = s2 + hmm_tprob_5st(2, 3);
285  t2 = s1 + hmm_tprob_5st(1, 3);
286  if (t0 BETTER_THAN t1) {
287  if (t2 BETTER_THAN t0) {
288  s3 = t2;
289  hmm_history(hmm, 3) = hmm_history(hmm, 1);
290  } else
291  s3 = t0;
292  } else {
293  if (t2 BETTER_THAN t1) {
294  s3 = t2;
295  hmm_history(hmm, 3) = hmm_history(hmm, 1);
296  } else {
297  s3 = t1;
298  hmm_history(hmm, 3) = hmm_history(hmm, 2);
299  }
300  }
301  if (s3 WORSE_THAN WORST_SCORE) s3 = WORST_SCORE;
302  if (s3 BETTER_THAN bestScore) bestScore = s3;
303  hmm_score(hmm, 3) = s3;
304  }
305 
306  s0 = hmm_in_score(hmm) + nonmpx_senscr(0);
307  /* All transitions into state 2 (state 0 is always active) */
308  t0 = s2 + hmm_tprob_5st(2, 2);
309  t1 = s1 + hmm_tprob_5st(1, 2);
310  t2 = s0 + hmm_tprob_5st(0, 2);
311  if (t0 BETTER_THAN t1) {
312  if (t2 BETTER_THAN t0) {
313  s2 = t2;
314  hmm_history(hmm, 2) = hmm_in_history(hmm);
315  } else
316  s2 = t0;
317  } else {
318  if (t2 BETTER_THAN t1) {
319  s2 = t2;
320  hmm_history(hmm, 2) = hmm_in_history(hmm);
321  } else {
322  s2 = t1;
323  hmm_history(hmm, 2) = hmm_history(hmm, 1);
324  }
325  }
326  if (s2 WORSE_THAN WORST_SCORE) s2 = WORST_SCORE;
327  if (s2 BETTER_THAN bestScore) bestScore = s2;
328  hmm_score(hmm, 2) = s2;
329 
330 
331  /* All transitions into state 1 */
332  t0 = s1 + hmm_tprob_5st(1, 1);
333  t1 = s0 + hmm_tprob_5st(0, 1);
334  if (t0 BETTER_THAN t1) {
335  s1 = t0;
336  } else {
337  s1 = t1;
338  hmm_history(hmm, 1) = hmm_in_history(hmm);
339  }
340  if (s1 WORSE_THAN WORST_SCORE) s1 = WORST_SCORE;
341  if (s1 BETTER_THAN bestScore) bestScore = s1;
342  hmm_score(hmm, 1) = s1;
343 
344  /* All transitions into state 0 */
345  s0 = s0 + hmm_tprob_5st(0, 0);
346  if (s0 WORSE_THAN WORST_SCORE) s0 = WORST_SCORE;
347  if (s0 BETTER_THAN bestScore) bestScore = s0;
348  hmm_in_score(hmm) = s0;
349 
350  hmm_bestscore(hmm) = bestScore;
351  return bestScore;
352 }
353 
354 #define mpx_senid(st) sseq[ssid[st]][st]
355 #define mpx_senscr(st) (-senscore[mpx_senid(st)])
356 
357 static int32
358 hmm_vit_eval_5st_lr_mpx(hmm_t * hmm)
359 {
360  uint8 const *tp = hmm->ctx->tp[hmm->tmatid][0];
361  int16 const *senscore = hmm->ctx->senscore;
362  uint16 * const *sseq = hmm->ctx->sseq;
363  uint16 *ssid = hmm->senid;
364  int32 bestScore;
365  int32 s5, s4, s3, s2