Skip to content

Latest commit

 

History

History
168 lines (125 loc) · 6.99 KB

PATCHING.md

File metadata and controls

168 lines (125 loc) · 6.99 KB

How to patch STUMPY with NumFast

No package, no install step: one file, one import.

import sys
sys.path.insert(0, "path/to/numfast_stumpy/patch")   # or copy the file next to your code

import numfast_stumpy
numfast_stumpy.patch()

import stumpy
mp = stumpy.mass(Q, T)      # unchanged API, computed by NumFast on the GPU

patch() prints one line and returns the same facts as a dict:

numfast_stumpy: patched core.sliding_dot_product, stumpy.stump |
GPU NVIDIA GeForce RTX 2060 (Vulkan, driver 621.40) | double-float: cpu-fallback

numfast_stumpy.info() returns that adapter/backend/driver description without patching anything — useful when someone reports "it did not get faster" and you need to know which adapter was actually picked.

Full report shape:

{'patched': True,
 'targets': ['sliding_dot_product'],
 'precision': 'double-float',
 'df_backend': 'gpu' | 'cpu-fallback'}

df_backend is measured, not assumed: the package probes the shader compiler and falls back to a float64 host path when the compiler rewrites the error-free transformations away. See TESTING.md.

What is replaced

name why
stumpy.stump the matrix profile itself: self-join, normalized, p=2, k=1 goes to the GPU kernel
stumpy.core.sliding_dot_product the hot primitive behind mass and friends
stumpy.core._sliding_dot_product not patched: a numba dispatcher; swapping it breaks jitted callers
stumpy.core.mass not patched: its wrapper handles M_T/Σ_T ordering, p≠2, normalize=False and NaN/Inf semantics that a bare GPU call does not reproduce (measured: 16 failures)

Everything else keeps STUMPY's own code. Chains (allc, atsc), segmentation (fluss, floss), motifs and snippets all work on an already computed array and are untouched.

Driver switch

sliding_profile(T, m, driver="auto") accepts "gpu", "cpu" or "auto". The NumFast driver layer is frozen, so the switch lives inside the extension: same interface, same four columns, two implementations behind it. "auto" takes the GPU when an adapter exists and the series is long enough to amortise the transfer, the CPU otherwise.

What is NOT replaced yet

  • aamp (non-normalized), AB-joins, k > 1: the GPU path covers the self-join, normalized, p=2, k=1 case and defers everything else to STUMPY.
  • mstump, scrump, stumpi, ostinato, stimp: still on the CPU. They need their own wrappers on top of the same kernel.

So the honest state: stump runs on the GPU (19-33× measured) and sliding_dot_product is exact; the rest of the API is untouched and still works, just without acceleration.

Fallback and safety

  • patch() calls into NumFast first and raises if there is no GPU adapter. It does not leave a half-patched library behind.
  • unpatch() restores the originals.
  • If NumFast is not installed, nothing happens; STUMPY runs exactly as before.

Precision, stated up front

NumFast computes in float32. Against STUMPY's float64 path:

  • distance profiles (mass) agree to about 1e-6 relative;
  • the matrix profile (stump) is much rougher: median 5e-4 absolute, up to 2.3e-2 on near-duplicate windows (n=8192, m=50). The recurrence accumulates f32 error along a diagonal, and two neighbours 0.01 apart cannot be separated;
  • raw dot products are exact to 3e-14 through double-float.

That is enough for a distance profile and not enough for bit-exact equality. A double-float accumulation mode exists in the kernel and reaches 3e-14 relative against float64 at 1.7× the cost on the measured workload (see TESTING.md); it is not exposed through this patch yet. STUMPY's own unit tests assert atol=1.5e-07 on dot products with magnitudes around 1e5, which float32 cannot represent (one ulp at 1e5 is 0.0078). See TESTING.md for the measured result and what it would take to close the gap.

Requirements

  • Python 3.11+
  • NumFast with the Sliding extension (this repository's demo uses the same WGSL)
  • a GPU that speaks Vulkan, DX12 or Metal — no CUDA, no admin rights

Why not a PyPI package

Not yet. A package implies a support surface: version pinning, wheels, CI against several STUMPY releases. Until the matrix-profile kernel exists and the test picture is clean, a single file plus these instructions is the honest shape.

If you are a STUMPY maintainer

Where the seam is, in your codebase:

  • stumpy.core.sliding_dot_product — a leaf function. Replacing it is invisible to everything above.
  • stumpy.stump — one monolithic numba routine. It cannot be partially replaced, so any alternative backend has to provide it whole, with the same four-column output.

A shape that would work for you, and that we would be glad to implement:

  1. stumpy/backend.py with three callables: sliding_dot_product(Q, T), stump(T, m, ...), available().
  2. The default implementation is the current code, unchanged.
  3. Registration through an entry point, or an explicit stumpy.set_backend(...).
  4. The existing tests stay the contract — test_core.py and test_stump.py are what we run, not a bespoke suite.

Two traps we hit, so you do not have to

  • core._sliding_dot_product is a numba dispatcher referenced by name from jitted code. Swapping it for a Python function fails at typing time (Untyped global name '_sliding_dot_product', three failures in test_p_norm_distance_profile). Only the Python-level function is safe to replace.
  • A checkout directory named after the import shadows the installed module (implicit namespace package), so running from inside numfast_stumpy/ imports the folder, not the file. Costs ten minutes and one confusing traceback.

Measured, so the numbers are not abstract

platform backend n=16 384, m=50 speedup vs STUMPY
Windows 11 Vulkan (native driver) 73 ms 20×
Windows 11 D3D12 76 ms 19×
WSL2 Ubuntu 24.04 OpenGL (Mesa, dzn rejected) 114 ms 3.5×

Same GPU in all three rows. The backend matters more than the card: the WSL stack falls back to OpenGL because dzn is not a conformant Vulkan implementation, and that costs a factor of five.

Precision, restated for a maintainer

  • The matrix profile itself runs in float32 and still passes test_stump.py (29/29), because the recurrence is restarted from an exact dot product every 128 steps.
  • sliding_dot_product is compared at atol=1.5e-07 on values around 1e5, which float32 cannot reach: one ulp there is 0.0078. It uses double-float; on compilers that rewrite the error-free transformations it falls back to a float64 host path and says so through df_status().

Licensing, since it will come up

STUMPY is BSD-3 and stays untouched. The extension lives in this repository under AGPL-3.0, with a commercial licence available on request; nothing here is vendored into STUMPY. If the project ever wanted this inside the main repository, the kernel would have to be relicensed for that contribution, and that is a conversation worth having explicitly rather than by accident.