lling-llang 0.1.0

WFST framework for text normalization and grammar correction
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# Phase 7: Optimizations

**Branch**: `feature/optimizations`
**Depends on**: Phases 1-6
**Started**: 2025-12-27
**Status**: COMPLETED

## Overview

Phase 7 implements critical optimization techniques identified from the WFST literature:

1. **Log Semiring Weight Pushing for Beam Search** (7.1): Critical optimization from Mohri et al.
   - Creates stochastic automaton where weights sum to 1 at each state
   - "Synchronizes" acoustic likelihoods with transducer probabilities
   - Papers report up to 18× speedup for beam-pruned Viterbi decoding

2. **Token Grouping + Lazy Evaluation** (7.2): LET-Decoder approach
   - Groups tokens with same base-graph state but different grammar states
   - Defers expansion until word boundaries
   - 10-20× reduction in composition operations

3. **N-gram Back-off Structure** (7.3): Compact LM representation
   - Avoids O(|V|²) transitions in language model graphs
   - Uses back-off ε-transitions to lower-order n-grams

---

## 7.1 Log Semiring Weight Pushing for Beam Search

**Date**: 2025-12-27
**Status**: COMPLETED

### Hypothesis

Weight pushing in the **log semiring** (NOT tropical!) significantly improves beam search
pruning efficacy. Unlike tropical pushing which uses min-weight potentials, log pushing
uses the sum of all path probabilities.

**From Mohri, Pereira, Riley (2002)**:
> "Weight pushing in the log semiring has a very large beneficial impact on the
> pruning efficacy of a standard Viterbi beam search"
>
> "In contrast, weight pushing in the tropical semiring... may slow down beam-pruned
> Viterbi decoding many fold"

**Key insight**: Log pushing creates a stochastic automaton where weights at each state
sum to 1 in probability space, providing optimal likelihood ratio decisions for pruning.

### Design

```rust
/// Configuration for log-semiring weight pushing.
pub struct LogPushConfig {
    pub direction: PushDirection,
    pub remove_total_weight: bool,
    pub verify_stochastic: bool,
}

/// Result of beam search preparation.
pub struct BeamSearchPrepResult {
    pub pushed: bool,
    pub total_weight: LogWeight,
    pub is_stochastic: Option<bool>,
    pub num_states: usize,
    pub num_transitions: usize,
}

pub fn prepare_for_beam_search<L, F>(
    fst: &mut F,
    config: LogPushConfig,
) -> Result<BeamSearchPrepResult, LogPushError>
```

### Implementation

**Files created**:
- `src/optimization/mod.rs` (~55 lines): Module with exports
- `src/optimization/log_push.rs` (~660 lines): Log push implementation
- `src/optimization/lookahead.rs` (~290 lines): Lookahead scoring tables

**Key functions**:
- `prepare_for_beam_search()` - High-level API for beam search optimization
- `compute_log_potentials()` - Backward potentials: V(q) = -log(Σ exp(-path_weight))
- `apply_log_push()` - Apply potentials to reweight arcs and finals
- `build_lookahead_table()` - Precompute lookahead for fast state scoring
- `normalize_score()` - Combine accumulated weight with lookahead

**Exports**:
- `prepare_for_beam_search`, `LogPushConfig`, `BeamSearchPrepResult`
- `compute_log_potentials`, `apply_log_push`
- `LookaheadTable`, `build_lookahead_table`, `LookaheadConfig`

### Algorithm

**Log Semiring Weight Pushing**:
1. Compute backward potentials V(q) for all states:
   - V(final) = final_weight
   - V(q) = logadd_{arcs from q} (arc_weight + V(target))
2. Reweight arcs: w'(a) = V(source) + w(a) - V(target)
3. Reweight finals: ρ'(f) = ρ(f) - V(f) + V(start)
4. Result: Stochastic automaton where outgoing weights sum to 1

**Lookahead Scoring**:
- Precompute V(q) for all states
- During beam search: normalize_score(state, accumulated) = accumulated + V(state)
- Enables comparison of hypotheses at different completion stages

### Benchmark Results

**Configuration**: taskset -c 0-3, 100 samples, 3s warmup

#### Log Push Performance

| Size | Linear WFST | Diamond WFST (branching 3) |
|------|-------------|---------------------------|
| 10 | 922 ns | 1.97 µs |
| 50 | 4.94 µs | 10.23 µs |
| 100 | 10.65 µs | 20.64 µs |
| 200 | 20.16 µs | 39.81 µs |

#### Lookahead Table Construction

| Size | Linear WFST | Diamond WFST (branching 3) |
|------|-------------|---------------------------|
| 10 | 225 ns | 1.07 µs |
| 50 | 846 ns | 4.98 µs |
| 100 | 1.57 µs | 10.06 µs |
| 200 | 3.00 µs | 19.03 µs |

#### Lookahead Query Performance

| Operation | Time (100 states) |
|-----------|-------------------|
| Query all states | 3.63 µs |
| Normalize scores | 3.77 µs |

### Analysis

1. **Log push scales linearly** with graph size: O(|Q| + |E|)
2. **Diamond ~2x linear cost** due to higher edge density
3. **Lookahead table construction** faster than full push (read-only)
4. **Query performance** excellent: ~36 ns/state
5. **Normalize scores** add minimal overhead (~1.4 ns/score)

### Complexity Verification

| Operation | Theory | Observed | Match |
|-----------|--------|----------|-------|
| Log push | O(\|Q\| + \|E\|) | Linear ✓ ||
| Lookahead table | O(\|Q\| + \|E\|) | Linear ✓ ||
| Lookahead query | O(1) | ~36 ns ✓ ||

### Result

- [x] **ACCEPTED**: Log push implemented with correct semantics
- [x] 18 unit tests passing
- [x] 12 benchmark cases added
- [x] Lookahead table for efficient beam search pruning
- [x] Correct handling of negative log probability semiring

---

## 7.2 Token Grouping + Lazy Evaluation (LET-Decoder)

**Date**: 2025-12-27
**Status**: COMPLETED

### Hypothesis

Token grouping from LET-Decoder paper provides:
- 10-20× reduction in composition operations
- Significant speedup for on-the-fly rescoring
- Memory savings from deferred expansion

Key insight: Tokens with same base-graph state but different grammar states can be grouped, with expansion deferred until word boundaries.

### Design

```rust
/// Token representing a hypothesis during decoding.
pub struct Token {
    pub base_state: StateId,
    pub grammar_state: StateId,
    pub forward_prob: LogWeight,
    pub prev_token: Option<TokenId>,
    pub prev_arc: Option<ArcId>,
}

/// Group of tokens sharing the same base-graph state.
pub struct TokenGroup {
    pub base_state: StateId,
    pub best_forward_prob: LogWeight,
    pub expanded: bool,
    tokens: SmallVec<[Token; 4]>,
    preceding_links: SmallVec<[GroupLink; 4]>,
    succeeding_links: SmallVec<[GroupLink; 4]>,
    pub frame: u32,
}

/// Priority queue for histogram-based pruning.
pub struct BucketQueue<T> {
    buckets: Vec<VecDeque<T>>,
    min_bucket: usize,
    len: usize,
    scale: f64,
    offset: f64,
}
```

### Implementation

**Files created**:
- `src/optimization/token_group.rs` (~900 lines)

**Key components**:
- `Token`: Hypothesis representation with back-tracing info
- `TokenGroup`: Collection of tokens at same base state, with lazy expansion
- `TokenGroupPool`: Frame-aware storage for token groups
- `BucketQueue`: O(1) priority queue for histogram pruning
- `TokenGroupManager`: High-level API coordinating all components
- `GroupedFrame`: Snapshot of active groups when advancing frames
- `GroupLink`: Links for lazy back-tracing without materializing tokens

**Tests**: 14 unit tests covering all components

### Benchmark Results

#### BucketQueue (insert + pop throughput)

| Size | Time | Per-Op | Scaling |
|------|------|--------|---------|
| 100 | 3.31 µs | 33.1 ns | 1.0× |
| 500 | 17.6 µs | 35.2 ns | 1.06× |
| 1000 | 29.5 µs | 29.5 ns | 0.89× |
| 5000 | 102.8 µs | 20.6 ns | 0.62× |

**Analysis**: Sub-linear scaling - amortized O(1) insert/pop confirmed.
Better per-operation time at larger sizes due to fewer bucket reallocations.

#### TokenGroup add_token

| Tokens | Time | Per-Token |
|--------|------|-----------|
| 10 | 463 ns | 46.3 ns |
| 50 | 2.19 µs | 43.8 ns |
| 100 | 4.17 µs | 41.7 ns |

**Analysis**: Linear O(n) with ~43 ns/token. SmallVec inline storage helps for small groups.

#### TokenGroupPool get_or_create

| Base States | Time | Per-State |
|-------------|------|-----------|
| 100 | 3.34 µs | 33.4 ns |
| 500 | 18.9 µs | 37.8 ns |
| 1000 | 40.5 µs | 40.5 ns |

**Analysis**: Linear O(n) with FxHashMap providing fast lookups.

#### TokenGroupPool lookup (1000 groups)

| Operation | Time |
|-----------|------|
| 1000 lookups | 383 ns |

**Analysis**: 0.38 ns/lookup - excellent cache performance with direct indexing.

#### TokenGroupManager process_token

| Tokens | Time | Per-Token | Groups |
|--------|------|-----------|--------|
| 100 | 6.73 µs | 67.3 ns | ~100 |
| 500 | 32.6 µs | 65.2 ns | ~100 |
| 1000 | 64.5 µs | 64.5 ns | ~100 |

**Analysis**: Consistent ~65 ns/token regardless of total count.
Groups efficiently aggregate tokens to same base states.

#### TokenGroupManager with word arcs (500 tokens, 20% word arcs)

| Operation | Time |
|-----------|------|
| 500 mixed tokens | 35.7 µs |

**Analysis**: ~71 ns/token with expansion overhead.
Word arcs trigger immediate expansion (~6 ns overhead per expansion).

#### TokenGroupManager advance_frame (10 frames)

| Operation | Time |
|-----------|------|
| 10 frame advances | 1.66 µs |

**Analysis**: ~166 ns/frame advance. Efficient pool clearing and queue reset.

### Performance Summary

| Operation | Amortized Cost |
|-----------|---------------|
| Token processing | 65 ns |
| Token grouping | 34 ns |
| Group lookup | 0.38 ns |
| Frame advance | 166 ns |
| Bucket insert/pop | 25-35 ns |

### Complexity Analysis

| Component | Time Complexity | Space Complexity |
|-----------|-----------------|------------------|
| BucketQueue insert | O(1) amortized | O(num_buckets + n) |
| BucketQueue pop | O(1) amortized | - |
| TokenGroup add | O(1) amortized | O(tokens) |
| Pool get_or_create | O(1) expected | O(groups) |
| Manager process | O(1) expected | O(tokens) |

### Integration Notes

Token grouping is designed for on-the-fly composition scenarios:
- Works with `LazyComposition` from `src/composition/`
- Compatible with beam search via `BucketQueue`
- α-stable property ensures correct lattice generation

**Usage example**:
```rust
let mut manager = TokenGroupManager::new(TokenGroupConfig::default());

// Process tokens from arc expansion
for (arc, weight) in arcs {
    let token = Token {
        base_state: arc.next_state,
        grammar_state: grammar.next_state(&arc),
        forward_prob: current_prob.plus(&weight),
        prev_token: Some(current_token_id),
        prev_arc: Some(arc.id),
    };
    let group_id = manager.process_token(token, arc.is_word);
}

// Advance to next frame
let frame_info = manager.advance_frame();
```

---

## 7.3 N-gram Back-off Structure

**Date**: 2025-12-27
**Status**: COMPLETED

### Hypothesis

N-gram language models in WFST form can grow exponentially with vocabulary size.
A naive bigram representation requires O(|V|²) transitions. Using back-off states
with ε-transitions to lower-order n-grams maintains the same probability distribution
with only O(|V|) states and transitions.

**From Mohri et al.**:
> "For large vocabulary language models, directly representing all n-grams creates
> O(|V|²) transitions. Using back-off states with ε-transitions to lower-order
> n-grams keeps the graph compact while preserving the language model distribution."

**Key insight**: Seen n-grams get direct transitions; unseen n-grams use back-off
state with ε-transition carrying the back-off weight β(w₁).

### Design

```rust
/// Compact bigram LM with back-off structure.
pub struct BigramLm {
    unigram_probs: Vec<f64>,                          // P(w)
    bigram_probs: FxHashMap<(VocabId, VocabId), f64>, // P(w2|w1)
    backoff_weights: Vec<f64>,                        // β(w1)
    vocab_size: usize,
}

/// Builder for arbitrary n-gram LMs as WFSTs.
pub struct NgramLmBuilder {
    config: NgramLmConfig,
    context_to_state: FxHashMap<SmallVec<[VocabId; 4]>, StateId>,
    backoff_weights: FxHashMap<SmallVec<[VocabId; 4]>, f64>,
    ngrams: Vec<NgramEntry>,
    vocab: FxHashMap<VocabId, bool>,
}

/// Pruning strategies for n-gram models.
pub enum PruningStrategy {
    CountThreshold(f64),    // Prune ngrams below count threshold
    ProbabilityThreshold(f64), // Prune low-probability ngrams
    EntropyPruning(f64),    // Prune based on entropy contribution
}
```

### Implementation

**Files created**:
- `src/optimization/ngram_backoff.rs` (~700 lines)

**Key components**:
- `BigramLm`: Efficient bigram LM with O(1) probability lookup
- `NgramLmBuilder`: Build arbitrary n-gram WFSTs with back-off structure
- `NgramLmConfig`: Configuration for LM construction
- `NgramStats` / `BigramStats`: Statistics about LM structure
- `PruningStrategy`: Various pruning methods for compact models
- `compute_size_reduction()`: Compare naive vs back-off representation sizes

**Exports**:
- `VocabId`, `UNK_ID`, `BOS_ID`, `EOS_ID`
- `NgramEntry`, `BackoffWeight`, `NgramLmConfig`, `NgramLmBuilder`, `NgramStats`
- `BigramLm`, `BigramStats`, `PruningStrategy`
- `compute_size_reduction`, `SizeReduction`

**Tests**: 8 unit tests covering all components

### Benchmark Results

**Configuration**: taskset -c 0-3, 100 samples, 3s warmup

#### BigramLm Creation

| Vocab Size | Time | Per-Word |
|------------|------|----------|
| 100 | 700 ns | 7 ns |
| 500 | 3.24 µs | 6.5 ns |
| 1000 | 6.18 µs | 6.2 ns |

**Analysis**: Linear O(|V|) scaling with ~6.5 ns/word. Efficient allocation with
pre-sized vectors.

#### BigramLm Probability Lookup

| Vocab Size | Time (100 lookups) | Per-Lookup |
|------------|-------------------|------------|
| 100 | 1.41 µs | 14.1 ns |
| 500 | 1.43 µs | 14.3 ns |
| 1000 | 1.41 µs | 14.1 ns |

**Analysis**: O(1) lookup regardless of vocabulary size. FxHashMap provides
consistent ~14 ns per bigram lookup. Includes back-off computation when needed.

#### BigramLm to WFST Conversion

| Vocab Size | Time | Transitions |
|------------|------|-------------|
| 50 | 1.72 µs | O(50) |
| 100 | 2.60 µs | O(100) |
| 200 | 4.67 µs | O(200) |

**Analysis**: Linear O(|V|) for back-off WFST. Would be O(|V|²) without back-off.
At V=200: 4.67 µs for ~200 transitions vs ~40,000 for naive representation.

#### NgramLmBuilder (Trigram)

| N-grams | Time | Per-Ngram |
|---------|------|-----------|
| 100 | 32.84 µs | 328 ns |
| 500 | 79.82 µs | 160 ns |
| 1000 | 131.71 µs | 132 ns |

**Analysis**: Amortized per-ngram cost decreases with batch size due to hash
table efficiency. Context deduplication provides significant savings.

#### Size Reduction Calculation

| Operation | Time |
|-----------|------|
| compute_size_reduction | 147 ns |

**Analysis**: Negligible overhead for size comparison.

### Space Complexity Comparison

| Vocab Size | Naive Bigram | Back-off | Reduction |
|------------|--------------|----------|-----------|
| 100 | 10,000 arcs | ~200 arcs | 50× |
| 1000 | 1,000,000 arcs | ~2,000 arcs | 500× |
| 10,000 | 100,000,000 arcs | ~20,000 arcs | 5,000× |

**Key result**: Back-off structure provides O(|V|²) → O(|V|) reduction.

### Integration Notes

N-gram back-off WFSTs are designed for:
- Integration with CTC/ASR decoding pipelines
- On-the-fly composition with acoustic models
- Large vocabulary language model rescoring

**Usage example**:
```rust
// Create bigram LM
let lm = BigramLm::new(unigram_probs, bigram_probs, backoff_weights);

// Convert to WFST with back-off structure
let wfst: VectorWfst<u32, LogWeight> = lm.to_wfst();

// Query probability
let prob = lm.prob(word1_id, word2_id); // Uses back-off if unseen
```

### Result

- [x] **ACCEPTED**: N-gram back-off implemented with correct semantics
- [x] 8 unit tests passing
- [x] 5 benchmark cases added
- [x] O(|V|) complexity for back-off WFST construction
- [x] Pruning strategies for compact models

---

## Phase 7 Summary

**Total Lines Added**: ~1,850 lines across 4 source files
**Tests Added**: 40 unit tests (all passing)
**Benchmarks Added**: 30+ benchmark cases

### All Algorithms Verified

| Algorithm | Complexity (Expected) | Complexity (Observed) | Status |
|-----------|----------------------|----------------------|--------|
| Log Push | O(\|Q\| + \|E\|) | Linear ✓ | ACCEPTED |
| Lookahead Table | O(\|Q\| + \|E\|) | Linear ✓ | ACCEPTED |
| Token Group Process | O(1) expected | ~65 ns/token ✓ | ACCEPTED |
| BucketQueue | O(1) amortized | ~25-35 ns/op ✓ | ACCEPTED |
| BigramLm Lookup | O(1) | ~14 ns ✓ | ACCEPTED |
| Back-off WFST | O(\|V\|) | Linear ✓ | ACCEPTED |
| Trigram Build | O(n-grams) | ~150 ns/ngram ✓ | ACCEPTED |

### Optimization Summary

| Optimization | Source | Benefit | Verified |
|--------------|--------|---------|----------|
| Log Weight Pushing | Mohri et al. 2002 | Up to 18× beam search speedup ||
| Token Grouping | LET-Decoder 2023 | 10-20× fewer composition ops ||
| N-gram Back-off | WFST Literature | O(\|V\|²) → O(\|V\|) space ||

---