← Back to Pull requests

[ENH] Implementation for Dynamic Time Warping (DTW) in cython #5

Open
fnhirwa opened this pull request with 953 lines diff across 10 files
View on GitHub
fnhirwa fnhirwa commented on Jul 27, 2026 16:17 UTC
Author

<!--
Thanks for contributing a pull request! Please ensure you have taken a look
at our contribution guide: https://github.com/alan-turing-institute/sktime/blob/main/CONTRIBUTING.md
-->

Reference Issues/PRs


<!--
Example: Fixes #1234. See also #3456.

Please use keywords (e.g., Fixes) to create link to the issues or pull requests
you resolved, so that they will automatically be closed when your pull request
is merged. See https://github.com/blog/1506-closing-issues-via-pull-requests
-->
Part of #4

What does this implement/fix? Explain your changes.

This PR implements ahead-of-time (AOT) compiled Cython dynamic time warping (DTW) kernels (dtw_distance and dtw_cost_matrix), providing a zero-warmup, high-performance C-extension alternative to sktime's Numba implementation.

Key Changes

1. Cython DTW Kernels (dtw_distance & dtw_cost_matrix)
Ported the dependent multivariate squared-Euclidean local cost (DTW_D) recurrence to Cython memoryviews (double[:, ::1]) with nogil.
Memory Optimization: dtw_distance computes the final DTW cost via a rolling two-row buffer, reducing memory overhead to $O(m_2)$ instead of allocating a full $O(m_1 \times m_2)$ matrix when alignment path recovery isn't required.

2. Precomputed Vectorized Masking (unsigned char[:, ::1])
Replaced in-kernel floating-point sentinel checks with a precomputed 2D uint8 mask (np.isfinite(bounding_matrix)).
Sidesteps cross-platform compiler optimizations and strict-aliasing issues under -O2/-O3 / -ffast-math (e.g., Clang on macOS), providing ultra-fast $O(1)$ cell validity lookups inside nogil loops.

Benchmark Results

Below is the end-to-end performance comparison between sktime's Numba implementation (sktime.dists_kernels._numba_distances.dtw_distance) and the new ahead-of-time compiled Cython extension.

Times in microseconds (µs). Benchmark measured across 500 calls with pre-allocated bounding matrices.

| Series Shape (d, m) | Numba dtw_distance (µs) | Cython dtw_distance (µs) | Speedup | Cython cost_matrix (µs) |
|---|---|---|---|---|
| d=1, m=50 | 24,681.96 | 10.74 | 2,298.51x | 9.84 |
| d=1, m=200 | 30,798.19 | 148.33 | 207.63x | 141.62 |
| d=3, m=200 | 31,617.42 | 151.80 | 208.29x | 159.52 |
| d=5, m=500 | 125,121.70 | 930.15 | 134.52x | 970.37 |

Key Performance


Sub-Millisecond Execution: Even for multi-channel series (d=5, m=500), distance calculation finishes in < 1 ms (930 µs).
Zero Dispatching Overhead: Bypasses pure-Python wrapper allocation and argument verification overhead (~25–30 ms saved per call).
  • No JIT Warmup: AOT compilation avoids cold-start latency on first invocation.
  • Diff: 953 lines (+951 / -2) across 10 files Reviewer: sssilvar Review duration: 99510m
    All Vouch review confidence checks passed
    Residual risk 0.15 = Change Risk (0.63) × (1 − Review Confidence (0.76)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.63 · 953 lines diff across 10 files
    0.63
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.60 · 1 substantive comment(s)
    0.60
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    75%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.400
    Real PR Conversation Timeline
    3 items
    sssilvar sssilvar requested changes on Sep 03, 2026 18:22 UTC
    Changes Requested Reviewer

    I found several issues:

    1. [P1] Validate channel counts before entering the unchecked Cython loop

    In _dtw_cython.pyx, _local_cost loops over x.shape[0] and reads y[k, j] with bounds checking disabled. If x has more channels than y, this reads beyond the memory allocated for y. A reproduction with shapes (2, 4) and (1, 4) returned the incorrect value 4.0 instead of raising an error. Both public entry points should require _x.shape[0] == _y.shape[0].

    2. [P1] Validate the bounding matrix shape

    A custom bounding matrix is accepted without ensuring its shape is (m1, m2). The Cython kernels then access bm_mask[i, j] with bounds checking disabled, so an undersized matrix causes out-of-bounds reads and undefined behavior. This also affects automatically generated Itakura bounds for unequal-length series: _itakura_parallelogram creates (y_size, x_size), while the kernels require (x_size, y_size).

    3. [P1] Do not combine the infinity-based recurrence with incompatible fast-math assumptions

    The extension uses INFINITY to initialize and block dynamic-programming cells, but setup.py compiles it with -ffast-math. Clang reports multiple use of infinity via a macro is undefined behavior warnings while building this PR. Precomputing the bounding mask avoids finite checks in the loop, but it does not protect the infinity values used by the recurrence. The DTW extension should be compiled without incompatible finite-math assumptions, or use a finite sentinel with overflow-safe handling.

    4. [P2] Expand legacy equivalence coverage beyond equal-length scalar distances

    The current test_cython_matches_numba is a useful start, but it only exercises equal-length inputs and dtw_distance. Please mirror the Rocket regression approach across unequal lengths in both orientations, univariate and multivariate inputs, all bounding modes, custom masks, and dtw_cost_matrix. Add explicit validation tests for mismatched channel counts and incorrectly shaped bounding matrices. At least one regular CI job should install the dev extra so these comparisons run instead of being skipped; wheel tests can continue to skip them.

    fnhirwa fnhirwa commented on Sep 11, 2026 00:13 UTC
    Author

    Thanks for the review!
    all four items are addressed: channel-count and bounding-matrix shape validation on both entry points and in the kernels, the DTW extension now compiles without -ffast-math, and the legacy equivalence tests are expanded across unequal lengths in both orientations, univariate/multivariate, all bounding modes, custom masks, and dtw_cost_matrix, with one CI job set to fail rather than skip them.
    Ready for another look.

    Reviewers
    Assignees
    fnhirwa fnhirwa
    Labels
    risk: low confidence: 75.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 63.2% (MEDIUM) · Higher = larger blast radius
    63.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 75.7% · Lower = rushed or shallow review
    75.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 15.4% · Safe to merge
    15.4%