Skip to content

Add batched block-tridiagonal CUDA linear solver for independent systems - #2954

Open
kshaji3 wants to merge 1 commit into
borglab:developfrom
kshaji3:gpu-batched-linear-solvers
Open

kshaji3 wants to merge 1 commit into
borglab:developfrom
kshaji3:gpu-batched-linear-solvers

Conversation

@kshaji3

@kshaji3 kshaji3 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor
  • This PR adds a cuSOLVER/cuBLAS backend that factorizes and solves block-tridiagonal semi-positive definite linear systems in a single batched call
  • It also sets up the Hessian assembler that builds those block-diagonal normal equations.
  • Also adds appropriate unit tests

New classes

  • BatchedBlockTridiagonalCholeskySolver (gtsam/linear/cuda/internal): factorize()/solve() over DeviceArray<double> diagonal/off-diagonal blocks and RHS, reusing persistent cuSOLVER/cuBLAS handles and device buffers
  • BatchedBlockHessianAssembler (gtsam/linear/cuda/internal): builds the block-tridiagonal A^T A / A^T b blocks for a batch of linearized systems directly on the GPU via cuBLAS
  • BatchedSolverOptions (gtsam/linear/cuda/internal): the device-selection struct shared by every batched CUDA backend, including ones built later
  • For device settings: use (deviceId -1 keeps the calling thread's current device, >= 0 pins an instance to one device).

Follow up PR - #2955

kshaji3 pushed a commit to kshaji3/gtsam that referenced this pull request Nov 27, 2026
Adds BatchedBlockTridiagonalLevenbergMarquardtOptimizer and
BatchedBlockTridiagonalDeviceGradients together, since they are a
matched forward/backward pair used jointly in practice (one GTSAM
batched solve per device in a multi-process training setup, with the
optimizer producing the forward solution and the gradients class
producing the backward signal for the surrounding neural-network
parameters) and cannot be used independently of each other for that
use case. Both build on the batched block-tridiagonal linear solver
and Hessian assembler added in borglab#2954.

BatchedBlockTridiagonalLevenbergMarquardtOptimizer runs Levenberg-
Marquardt over B independent nonlinear factor graphs of the same
block-tridiagonal shape in a single batched GPU call. This targets
GTSAM's existing data-parallel-training use case -- many small,
independent, same-shaped graphs (e.g. one per timestep/window) --
where per-item CPU or single-graph-GPU optimization is dominated by
kernel-launch/dispatch overhead rather than compute. Per-graph lambda,
acceptance, and convergence state are tracked independently per item
(BatchedBlockTridiagonalLevenbergMarquardtItemResult), and the
optimizer reuses the same LM damping/acceptance policy as the existing
single-graph implementation (gtsam::internal::LevenbergMarquardtPolicy)
rather than reimplementing it, so behavior such as diagonal damping
stays consistent with gtsam::LevenbergMarquardtOptimizer. optimize()
runs to convergence per gtsam::LevenbergMarquardtParams; polish(n)
takes n further accepted-if-improving steps per item afterward.
BatchedFactorGroupHook lets a caller batch an expensive, cross-item
factor evaluation across the whole batch in one call instead of once
per item per graph.

BatchedBlockTridiagonalDeviceGradients is a batched GPU adjoint/
gradient API over the same batch of block-tridiagonal systems, tested
against gtsam::Gradients on scalar CPU graphs.

Multi-GPU usage: when BatchedSolverOptions.deviceId is left at its
default (-1), an instance binds to whatever CUDA device is already
current for the constructing thread rather than forcing a particular
device. In practice this is used from a multi-process, one-GPU-per-
process training setup: each process selects its own device before
constructing the optimizer and gradients instances, so one batched
pair runs independently per GPU. Cross-device gradient synchronization
of the surrounding neural-network parameters is handled externally
(e.g. PyTorch DistributedDataParallel), not by these classes.

Tests: testCudaBatchedBlockTridiagonalLevenbergMarquardt.cpp compares
batched results against gtsam::LevenbergMarquardtOptimizer on scalar
CPU Pose2 graphs, including an ill-scaled chain graph with diagonal
damping enabled (one LM step), and exercises the factor-group hook
path. A timing harness (timing/batched_lm) reports linearization,
assembly, transfer, factorize, solve, and total time separately
against a scalar CPU baseline, measured on DFG-TAMP's production
chain-graph shape.

This supersedes the separately-proposed optimizer-only PR; it and this
PR's prior gradients-only content are combined here so the forward and
backward paths needed together for multi-GPU training ship as one
reviewable, independently buildable unit, while the batched linear
solver (borglab#2954) remains a separate foundational PR that both depend on.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Adds a persistent cuSOLVER/cuBLAS backend that factorizes and solves B
independent block-tridiagonal SPD linear systems in a single batched GPU
call (block Thomas algorithm), plus the Hessian assembler that builds
those block-tridiagonal normal equations from a batch of Jacobians on
the device. This targets graphs whose factors only couple one or two
adjacent variable blocks -- chain/window-graph structures such as
DFG-TAMP's build_window_graph -- reducing factorize cost to
O(numBlocks * blockDim^3) instead of forming and factorizing a dense
n x n Hessian per item.

This is scoped as a standalone linear-algebra layer, analogous to the
existing LinearSolverSession split of system representation from
backend selection and factorize/solve. It does not itself expose an
optimizer or a differentiation API; those are separate, independent
consumers of this solver (to follow as separate PRs) and should not
need to re-implement block-tridiagonal factorization or Hessian
assembly themselves.

New classes:
- BatchedBlockTridiagonalCholeskySolver (gtsam/linear/cuda/internal):
  factorize()/solve() over DeviceArray<double> diagonal/off-diagonal
  blocks and RHS, reusing persistent cuSOLVER/cuBLAS handles and device
  buffers across calls instead of allocating per step.
- BatchedBlockHessianAssembler (gtsam/linear/cuda/internal): builds the
  block-tridiagonal A^T A / A^T b blocks for a batch of linearized
  systems directly on the GPU via cuBLAS, avoiding a host round-trip.
- BatchedSolverOptions (gtsam/linear/cuda/internal): the device-
  selection struct shared by every batched CUDA backend built on top of
  this solver (deviceId -1 keeps the calling thread's current device,
  >= 0 pins an instance to one device).

Tests: testCudaBatchedBlockHessianAssembler.cpp and
testCudaBatchedBlockTridiagonalCholeskySolver.cpp validate assembly and
factorize/solve against an Eigen ldlt() reference on the equivalent
dense system, including rejection of a non-positive-definite block.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@kshaji3
kshaji3 force-pushed the gpu-batched-linear-solvers branch from 6c6e4fb to 85ee321 Compare December 2, 2026 15:23
kshaji3 pushed a commit to kshaji3/gtsam that referenced this pull request Dec 2, 2026
Adds BatchedBlockTridiagonalLevenbergMarquardtOptimizer and
BatchedBlockTridiagonalDeviceGradients together, since they are a
matched forward/backward pair used jointly in practice (one GTSAM
batched solve per device in a multi-process training setup, with the
optimizer producing the forward solution and the gradients class
producing the backward signal for the surrounding neural-network
parameters) and cannot be used independently of each other for that
use case. Both build on the batched block-tridiagonal linear solver
and Hessian assembler added in borglab#2954.

BatchedBlockTridiagonalLevenbergMarquardtOptimizer runs Levenberg-
Marquardt over B independent nonlinear factor graphs of the same
block-tridiagonal shape in a single batched GPU call. This targets
GTSAM's existing data-parallel-training use case -- many small,
independent, same-shaped graphs (e.g. one per timestep/window) --
where per-item CPU or single-graph-GPU optimization is dominated by
kernel-launch/dispatch overhead rather than compute. Per-graph lambda,
acceptance, and convergence state are tracked independently per item
(BatchedBlockTridiagonalLevenbergMarquardtItemResult), and the
optimizer reuses the same LM damping/acceptance policy as the existing
single-graph implementation (gtsam::internal::LevenbergMarquardtPolicy)
rather than reimplementing it, so behavior such as diagonal damping
stays consistent with gtsam::LevenbergMarquardtOptimizer. optimize()
runs to convergence per gtsam::LevenbergMarquardtParams; polish(n)
takes n further accepted-if-improving steps per item afterward.
BatchedFactorGroupHook lets a caller batch an expensive, cross-item
factor evaluation across the whole batch in one call instead of once
per item per graph.

BatchedBlockTridiagonalDeviceGradients is a batched GPU adjoint/
gradient API over the same batch of block-tridiagonal systems, tested
against gtsam::Gradients on scalar CPU graphs.

Multi-GPU usage: when BatchedSolverOptions.deviceId is left at its
default (-1), an instance binds to whatever CUDA device is already
current for the constructing thread rather than forcing a particular
device. In practice this is used from a multi-process, one-GPU-per-
process training setup: each process selects its own device before
constructing the optimizer and gradients instances, so one batched
pair runs independently per GPU. Cross-device gradient synchronization
of the surrounding neural-network parameters is handled externally
(e.g. PyTorch DistributedDataParallel), not by these classes.

Tests: testCudaBatchedBlockTridiagonalLevenbergMarquardt.cpp compares
batched results against gtsam::LevenbergMarquardtOptimizer on scalar
CPU Pose2 graphs, including an ill-scaled chain graph with diagonal
damping enabled (one LM step), and exercises the factor-group hook
path. A timing harness (timing/batched_lm) reports linearization,
assembly, transfer, factorize, solve, and total time separately
against a scalar CPU baseline, measured on DFG-TAMP's production
chain-graph shape.

This supersedes the separately-proposed optimizer-only PR; it and this
PR's prior gradients-only content are combined here so the forward and
backward paths needed together for multi-GPU training ship as one
reviewable, independently buildable unit, while the batched linear
solver (borglab#2954) remains a separate foundational PR that both depend on.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@kshaji3
kshaji3 marked this pull request as ready for review December 2, 2026 15:36
@kshaji3

kshaji3 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Updated PR description.

This branch has not been deployed

No deployments
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