Repository navigation
Commit da15a97
feat(metal): 1-vs-N / K-vs-N kernels + fix pre-existing band-edge bug
Adds dtw_kvn_wavefront and dtw_kvn_wavefront_global MSL kernels plus
four host-side entry points mirroring the CUDA K-vs-N API:
compute_dtw_one_vs_all_metal(series, query_index, opts)
compute_dtw_one_vs_all_metal(query, targets, opts)
compute_dtw_k_vs_all_metal(series, query_indices, opts)
compute_dtw_k_vs_all_metal(queries, targets, opts)
Enables GPU-accelerated k-medoids cluster-assignment on Apple Silicon
(K*N pairs per iteration instead of precomputing N*N). Kernel params
are packed into a struct bound as a single constant buffer; pair
decoding uses divmod over (K, N) instead of upper-triangle enumeration.
Bug fix (affects ALL four wavefront kernels — dtw_wavefront,
dtw_wavefront_global, dtw_kvn_wavefront, dtw_kvn_wavefront_global):
the band-range iteration optimization assumed out-of-band cells in
the rotating scratch buffers retain INF from initialization. That is
false: the 3-buffer rotation reuses each buffer as 'cur' every 3
diagonals, so cells outside the current band can hold real DTW values
from 3 diagonals ago. When the band range shifts by 1 between
diagonals (always happens on odd-parity k+band transitions), the new
band edge reads stale data and produces distances SMALLER than the
true banded DTW.
Pre-existing NxN wavefront banded paths silently returned wrong
answers — the existing test suite never cross-checked banded Metal
against CPU dtwBanded. For the specific seeds we had, the error was
0.12% (random chance that stale cells weren't on the DP path). For
different data the error can exceed 7%: cpu_banded=680.17 vs
gpu_band=633.06 at 75x10000 band=20. Uncovered by a new K-vs-N
banded test.
Fix: after each diagonal's in-band writes, stamp INF into the two
band-adjacent positions (i_lo-1 and i_hi+1). Two extra writes per
diagonal per pair. Performance impact on wavefront banded
workloads: 0-9% slower (75x2500 band=250: 212 -> 232 ms;
75x10000 band=1000: 2010 -> 2083 ms). Tight-band 75x10000 band=100
is unaffected because it uses the row-major dtw_banded_row kernel.
Tests: test_metal_correctness.cpp grows from 9 cases / 91 assertions
to 14 cases / 149 assertions — adds an NxN-wavefront-banded vs CPU
dtwBanded cross-check and four K-vs-N cases (1-vs-N by index, 1-vs-N
external query, K-vs-N unbanded by indices, K-vs-N banded external).
Full ctest suite: 69/69 pass.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 6db6621 commit da15a97
4 files changed
Lines changed: 764 additions & 46 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
38 | 40 | | |
39 | 41 | | |
40 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 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 | + | |
61 | 108 | | |
62 | 109 | | |
63 | 110 | | |
0 commit comments