Skip to content

Camera tracking: factor narrow camera systems within their band - #2342

Merged
bflatastic merged 1 commit into
storytold:mainfrom
vangel76:camtrack-banded-cholesky
Oct 29, 2026
Merged

bflatastic merged 1 commit into
storytold:mainfrom
vangel76:camtrack-banded-cholesky

Conversation

@vangel76

@vangel76 vangel76 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Bundle adjustment can produce a banded reduced camera system when feature tracks span only nearby frames. cholesky_solve now detects that band from the lower triangle and skips structural zeros in factorization and triangular solves. Wide systems fall back to the dense path early, including systems with shared global parameters.

The four-lane dot-product alignment stays unchanged. Zero accumulators still perform the otherwise-skipped signed-zero subtractions, preserving -0.0/+0.0 behavior. Matrix dimensions are checked before indexing.

Fixes #2325

Measurements

Release builds based on upstream/main 99190488, Ryzen 7 7800X3D. RAYON_NUM_THREADS=1, pinned to core 12, perf stat -e instructions:u,cycles:u; five alternating original/changed pairs, medians. Each BA call uses 120 cameras, 1,000 points, fixed focal length, two gauge-fixed cameras and up to three LM iterations. Matrices remain densely stored.

Scenario / metric Before After
Track length 12, 10 BA calls: user instructions 8,340,108,343 3,395,585,905
Track length 12: user cycles 2,386,110,175 785,039,895
Track length 12: timed BA loop 545.363 ms 213.523 ms
Track length 120, 2 BA calls: user instructions 44,645,814,936 44,712,622,245
Track length 120: user cycles 8,704,319,932 8,701,443,643
Track length 120: timed BA loop 1953.148 ms 1954.648 ms

Short tracks use 59.29% fewer instructions (2.46× instruction ratio); the dense control adds 0.15% instructions. All hashes match within each scenario. These are BA-kernel measurements, not a speedup measured for the complete camera tracker. Shared-focal/distortion parameters can widen the band; this does not implement a bordered sparse system or reduce matrix storage. Counters include process setup/input generation; timing includes identical input cloning.

Verification

  • cargo fmt --all --check
  • cargo clippy -p effectcraft-track --lib --tests -- -D warnings
  • cargo test --release -p effectcraft-track: 61 library tests passed, 3 ignored.
  • Committed tests compare every factor and solution value with to_bits() against the previous dense algorithm on 648 SPD cases: sizes 1–127, six bandwidth choices, three seeds and scales 1, 1e-100 and 1e100. Separate cases cover signed zeros, underflow, invalid dimensions and size overflow.
  • Uncommitted full short-track BA comparison: all camera/point values, initial/final costs and iteration counts match bitwise with one Rayon thread. With four threads, all camera/point values and iterations also match; cost uses a 1e-12 relative tolerance because the original parallel reduction itself can return different bits for identical inputs (reproduced independently on the unchanged cost implementation).
  • No public signature changes. Throwaway benchmarks/reference modules are excluded from the commit.

Overlap

Only camtrack/linalg.rs changes: cholesky_solve and its regression tests. umeyama, edited by open PR #1876, is byte-for-byte unchanged. bundle.rs, geometry.rs, and all other agents' files are untouched. This branch starts from fresh upstream/main and does not depend on #2338, #2340 or #2341.

Generated with Codex

Summary by CodeRabbit

  • Bug Fixes
    • Invalid matrix dimensions are now rejected safely, including dimensions that would overflow during size calculation. Empty systems remain supported.
    • Banded systems use focused computations when appropriate, while dense systems retain their established floating-point behavior. Results remain consistent across different matrix sizes and scales, including underflow and signed-zero cases.

Detect narrow lower-triangle bandwidth and skip structural zeros while preserving dot-product lanes and signed-zero arithmetic. Keep a dense fallback for wide systems and compare factors and solutions against the original algorithm.

Fixes storytold#2325
@coderabbitai

coderabbitai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 9919048 and e5afbcc.

Files selected for processing: 1
  • crates/track/src/camtrack/linalg.rs

Limit details: You’ve used all 10 included reviews currently available.

Walkthrough

cholesky_solve validates dimensions and uses detected bandwidth to limit factorization and triangular-solve work when applicable. Tests compare its factor and solution bit patterns with a dense reference and cover signed-zero behavior and invalid dimensions.

Changes: Banded Cholesky solve

Layer / File(s) Summary
Banded solve and dense equivalence tests
crates/track/src/camtrack/linalg.rs
cholesky_solve checks matrix and RHS dimensions before indexing. For finite RHS values, it detects bandwidth and falls back to full width when the bandwidth is at least n / 2. Factorization and triangular solves limit work to the band while preserving tested dense-reference bit patterns. Tests cover multiple sizes, bandwidths, scales, signed-zero cases, invalid dimensions, and the empty system.

Priority: Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Possibly related issues

  • storytold/effectcraft#2328 concerns Schur-reduction cost in bundle adjustment; this PR optimizes the camera-system solve, not Schur reduction.

Possibly related PRs

Suggested reviewers: echelon

Merge Risk: Minimal · up to e5afb

The banded solve appears ready to merge after normal checks; no actionable merge-blocking risk is established.

Architecture risk: Low · up to e5afb

The change affects 1 system.

Changed systems: crates

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — crates (service) was modified; 1 changed file maps to changed impact.

Before / after behavior

  • observed — Modified behavior in crates/track/src/camtrack/linalg.rs: cholesky_solve now validates matrix and RHS lengths before indexing, derives a bandwidth when all RHS values are finite, and falls back to full bandwidth when the detected width is at least n / 2. The factorization inner product now begins at the band boundary instead of always processing all preceding columns.
  • observed — Modified behavior in crates/track/src/camtrack/linalg.rs: Rows below each Cholesky pivot are limited to the next band rows, and their dot products begin at a four-element-aligned offset to preserve dot_n lane ordering while skipping full groups of out-of-band zeros.
  • observed — Modified behavior in crates/track/src/camtrack/linalg.rs: The forward solve now processes only the preceding band entries. When the accumulator is zero, it first performs the omitted out-of-band subtractions to preserve signed-zero behavior.
  • observed — Modified behavior in crates/track/src/camtrack/linalg.rs: The backward solve now limits its main loop to band following entries and performs the omitted tail subtractions only when the accumulator is zero, preserving the dense implementation’s signed-zero result.
Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: limiting factorization of narrow camera systems to their detected band.
Linked Issues check Passed The changes satisfy the coding requirements in directly linked issue #2325. cholesky_solve detects lower-triangle bandwidth, limits Cholesky factorization and triangular solves to the band, preserve…
Out of Scope Changes check Passed The pull request changes only crates/track/src/camtrack/linalg.rs. The dimension checks, signed-zero handling, dense fallback, and regression tests directly support the cholesky_solve objective in…
Full details: Linked Issues check

Explanation

The changes satisfy the coding requirements in directly linked issue #2325. cholesky_solve detects lower-triangle bandwidth, limits Cholesky factorization and triangular solves to the band, preserves four-lane dot_n alignment, and selects the dense path for wide systems. The added tests compare factors and solutions bitwise across band widths and scales, and cover signed zero, underflow, invalid dimensions, and zero-sized systems.

Full details: Out of Scope Changes check

Explanation

The pull request changes only crates/track/src/camtrack/linalg.rs. The dimension checks, signed-zero handling, dense fallback, and regression tests directly support the cholesky_solve objective in #2325. No unrelated implementation or test changes are shown.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Usage-based review receipt

  • Mode: Continue automatically
  • Reviewed files: 1
  • Waived: $0.25 (charged $0.00)
  • View usage details

Note

This review exceeded your plan’s limits and used usage-based reviews—free during trial, billed after paid activation unless disabled. Manage usage-based reviews.

Comment @coderabbitai help to get the list of available commands.

@vangel76

vangel76 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor Author

This PR is one of 17 from a speed audit of crates/track and its callers (issues #2317-#2330, #2322, #2349). Overview of what each one measured, so the effect is visible in one place:

PR Area Measured (single thread, before -> after) Output
#2332 Subspace Warp plan() 22.3 G -> 4.8 G instructions (4.7x, Automatic); 33.7 G -> 5.2 G (6.5x, Enhanced); 24x on clean low-rank trajectories rounding level, at most 2.7e-6 px
#2331 KLT lk_point (Warp Stabilizer analysis, mask tracking) analysis 3.84 s -> 1.44 s (2.6x), mask tracking 1.57 s -> 0.95 s bit-identical
#2339 Point tracker (NCC taps, gradient window) 54.6 G -> 31.0 G instructions (1.76x) bit-identical
#2334 Face tracker polygon test 12.5 G -> 2.2 G instructions (5.7x) bit-identical
#2337 Mask tracker frame clone 4K: 3.42 s -> 2.19 s (-36 %); 1080p -5 % identical outline
#2335 Roto Brush propagate band crop 208.5 G -> 172.0 G instructions per 20 frames (-17.5 %) bit-identical
#2333 Roto Brush distance transform 3.04 G -> 2.80 G instructions (-8 %) bit-identical
#2346 Camera solve, long tracks (matrix-free Schur) 44.6 G -> 4.2 G instructions (10.6x), timed loop 1966 ms -> 216 ms practically identical (reprojection < 1e-9 px), direct fallback
#2342 Camera solve, short tracks (band Cholesky) 8.34 G -> 3.40 G instructions (2.46x) bit-identical
#2338 triangulate allocations 2.38 G -> 1.32 G instructions (1.81x) bit-identical
#2340 BA Jacobian on the stack 13.4 G -> 12.6 G instructions (-6.2 %) bit-identical
#2345 null_vector without unused SVD output -0.96 % instructions bit-identical
#2351 Point tracker, 3x3 refine when the motion was small (stacked on #2339) another 31.0 G -> 20.2 G instructions (1.54x) on top of #2339 not bit-identical by design, accuracy unchanged in all 8 ground-truth cases
#2348 Warp Stabilizer / Camera Tracker analysis lookup per render 0.73 ms -> 0.014 ms per call for 300 frames (about 50x) identical
#2347 Warp Stabilizer default plan computed twice first render 2313 ms -> 1156 ms (-50 % instructions) identical pixels
#2350 Warp analysis shares one luma pyramid 87.7 G -> 81.8 G instructions (-6.8 %) identical analysis JSON
#2341 Input validation (panics, NaN, allocation budget) correctness fix, +6.1 % instructions on valid input valid input unchanged

Notes:

@bflatastic

Copy link
Copy Markdown
Member

Merged, thank you @vangel76! It landed on main with a batch of community PRs, after a full workspace build and test run on Windows against the refactored main (fmt, strict clippy, about 4,100 tests, layers, assets and wasm).

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.

camtrack: bundle adjustment factorises the banded camera system densely (2.45x on short tracks)

2 participants