42 #include <sphinxbase/logmath.h>
43 #include <sphinxbase/err.h>
44 #include <sphinxbase/ckd_alloc.h>
45 #include <sphinxbase/bio.h>
52 #define TMAT_PARAM_VERSION "1.0"
59 static int32 tmat_chk_uppertri(
tmat_t *tmat, logmath_t *lmath);
68 static int32 tmat_chk_1skip(
tmat_t *tmat, logmath_t *lmath);
76 for (i = 0; i < tmat->
n_tmat; i++) {
77 fprintf(fp,
"TMAT %d = %d x %d\n", i, tmat->
n_state,
79 for (src = 0; src < tmat->
n_state; src++) {
80 for (dst = 0; dst <= tmat->
n_state; dst++)
81 fprintf(fp,
" %12d", tmat->
tp[i][src][dst]);
95 tmat_chk_uppertri(
tmat_t * tmat, logmath_t *lmath)
100 for (i = 0; i < tmat->
n_tmat; i++) {
101 for (dst = 0; dst < tmat->
n_state; dst++)
102 for (src = dst + 1; src < tmat->
n_state; src++)
103 if (tmat->
tp[i][src][dst] < 255) {
104 E_ERROR(
"tmat[%d][%d][%d] = %d\n",
105 i, src, dst, tmat->
tp[i][src][dst]);
115 tmat_chk_1skip(
tmat_t * tmat, logmath_t *lmath)
119 for (i = 0; i < tmat->
n_tmat; i++) {
120 for (src = 0; src < tmat->
n_state; src++)
121 for (dst = src + 3; dst <= tmat->
n_state; dst++)
122 if (tmat->
tp[i][src][dst] < 255) {
123 E_ERROR(
"tmat[%d][%d][%d] = %d\n",
124 i, src, dst, tmat->
tp[i][src][dst]);
134 tmat_init(
char const *file_name, logmath_t *lmath, float64 tpfloor, int32 breport)
137 int32 n_src, n_dst, n_tmat;
139 int32 byteswap, chksum_present;
142 int32 i, j, k, tp_per_tmat;
143 char **argname, **argval;
148 E_INFO(
"Reading HMM transition probability matrices: %s\n",
154 if ((fp = fopen(file_name,
"rb")) == NULL)
155 E_FATAL_SYSTEM(
"Failed to open transition file '%s' for reading", file_name);
158 if (bio_readhdr(fp, &argname, &argval, &byteswap) < 0)
159 E_FATAL(
"Failed to read header from file '%s'\n", file_name);
163 for (i = 0; argname[i]; i++) {
164 if (strcmp(argname[i],
"version") == 0) {
165 if (strcmp(argval[i], TMAT_PARAM_VERSION) != 0)
166 E_WARN(
"Version mismatch(%s): %s, expecting %s\n",
167 file_name, argval[i], TMAT_PARAM_VERSION);
169 else if (strcmp(argname[i],
"chksum0") == 0) {
173 bio_hdrarg_free(argname, argval);
174 argname = argval = NULL;
179 if ((bio_fread(&n_tmat,
sizeof(int32), 1, fp, byteswap, &chksum)
181 || (bio_fread(&n_src,
sizeof(int32), 1, fp, byteswap, &chksum) !=
183 || (bio_fread(&n_dst,
sizeof(int32), 1, fp, byteswap, &chksum) !=
185 || (bio_fread(&i,
sizeof(int32), 1, fp, byteswap, &chksum) != 1)) {
186 E_FATAL(
"Failed to read header from '%s'\n", file_name);
188 if (n_tmat >= MAX_INT16)
189 E_FATAL(
"%s: Number of transition matrices (%d) exceeds limit (%d)\n", file_name,
193 if (n_dst != n_src + 1)
194 E_FATAL(
"%s: Unsupported transition matrix. Number of source states (%d) != number of target states (%d)-1\n", file_name,
198 if (i != t->
n_tmat * n_src * n_dst) {
200 (
"%s: Invalid transitions. Number of coefficients (%d) doesn't match expected array dimension: %d x %d x %d\n",
201 file_name, i, t->
n_tmat, n_src, n_dst);
205 t->
tp = ckd_calloc_3d(t->
n_tmat, n_src, n_dst,
sizeof(***t->
tp));
208 tp = ckd_calloc_2d(n_src, n_dst,
sizeof(**tp));
211 tp_per_tmat = n_src * n_dst;
212 for (i = 0; i < t->
n_tmat; i++) {
213 if (bio_fread(tp[0],
sizeof(float32), tp_per_tmat, fp,
214 byteswap, &chksum) != tp_per_tmat) {
215 E_FATAL(
"Failed to read transition matrix %d from '%s'\n", i, file_name);
219 for (j = 0; j < n_src; j++) {
220 if (vector_sum_norm(tp[j], n_dst) == 0.0)
221 E_WARN(
"Normalization failed for transition matrix %d from state %d\n",
223 vector_nz_floor(tp[j], n_dst, tpfloor);
224 vector_sum_norm(tp[j], n_dst);
227 for (k = 0; k < n_dst; k++) {
232 if (k >= j && k-j < 3 && tp[j][k] == 0.0f)
237 if (ltp > 255) ltp = 255;
238 t->
tp[i][j][k] = (uint8)ltp;
246 bio_verify_chksum(fp, byteswap, chksum);
248 if (fread(&tmp, 1, 1, fp) == 1)
249 E_ERROR(
"Non-empty file beyond end of data\n");
253 if (tmat_chk_uppertri(t, lmath) < 0)
254 E_FATAL(
"Tmat not upper triangular\n");
255 if (tmat_chk_1skip(t, lmath) < 0)
256 E_FATAL(
"Topology not Left-to-Right or Bakis\n");
264 E_INFO_NOFN(
"Initialization of tmat_t, report:\n");
265 E_INFO_NOFN(
"Read %d transition matrices of size %dx%d\n",
uint8 *** tp
The transition matrices; kept in the same scale as acoustic scores; tp[tmatid][from-state][to-state]...
int16 n_tmat
Number matrices.
void tmat_free(tmat_t *t)
RAH, add code to remove memory allocated by tmat_init.
Implementation of HMM base structure.
tmat_t * tmat_init(char const *tmatfile, logmath_t *lmath, float64 tpfloor, int32 breport)
Initialize transition matrix.
int16 n_state
Number source states in matrix (only the emitting states); Number destination states = n_state+1...
void tmat_dump(tmat_t *tmat, FILE *fp)
Dumping the transition matrix for debugging.
#define SENSCR_SHIFT
Shift count for senone scores.
Transition matrix data structure.
Transition matrix data structure.
void tmat_report(tmat_t *t)
Report the detail of the transition matrix structure.