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
//! Forward score computation for differentiable WFSTs.
//!
//! This module computes the forward score (total path weight) of a WFST
//! using the log semiring, enabling gradient computation for training.

use super::gradient::GradientWfst;
use crate::semiring::{LogWeight, Semiring};
use crate::wfst::StateId;

/// Compute the forward score of a WFST in the log semiring.
///
/// The forward score is the log-sum-exp over all path weights, which corresponds
/// to the total probability mass of all paths when weights are log-probabilities.
///
/// # Algorithm
///
/// 1. Initialize α[start] = 1̄ (log semiring one = 0.0)
/// 2. Process states in topological order
/// 3. For each arc (s, t, w): α[t] = α[t] ⊕ (α[s] ⊗ w)
/// 4. Total score = ⊕_{f ∈ F} (α[f] ⊗ final_weight[f])
///
/// # Complexity
///
/// O(|Q| + |E|) for acyclic WFSTs.
///
/// # Example
///
/// ```rust
/// use lling_llang::differentiable::{forward_score, GradientWfst};
/// use lling_llang::wfst::{VectorWfst, MutableWfst};
/// use lling_llang::semiring::{LogWeight, Semiring};
///
/// let mut fst = VectorWfst::<char, LogWeight>::new();
/// let s0 = fst.add_state();
/// let s1 = fst.add_state();
/// fst.set_start(s0);
/// fst.set_final(s1, LogWeight::one());
/// fst.add_arc(s0, Some('a'), Some('a'), s1, LogWeight::new(1.0));
///
/// let grad_fst = GradientWfst::from_wfst(&fst);
/// let score = forward_score(&grad_fst);
/// assert!((score.value() - 1.0).abs() < 1e-6);
/// ```
pub fn forward_score<L: Clone + Send + Sync>(grad_fst: &GradientWfst<L>) -> LogWeight {
    let num_states = grad_fst.num_states();

    if num_states == 0 {
        return LogWeight::zero();
    }

    let start = grad_fst.start();

    // Initialize forward scores
    // All states start at zero (log semiring zero = -∞)
    // Start state gets one (log semiring one = 0.0)
    for s in 0..num_states as StateId {
        grad_fst.set_forward_score(s, LogWeight::zero());
    }
    grad_fst.set_forward_score(start, LogWeight::one());

    // Compute topological order
    let topo_order = compute_topological_order(grad_fst);

    // Forward pass: compute α values
    for &state in &topo_order {
        let alpha_state = grad_fst.forward_score(state);

        // Skip if this state is unreachable
        if alpha_state.is_zero() {
            continue;
        }

        // Propagate to successors
        for trans in grad_fst.transitions(state) {
            let to_state = trans.to;
            let arc_weight = trans.weight;

            // α[to] = α[to] ⊕ (α[from] ⊗ arc_weight)
            let contribution = alpha_state.times(&arc_weight);
            let current = grad_fst.forward_score(to_state);
            grad_fst.set_forward_score(to_state, current.plus(&contribution));
        }
    }

    // Compute total score: sum over final states
    let mut total = LogWeight::zero();
    for s in 0..num_states as StateId {
        if grad_fst.is_final(s) {
            let alpha_s = grad_fst.forward_score(s);
            let final_weight = grad_fst.final_weight(s);
            let contribution = alpha_s.times(&final_weight);
            total = total.plus(&contribution);
        }
    }

    // Mark forward pass as complete and cache total score
    grad_fst.set_forward_computed(true);
    grad_fst.set_total_score(total);

    total
}

/// Compute log-sum-exp over all paths (alias for forward_score).
///
/// This function is an alias for `forward_score` that emphasizes
/// the mathematical operation being performed.
pub fn log_sum_exp_paths<L: Clone + Send + Sync>(grad_fst: &GradientWfst<L>) -> LogWeight {
    forward_score(grad_fst)
}

/// Compute topological order for forward pass.
fn compute_topological_order<L: Clone + Send + Sync>(grad_fst: &GradientWfst<L>) -> Vec<StateId> {
    let num_states = grad_fst.num_states();
    let mut in_degree = vec![0usize; num_states];
    let mut order = Vec::with_capacity(num_states);

    // Count in-degrees
    for s in 0..num_states as StateId {
        for trans in grad_fst.transitions(s) {
            in_degree[trans.to as usize] += 1;
        }
    }

    // Start with states having zero in-degree
    let mut queue: Vec<StateId> = (0..num_states as StateId)
        .filter(|&s| in_degree[s as usize] == 0)
        .collect();

    while let Some(state) = queue.pop() {
        order.push(state);
        for trans in grad_fst.transitions(state) {
            let to = trans.to as usize;
            in_degree[to] -= 1;
            if in_degree[to] == 0 {
                queue.push(trans.to);
            }
        }
    }

    // If not all states are in order, graph has cycles - use BFS order as fallback
    if order.len() < num_states {
        order = (0..num_states as StateId).collect();
    }

    order
}

// =============================================================================
// Property-Based Tests
// =============================================================================

#[cfg(test)]
mod property_tests {
    use super::*;
    use crate::wfst::{MutableWfst, VectorWfst, Wfst};
    use proptest::prelude::*;

    /// Strategy for generating simple chain WFSTs.
    fn arb_chain_wfst(max_length: usize) -> impl Strategy<Value = VectorWfst<char, LogWeight>> {
        (1..=max_length).prop_flat_map(|len| {
            proptest::collection::vec(-5.0f64..5.0, len).prop_map(move |weights| {
                let mut fst = VectorWfst::new();
                for _ in 0..=len {
                    fst.add_state();
                }
                fst.set_start(0);
                fst.set_final(len as u32, LogWeight::one());
                for (i, w) in weights.iter().enumerate() {
                    let label = (b'a' + (i % 26) as u8) as char;
                    fst.add_arc(
                        i as u32,
                        Some(label),
                        Some(label),
                        (i + 1) as u32,
                        LogWeight::new(*w),
                    );
                }
                fst
            })
        })
    }

    /// Strategy for generating parallel path WFSTs.
    fn arb_parallel_wfst(max_paths: usize) -> impl Strategy<Value = VectorWfst<char, LogWeight>> {
        proptest::collection::vec(-5.0f64..5.0, 1..=max_paths).prop_map(|weights| {
            let mut fst = VectorWfst::new();
            let s0 = fst.add_state();
            let s1 = fst.add_state();
            fst.set_start(s0);
            fst.set_final(s1, LogWeight::one());
            for (i, w) in weights.iter().enumerate() {
                let label = (b'a' + (i % 26) as u8) as char;
                fst.add_arc(s0, Some(label), Some(label), s1, LogWeight::new(*w));
            }
            fst
        })
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(50))]

        /// Forward score of chain equals sum of arc weights.
        #[test]
        fn forward_chain_equals_weight_sum(fst in arb_chain_wfst(5)) {
            let grad_fst = GradientWfst::from_wfst(&fst);
            let score = forward_score(&grad_fst);

            // For a chain, forward score = sum of arc weights
            let mut expected = 0.0;
            for s in 0..fst.num_states() as u32 {
                for arc in fst.transitions(s) {
                    expected += arc.weight.value();
                }
            }

            prop_assert!((score.value() - expected).abs() < 1e-6,
                "Chain score {} != expected {}", score.value(), expected);
        }

        /// Forward score of parallel paths is log-sum-exp.
        #[test]
        fn forward_parallel_is_logsumexp(fst in arb_parallel_wfst(5)) {
            let grad_fst = GradientWfst::from_wfst(&fst);
            let score = forward_score(&grad_fst);

            // For parallel paths, total = -log(sum_i exp(-w_i))
            let weights: Vec<f64> = fst.transitions(0).iter()
                .map(|arc| arc.weight.value())
                .collect();

            if !weights.is_empty() {
                let probs: f64 = weights.iter().map(|w| (-w).exp()).sum();
                let expected = -probs.ln();
                prop_assert!((score.value() - expected).abs() < 1e-6,
                    "Parallel score {} != expected {}", score.value(), expected);
            }
        }

        /// Forward pass sets computed flag.
        #[test]
        fn forward_sets_computed_flag(fst in arb_chain_wfst(3)) {
            let grad_fst = GradientWfst::from_wfst(&fst);
            prop_assert!(!grad_fst.is_forward_computed());
            let _ = forward_score(&grad_fst);
            prop_assert!(grad_fst.is_forward_computed());
        }

        /// Forward score is deterministic.
        #[test]
        fn forward_is_deterministic(fst in arb_parallel_wfst(4)) {
            let grad_fst1 = GradientWfst::from_wfst(&fst);
            let grad_fst2 = GradientWfst::from_wfst(&fst);

            let score1 = forward_score(&grad_fst1);
            let score2 = forward_score(&grad_fst2);

            prop_assert!((score1.value() - score2.value()).abs() < 1e-9,
                "Scores differ: {} vs {}", score1.value(), score2.value());
        }

        /// log_sum_exp_paths is alias for forward_score.
        #[test]
        fn logsumexp_alias(fst in arb_chain_wfst(3)) {
            let grad_fst1 = GradientWfst::from_wfst(&fst);
            let grad_fst2 = GradientWfst::from_wfst(&fst);

            let score1 = forward_score(&grad_fst1);
            let score2 = log_sum_exp_paths(&grad_fst2);

            prop_assert!((score1.value() - score2.value()).abs() < 1e-9,
                "forward_score {} != log_sum_exp_paths {}", score1.value(), score2.value());
        }

        /// Forward score caches total in GradientWfst.
        #[test]
        fn forward_caches_total(fst in arb_chain_wfst(3)) {
            let grad_fst = GradientWfst::from_wfst(&fst);
            prop_assert!(grad_fst.total_score().is_none());

            let score = forward_score(&grad_fst);
            let cached = grad_fst.total_score();

            prop_assert!(cached.is_some());
            prop_assert!((cached.expect("differentiable/forward_score.rs: required value was None/Err").value() - score.value()).abs() < 1e-9);
        }

        /// Forward scores at final states match total for chain.
        #[test]
        fn forward_final_matches_total(fst in arb_chain_wfst(4)) {
            let grad_fst = GradientWfst::from_wfst(&fst);
            let score = forward_score(&grad_fst);

            // For a chain, forward score at final state equals total
            let final_state = (fst.num_states() - 1) as u32;
            let final_score = grad_fst.forward_score(final_state);

            // Total includes final weight (which is 1 = log 0)
            prop_assert!((final_score.value() - score.value()).abs() < 1e-6,
                "Final state score {} != total {}", final_score.value(), score.value());
        }

        /// Forward score is non-zero for connected WFST.
        #[test]
        fn forward_connected_non_zero(fst in arb_chain_wfst(3)) {
            let grad_fst = GradientWfst::from_wfst(&fst);
            let score = forward_score(&grad_fst);
            prop_assert!(!score.is_zero(), "Forward score should not be zero for connected WFST");
        }

        /// Forward score increases with added high-probability path.
        #[test]
        fn forward_increases_with_better_path(
            base_weight in -5.0f64..5.0,
            better_weight in -10.0f64..-5.0
        ) {
            // Create base FST with one path
            let mut fst1 = VectorWfst::<char, LogWeight>::new();
            let s0 = fst1.add_state();
            let s1 = fst1.add_state();
            fst1.set_start(s0);
            fst1.set_final(s1, LogWeight::one());
            fst1.add_arc(s0, Some('a'), Some('a'), s1, LogWeight::new(base_weight));

            // Create FST with additional better path (lower weight = higher probability)
            let mut fst2 = VectorWfst::<char, LogWeight>::new();
            let s0 = fst2.add_state();
            let s1 = fst2.add_state();
            fst2.set_start(s0);
            fst2.set_final(s1, LogWeight::one());
            fst2.add_arc(s0, Some('a'), Some('a'), s1, LogWeight::new(base_weight));
            fst2.add_arc(s0, Some('b'), Some('b'), s1, LogWeight::new(better_weight));

            let grad1 = GradientWfst::from_wfst(&fst1);
            let grad2 = GradientWfst::from_wfst(&fst2);

            let score1 = forward_score(&grad1);
            let score2 = forward_score(&grad2);

            // More paths = lower total weight (higher total probability)
            prop_assert!(score2.value() <= score1.value() + 1e-9,
                "Adding path should decrease weight: {} should be <= {}", score2.value(), score1.value());
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::wfst::{MutableWfst, VectorWfst};

    #[test]
    fn test_forward_score_empty() {
        let fst = VectorWfst::<char, LogWeight>::new();
        let grad_fst = GradientWfst::from_wfst(&fst);
        let score = forward_score(&grad_fst);
        assert!(score.is_zero());
    }

    #[test]
    fn test_forward_score_no_path() {
        // Start state with no transitions to final
        let mut fst = VectorWfst::<char, LogWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        fst.set_start(s0);
        fst.set_final(s1, LogWeight::one());
        // No arc from s0 to s1

        let grad_fst = GradientWfst::from_wfst(&fst);
        let score = forward_score(&grad_fst);
        assert!(score.is_zero());
    }

    #[test]
    fn test_forward_score_single_arc() {
        let mut fst = VectorWfst::<char, LogWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        fst.set_start(s0);
        fst.set_final(s1, LogWeight::new(-0.5)); // Final weight = -0.5
        fst.add_arc(s0, Some('a'), Some('a'), s1, LogWeight::new(-1.0));

        let grad_fst = GradientWfst::from_wfst(&fst);
        let score = forward_score(&grad_fst);

        // Path weight = 0.0 (start) + (-1.0) (arc) + (-0.5) (final) = -1.5
        assert!((score.value() - (-1.5)).abs() < 1e-6);
    }

    #[test]
    fn test_forward_score_chain() {
        // 0 --(-1.0)--> 1 --(-2.0)--> 2 (final, weight -0.5)
        let mut fst = VectorWfst::<char, LogWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        let s2 = fst.add_state();
        fst.set_start(s0);
        fst.set_final(s2, LogWeight::new(-0.5));
        fst.add_arc(s0, Some('a'), Some('a'), s1, LogWeight::new(-1.0));
        fst.add_arc(s1, Some('b'), Some('b'), s2, LogWeight::new(-2.0));

        let grad_fst = GradientWfst::from_wfst(&fst);
        let score = forward_score(&grad_fst);

        // Path weight = 0 + (-1) + (-2) + (-0.5) = -3.5
        assert!((score.value() - (-3.5)).abs() < 1e-6);
    }

    #[test]
    fn test_forward_score_parallel_paths() {
        // Two parallel paths: 0 -> 1 with weights 1.0 and 2.0
        // (representing -log(prob), so prob = e^-1 and e^-2)
        let mut fst = VectorWfst::<char, LogWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        fst.set_start(s0);
        fst.set_final(s1, LogWeight::one());
        fst.add_arc(s0, Some('a'), Some('a'), s1, LogWeight::new(1.0));
        fst.add_arc(s0, Some('b'), Some('b'), s1, LogWeight::new(2.0));

        let grad_fst = GradientWfst::from_wfst(&fst);
        let score = forward_score(&grad_fst);

        // Sum of probabilities: e^-1 + e^-2 ≈ 0.503
        // Negative log: -log(0.503) ≈ 0.687
        let prob_sum = (-1.0_f64).exp() + (-2.0_f64).exp();
        let expected = -prob_sum.ln();
        assert!((score.value() - expected).abs() < 1e-6);
    }

    #[test]
    fn test_forward_score_diamond() {
        // Diamond: 0 -> 1 -> 2 and 0 -> 2
        // LogWeight stores negative log probabilities (positive values = valid probs < 1)
        let mut fst = VectorWfst::<char, LogWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        let s2 = fst.add_state();
        fst.set_start(s0);
        fst.set_final(s2, LogWeight::one());
        fst.add_arc(s0, Some('a'), Some('a'), s1, LogWeight::new(1.0)); // prob e^-1
        fst.add_arc(s1, Some('b'), Some('b'), s2, LogWeight::new(1.0)); // prob e^-1
        fst.add_arc(s0, Some('c'), Some('c'), s2, LogWeight::new(1.5)); // prob e^-1.5

        let grad_fst = GradientWfst::from_wfst(&fst);
        let score = forward_score(&grad_fst);

        // Path 1: 1.0 + 1.0 = 2.0 (prob e^-2)
        // Path 2: 1.5 (prob e^-1.5)
        // Total weight = -log(e^-2 + e^-1.5) ≈ 1.03
        let expected = -((-2.0_f64).exp() + (-1.5_f64).exp()).ln();
        assert!((score.value() - expected).abs() < 1e-6);
    }

    #[test]
    fn test_forward_score_multiple_finals() {
        // Two final states
        // LogWeight stores negative log probabilities (positive values = valid probs < 1)
        let mut fst = VectorWfst::<char, LogWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        let s2 = fst.add_state();
        fst.set_start(s0);
        fst.set_final(s1, LogWeight::one());
        fst.set_final(s2, LogWeight::one());
        fst.add_arc(s0, Some('a'), Some('a'), s1, LogWeight::new(1.0)); // prob e^-1
        fst.add_arc(s0, Some('b'), Some('b'), s2, LogWeight::new(2.0)); // prob e^-2

        let grad_fst = GradientWfst::from_wfst(&fst);
        let score = forward_score(&grad_fst);

        // Two paths: weight 1.0 (prob e^-1) and weight 2.0 (prob e^-2)
        // Total weight = -log(e^-1 + e^-2) ≈ 0.687
        let expected = -((-1.0_f64).exp() + (-2.0_f64).exp()).ln();
        assert!((score.value() - expected).abs() < 1e-6);
    }

    #[test]
    fn test_forward_computed_flag() {
        let mut fst = VectorWfst::<char, LogWeight>::new();
        let s0 = fst.add_state();
        fst.set_start(s0);
        fst.set_final(s0, LogWeight::one());

        let grad_fst = GradientWfst::from_wfst(&fst);
        assert!(!grad_fst.is_forward_computed());

        let _ = forward_score(&grad_fst);
        assert!(grad_fst.is_forward_computed());
    }
}