1
/* -*- c-basic-offset: 4 -*- */
2
/* ====================================================================
3
* Copyright (c) 1996-2000 Carnegie Mellon University. All rights
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions
10
* 1. Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
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
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.
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.
34
* ====================================================================
37
/*********************************************************************
39
* File: s3gau_full_io.c
42
* Gaussian full covariance matrix file I/O
44
* Eric Thayer (eht@cs.cmu.edu)
45
* David Huggins-Daines (dhuggins@cs.cmu.edu)
46
*********************************************************************/
48
#include <s3/s3gau_io.h>
51
#include <sphinxbase/matrix.h>
52
#include <sphinxbase/ckd_alloc.h>
59
s3gau_read_maybe_full(const char *fn,
63
uint32 *out_n_density,
70
uint32 n_mgau, n_feat, n_density;
71
uint32 *veclen, maxveclen;
72
uint32 blk, i, j, k, l, r, n;
74
uint32 sv_chksum, ignore = 0;
79
fp = s3open(fn, "rb", &swap);
83
/* check version id */
84
ver = s3get_gvn_fattr("version");
86
if (strcmp(ver, GAU_FILE_VERSION) != 0) {
87
E_FATAL("Version mismatch for %s, file ver: %s != reader ver: %s\n",
88
fn, ver, GAU_FILE_VERSION);
92
E_FATAL("No version attribute for %s\n", fn);
95
/* if do_chk is non-NULL, there is a checksum after the data in the file */
96
do_chk = s3get_gvn_fattr("chksum0");
98
if (do_chk && !strcmp(do_chk, "no")) {
102
if (s3read(&n_mgau, sizeof(uint32), 1, fp, swap, &chksum) != 1) {
106
if (s3read(&n_feat, sizeof(uint32), 1, fp, swap, &chksum) != 1) {
110
if (s3read(&n_density, sizeof(uint32), 1, fp, swap, &chksum) != 1) {
114
veclen = ckd_calloc(n_feat, sizeof(uint32));
115
if (s3read(veclen, sizeof(uint32), n_feat, fp, swap, &chksum) != n_feat) {
119
if (s3read_1d((void **)&raw, sizeof(float32), &n, fp, swap, &chksum) != S3_SUCCESS) {
125
for (i = 0, blk = 0, maxveclen = 0; i < n_feat; i++) {
126
blk += veclen[i] * veclen[i];
127
if (veclen[i] > maxveclen) maxveclen = veclen[i];
129
if (n != n_mgau * n_density * blk) {
131
E_ERROR("Failed to read full covariance file %s (expected %d values, got %d)\n",
132
fn, n_mgau * n_density * blk, n);
136
o = (vector_t ****)ckd_calloc_4d(n_mgau, n_feat, n_density,
137
maxveclen, sizeof(vector_t));
139
for (i = 0, r = 0; i < n_mgau; i++) {
140
for (j = 0; j < n_feat; j++) {
141
for (k = 0; k < n_density; k++) {
142
for (l = 0; l < veclen[j]; l++) {
143
o[i][j][k][l] = &raw[r];
152
/* See if the checksum in the file matches that which
153
was computed from the read data */
155
if (s3read(&sv_chksum, sizeof(uint32), 1, fp, swap, &ignore) != 1) {
159
if (sv_chksum != chksum) {
160
E_FATAL("Checksum error; read corrupt data.\n");
166
*out_n_mgau = n_mgau;
167
*out_n_feat = n_feat;
168
*out_n_density = n_density;
169
*out_veclen = veclen;
173
E_INFO("Read %s [%ux%ux%u array of full matrices]\n",
174
fn, n_mgau, n_feat, n_density);
185
s3gau_read_full(const char *fn,
189
uint32 *out_n_density,
192
return s3gau_read_maybe_full(fn, out, out_n_mgau, out_n_feat,
193
out_n_density, out_veclen, TRUE);
197
s3gau_write_full(const char *fn,
198
const vector_t ****out,
202
const uint32 *veclen)
206
uint32 blk, i, ignore = 0;
209
s3add_fattr("version", GAU_FILE_VERSION, TRUE);
210
s3add_fattr("chksum0", "yes", TRUE);
212
fp = s3open(fn, "wb", NULL);
216
if (s3write(&n_mgau, sizeof(uint32), 1, fp, &chksum) != 1) {
220
if (s3write(&n_feat, sizeof(uint32), 1, fp, &chksum) != 1) {
224
if (s3write(&n_density, sizeof(uint32), 1, fp, &chksum) != 1) {
228
if (s3write(veclen, sizeof(uint32), n_feat, fp, &chksum) != n_feat) {
232
for (blk = 0, i = 0; i < n_feat; i++)
233
blk += veclen[i] * veclen[i];
235
if (s3write_1d(out[0][0][0][0], sizeof(float32),
236
n_mgau*n_density*blk, fp, &chksum) != S3_SUCCESS) {
240
if (s3write(&chksum, sizeof(uint32), 1, fp, &ignore) != 1) {
246
E_INFO("Wrote %s [%ux%ux%u array of full matrices]\n",
247
fn, n_mgau, n_feat, n_density);
257
s3gaucnt_read_full(const char *fn,
258
vector_t ****out_wt_mean,
259
vector_t *****out_wt_var,
261
float32 ****out_dnom,
264
uint32 *out_n_density,
267
uint32 rd_chksum = 0;
284
uint32 n, i, b_i, j, k, l, d1, d2, d3;
285
vector_t ***wt_mean = NULL;
286
vector_t ****wt_var = NULL;
288
fp = s3open(fn, "rb", &swap);
292
/* check version id */
293
ver = s3get_gvn_fattr("version");
295
if (strcmp(ver, GAUCNT_FILE_VERSION) != 0) {
296
E_FATAL("Version mismatch for %s, file ver: %s != reader ver: %s\n",
297
fn, ver, GAUCNT_FILE_VERSION);
301
E_FATAL("No version attribute for %s\n", fn);
304
/* if do_chk is non-NULL, there is a checksum after the data in the file */
305
do_chk = s3get_gvn_fattr("chksum0");
307
if (s3read((void *)&has_means, sizeof(uint32), 1, fp, swap, &rd_chksum) != 1) {
311
if (s3read((void *)&has_vars, sizeof(uint32), 1, fp, swap, &rd_chksum) != 1) {
315
if (s3read((void *)&pass2var, sizeof(uint32), 1, fp, swap, &rd_chksum) != 1) {
319
if (s3read((void *)&n_cb, sizeof(uint32), 1, fp, swap, &rd_chksum) != 1) {
323
if (s3read((void *)&n_density, sizeof(uint32), 1, fp, swap, &rd_chksum) != 1) {
327
if (s3read_1d((void **)&veclen, sizeof(uint32), &n_feat, fp, swap, &rd_chksum) != S3_SUCCESS) {
332
if (s3read_1d((void *)&buf, sizeof(float32), &n, fp, swap, &rd_chksum) != S3_SUCCESS) {
336
wt_mean = (vector_t ***)ckd_calloc_3d(n_cb, n_feat, n_density, sizeof(vector_t));
338
for (i = 0, b_i = 0; i < n_cb; i++) {
339
for (j = 0; j < n_feat; j++) {
340
for (k = 0; k < n_density; k++) {
341
wt_mean[i][j][k] = &buf[b_i];
350
uint32 blk, maxveclen;
352
for (i = 0, blk = 0, maxveclen = 0; i < n_feat; i++) {
354
if (veclen[i] > maxveclen) maxveclen = veclen[i];
357
if (s3read_1d((void *)&buf, sizeof(float32), &n, fp, swap, &rd_chksum) != S3_SUCCESS) {
360
assert(n == n_cb * n_density * blk * blk);
362
wt_var = (vector_t ****)ckd_calloc_4d(n_cb, n_feat, n_density,
363
maxveclen, sizeof(vector_t));
365
for (i = 0, b_i = 0; i < n_cb; i++) {
366
for (j = 0; j < n_feat; j++) {
367
for (k = 0; k < n_density; k++) {
368
for (l = 0; l < veclen[j]; l++) {
369
wt_var[i][j][k][l] = &buf[b_i];
378
if (s3read_3d((void ****)&dnom, sizeof(float32), &d1, &d2, &d3, fp, swap, &rd_chksum) != S3_SUCCESS) {
383
assert(d2 == n_feat);
384
assert(d3 == n_density);
387
/* See if the checksum in the file matches that which
388
was computed from the read data */
390
if (s3read(&sv_chksum, sizeof(uint32), 1, fp, swap, &ignore) != 1) {
395
if (sv_chksum != rd_chksum) {
396
E_FATAL("Checksum error; read corrupt data.\n");
402
*out_wt_mean = wt_mean;
403
*out_wt_var = wt_var;
404
*out_pass2var = pass2var;
407
*out_n_feat = n_feat;
408
*out_n_density = n_density;
409
*out_veclen = veclen;
411
E_INFO("Read %s%s%s%s [%ux%ux%u vector arrays]\n",
413
(has_means ? " with means" : ""),
414
(has_vars ? " with vars" : ""),
415
(has_vars && pass2var ? " (2pass)" : ""),
416
n_cb, n_feat, n_density);
422
s3gaucnt_write_full(const char *fn,
430
const uint32 *veclen)
435
uint32 n_elem, blk, j, has_means, has_vars;
438
s3add_fattr("version", GAUCNT_FILE_VERSION, TRUE);
439
s3add_fattr("chksum0", "yes", TRUE);
441
fp = s3open(fn, "wb", NULL);
450
if (s3write((void *)&has_means, sizeof(uint32), 1, fp, &chksum) != 1) {
458
if (s3write((void *)&has_vars, sizeof(uint32), 1, fp, &chksum) != 1) {
461
if (s3write((void *)&pass2var, sizeof(uint32), 1, fp, &chksum) != 1) {
464
if (s3write((void *)&n_cb, sizeof(uint32), 1, fp, &chksum) != 1) {
467
if (s3write((void *)&n_density, sizeof(uint32), 1, fp, &chksum) != 1) {
471
if (s3write_1d((void *)veclen, sizeof(uint32), n_feat, fp, &chksum) != S3_SUCCESS) {
475
for (j = 0, blk = 0; j < n_feat; j++)
478
n_elem = n_cb * n_density * blk;
481
band_nz_1d(wt_mean[0][0][0], n_elem, MIN_POS_FLOAT32);
483
if (s3write_1d((void *)wt_mean[0][0][0], sizeof(float32), n_elem, fp, &chksum) != S3_SUCCESS)
488
/* Don't floor full variances!!! */
489
if (s3write_1d((void *)wt_var[0][0][0][0], sizeof(float32),
490
n_elem * blk, fp, &chksum) != S3_SUCCESS)
494
/* floor all non-zero entries to this value to make sure
495
that results are compatible between machines */
496
floor_nz_3d(dnom, n_cb, n_feat, n_density, MIN_POS_FLOAT32);
498
if (s3write_3d((void ***)dnom, sizeof(float32),
499
n_cb, n_feat, n_density, fp, &chksum) != S3_SUCCESS) {
503
if (s3write(&chksum, sizeof(uint32), 1, fp, &ignore) != 1) {
511
E_INFO("Wrote %s%s%s%s [%ux%ux%u vector/matrix arrays]\n",
513
(has_means ? " with means" : ""),
514
(has_vars ? " with full vars" : ""),
515
(has_vars && pass2var ? " (2pass)" : ""),
516
n_cb, n_feat, n_density);
521
* Log record. Maintained by RCS.