File size: 10,505 Bytes
5610573 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* ====================================================================
* Copyright (c) 2015 Carnegie Mellon University. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* This work was supported in part by funding from the Defense Advanced
* Research Projects Agency and the National Science Foundation of the
* United States of America, and the CMU Sphinx Speech Consortium.
*
* THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
* NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ====================================================================
*
*/
#include <math.h>
#include <pocketsphinx/prim_type.h>
#include <pocketsphinx/err.h>
#include "util/ckd_alloc.h"
#include "util/byteorder.h"
#include "ngram_model_internal.h"
#include "lm_trie_quant.h"
/* FIXME: WTF, no, that's not how this works!!! */
#define FLOAT_INF (0x7f800000)
typedef struct bins_s {
float32 *begin;
const float32 *end;
} bins_t;
struct lm_trie_quant_s {
bins_t tables[NGRAM_MAX_ORDER - 1][2];
bins_t *longest;
float32 *values;
size_t nvalues;
uint8 prob_bits;
uint8 bo_bits;
uint32 prob_mask;
uint32 bo_mask;
};
static void
bins_create(bins_t * bins, uint8 bits, float32 *begin)
{
bins->begin = begin;
bins->end = bins->begin + (1ULL << bits);
}
static float32 *
lower_bound(float32 *first, const float32 *last, float32 val)
{
int count, step;
float32 *it;
count = last - first;
while (count > 0) {
it = first;
step = count / 2;
it += step;
if (*it < val) {
first = ++it;
count -= step + 1;
}
else {
count = step;
}
}
return first;
}
static uint64
bins_encode(bins_t * bins, float32 value)
{
float32 *above = lower_bound(bins->begin, bins->end, value);
if (above == bins->begin)
return 0;
if (above == bins->end)
return bins->end - bins->begin - 1;
return above - bins->begin - (value - *(above - 1) < *above - value);
}
static float32
bins_decode(bins_t * bins, size_t off)
{
return bins->begin[off];
}
static size_t
quant_size(int order)
{
int prob_bits = 16;
int bo_bits = 16;
size_t longest_table = (1U << prob_bits);
size_t middle_table = (1U << bo_bits) + longest_table;
/* unigrams are currently not quantized so no need for a table. */
return (order - 2) * middle_table + longest_table;
}
lm_trie_quant_t *
lm_trie_quant_create(int order)
{
float32 *start;
int i;
lm_trie_quant_t *quant =
(lm_trie_quant_t *) ckd_calloc(1, sizeof(*quant));
quant->nvalues = quant_size(order);
quant->values =
(float32 *) ckd_calloc(quant->nvalues, sizeof(*quant->values));
quant->prob_bits = 16;
quant->bo_bits = 16;
quant->prob_mask = (1U << quant->prob_bits) - 1;
quant->bo_mask = (1U << quant->bo_bits) - 1;
start = (float32 *) (quant->values);
for (i = 0; i < order - 2; i++) {
bins_create(&quant->tables[i][0], quant->prob_bits, start);
start += (1ULL << quant->prob_bits);
bins_create(&quant->tables[i][1], quant->bo_bits, start);
start += (1ULL << quant->bo_bits);
}
bins_create(&quant->tables[order - 2][0], quant->prob_bits, start);
quant->longest = &quant->tables[order - 2][0];
return quant;
}
lm_trie_quant_t *
lm_trie_quant_read_bin(FILE * fp, int order)
{
int dummy;
lm_trie_quant_t *quant;
fread(&dummy, sizeof(dummy), 1, fp);
quant = lm_trie_quant_create(order);
if (fread(quant->values, sizeof(*quant->values),
quant->nvalues, fp) != quant->nvalues) {
E_ERROR("Failed to read %d quantization values\n",
quant->nvalues);
lm_trie_quant_free(quant);
return NULL;
}
if (SWAP_LM_TRIE) {
size_t i;
for (i = 0; i < quant->nvalues; ++i)
SWAP_FLOAT32(&quant->values[i]);
}
return quant;
}
void
lm_trie_quant_write_bin(lm_trie_quant_t * quant, FILE * fp)
{
/* Before it was quantization type */
int dummy = 1;
fwrite(&dummy, sizeof(dummy), 1, fp);
if (SWAP_LM_TRIE) {
size_t i;
for (i = 0; i < quant->nvalues; ++i) {
float32 value = quant->values[i];
SWAP_FLOAT32(&value);
if (fwrite(&value, sizeof(value), 1, fp) != 1) {
E_ERROR("Failed to write quantization value\n");
return; /* WTF, FIXME */
}
}
}
else {
if (fwrite(quant->values, sizeof(*quant->values),
quant->nvalues, fp) != quant->nvalues) {
E_ERROR("Failed to write %d quantization values\n",
quant->nvalues);
}
}
}
void
lm_trie_quant_free(lm_trie_quant_t * quant)
{
if (quant->values)
ckd_free(quant->values);
ckd_free(quant);
}
uint8
lm_trie_quant_msize(lm_trie_quant_t * quant)
{
(void)quant;
return 32;
}
uint8
lm_trie_quant_lsize(lm_trie_quant_t * quant)
{
(void)quant;
return 16;
}
static int
weights_comparator(const void *a, const void *b)
{
return (int) (*(float32 *) a - *(float32 *) b);
}
static void
make_bins(float32 *values, uint32 values_num, float32 *centers, uint32 bins)
{
float32 *finish, *start;
uint32 i;
qsort(values, values_num, sizeof(*values), &weights_comparator);
start = values;
for (i = 0; i < bins; i++, centers++, start = finish) {
finish = values + (size_t) ((uint64) values_num * (i + 1) / bins);
if (finish == start) {
/* zero length bucket. */
*centers = i ? *(centers - 1) : -FLOAT_INF;
}
else {
float32 sum = 0.0f;
float32 *ptr;
for (ptr = start; ptr != finish; ptr++) {
sum += *ptr;
}
*centers = sum / (float32) (finish - start);
}
}
}
void
lm_trie_quant_train(lm_trie_quant_t * quant, int order, uint32 counts,
ngram_raw_t * raw_ngrams)
{
float32 *probs;
float32 *backoffs;
float32 *centers;
uint32 backoff_num;
uint32 prob_num;
ngram_raw_t *raw_ngrams_end;
probs = (float32 *) ckd_calloc(counts, sizeof(*probs));
backoffs = (float32 *) ckd_calloc(counts, sizeof(*backoffs));
raw_ngrams_end = raw_ngrams + counts;
for (backoff_num = 0, prob_num = 0; raw_ngrams != raw_ngrams_end;
raw_ngrams++) {
probs[prob_num++] = raw_ngrams->prob;
backoffs[backoff_num++] = raw_ngrams->backoff;
}
make_bins(probs, prob_num, quant->tables[order - 2][0].begin,
1ULL << quant->prob_bits);
centers = quant->tables[order - 2][1].begin;
make_bins(backoffs, backoff_num, centers, (1ULL << quant->bo_bits));
ckd_free(probs);
ckd_free(backoffs);
}
void
lm_trie_quant_train_prob(lm_trie_quant_t * quant, int order, uint32 counts,
ngram_raw_t * raw_ngrams)
{
float32 *probs;
uint32 prob_num;
ngram_raw_t *raw_ngrams_end;
probs = (float32 *) ckd_calloc(counts, sizeof(*probs));
raw_ngrams_end = raw_ngrams + counts;
for (prob_num = 0; raw_ngrams != raw_ngrams_end; raw_ngrams++) {
probs[prob_num++] = raw_ngrams->prob;
}
make_bins(probs, prob_num, quant->tables[order - 2][0].begin,
1ULL << quant->prob_bits);
ckd_free(probs);
}
void
lm_trie_quant_mwrite(lm_trie_quant_t * quant, bitarr_address_t address,
int order_minus_2, float32 prob, float32 backoff)
{
bitarr_write_int57(address, quant->prob_bits + quant->bo_bits,
(uint64) ((bins_encode
(&quant->tables[order_minus_2][0],
prob) << quant->
bo_bits) | bins_encode(&quant->
tables
[order_minus_2]
[1],
backoff)));
}
void
lm_trie_quant_lwrite(lm_trie_quant_t * quant, bitarr_address_t address,
float32 prob)
{
bitarr_write_int25(address, quant->prob_bits,
(uint32) bins_encode(quant->longest, prob));
}
float32
lm_trie_quant_mboread(lm_trie_quant_t * quant, bitarr_address_t address,
int order_minus_2)
{
return bins_decode(&quant->tables[order_minus_2][1],
bitarr_read_int25(address, quant->bo_bits,
quant->bo_mask));
}
float32
lm_trie_quant_mpread(lm_trie_quant_t * quant, bitarr_address_t address,
int order_minus_2)
{
address.offset += quant->bo_bits;
return bins_decode(&quant->tables[order_minus_2][0],
bitarr_read_int25(address, quant->prob_bits,
quant->prob_mask));
}
float32
lm_trie_quant_lpread(lm_trie_quant_t * quant, bitarr_address_t address)
{
return bins_decode(quant->longest,
bitarr_read_int25(address, quant->prob_bits,
quant->prob_mask));
}
|