Skip to content

[ENH] Faster unbounded dependent MSM - #3918

Open
TonyBagnall wants to merge 2 commits into
mainfrom
ajb/msm
Open

TonyBagnall wants to merge 2 commits into
mainfrom
ajb/msm

Conversation

@TonyBagnall

Copy link
Copy Markdown
Contributor

tl:dr: similar to #3915, follow up from #3562 speed up multivariate dependent MSM by transposing to get contiguous memory and avoid slicing, cache to avoid recalculating values. Speeds up around 30x, same as TWE.

Speeds up the unbounded dependent MSM kernel (_msm_dependent_distance_unbounded_impl), which is used by
msm_distance and msm_pairwise_distance with independent=False and no window. Nothing else changes.

Previously each cell of the rolling-row kernel made three passes over the channels using strided column views
x[:, i]: the squared distance for the match, plus two _cost_dependent calls for split and merge, each summing
four squared distances. Most of those sums are already known from neighbouring cells. Writing sq(a, b) for the
squared distance between two time points:

  • the split diameter sq(x_{i-1}, y_j) is the previous row's match cost, kept in a rolling buffer
  • the merge diameter sq(x_i, y_{j-1}) is the match cost of the cell to the left
  • sq(x_{i-1}, x_i) is constant along a row
  • sq(y_{j-1}, y_j) depends only on y, so it is computed once per call

Each cell now makes a single pass over time-major contiguous copies of x and y, accumulating only the match
cost and the two midpoint distances. The split/merge choice is written as a conditional expression rather than
if/else: the comparison is data dependent, and as a branch it mispredicts often enough to cost about a third
of the gain.

Results are unchanged apart from the last bit in some cases (at most about 4e-16 relative), because fastmath may
sum in a different order. No split/merge decision changed in testing, and integer-valued inputs, where the
arithmetic is exact, give identical results.

Timings. Before this PR against after, n_jobs=1, median of per-round speed-ups from paired interleaved runs
on one core, interquartile range in brackets. Random normal series unless stated; datasets are test x train.

Case Before After Speed-up
c=1, n=1000 163 ms 4.17 ms 34.3x (26.8 to 38.6)
c=3, n=1000 209 ms 7.18 ms 29.6x (28.7 to 31.5)
c=12, n=1000 194 ms 15.1 ms 12.4x (10.7 to 14.5)
c=50, n=1000 265 ms 32.2 ms 7.9x (7.0 to 8.4)
c=3, n=250 9.46 ms 0.40 ms 23.9x (18.5 to 25.4)
c=3, n=4000 2.75 s 0.116 s 23.6x (23.1 to 24.2)
c=3, n=8000 11.7 s 0.478 s 23.3x (22.9 to 25.7)
BasicMotions 2.88 s 0.217 s 13.3x (13.1 to 13.7)
JapaneseVowels 4.61 s 0.787 s 5.8x (5.7 to 6.0)

Tests added to test_msm.py, against a plain Python full cost matrix reference with no Numba or fastmath:

  • randomised univariate, independent and dependent cases, with unequal lengths and c from 0 to 1e6 (rtol 1e-12)
  • exact equality on tie-heavy inputs: integer-valued series, long runs of repeated values, constant series and x
    == y
  • mixed dtypes (int, float32, float64) on both paths
  • pairwise and test x train matrices on BasicMotions and JapaneseVowels, equal cell by cell to per-pair
    msm_distance

Each cell of the dependent kernel previously made three channel loops
over strided column views: the squared distance plus two _cost_dependent
calls, each summing four squared distances. Most of those sums are
already known from neighbouring cells: the split diameter is the
previous row's d1, the merge diameter is the previous column's d1,
sq(x_{i-1}, x_i) is constant along a row and sq(y_{j-1}, y_j) depends
only on y. Each cell now makes one pass over time-major contiguous
copies, accumulating d1 and the two midpoint distances. The split/merge
choice is a select rather than a branch, since the comparison is data
dependent.

Distances can differ from the previous kernel in the last bit (fastmath
sums in a different order); no split/merge decision changed in testing.

Adds tests against a plain Python full cost matrix reference: randomised
cases, exact equality on tie-heavy integer-valued inputs, mixed dtypes,
and pairwise matrices on BasicMotions and JapaneseVowels against
per-pair calls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TonyBagnall TonyBagnall added the distances Distances package label Oct 19, 2026
@aeon-actions-bot aeon-actions-bot Bot added the enhancement New feature, improvement request or other non-bug code enhancement label Oct 19, 2026
@aeon-actions-bot

Copy link
Copy Markdown
Contributor

Thank you for contributing to aeon

I have added the following labels to this PR based on the title: enhancement.
I would have added the following labels to this PR based on the changes made: distances, however some package labels are already present.

The Checks tab will show the status of our automated tests. You can click on individual test runs in the tab or "Details" in the panel below to see more information if there is a failure.

If our pre-commit code quality check fails, please run pre-commit locally and push the fixes to your PR branch.

Don't hesitate to ask questions on the aeon Discord channel if you have any.

PR CI actions

These checkboxes will add labels to enable or disable CI functionality for this PR. This may not take effect immediately, and a new commit may be required to run the new configuration.

  • Run pre-commit checks for all files
  • Run mypy typecheck tests
  • Run all pytest tests and configurations
  • Run all notebook example tests
  • Run numba-disabled codecov tests
  • Disable numba cache loading
  • Regenerate expected results for testing
  • Push an empty commit to re-run CI checks

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

distances Distances package enhancement New feature, improvement request or other non-bug code enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant