CUDA GNC: run GncOptimizer with the CUDA SFM LM inner solver, ~20x over CPU GNC#2593
Open
leolrg wants to merge 3 commits into
Open
CUDA GNC: run GncOptimizer with the CUDA SFM LM inner solver, ~20x over CPU GNC#2593leolrg wants to merge 3 commits into
leolrg wants to merge 3 commits into
Conversation
GncOptimizer<GncParams<CudaSfmLevenbergMarquardtParams>> now works as a drop-in: add the equals() method GncParams expects from its base params type, and accept zero-diagonal sqrt-information in the CUDA SFM conversion, since GNC weighted graphs scale factor information by weights that TLS drives to exactly zero. Add parity tests against CPU GNC (TLS and GM classification on a synthetic BAL problem with injected outliers) and a zero-information weighted-graph regression test. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Instrument GncOptimizer::optimize() with per-outer-iteration stage timings (weight update, weighted graph rebuild, base optimize, cost evaluation), exposed via getTiming(). Add --gnc cpu|cuda to timeCudaSFMBAL with deterministic outlier injection (--gnc-outlier-fraction/-pixels/-seed, corrupting only tracks that keep >= 2 clean views), loss selection, per-iteration timing output, and outlier-classification precision/recall plus inlier-only RMS reprojection error against the injected ground truth. On dubrovnik 16/88/135 with 10% outliers at 12 px (TLS), CUDA GNC is 18-22x faster than CPU GNC at matched classification quality; >95% of remaining CUDA GNC time is host-side graph rebuild and per-iteration re-upload, motivating a device-resident GNC loop. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot couldn't run its full agentic review because no GitHub Actions runner was available. Make sure your repository has a runner available to run Copilot's review, or add a copilot-setup-steps.yml file specifying one with the runs-on attribute. See the docs for more details.
Enables running GncOptimizer with the CUDA SFM Levenberg–Marquardt backend and adds benchmarking/profiling instrumentation to compare CPU vs CUDA GNC performance.
Changes:
- Adds GNC benchmarking mode to
timeCudaSFMBAL(CPU/CUDA backends), including deterministic outlier injection and timing/quality metrics. - Extends CUDA LM profiling (transfer breakdown, per-iteration/per-attempt stats) and adds GNC per-outer-iteration timing.
- Extends CUDA SFM graph conversion and utilities to support batched factors and fully down-weighted (zero-information) measurements.
Reviewed changes
Copilot reviewed 13 out of 14 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| timing/timeSFMBAL.cpp | Adjusts batch SFM graph construction behavior. |
| timing/sfm_ba/timeCudaSFMBAL.cpp | Adds --gnc benchmarking mode, graph-kind options, and detailed reporting. |
| gtsam/slam/tests/testCudaSfm.cpp | Adds tests for batched conversion, timing artifacts, GNC parity, and zero-information regression. |
| gtsam/slam/cuda/cuda_sfm.i | Exposes additional CUDA LM profiling fields to bindings. |
| gtsam/slam/cuda/CudaSfmValues.h | Adds profiled pack/download paths with transfer breakdown. |
| gtsam/slam/cuda/CudaSfmProjectionBatch.h | Allows zero-information sqrt-info and adds transfer profiling. |
| gtsam/slam/cuda/CudaSfmLevenbergMarquardt.h | Adds params equals() and expands result profiling structs/fields. |
| gtsam/slam/cuda/CudaSfmLevenbergMarquardt.cu | Implements batched-factor conversion and detailed profiling instrumentation. |
| gtsam/nonlinear/cuda/DeviceValues.h | Adds optional upload timing and delta allocation timing. |
| gtsam/nonlinear/cuda/DeviceSparseNormalEquations.h | Adds optional upload profiling for sparsity pattern transfers. |
| gtsam/nonlinear/GncOptimizer.h | Adds per-iteration timing collection via getTiming(). |
| gtsam/nonlinear/BatchFactor.h | Exposes batch child factors for conversion/introspection. |
| gtsam/base/cuda/CudaDeviceArray.h | Adds profiled upload/download helpers and transfer summary structs. |
| .gitignore | Ignores benchmark log artifact directories. |
Comment on lines
+949
to
+951
| std::mt19937 rng(seed); | ||
| std::shuffle(eligible.begin(), eligible.end(), rng); | ||
| std::uniform_real_distribution<double> angle(0.0, 2.0 * M_PI); |
Comment on lines
+94
to
+110
| CudaDeviceTransferTiming uploadProfiled( | ||
| const std::vector<T>& host, cudaStream_t stream = nullptr) { | ||
| CudaDeviceTransferTiming timing; | ||
| timing.bytes = sizeof(T) * host.size(); | ||
|
|
||
| const auto resizeStart = std::chrono::steady_clock::now(); | ||
| resize(host.size()); | ||
| timing.resizeElapsed = internal::CudaTransferElapsedSince(resizeStart); | ||
|
|
||
| if (host.empty()) return timing; | ||
| const auto copyStart = std::chrono::steady_clock::now(); | ||
| GTSAM_CUDA_CHECK(cudaMemcpyAsync(data_, host.data(), sizeof(T) * size_, | ||
| cudaMemcpyHostToDevice, stream)); | ||
| GTSAM_CUDA_CHECK(cudaStreamSynchronize(stream)); | ||
| timing.copyElapsed = internal::CudaTransferElapsedSince(copyStart); | ||
| return timing; | ||
| } |
Comment on lines
838
to
851
| if (params.linearSolver == CudaSfmLinearSolverType::DenseSchur) { | ||
| stageStart = Clock::now(); | ||
| if (params.diagonalDamping) { | ||
| denseSchurSolver.solve(current, batch, numCameras, lambda, | ||
| dampingDiagonal, &delta, context.stream()); | ||
| } else { | ||
| denseSchurSolver.solve(current, batch, numCameras, lambda, &delta, | ||
| context.stream()); | ||
| } | ||
| attemptProfile.denseSchurSolveElapsed = | ||
| ElapsedSinceAfterSync(stageStart, context.stream()); | ||
| result.denseSchurSolveElapsed += | ||
| attemptProfile.denseSchurSolveElapsed; | ||
| } else { |
Comment on lines
273
to
+279
| Values optimize() { | ||
| using Clock = std::chrono::steady_clock; | ||
| const auto elapsedSince = [](Clock::time_point start) { | ||
| return std::chrono::duration<double>(Clock::now() - start).count(); | ||
| }; | ||
| timing_ = GncTiming(); | ||
| const auto totalStart = Clock::now(); |
Comment on lines
+936
to
+941
| if (n <= 2) continue; | ||
| // Leave at least two clean measurements per track. | ||
| const size_t maxCorruptible = n - 2; | ||
| for (size_t m = 0; m < maxCorruptible; ++m) { | ||
| eligible.emplace_back(j, m); | ||
| } |
ProfFan
reviewed
Jul 8, 2026
| double totalElapsed = 0.0; | ||
| std::vector<GncIterationTiming> iterations; | ||
|
|
||
| double sumWeightsUpdate() const { |
Collaborator
There was a problem hiding this comment.
What are these functions for?
Member
|
@leolrg Please evaluate Copilot review comments - and Fan's :-) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR makes
GncOptimizerwork with the CUDA SFM Levenberg-Marquardt backend as itsinner solver, adds a GNC benchmark mode to
timeCudaSFMBAL, and extends the CUDA LMprofiling instrumentation used to measure it.
GncOptimizer<GncParams<CudaSfmLevenbergMarquardtParams>>(graph, values, params).optimize()is the entire user story — GNC's template only requires an
OptimizerTypetypedef and aworking
optimize()from its base params type, which the CUDA optimizer already provided.Changes
GNC enablement (2 small functional changes)
CudaSfmProjectionBatch::IsUsableSqrtInfo.GNC's
makeWeightedGraphscales factor information by per-factor weights, and TLSdrives outlier weights to exactly 0; the conversion previously rejected the resulting
zero-information noise models. Zero rows are safe: kernels only multiply by sqrt-info,
and Schur point blocks stay invertible via lambda damping.
equals()toCudaSfmLevenbergMarquardtParams, completing the duck-typedinterface
GncParamsexpects (CPU params inherit it fromNonlinearOptimizerParams;the CUDA params class is standalone).
Usage
Switching an existing GNC setup to the CUDA inner solver is a one-type change — swap
LevenbergMarquardtParamsforCudaSfmLevenbergMarquardtParams:Benchmarking
GncOptimizer(getTiming()): weight update,weighted graph rebuild, base optimize, cost evaluation. Header-only, no behavior change.
--gnc cpu|cudamode intimeCudaSFMBALwith deterministic outlier injection(
--gnc-outlier-fraction/-pixels/-seed, corrupting only tracks that keep >= 2 cleanviews),
--gnc-loss tls|gm, timing breakdown, and quality metrics against the injectedground truth (classification precision/recall, inlier-only RMS reprojection error).
host-build vs device-alloc split), per-iteration/per-attempt solve-loop profiles.
Benchmark results
Dubrovnik BAL datasets, 10% injected outliers at 12 px, TLS loss, seed 42, A100:
Testing
testCudaSfm: TLS and GM classification parity vsGncOptimizer<GncParams<LevenbergMarquardtParams>>on a synthetic BAL problem withinjected outliers; zero-information weighted-graph regression test; params
equals().testCudaSfm(41 tests) andtestGncOptimizersuites pass.Reproduce benchmarks:
./timeCudaSFMBAL --gnc cuda [BALfile]
./timeCudaSFMBAL --gnc cpu [BALfile]