Skip to content

shaper_calibrate: speed up SHAPER_CALIBRATE smoother fitting ~2x (bit-identical)#914

Open
Hannott wants to merge 1 commit into
KalicoCrew:bleeding-edge-v2from
Hannott:shaper-calibrate-speedup-upstream
Open

shaper_calibrate: speed up SHAPER_CALIBRATE smoother fitting ~2x (bit-identical)#914
Hannott wants to merge 1 commit into
KalicoCrew:bleeding-edge-v2from
Hannott:shaper-calibrate-speedup-upstream

Conversation

@Hannott

@Hannott Hannott commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Speeds up the CPU-bound find_best_shaper autotune that runs after a resonance sweep — the compute that fits shaper candidates and produces the shaper graph (e.g. via Shake&Tune). End-to-end drops ~2.2× (7.5s → 3.5s on synthetic data) with bit-identical results. Pure performance change; self-contained (no dependency on other PRs).

The bottleneck

Profiling find_best_shaper showed ~81% of the time in _get_smoother_smoothing, called ~153,000 times for a full 9-candidate autotune. find_max_accel bisects the max acceleration for each candidate smoother at each test frequency (~30 evaluations per bisection), and every evaluation rebuilt a 100-point grid, evaluated the smoother polynomial, and ran 5 trapezoid integrations — all of which depend only on the smoother, not on the accel/scv values the bisection varies.

The fix

The smoothing offsets are linear in the accel- and scv-dependent terms, so they decompose into fixed geometry integrals of the smoother kernel:

offset_180  = (accel/2) · I2
offset_90_x = scv·Jp1 + (accel/2)·Jp2
offset_90_y = scv·Jn1 - (accel/2)·Jn2

_calc_smoother_integrals computes I2, Jp1, Jp2, Jn1, Jn2 (integrals of t²·w and t·w over the kernel — full range and each half) once per smoother and caches them; _get_smoother_smoothing evaluates the closed form. The bisection's ~30 evaluations per frequency now share one geometry computation.

The masked half-range integrals (t≥0 / t<0) are split exactly the way the original masked-then-integrate code did, so the decomposition is algebraically exact — not an approximation.

Verification

  • Bit-identical: max absolute difference between the closed form and the original computation is 2×10⁻¹⁶ across all 6 smoothers and a wide accel/scv grid. find_best_shaper on synthetic resonance data produces the identical recommended shaper, frequency, vibration, smoothing, and max_accel for every candidate (full result table diff: no differences).
  • Per-evaluation microbenchmark: ~62× faster; end-to-end 7.5s → 3.5s.
  • ruff check / ruff format pass.

The remaining post-sweep cost is estimate_smoother's convolution, intentionally left untouched: the only ways to speed it are FFT convolution (changes numerics) or reducing grid resolution (changes results), neither worth trading the exact-results guarantee for.

Running ShakeTune CREATE_VIBRATIONS_PROFILE massively reduced calculation time. Tested on RPi 4:

08:37:25 // Machine estimated vibration symmetry: 25.7%
08:36:13 // This may take some time (5-8min)
08:36:13 // Machine vibrations profile generation...

Signed-off-by: Åsmund Collin aakjaergaard@gmail.com

@rogerlz

rogerlz commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Nice speedup — the decomposition is exact (trapz is linear and the t>=0/t<0 masks are applied after the mean-shift, same as before), the cache key correctly captures the smoother geometry, and since ShaperCalibrate is created per command and fit_shaper runs in the background process, cache lifetime is a non-issue.

Two small things before merge: please add a Signed-off-by trailer to the commit per our commit conventions, and drop the Co-Authored-By/Generated-with attribution from the commit and PR description.

The build CI failure is just a Docker Hub pull timeout — rerunning it.

@Hannott
Hannott force-pushed the shaper-calibrate-speedup-upstream branch from e1e3f7d to 89d1b6b Compare July 14, 2026 18:12
@Hannott

Hannott commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

@rogerlz Done

find_max_accel bisects the max acceleration for each candidate smoother
at each test frequency, calling _get_smoother_smoothing ~30 times per
bisection (150k+ times for a full autotune). Each call rebuilt a
100-point grid, evaluated the smoother polynomial, and ran 5 trapezoid
integrations - all of which depend only on the smoother, not on the
accel/scv values the bisection varies.

The smoothing offsets are linear in the accel- and scv-dependent terms:
  offset_180  = (accel/2) * I2
  offset_90_x = scv*Jp1 + (accel/2)*Jp2
  offset_90_y = scv*Jn1 - (accel/2)*Jn2
where I2/Jp1/Jp2/Jn1/Jn2 are fixed geometry integrals of the smoother.
Precompute and cache those per smoother (_calc_smoother_integrals) and
evaluate the closed form in _get_smoother_smoothing.

Results are bit-identical (max abs difference 2e-16 across all smoothers
and a wide accel/scv grid; find_best_shaper produces identical
recommendations, vibration, smoothing, and max_accel for every
candidate). End-to-end find_best_shaper on synthetic data drops from
~7.5s to ~3.5s - the compute that runs after a resonance sweep to
generate the shaper graph, so it directly reduces the post-test wait.

Signed-off-by: Åsmund Collin <aakjaergaard@gmail.com>
@Hannott
Hannott force-pushed the shaper-calibrate-speedup-upstream branch from 89d1b6b to 1d5dde2 Compare July 14, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants