Dispatch — templated vs runtime-sized; ISA backends
fast-kalman has two orthogonal dispatch layers, both invisible at the user
API surface. Code that calls kalman.LinearKalmanFilter(nx, nz) doesn't have
to know either is happening.
Layer 1 — templated vs runtime-sized C++ classes
The library has fixed-size C++ templates (LinearKalmanFilter<NX, NZ>,
AdaptiveKalmanFilter<NX, NZ>, LinearKalmanFilterBank<NX, NZ>) and a single
runtime-sized class per filter kind (DynLinearKalmanFilter,
DynAdaptiveKalmanFilter, DynLinearKalmanFilterBank, etc.) backed by
std::vector<double>.
The Python factory picks based on (nx, nz):
| (nx, nz) | Python returns |
|---|---|
(1,1), (2,1), (3,1), (4,2), (4,4), (6,2), (6,3), (8,4), (8,8) |
templated _LinearKalmanFilter_NX_NZ |
| anything else | _DynLinearKalmanFilter |
The 9 kept pairs are the Tier 1 + Tier 2 shapes the finance benchmark workload hits. They get the full compiler unroll / autovectorize treatment. Everything else uses runtime loop bounds — slower per call (~2-5× depending on shape) but still C++; no Python loop on the hot path.
EKF, IteratedEKF, UKF, EnKF, and RangeBearingEKF2D are always
runtime-sized in the binding. Their per-step Python callbacks dominate the
per-tick budget; specializing them per (NX, NZ) was pure compile-cost.
Why the trim
Before v0.3.0 the binding instantiated nb::class_<> for every filter kind ×
every (nx, nz) ∈ [1..8] × [1..8]. Compiling bindings.cpp cost several GB
of RAM and OOM-killed on small build machines. v0.3.0 brought peak RSS down to
~937 MB single-threaded with no observable speed regression on the 9 kept
shapes.
Which class did I get?
kf = kalman.LinearKalmanFilter(2, 1)
type(kf).__name__ # '_LinearKalmanFilter_2_1' — templated
kf = kalman.LinearKalmanFilter(5, 3)
type(kf).__name__ # '_DynLinearKalmanFilter' — runtime
Both expose the same predict / correct / update / update_many / smooth API surface and accept the same x, P, F, Q, H, R property setters.
When runtime-sized isn't enough
delta_t= and measurement_variance= overrides on update_many / smooth
are only implemented on the templated path right now. The Dyn class raises
ValueError if you pass them. If you need varying-dt on a non-kept dim, file
an issue — the kinematic predict path is in scope for a future release.
Layer 2 — ISA backend at import time
The wheel ships one Python module per ISA backend (_core_generic,
_core_x86_avx, _core_x86_avx2, _core_x86_avx512,
_core_linux_aarch64_sve, …). All compile from the same bindings.cpp +
kalman.cpp translation units with different -march= flags. We don't write
any manual SIMD intrinsics; the win comes from -march= letting gcc 16
autovectorize the small Vec / Mat templates for the target ISA.
python/kalman/__init__.py walks a priority list at import time and picks
the first module whose features the host CPU supports:
avx512 X86_AVX512F (Skylake-X / Ice Lake+, 2017)
avx2 X86_AVX2 + X86_FMA (Haswell+, 2013)
avx X86_AVX (Sandy/Ivy Bridge, 2011-2013)
linux_aarch64_sve ARM_SVE
linux_aarch64_asimd ARM_ASIMD
macos_arm64_neon ARM_ASIMD
loongarch_lasx LA_LASX
loongarch_lsx LA_LSX
powerpc64_vsx PPC_VSX
generic (always wins as a fallback)
Feature detection runs through goblin-cpu-features, which is a tiny runtime
dependency.
Inspect what got picked:
import kalman
kalman.backend()
# 'avx512'
kalman.available_backends()
# [('avx512', True, True), ('avx2', True, True), ('avx', True, True),
# ('generic', True, True), ...]
# (short_name, compiled_into_wheel, supported_by_host)
Both layers in one diagram
user calls kalman.LinearKalmanFilter(nx, nz)
│
▼
┌─────────────────────────────────┐
│ python/kalman/__init__.py │ import-time SIMD pick
│ _core = _select_backend() │ (Layer 2)
└─────────────────────────────────┘
│
▼
_core.LinearKalmanFilter(nx, nz)
│
▼
┌─────────────────────────────────┐
│ construct_linear_dispatch │ per-call shape pick
│ in src/python/bindings.cpp │ (Layer 1)
└─────────────────────────────────┘
┌───┴───┐
kept? │ │ no
yes ▼ ▼
_LinearKF _DynLinearKalmanFilter
_NX_NZ
(templated) (runtime-sized)