SphinxBase 5prealpha
lm_trie.c
1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/* ====================================================================
3 * Copyright (c) 2015 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
38#include <string.h>
39#include <stdio.h>
40#include <assert.h>
41
44#include <sphinxbase/err.h>
45#include <sphinxbase/priority_queue.h>
46
47#include "lm_trie.h"
48#include "lm_trie_quant.h"
49
50static void lm_trie_alloc_ngram(lm_trie_t * trie, uint32 * counts, int order);
51
52static uint32
53base_size(uint32 entries, uint32 max_vocab, uint8 remaining_bits)
54{
55 uint8 total_bits = bitarr_required_bits(max_vocab) + remaining_bits;
56 /* Extra entry for next pointer at the end.
57 * +7 then / 8 to round up bits and convert to bytes
58 * +sizeof(uint64) so that ReadInt57 etc don't go segfault.
59 * Note that this waste is O(order), not O(number of ngrams).*/
60 return ((1 + entries) * total_bits + 7) / 8 + sizeof(uint64);
61}
62
63uint32
64middle_size(uint8 quant_bits, uint32 entries, uint32 max_vocab,
65 uint32 max_ptr)
66{
67 return base_size(entries, max_vocab,
68 quant_bits + bitarr_required_bits(max_ptr));
69}
70
71uint32
72longest_size(uint8 quant_bits, uint32 entries, uint32 max_vocab)
73{
74 return base_size(entries, max_vocab, quant_bits);
75}
76
77static void
78base_init(base_t * base, void *base_mem, uint32 max_vocab,
79 uint8 remaining_bits)
80{
81 base->word_bits = bitarr_required_bits(max_vocab);
82 base->word_mask = (1U << base->word_bits) - 1U;
83 if (base->word_bits > 25)
85 ("Sorry, word indices more than %d are not implemented. Edit util/bit_packing.hh and fix the bit packing functions\n",
86 (1U << 25));
87 base->total_bits = base->word_bits + remaining_bits;
88
89 base->base = (uint8 *) base_mem;
90 base->insert_index = 0;
91 base->max_vocab = max_vocab;
92}
93
94void
95middle_init(middle_t * middle, void *base_mem, uint8 quant_bits,
96 uint32 entries, uint32 max_vocab, uint32 max_next,
97 void *next_source)
98{
99 middle->quant_bits = quant_bits;
100 bitarr_mask_from_max(&middle->next_mask, max_next);
101 middle->next_source = next_source;
102 if (entries + 1 >= (1U << 25) || (max_next >= (1U << 25)))
103 E_ERROR
104 ("Sorry, this does not support more than %d n-grams of a particular order. Edit util/bit_packing.hh and fix the bit packing functions\n",
105 (1U << 25));
106 base_init(&middle->base, base_mem, max_vocab,
107 quant_bits + middle->next_mask.bits);
108}
109
110void
111longest_init(longest_t * longest, void *base_mem, uint8 quant_bits,
112 uint32 max_vocab)
113{
114 base_init(&longest->base, base_mem, max_vocab, quant_bits);
115}
116
117static bitarr_address_t
118middle_insert(middle_t * middle, uint32 word, int order, int max_order)
119{
120 uint32 at_pointer;
121 uint32 next;
122 bitarr_address_t address;
123 assert(word <= middle->base.word_mask);
124 address.base = middle->base.base;
125 address.offset = middle->base.insert_index * middle->base.total_bits;
126 bitarr_write_int25(address, middle->base.word_bits, word);
127 address.offset += middle->base.word_bits;
128 at_pointer = address.offset;
129 address.offset += middle->quant_bits;
130 if (order == max_order - 1) {
131 next = ((longest_t *) middle->next_source)->base.insert_index;
132 }
133 else {
134 next = ((middle_t *) middle->next_source)->base.insert_index;
135 }
136
137 bitarr_write_int25(address, middle->next_mask.bits, next);
138 middle->base.insert_index++;
139 address.offset = at_pointer;
140 return address;
141}
142
143static bitarr_address_t
144longest_insert(longest_t * longest, uint32 index)
145{
146 bitarr_address_t address;
147 assert(index <= longest->base.word_mask);
148 address.base = longest->base.base;
149 address.offset = longest->base.insert_index * longest->base.total_bits;
150 bitarr_write_int25(address, longest->base.word_bits, index);
151 address.offset += longest->base.word_bits;
152 longest->base.insert_index++;
153 return address;
154}
155
156static void
157middle_finish_loading(middle_t * middle, uint32 next_end)
158{
159 bitarr_address_t address;
160 address.base = middle->base.base;
161 address.offset =
162 (middle->base.insert_index + 1) * middle->base.total_bits -
163 middle->next_mask.bits;
164 bitarr_write_int25(address, middle->next_mask.bits, next_end);
165}
166
167static uint32
168unigram_next(lm_trie_t * trie, int order)
169{
170 return order ==
171 2 ? trie->longest->base.insert_index : trie->middle_begin->base.
172 insert_index;
173}
174
175void
176lm_trie_fix_counts(ngram_raw_t ** raw_ngrams, uint32 * counts,
177 uint32 * fixed_counts, int order)
178{
179 priority_queue_t *ngrams =
180 priority_queue_create(order - 1, &ngram_ord_comparator);
181 uint32 raw_ngram_ptrs[NGRAM_MAX_ORDER - 1];
182 uint32 words[NGRAM_MAX_ORDER];
183 int i;
184
185 memset(words, -1, sizeof(words));
186 memcpy(fixed_counts, counts, order * sizeof(*fixed_counts));
187 for (i = 2; i <= order; i++) {
188 ngram_raw_t *tmp_ngram;
189
190 if (counts[i - 1] <= 0)
191 continue;
192
193 raw_ngram_ptrs[i - 2] = 0;
194
195 tmp_ngram =
196 (ngram_raw_t *) ckd_calloc(1, sizeof(*tmp_ngram));
197 *tmp_ngram = raw_ngrams[i - 2][0];
198 tmp_ngram->order = i;
199 priority_queue_add(ngrams, tmp_ngram);
200 }
201
202 for (;;) {
203 int32 to_increment = TRUE;
204 ngram_raw_t *top;
205 if (priority_queue_size(ngrams) == 0) {
206 break;
207 }
208 top = (ngram_raw_t *) priority_queue_poll(ngrams);
209 if (top->order == 2) {
210 memcpy(words, top->words, 2 * sizeof(*words));
211 }
212 else {
213 for (i = 0; i < top->order - 1; i++) {
214 if (words[i] != top->words[i]) {
215 int num;
216 num = (i == 0) ? 1 : i;
217 memcpy(words, top->words,
218 (num + 1) * sizeof(*words));
219 fixed_counts[num]++;
220 to_increment = FALSE;
221 break;
222 }
223 }
224 words[top->order - 1] = top->words[top->order - 1];
225 }
226 if (to_increment) {
227 raw_ngram_ptrs[top->order - 2]++;
228 }
229 if (raw_ngram_ptrs[top->order - 2] < counts[top->order - 1]) {
230 *top = raw_ngrams[top->order - 2][raw_ngram_ptrs[top->order - 2]];
231 priority_queue_add(ngrams, top);
232 }
233 else {
234 ckd_free(top);
235 }
236 }
237
238 assert(priority_queue_size(ngrams) == 0);
239 priority_queue_free(ngrams, NULL);
240}
241
242
243static void
244recursive_insert(lm_trie_t * trie, ngram_raw_t ** raw_ngrams,
245 uint32 * counts, int order)
246{
247 uint32 unigram_idx = 0;
248 uint32 *words;
249 float *probs;
250 const uint32 unigram_count = (uint32) counts[0];
251 priority_queue_t *ngrams =
252 priority_queue_create(order, &ngram_ord_comparator);
253 ngram_raw_t *ngram;
254 uint32 *raw_ngrams_ptr;
255 int i;
256
257 words = (uint32 *) ckd_calloc(order, sizeof(*words));
258 probs = (float *) ckd_calloc(order - 1, sizeof(*probs));
259 ngram = (ngram_raw_t *) ckd_calloc(1, sizeof(*ngram));
260 ngram->order = 1;
261 ngram->words = &unigram_idx;
262 priority_queue_add(ngrams, ngram);
263 raw_ngrams_ptr =
264 (uint32 *) ckd_calloc(order - 1, sizeof(*raw_ngrams_ptr));
265 for (i = 2; i <= order; ++i) {
266 ngram_raw_t *tmp_ngram;
267
268 if (counts[i - 1] <= 0)
269 continue;
270
271 raw_ngrams_ptr[i - 2] = 0;
272 tmp_ngram =
273 (ngram_raw_t *) ckd_calloc(1, sizeof(*tmp_ngram));
274 *tmp_ngram = raw_ngrams[i - 2][0];
275 tmp_ngram->order = i;
276
277 priority_queue_add(ngrams, tmp_ngram);
278 }
279
280 for (;;) {
281 ngram_raw_t *top =
282 (ngram_raw_t *) priority_queue_poll(ngrams);
283
284 if (top->order == 1) {
285 trie->unigrams[unigram_idx].next = unigram_next(trie, order);
286 words[0] = unigram_idx;
287 probs[0] = trie->unigrams[unigram_idx].prob;
288 if (++unigram_idx == unigram_count + 1) {
289 ckd_free(top);
290 break;
291 }
292 priority_queue_add(ngrams, top);
293 }
294 else {
295 for (i = 0; i < top->order - 1; i++) {
296 if (words[i] != top->words[i]) {
297 /* need to insert dummy suffixes to make ngram of higher order reachable */
298 int j;
299 assert(i > 0); /* unigrams are not pruned without removing ngrams that contains them */
300 for (j = i; j < top->order - 1; j++) {
301 middle_t *middle = &trie->middle_begin[j - 1];
302 bitarr_address_t address =
303 middle_insert(middle, top->words[j],
304 j + 1, order);
305 /* calculate prob for blank */
306 float calc_prob =
307 probs[j - 1] +
308 trie->unigrams[top->words[j]].bo;
309 probs[j] = calc_prob;
310 lm_trie_quant_mwrite(trie->quant, address, j - 1,
311 calc_prob, 0.0f);
312 }
313 }
314 }
315 memcpy(words, top->words,
316 top->order * sizeof(*words));
317 if (top->order == order) {
318 bitarr_address_t address =
319 longest_insert(trie->longest,
320 top->words[top->order - 1]);
321 lm_trie_quant_lwrite(trie->quant, address, top->prob);
322 }
323 else {
324 middle_t *middle = &trie->middle_begin[top->order - 2];
325 bitarr_address_t address =
326 middle_insert(middle,
327 top->words[top->order - 1],
328 top->order, order);
329 /* write prob and backoff */
330 probs[top->order - 1] = top->prob;
331 lm_trie_quant_mwrite(trie->quant, address, top->order - 2,
332 top->prob, top->backoff);
333 }
334 raw_ngrams_ptr[top->order - 2]++;
335 if (raw_ngrams_ptr[top->order - 2] < counts[top->order - 1]) {
336 *top = raw_ngrams[top->order -
337 2][raw_ngrams_ptr[top->order - 2]];
338
339 priority_queue_add(ngrams, top);
340 }
341 else {
342 ckd_free(top);
343 }
344 }
345 }
346 assert(priority_queue_size(ngrams) == 0);
347 priority_queue_free(ngrams, NULL);
348 ckd_free(raw_ngrams_ptr);
349 ckd_free(words);
350 ckd_free(probs);
351}
352
353static lm_trie_t *
354lm_trie_init(uint32 unigram_count)
355{
356 lm_trie_t *trie;
357
358 trie = (lm_trie_t *) ckd_calloc(1, sizeof(*trie));
359 memset(trie->hist_cache, -1, sizeof(trie->hist_cache)); /* prepare request history */
360 memset(trie->backoff_cache, 0, sizeof(trie->backoff_cache));
361 trie->unigrams =
362 (unigram_t *) ckd_calloc((unigram_count + 1),
363 sizeof(*trie->unigrams));
364 trie->ngram_mem = NULL;
365 return trie;
366}
367
368lm_trie_t *
369lm_trie_create(uint32 unigram_count, int order)
370{
371 lm_trie_t *trie = lm_trie_init(unigram_count);
372 trie->quant =
373 (order > 1) ? lm_trie_quant_create(order) : 0;
374 return trie;
375}
376
377lm_trie_t *
378lm_trie_read_bin(uint32 * counts, int order, FILE * fp)
379{
380 lm_trie_t *trie = lm_trie_init(counts[0]);
381 trie->quant = (order > 1) ? lm_trie_quant_read_bin(fp, order) : NULL;
382 fread(trie->unigrams, sizeof(*trie->unigrams), (counts[0] + 1), fp);
383 if (order > 1) {
384 lm_trie_alloc_ngram(trie, counts, order);
385 fread(trie->ngram_mem, 1, trie->ngram_mem_size, fp);
386 }
387 return trie;
388}
389
390void
391lm_trie_write_bin(lm_trie_t * trie, uint32 unigram_count, FILE * fp)
392{
393
394 if (trie->quant)
395 lm_trie_quant_write_bin(trie->quant, fp);
396 fwrite(trie->unigrams, sizeof(*trie->unigrams), (unigram_count + 1),
397 fp);
398 if (trie->ngram_mem)
399 fwrite(trie->ngram_mem, 1, trie->ngram_mem_size, fp);
400}
401
402void
403lm_trie_free(lm_trie_t * trie)
404{
405 if (trie->ngram_mem) {
406 ckd_free(trie->ngram_mem);
407 ckd_free(trie->middle_begin);
408 ckd_free(trie->longest);
409 }
410 if (trie->quant)
411 lm_trie_quant_free(trie->quant);
412 ckd_free(trie->unigrams);
413 ckd_free(trie);
414}
415
416static void
417lm_trie_alloc_ngram(lm_trie_t * trie, uint32 * counts, int order)
418{
419 int i;
420 uint8 *mem_ptr;
421 uint8 **middle_starts;
422
423 trie->ngram_mem_size = 0;
424 for (i = 1; i < order - 1; i++) {
425 trie->ngram_mem_size +=
426 middle_size(lm_trie_quant_msize(trie->quant), counts[i],
427 counts[0], counts[i + 1]);
428 }
429 trie->ngram_mem_size +=
430 longest_size(lm_trie_quant_lsize(trie->quant), counts[order - 1],
431 counts[0]);
432 trie->ngram_mem =
433 (uint8 *) ckd_calloc(trie->ngram_mem_size,
434 sizeof(*trie->ngram_mem));
435 mem_ptr = trie->ngram_mem;
436 trie->middle_begin =
437 (middle_t *) ckd_calloc(order - 2, sizeof(*trie->middle_begin));
438 trie->middle_end = trie->middle_begin + (order - 2);
439 middle_starts =
440 (uint8 **) ckd_calloc(order - 2, sizeof(*middle_starts));
441 for (i = 2; i < order; i++) {
442 middle_starts[i - 2] = mem_ptr;
443 mem_ptr +=
444 middle_size(lm_trie_quant_msize(trie->quant), counts[i - 1],
445 counts[0], counts[i]);
446 }
447 trie->longest = (longest_t *) ckd_calloc(1, sizeof(*trie->longest));
448 /* Crazy backwards thing so we initialize using pointers to ones that have already been initialized */
449 for (i = order - 1; i >= 2; --i) {
450 middle_t *middle_ptr = &trie->middle_begin[i - 2];
451 middle_init(middle_ptr, middle_starts[i - 2],
452 lm_trie_quant_msize(trie->quant), counts[i - 1],
453 counts[0], counts[i],
454 (i ==
455 order -
456 1) ? (void *) trie->longest : (void *) &trie->
457 middle_begin[i - 1]);
458 }
459 ckd_free(middle_starts);
460 longest_init(trie->longest, mem_ptr, lm_trie_quant_lsize(trie->quant),
461 counts[0]);
462}
463
464void
465lm_trie_build(lm_trie_t * trie, ngram_raw_t ** raw_ngrams, uint32 * counts, uint32 *out_counts,
466 int order)
467{
468 int i;
469
470 lm_trie_fix_counts(raw_ngrams, counts, out_counts, order);
471 lm_trie_alloc_ngram(trie, out_counts, order);
472
473 if (order > 1)
474 E_INFO("Training quantizer\n");
475 for (i = 2; i < order; i++) {
476 lm_trie_quant_train(trie->quant, i, counts[i - 1],
477 raw_ngrams[i - 2]);
478 }
479 lm_trie_quant_train_prob(trie->quant, order, counts[order - 1],
480 raw_ngrams[order - 2]);
481
482 E_INFO("Building LM trie\n");
483 recursive_insert(trie, raw_ngrams, counts, order);
484 /* Set ending offsets so the last entry will be sized properly */
485 /* Last entry for unigrams was already set. */
486 if (trie->middle_begin != trie->middle_end) {
487 middle_t *middle_ptr;
488 for (middle_ptr = trie->middle_begin;
489 middle_ptr != trie->middle_end - 1; ++middle_ptr) {
490 middle_t *next_middle_ptr = middle_ptr + 1;
491 middle_finish_loading(middle_ptr,
492 next_middle_ptr->base.insert_index);
493 }
494 middle_ptr = trie->middle_end - 1;
495 middle_finish_loading(middle_ptr,
496 trie->longest->base.insert_index);
497 }
498}
499
500unigram_t *
501unigram_find(unigram_t * u, uint32 word, node_range_t * next)
502{
503 unigram_t *ptr = &u[word];
504 next->begin = ptr->next;
505 next->end = (ptr + 1)->next;
506 return ptr;
507}
508
509static size_t
510calc_pivot(uint32 off, uint32 range, uint32 width)
511{
512 return (size_t) ((off * width) / (range + 1));
513}
514
515static uint8
516uniform_find(void *base, uint8 total_bits, uint8 key_bits, uint32 key_mask,
517 uint32 before_it, uint32 before_v,
518 uint32 after_it, uint32 after_v, uint32 key, uint32 * out)
519{
520 bitarr_address_t address;
521 address.base = base;
522
523 /* If we look for unigram added later */
524 if (key > after_v)
525 return FALSE;
526
527 while (after_it - before_it > 1) {
528 uint32 mid;
529 uint32 pivot =
530 before_it + (1 +
531 calc_pivot(key - before_v, after_v - before_v,
532 after_it - before_it - 1));
533 /* access by pivot */
534 address.offset = pivot * (uint32) total_bits;
535 mid = bitarr_read_int25(address, key_bits, key_mask);
536 if (mid < key) {
537 before_it = pivot;
538 before_v = mid;
539 }
540 else if (mid > key) {
541 after_it = pivot;
542 after_v = mid;
543 }
544 else {
545 *out = pivot;
546 return TRUE;
547 }
548 }
549 return FALSE;
550}
551
552static bitarr_address_t
553middle_find(middle_t * middle, uint32 word, node_range_t * range)
554{
555 uint32 at_pointer;
556 bitarr_address_t address;
557
558 /* finding BitPacked with uniform find */
559 if (!uniform_find
560 ((void *) middle->base.base, middle->base.total_bits,
561 middle->base.word_bits, middle->base.word_mask, range->begin - 1,
562 0, range->end, middle->base.max_vocab, word, &at_pointer)) {
563 address.base = NULL;
564 address.offset = 0;
565 return address;
566 }
567
568 address.base = middle->base.base;
569 at_pointer *= middle->base.total_bits;
570 at_pointer += middle->base.word_bits;
571 address.offset = at_pointer + middle->quant_bits;
572 range->begin =
573 bitarr_read_int25(address, middle->next_mask.bits,
574 middle->next_mask.mask);
575 address.offset += middle->base.total_bits;
576 range->end =
577 bitarr_read_int25(address, middle->next_mask.bits,
578 middle->next_mask.mask);
579 address.offset = at_pointer;
580
581 return address;
582}
583
584static bitarr_address_t
585longest_find(longest_t * longest, uint32 word, node_range_t * range)
586{
587 uint32 at_pointer;
588 bitarr_address_t address;
589
590 /* finding BitPacked with uniform find */
591 if (!uniform_find
592 ((void *) longest->base.base, longest->base.total_bits,
593 longest->base.word_bits, longest->base.word_mask,
594 range->begin - 1, 0, range->end, longest->base.max_vocab, word,
595 &at_pointer)) {
596 address.base = NULL;
597 address.offset = 0;
598 return address;
599 }
600 address.base = longest->base.base;
601 address.offset =
602 at_pointer * longest->base.total_bits + longest->base.word_bits;
603 return address;
604}
605
606static float
607get_available_prob(lm_trie_t * trie, int32 wid, int32 * hist,
608 int max_order, int32 n_hist, int32 * n_used)
609{
610 float prob;
611 node_range_t node;
612 bitarr_address_t address;
613 int order_minus_2;
614 uint8 independent_left;
615 int32 *hist_iter, *hist_end;
616
617 *n_used = 1;
618 prob = unigram_find(trie->unigrams, wid, &node)->prob;
619 if (n_hist == 0) {
620 return prob;
621 }
622
623 /* find ngrams of higher order if any */
624 order_minus_2 = 0;
625 independent_left = (node.begin == node.end);
626 hist_iter = hist;
627 hist_end = hist + n_hist;
628 for (;; order_minus_2++, hist_iter++) {
629 if (hist_iter == hist_end)
630 return prob;
631 if (independent_left)
632 return prob;
633 if (order_minus_2 == max_order - 2)
634 break;
635
636 address =
637 middle_find(&trie->middle_begin[order_minus_2], *hist_iter,
638 &node);
639 independent_left = (address.base == NULL)
640 || (node.begin == node.end);
641
642 /* didn't find entry */
643 if (address.base == NULL)
644 return prob;
645 prob = lm_trie_quant_mpread(trie->quant, address, order_minus_2);
646 *n_used = order_minus_2 + 2;
647 }
648
649 address = longest_find(trie->longest, *hist_iter, &node);
650 if (address.base != NULL) {
651 prob = lm_trie_quant_lpread(trie->quant, address);
652 *n_used = max_order;
653 }
654 return prob;
655}
656
657static float
658get_available_backoff(lm_trie_t * trie, int32 start, int32 * hist,
659 int32 n_hist)
660{
661 float backoff = 0.0f;
662 int order_minus_2;
663 int32 *hist_iter;
664 node_range_t node;
665 unigram_t *first_hist = unigram_find(trie->unigrams, hist[0], &node);
666 if (start <= 1) {
667 backoff += first_hist->bo;
668 start = 2;
669 }
670 order_minus_2 = start - 2;
671 for (hist_iter = hist + start - 1; hist_iter < hist + n_hist;
672 hist_iter++, order_minus_2++) {
673 bitarr_address_t address =
674 middle_find(&trie->middle_begin[order_minus_2], *hist_iter,
675 &node);
676 if (address.base == NULL)
677 break;
678 backoff +=
679 lm_trie_quant_mboread(trie->quant, address, order_minus_2);
680 }
681 return backoff;
682}
683
684static float
685lm_trie_nobo_score(lm_trie_t * trie, int32 wid, int32 * hist,
686 int max_order, int32 n_hist, int32 * n_used)
687{
688 float prob =
689 get_available_prob(trie, wid, hist, max_order, n_hist, n_used);
690 if (n_hist < *n_used)
691 return prob;
692 return prob + get_available_backoff(trie, *n_used, hist, n_hist);
693}
694
695static float
696lm_trie_hist_score(lm_trie_t * trie, int32 wid, int32 * hist, int32 n_hist,
697 int32 * n_used)
698{
699 float prob;
700 int i, j;
701 node_range_t node;
702 bitarr_address_t address;
703
704 *n_used = 1;
705 prob = unigram_find(trie->unigrams, wid, &node)->prob;
706 if (n_hist == 0)
707 return prob;
708 for (i = 0; i < n_hist - 1; i++) {
709 address = middle_find(&trie->middle_begin[i], hist[i], &node);
710 if (address.base == NULL) {
711 for (j = i; j < n_hist; j++) {
712 prob += trie->backoff_cache[j];
713 }
714 return prob;
715 }
716 else {
717 (*n_used)++;
718 prob = lm_trie_quant_mpread(trie->quant, address, i);
719 }
720 }
721 address = longest_find(trie->longest, hist[n_hist - 1], &node);
722 if (address.base == NULL) {
723 return prob + trie->backoff_cache[n_hist - 1];
724 }
725 else {
726 (*n_used)++;
727 return lm_trie_quant_lpread(trie->quant, address);
728 }
729}
730
731static uint8
732history_matches(int32 * hist, int32 * prev_hist, int32 n_hist)
733{
734 int i;
735 for (i = 0; i < n_hist; i++) {
736 if (hist[i] != prev_hist[i]) {
737 return FALSE;
738 }
739 }
740 return TRUE;
741}
742
743static void
744update_backoff(lm_trie_t * trie, int32 * hist, int32 n_hist)
745{
746 int i;
747 node_range_t node;
748 bitarr_address_t address;
749
750 memset(trie->backoff_cache, 0, sizeof(trie->backoff_cache));
751 trie->backoff_cache[0] = unigram_find(trie->unigrams, hist[0], &node)->bo;
752 for (i = 1; i < n_hist; i++) {
753 address = middle_find(&trie->middle_begin[i - 1], hist[i], &node);
754 if (address.base == NULL) {
755 break;
756 }
757 trie->backoff_cache[i] =
758 lm_trie_quant_mboread(trie->quant, address, i - 1);
759 }
760 memcpy(trie->hist_cache, hist, n_hist * sizeof(*hist));
761}
762
763float
764lm_trie_score(lm_trie_t * trie, int order, int32 wid, int32 * hist,
765 int32 n_hist, int32 * n_used)
766{
767 if (n_hist < order - 1) {
768 return lm_trie_nobo_score(trie, wid, hist, order, n_hist, n_used);
769 }
770 else {
771 assert(n_hist == order - 1);
772 if (!history_matches(hist, (int32 *) trie->hist_cache, n_hist)) {
773 update_backoff(trie, hist, n_hist);
774 }
775 return lm_trie_hist_score(trie, wid, hist, n_hist, n_used);
776 }
777}
778
779void
780lm_trie_fill_raw_ngram(lm_trie_t * trie,
781 ngram_raw_t * raw_ngrams, uint32 * raw_ngram_idx,
782 uint32 * counts, node_range_t range, uint32 * hist,
783 int n_hist, int order, int max_order)
784{
785 if (n_hist > 0 && range.begin == range.end) {
786 return;
787 }
788 if (n_hist == 0) {
789 uint32 i;
790 for (i = 0; i < counts[0]; i++) {
791 node_range_t node;
792 unigram_find(trie->unigrams, i, &node);
793 hist[0] = i;
794 lm_trie_fill_raw_ngram(trie, raw_ngrams, raw_ngram_idx, counts,
795 node, hist, 1, order, max_order);
796 }
797 }
798 else if (n_hist < order - 1) {
799 uint32 ptr;
800 node_range_t node;
801 bitarr_address_t address;
802 uint32 new_word;
803 middle_t *middle = &trie->middle_begin[n_hist - 1];
804 for (ptr = range.begin; ptr < range.end; ptr++) {
805 address.base = middle->base.base;
806 address.offset = ptr * middle->base.total_bits;
807 new_word =
808 bitarr_read_int25(address, middle->base.word_bits,
809 middle->base.word_mask);
810 hist[n_hist] = new_word;
811 address.offset += middle->base.word_bits + middle->quant_bits;
812 node.begin =
813 bitarr_read_int25(address, middle->next_mask.bits,
814 middle->next_mask.mask);
815 address.offset =
816 (ptr + 1) * middle->base.total_bits +
817 middle->base.word_bits + middle->quant_bits;
818 node.end =
819 bitarr_read_int25(address, middle->next_mask.bits,
820 middle->next_mask.mask);
821 lm_trie_fill_raw_ngram(trie, raw_ngrams, raw_ngram_idx, counts,
822 node, hist, n_hist + 1, order, max_order);
823 }
824 }
825 else {
826 bitarr_address_t address;
827 uint32 ptr;
828 float prob, backoff;
829 int i;
830 assert(n_hist == order - 1);
831 for (ptr = range.begin; ptr < range.end; ptr++) {
832 ngram_raw_t *raw_ngram = &raw_ngrams[*raw_ngram_idx];
833 if (order == max_order) {
834 longest_t *longest = trie->longest;
835 address.base = longest->base.base;
836 address.offset = ptr * longest->base.total_bits;
837 hist[n_hist] =
838 bitarr_read_int25(address, longest->base.word_bits,
839 longest->base.word_mask);
840 address.offset += longest->base.word_bits;
841 prob = lm_trie_quant_lpread(trie->quant, address);
842 }
843 else {
844 middle_t *middle = &trie->middle_begin[n_hist - 1];
845 address.base = middle->base.base;
846 address.offset = ptr * middle->base.total_bits;
847 hist[n_hist] =
848 bitarr_read_int25(address, middle->base.word_bits,
849 middle->base.word_mask);
850 address.offset += middle->base.word_bits;
851 prob =
852 lm_trie_quant_mpread(trie->quant, address, n_hist - 1);
853 backoff =
854 lm_trie_quant_mboread(trie->quant, address,
855 n_hist - 1);
856 raw_ngram->backoff = backoff;
857 }
858 raw_ngram->prob = prob;
859 raw_ngram->words =
860 (uint32 *) ckd_calloc(order, sizeof(*raw_ngram->words));
861 for (i = 0; i <= n_hist; i++) {
862 raw_ngram->words[i] = hist[n_hist - i];
863 }
864 (*raw_ngram_idx)++;
865 }
866 }
867}
SPHINXBASE_EXPORT uint8 bitarr_required_bits(uint32 max_value)
Computes amount of bits required ti store integers upto value provided.
Definition: bitarr.c:131
SPHINXBASE_EXPORT uint32 bitarr_read_int25(bitarr_address_t address, uint8 length, uint32 mask)
Read uint32 value from bit array.
Definition: bitarr.c:100
SPHINXBASE_EXPORT void bitarr_write_int25(bitarr_address_t address, uint8 length, uint32 value)
Write specified value into bit array.
Definition: bitarr.c:112
SPHINXBASE_EXPORT void bitarr_mask_from_max(bitarr_mask_t *bit_mask, uint32 max_value)
Fills mask for certain int range according to provided max value.
Definition: bitarr.c:125
Sphinx's memory allocation/deallocation routines.
SPHINXBASE_EXPORT void ckd_free(void *ptr)
Test and free a 1-D array.
Definition: ckd_alloc.c:244
#define ckd_calloc(n, sz)
Macros to simplify the use of above functions.
Definition: ckd_alloc.h:248
Implementation of logging routines.
#define E_ERROR(...)
Print error message to error log.
Definition: err.h:104
#define E_INFO(...)
Print logging information to standard error stream.
Definition: err.h:114
Basic type definitions used in Sphinx.
Definition: lm_trie.h:58
Structure that stores address of certain value in bit array.
Definition: bitarr.h:73