Skip to content

[Caspar] Scope subexpression elimination in Dabseg and CPSE to output parameter groups - #2

Open
sarlinpe wants to merge 1 commit into
caspar-fix-flush-sum-sharedfrom
caspar-barrier-scoped-cse
Open

sarlinpe wants to merge 1 commit into
caspar-fix-flush-sum-sharedfrom
caspar-barrier-scoped-cse

Conversation

@sarlinpe

@sarlinpe sarlinpe commented Dec 4, 2026

Copy link
Copy Markdown
Member

Mirrors symforce-org#475, rebased onto upstream main at acdedd0b and stacked on #1.

Changes relative to the upstream PR: regenerated the 10 golden kernels of the tridiagonal solver test that upstream added in the meantime (symforce_caspar_tridiagonal_test/kernel_chain_*_res_jac*.cu). The source changes and the rosenbrock goldens are unchanged.


Summary

In Caspar kernels, factors write to multiple distinct output parameter groups (e.g., residual out_res, camera pose out_pose_*, camera calibration out_calib_*, and 3D point out_point_*). Between these parameter groups, the kernel emits barrier synchronizations and shared memory reduction flushes (FlushSumShared).

Previously, Common Subexpression Elimination (CSE) in make_dabseg and do_cpse operated globally across the entire factor DAG. When a subexpression (such as -(1 + k*r^2)) computed during the camera pose Jacobian was reused hundreds of lines later in the calibration Jacobian across 12 FlushSumShared barriers, the variable remained live throughout all intermediate reductions. Under __launch_bounds__(1024, 1) (which limits threads to 64 registers), this extreme register pressure (>1.8 KB stack spill) caused registers to be spilled to local memory and corrupted in kernels with high occupancy.

In COLMAP's Caspar bundle adjustment integration (see colmap/colmap#4799), this register corruption caused double-precision calibration Jacobians to evaluate to wildly inaccurate values (~10^4 instead of fractional derivatives).

Proposed Solution

Rather than relying on string heuristics, this PR scopes subexpression elimination by output parameter group:

  1. Accessor: Added group: str | None = None parameter.
  2. Factor.make_kernels: Explicitly tags output accessors with their parameter group (group=k for node variables and group="res" for residual outputs).
  3. CallData & Dabseg: Included group in CallData's hash and equality, preventing Dabseg.data_to_id from deduplicating expressions across group boundaries.
  4. make_dabseg: Resets expr_map across out.group transitions during DAG construction.
  5. do_cpse: Partitions associative/commutative calls by call.group so partial CSE reductions only combine terms within the same group.
  6. function_fixers: Preserves call.group across DAG transformations.

Verification

  • test_generate_solver in symforce_caspar_codegen_test.py passes, confirming that cross-barrier live ranges are eliminated in the test Rosenbrock factor as well (reducing register pressure from 9 to 8 registers in kernel_rosenbrock_res_jac_first.cu).
  • Regenerated all 488 single- and double-precision Caspar kernels in COLMAP. All 17 unit tests in bundle_adjustment_caspar_test pass cleanly in both f32 and f64 without needing any manual kernel workarounds.

… parameter groups

In Caspar kernels, factors write to multiple distinct output parameter groups
(e.g., residual 'res', camera pose, camera calibration, and 3D point).
Between these parameter groups, the kernel emits barrier synchronizations
and shared memory reduction flushes (FlushSumShared).

Previously, Common Subexpression Elimination (CSE) in make_dabseg and
do_cpse operated globally across the entire factor DAG. When a subexpression
(such as -(1 + k*r^2)) computed during the camera pose Jacobian was reused
hundreds of lines later in the calibration Jacobian across 12 FlushSumShared
barriers, the variable remained live throughout all intermediate reductions.
Under __launch_bounds__(1024, 1) (which limits threads to 64 registers), this
extreme register pressure caused registers to be spilled to local memory and
corrupted in kernels with high occupancy.

This change scopes subexpression elimination per output parameter group:
1. Accessor accepts an optional group: str | None parameter, and Factor.make_kernels
   explicitly tags output accessors with their parameter group ('res' or parameter name).
2. CallData includes group in its hash and equality, preventing Dabseg.data_to_id
   from deduplicating identical subexpressions across group boundaries.
3. make_dabseg resets its expression-to-value map across output group transitions.
4. do_cpse partitions calls by call.group so partial associative/commutative
   reductions do not cross group boundaries.
5. Function fixers preserve call.group across DAG transformations.

This eliminates cross-barrier live ranges between parameter groups, preventing
stack spilling and register corruption across reduction flushes.
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.

1 participant