Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

perception-accel

verify license: MIT python

Profiling-driven C++ acceleration of the two numeric hotspots in a real-time stereo perception loop, exposed to Python via pybind11 / Eigen. Drop-in: the C++ kernel and a bit-for-bit pure-Python reference implement the same contract, and the package works with or without the compiled extension.

Measured per-call latency: RANSAC 343 µs → 32 µs (10.7× faster), Kalman 18 µs → 6 µs (3.0× faster)

What & why

Profiling a real-time stereo tracking loop found two per-frame hotspots:

  • ransac_disparity — a 20-iteration consensus loop, ~385 µs/call in pure Python.
  • kf_predict / kf_update — tiny-matrix Kalman steps where NumPy's per-call Python overhead dominates the actual arithmetic.

Both are pure numeric functions (NumPy in / NumPy out), so they move to C++ without changing the calling architecture: adaptive process noise, the gating decision and NIS telemetry stay on the Python side.

Design: correctness first

  • perception_accel/_fallback.py is the reference pure-Python implementation.
  • The C++ extension (_accel) must match it to within tolerance — tests/test_accel.py verifies this over 2000 randomized cases (predict, update, NIS, gating) plus RANSAC behavior.
  • If the compiled extension is absent, the package transparently falls back to Python (USING_CPP tells you which path is active). Nothing breaks if C++ isn't built.

Build

Requires g++ (C++17), pybind11 (pip) and Eigen3.

cd perception_accel && ./build.sh          # produces _accel*.so

Test

python3 tests/test_accel.py

The test asserts C++ ≡ Python equivalence, correct gating/RANSAC behavior, and that the C++ path is faster. It passes on the pure-Python path too (skipping the speed asserts).

Use

import perception_accel as accel
d = accel.ransac_disparity(disparities, 20, 10, 0.15, 0.40, seed=0)
x2, P2 = accel.kf_predict(x, P, F, Q)
status, nis, innov, x3, P3 = accel.kf_update(x, P, H, R, z, chi2=9.21, gating=True)

Result

Hotspot Pure Python C++ Speedup
ransac_disparity (20 iterations) 343 µs 32 µs 10.7×
kf_predict + kf_update (6-state) 18 µs 6 µs 3.0×

Median of 5 runs of tests/test_accel.py on an AMD Ryzen 5 7640HS, built with g++ -O3 -march=native. Measured, not estimated — reproduce it with one command. RANSAC gains the most because the 20-iteration consensus loop is pure interpreter overhead; the Kalman kernels gain less because they mainly shed NumPy's per-call dispatch cost.

License

MIT — see LICENSE.

About

Profiling-driven C++/pybind11 acceleration of a real-time perception loop (RANSAC + Kalman), with a verified pure-Python reference

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages