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>
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 bymsm_distanceandmsm_pairwise_distancewithindependent=Falseand 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_dependentcalls for split and merge, each summingfour squared distances. Most of those sums are already known from neighbouring cells. Writing
sq(a, b)for thesquared distance between two time points:
sq(x_{i-1}, y_j)is the previous row's match cost, kept in a rolling buffersq(x_i, y_{j-1})is the match cost of the cell to the leftsq(x_{i-1}, x_i)is constant along a rowsq(y_{j-1}, y_j)depends only ony, so it is computed once per callEach cell now makes a single pass over time-major contiguous copies of
xandy, accumulating only the matchcost 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 thirdof 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 runson one core, interquartile range in brackets. Random normal series unless stated; datasets are test x train.
Tests added to
test_msm.py, against a plain Python full cost matrix reference with no Numba or fastmath:== y
msm_distance