rustling 0.8.0

A blazingly fast library for computational linguistics
Documentation
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
#!/usr/bin/env python
"""Benchmark rustling.hmm vs hmmlearn CategoricalHMM.

Compares unsupervised Baum-Welch EM training, Viterbi decoding, and
Forward-algorithm scoring using HKCanCor corpus data.

Usage:
    python benchmarks/run_hmm.py
    python benchmarks/run_hmm.py --quick
    python benchmarks/run_hmm.py --export results.json
"""

from __future__ import annotations

import argparse
import gc
import json
import statistics
import sys
import time
from pathlib import Path
from typing import Any

import numpy as np

# Add parent directory to path for imports
sys.path.insert(0, str(Path(__file__).parent))

from common.data import hmm_data, load_hkcancor  # noqa: E402


def try_import_rustling_hmm() -> dict[str, Any]:
    """Try to import Rustling's HiddenMarkovModel."""
    try:
        from rustling.hmm import HiddenMarkovModel

        return {"available": True, "class": HiddenMarkovModel}
    except ImportError as e:
        return {"available": False, "error": str(e)}


def try_import_hmmlearn() -> dict[str, Any]:
    """Try to import hmmlearn's CategoricalHMM."""
    try:
        from hmmlearn.hmm import CategoricalHMM

        return {"available": True, "class": CategoricalHMM}
    except ImportError as e:
        return {"available": False, "error": str(e)}


def build_vocab(sequences: list[list[str]]) -> dict[str, int]:
    """Build vocabulary mapping from training sequences."""
    vocab: dict[str, int] = {}
    for seq in sequences:
        for word in seq:
            if word not in vocab:
                vocab[word] = len(vocab)
    return vocab


def prepare_hmmlearn_data(
    sequences: list[list[str]],
    vocab: dict[str, int],
) -> tuple[np.ndarray, np.ndarray]:
    """Convert string sequences to hmmlearn format.

    Returns
    -------
    tuple[np.ndarray, np.ndarray]
        (X, lengths) where X is shape (total_obs, 1) int array,
        lengths is shape (n_sequences,) int array.
    """
    oov_id = len(vocab)
    encoded = []
    lengths = []
    for seq in sequences:
        ids = [vocab.get(w, oov_id) for w in seq]
        encoded.extend(ids)
        lengths.append(len(seq))
    X = np.array(encoded, dtype=np.int32).reshape(-1, 1)
    lengths = np.array(lengths, dtype=np.int32)
    return X, lengths


def benchmark_rustling_fit(
    cls: type,
    train_sequences: list[list[str]],
    n_states: int,
    n_iter: int,
    tolerance: float,
    iterations: int,
) -> float:
    """Benchmark Rustling EM training time.

    Returns
    -------
    float
        Average fit time in seconds.
    """
    times = []
    for _ in range(iterations):
        gc.collect()
        model = cls(
            n_states=n_states,
            n_iter=n_iter,
            tolerance=tolerance,
            random_seed=42,
        )
        start = time.perf_counter()
        model.fit(train_sequences)
        times.append(time.perf_counter() - start)
    return statistics.mean(times)


def benchmark_hmmlearn_fit(
    cls: type,
    train_X: np.ndarray,
    train_lengths: np.ndarray,
    n_states: int,
    n_iter: int,
    tolerance: float,
    n_features: int,
    iterations: int,
) -> float:
    """Benchmark hmmlearn EM training time.

    Returns
    -------
    float
        Average fit time in seconds.
    """
    times = []
    for _ in range(iterations):
        gc.collect()
        model = cls(
            n_components=n_states,
            n_iter=n_iter,
            tol=tolerance,
            n_features=n_features,
            random_state=42,
        )
        start = time.perf_counter()
        model.fit(train_X, train_lengths)
        times.append(time.perf_counter() - start)
    return statistics.mean(times)


def benchmark_rustling_predict(
    model: Any,
    test_sequences: list[list[str]],
    iterations: int,
) -> float:
    """Benchmark Rustling Viterbi decoding time.

    Returns
    -------
    float
        Average predict time in seconds (excludes warmup iteration).
    """
    # Warmup: avoid cold-start effects
    model.predict(test_sequences)
    times = []
    for _ in range(iterations):
        gc.collect()
        start = time.perf_counter()
        model.predict(test_sequences)
        times.append(time.perf_counter() - start)
    return statistics.mean(times)


def benchmark_hmmlearn_predict(
    model: Any,
    test_X: np.ndarray,
    test_lengths: np.ndarray,
    iterations: int,
) -> float:
    """Benchmark hmmlearn Viterbi decoding time.

    Returns
    -------
    float
        Average predict time in seconds (excludes warmup iteration).
    """
    # Warmup: avoid cold-start effects
    model.predict(test_X, test_lengths)
    times = []
    for _ in range(iterations):
        gc.collect()
        start = time.perf_counter()
        model.predict(test_X, test_lengths)
        times.append(time.perf_counter() - start)
    return statistics.mean(times)


def benchmark_rustling_score(
    model: Any,
    test_sequences: list[list[str]],
    iterations: int,
) -> float:
    """Benchmark Rustling Forward-algorithm scoring time.

    Returns
    -------
    float
        Average score time in seconds.
    """
    times = []
    for _ in range(iterations):
        gc.collect()
        start = time.perf_counter()
        model.score(test_sequences)
        times.append(time.perf_counter() - start)
    return statistics.mean(times)


def benchmark_hmmlearn_score(
    model: Any,
    test_X: np.ndarray,
    test_lengths: np.ndarray,
    iterations: int,
) -> float:
    """Benchmark hmmlearn Forward-algorithm scoring time.

    Returns
    -------
    float
        Average score time in seconds.
    """
    times = []
    for _ in range(iterations):
        gc.collect()
        start = time.perf_counter()
        model.score(test_X, test_lengths)
        times.append(time.perf_counter() - start)
    return statistics.mean(times)


def run_benchmarks(
    quick: bool = False,
    verbose: bool = True,
) -> dict[str, Any]:
    """Run all benchmarks.

    Parameters
    ----------
    quick : bool, default=False
        If True, use smaller data and fewer iterations.
    verbose : bool, default=True
        If True, print results.

    Returns
    -------
    dict[str, Any]
        Benchmark results.
    """
    rustling_info = try_import_rustling_hmm()
    hmmlearn_info = try_import_hmmlearn()

    if verbose:
        if rustling_info["available"]:
            print("✓ rustling.hmm loaded successfully")
        else:
            print(f"✗ rustling.hmm not available: {rustling_info.get('error', '')}")
        if hmmlearn_info["available"]:
            print("✓ hmmlearn CategoricalHMM loaded successfully")
        else:
            print(f"✗ hmmlearn not available: {hmmlearn_info.get('error', '')}")

    if not rustling_info["available"] and not hmmlearn_info["available"]:
        print("\nError: Neither implementation is available.")
        sys.exit(1)

    # Load data
    if verbose:
        print("\nLoading HKCanCor corpus...")
    tagged_sents = load_hkcancor()
    train_sequences, test_sequences = hmm_data(tagged_sents)

    # Shared HMM parameters
    n_states = 10
    n_iter = 100
    tolerance = 1e-6

    if quick:
        train_sequences = train_sequences[:500]
        test_sequences = test_sequences[:100]
        n_iter = 20
        fit_iterations = 2
        predict_iterations = 3
        score_iterations = 3
    else:
        fit_iterations = 3
        predict_iterations = 5
        score_iterations = 5

    if verbose:
        print(f"Training sequences: {len(train_sequences)}")
        print(f"Test sequences: {len(test_sequences)}")
        print(f"Hidden states: {n_states}, EM iterations: {n_iter}")

    # Build vocabulary and prepare hmmlearn data (outside timing)
    vocab = build_vocab(train_sequences)
    n_features = len(vocab) + 1  # +1 for OOV
    train_X, train_lengths = prepare_hmmlearn_data(train_sequences, vocab)
    test_X, test_lengths = prepare_hmmlearn_data(test_sequences, vocab)

    results: dict[str, Any] = {
        "n_states": n_states,
        "n_iter": n_iter,
        "tolerance": tolerance,
        "num_train": len(train_sequences),
        "num_test": len(test_sequences),
        "n_features": int(n_features),
        "benchmarks": {},
    }

    print(
        "\n"
        + "=" * 70
        + "\nHMM BENCHMARK: Rustling (Rust) vs hmmlearn CategoricalHMM"
        + "\n"
        + "=" * 70
    )

    # --- Fit (EM Training) ---
    print(f"\n--- Fit / EM Training ({fit_iterations} iterations) ---")

    rustling_fit_time = None
    if rustling_info["available"]:
        rustling_fit_time = benchmark_rustling_fit(
            rustling_info["class"],
            train_sequences,
            n_states,
            n_iter,
            tolerance,
            fit_iterations,
        )
        if verbose:
            print(
                f"\n  rustling.hmm.HiddenMarkovModel:"
                f"\n    Fit time: {rustling_fit_time:.4f}s"
            )

    hmmlearn_fit_time = None
    if hmmlearn_info["available"]:
        hmmlearn_fit_time = benchmark_hmmlearn_fit(
            hmmlearn_info["class"],
            train_X,
            train_lengths,
            n_states,
            n_iter,
            tolerance,
            n_features,
            fit_iterations,
        )
        if verbose:
            print(
                f"\n  hmmlearn CategoricalHMM:"
                f"\n    Fit time: {hmmlearn_fit_time:.4f}s"
            )

    if rustling_fit_time and hmmlearn_fit_time:
        speedup = hmmlearn_fit_time / rustling_fit_time
        print(f"\n  ⚡ Fit speedup: {speedup:.1f}x faster")
        results["benchmarks"]["fit"] = {
            "rustling": {"time_seconds": rustling_fit_time},
            "hmmlearn": {"time_seconds": hmmlearn_fit_time},
            "speedup": speedup,
        }

    # --- Predict (Viterbi Decoding) ---
    print(f"\n--- Predict / Viterbi Decoding ({predict_iterations} iterations) ---")

    # Pre-train models for predict and score benchmarks
    rustling_model = None
    if rustling_info["available"]:
        rustling_model = rustling_info["class"](
            n_states=n_states,
            n_iter=n_iter,
            tolerance=tolerance,
            random_seed=42,
        )
        rustling_model.fit(train_sequences)

    hmmlearn_model = None
    if hmmlearn_info["available"]:
        hmmlearn_model = hmmlearn_info["class"](
            n_components=n_states,
            n_iter=n_iter,
            tol=tolerance,
            n_features=n_features,
            random_state=42,
        )
        hmmlearn_model.fit(train_X, train_lengths)

    rustling_predict_time = None
    if rustling_model is not None:
        rustling_predict_time = benchmark_rustling_predict(
            rustling_model, test_sequences, predict_iterations
        )
        sps = len(test_sequences) / rustling_predict_time
        if verbose:
            print(
                f"\n  rustling.hmm.HiddenMarkovModel:"
                f"\n    Predict time: {rustling_predict_time:.4f}s"
                f" ({sps:,.0f} sequences/sec)"
            )

    hmmlearn_predict_time = None
    if hmmlearn_model is not None:
        hmmlearn_predict_time = benchmark_hmmlearn_predict(
            hmmlearn_model, test_X, test_lengths, predict_iterations
        )
        sps = len(test_sequences) / hmmlearn_predict_time
        if verbose:
            print(
                f"\n  hmmlearn CategoricalHMM:"
                f"\n    Predict time: {hmmlearn_predict_time:.4f}s"
                f" ({sps:,.0f} sequences/sec)"
            )

    if rustling_predict_time and hmmlearn_predict_time:
        speedup = hmmlearn_predict_time / rustling_predict_time
        print(f"\n  ⚡ Predict speedup: {speedup:.1f}x faster")
        results["benchmarks"]["predict"] = {
            "rustling": {"time_seconds": rustling_predict_time},
            "hmmlearn": {"time_seconds": hmmlearn_predict_time},
            "speedup": speedup,
        }

    # --- Score (Forward Algorithm) ---
    print(f"\n--- Score / Forward Algorithm ({score_iterations} iterations) ---")

    rustling_score_time = None
    if rustling_model is not None:
        rustling_score_time = benchmark_rustling_score(
            rustling_model, test_sequences, score_iterations
        )
        sps = len(test_sequences) / rustling_score_time
        if verbose:
            print(
                f"\n  rustling.hmm.HiddenMarkovModel:"
                f"\n    Score time: {rustling_score_time:.4f}s"
                f" ({sps:,.0f} sequences/sec)"
            )

    hmmlearn_score_time = None
    if hmmlearn_model is not None:
        hmmlearn_score_time = benchmark_hmmlearn_score(
            hmmlearn_model, test_X, test_lengths, score_iterations
        )
        sps = len(test_sequences) / hmmlearn_score_time
        if verbose:
            print(
                f"\n  hmmlearn CategoricalHMM:"
                f"\n    Score time: {hmmlearn_score_time:.4f}s"
                f" ({sps:,.0f} sequences/sec)"
            )

    if rustling_score_time and hmmlearn_score_time:
        speedup = hmmlearn_score_time / rustling_score_time
        print(f"\n  ⚡ Score speedup: {speedup:.1f}x faster")
        results["benchmarks"]["score"] = {
            "rustling": {"time_seconds": rustling_score_time},
            "hmmlearn": {"time_seconds": hmmlearn_score_time},
            "speedup": speedup,
        }

    # Compute speedups summary
    speedups: dict[str, float] = {}
    for op in ["fit", "predict", "score"]:
        bench = results["benchmarks"].get(op, {})
        if "speedup" in bench:
            speedups[op.capitalize()] = bench["speedup"]
    results["speedups"] = speedups

    return results


def main() -> None:
    """Main entry point."""
    parser = argparse.ArgumentParser(
        description="Benchmark rustling.hmm vs hmmlearn CategoricalHMM"
    )
    parser.add_argument(
        "--quick",
        action="store_true",
        help="Run quick benchmark with smaller data and fewer iterations",
    )
    parser.add_argument(
        "--export",
        type=str,
        metavar="FILE",
        help="Export results to JSON file",
    )
    parser.add_argument(
        "--quiet",
        action="store_true",
        help="Suppress output (useful with --export)",
    )

    args = parser.parse_args()

    results = run_benchmarks(
        quick=args.quick,
        verbose=not args.quiet,
    )

    if args.export:
        export_path = Path(args.export)
        export_path.parent.mkdir(parents=True, exist_ok=True)
        with open(export_path, "w") as f:
            json.dump(results, f, indent=2)
        print(f"\nResults exported to: {export_path}")


if __name__ == "__main__":
    main()