Skip to content

Commit db7586c

Browse files
committed
Address review feedback on colmap#4895
- Fix CMake plain/keyword signature mixing for colmap_util_onnx_test, which broke configure on CUDA builds - Link colmap_feature against colmap_util_cuda (loma.cc needs the symbol) - Extract generic CudaDeviceSupportsBf16() helper to util/cuda and replace the LoMa-internal BF16 device check with a thin wrapper; the helper fails closed (false) on invalid input or query failure - Document that the CUDA probe inspects operator types only, and log allowed CPU nodes at VLOG(2) - Add cuda_test for the helper; document the onnx_test probe models
1 parent 5c4a61c commit db7586c

9 files changed

Lines changed: 112 additions & 23 deletions

File tree

‎src/colmap/feature/CMakeLists.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ if(ONNX_ENABLED)
5858
target_link_libraries(colmap_feature PRIVATE colmap_onnx_util)
5959
endif()
6060

61+
if(CUDA_ENABLED OR HIP_ENABLED)
62+
# loma.cc calls CudaDeviceSupportsBf16 from colmap_util_cuda.
63+
target_link_libraries(colmap_feature PRIVATE colmap_util_cuda)
64+
endif()
65+
6166
if(ONNX_ENABLED)
6267
COLMAP_ADD_TEST(
6368
NAME aliked_test

‎src/colmap/feature/loma.cc‎

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@
44

55
#include "colmap/feature/utils.h"
66
#include "colmap/geometry/pose_prior.h"
7+
#include "colmap/util/cuda.h"
78
#include "colmap/util/misc.h"
89
#include "colmap/util/onnx.h"
9-
#include "colmap/util/string.h"
1010

11-
#ifdef COLMAP_CUDA_ENABLED
12-
#include "colmap/util/cudacc.h"
13-
#endif
14-
15-
#include <algorithm>
1611
#include <cstring>
1712
#include <memory>
13+
#include <string>
14+
#include <vector>
1815

1916
#include <OpenImageIO/imagebuf.h>
2017
#include <OpenImageIO/imagebufalgo.h>
@@ -25,17 +22,14 @@ namespace {
2522
#ifdef COLMAP_ONNX_ENABLED
2623

2724
#ifdef COLMAP_CUDA_ENABLED
28-
// CUDA BF16 matrix kernels require Ampere or newer. Session construction can
29-
// succeed on an older GPU even though the first matrix operation would fail.
30-
void CheckLomaBf16CudaDevice(const ONNXModel& model,
31-
const std::string& gpu_index) {
32-
if (model.execution_provider() != ONNXExecutionProvider::CUDA) return;
33-
cudaDeviceProp properties{};
34-
const auto indices = CSVToVector<int>(gpu_index);
35-
THROW_CHECK_EQ(indices.size(), 1);
36-
CUDA_SAFE_CALL(
37-
cudaGetDeviceProperties(&properties, std::max(0, indices.front())));
38-
if (properties.major < 8) {
25+
// Throws Ort::Exception when a BF16 model would run on a device without BF16
26+
// support, so the caller falls back to FP32. Session construction can
27+
// succeed on an older GPU even though the first matrix operation would fail,
28+
// and LoMa only falls back on construction failures -- so fail fast here.
29+
void ThrowIfLomaBf16Unsupported(const ONNXModel& model,
30+
const std::string& gpu_index) {
31+
if (model.execution_provider() == ONNXExecutionProvider::CUDA &&
32+
!CudaDeviceSupportsBf16(gpu_index)) {
3933
throw Ort::Exception(
4034
"LoMa BF16 requires CUDA compute capability 8.0 or newer",
4135
ORT_NOT_IMPLEMENTED);
@@ -102,7 +96,7 @@ class LomaFeatureExtractor : public FeatureExtractor {
10296
probe_descriptor_support) {
10397
#ifdef COLMAP_CUDA_ENABLED
10498
if (probe_descriptor_support) {
105-
CheckLomaBf16CudaDevice(descriptor_, options.gpu_index);
99+
ThrowIfLomaBf16Unsupported(descriptor_, options.gpu_index);
106100
}
107101
#endif
108102
THROW_CHECK(options.Check());
@@ -298,7 +292,9 @@ class LomaFeatureMatcher : public FeatureMatcher {
298292
options.gpu_index,
299293
probe_model_support) {
300294
#ifdef COLMAP_CUDA_ENABLED
301-
if (probe_model_support) CheckLomaBf16CudaDevice(model_, options.gpu_index);
295+
if (probe_model_support) {
296+
ThrowIfLomaBf16Unsupported(model_, options.gpu_index);
297+
}
302298
#endif
303299
THROW_CHECK(options.Check());
304300
THROW_CHECK_EQ(model_.input_shapes().size(), 4);

‎src/colmap/util/CMakeLists.txt‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,9 @@ if(ONNX_ENABLED)
208208
onnxruntime::onnxruntime
209209
)
210210
if(TESTS_ENABLED AND CUDA_ENABLED)
211-
target_link_libraries(colmap_util_onnx_test PRIVATE colmap_util_cuda)
211+
# NOTE: plain signature to match COLMAP_ADD_TEST() above; mixing
212+
# keyword and plain signatures for one target is a CMake error.
213+
target_link_libraries(colmap_util_onnx_test colmap_util_cuda)
212214
endif()
213215
endif()
214216
COLMAP_ADD_TEST(
@@ -270,3 +272,13 @@ if(HIP_ENABLED)
270272
hip::host
271273
)
272274
endif()
275+
276+
if(CUDA_ENABLED OR HIP_ENABLED)
277+
# Placed after both colmap_util_cuda definitions above, which this
278+
# test links against.
279+
COLMAP_ADD_TEST(
280+
NAME cuda_test
281+
SRCS cuda_test.cc
282+
LINK_LIBS colmap_util_cuda
283+
)
284+
endif()

‎src/colmap/util/cuda.cc‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
#include "colmap/util/cudacc.h"
66
#include "colmap/util/logging.h"
7+
#include "colmap/util/misc.h"
78

89
#include <algorithm>
910
#include <iostream>
11+
#include <string>
12+
#include <vector>
1013

1114
namespace colmap {
1215
namespace {
@@ -61,4 +64,23 @@ void SetBestCudaDevice(const int gpu_index) {
6164
CUDA_SAFE_CALL(cudaSetDevice(selected));
6265
}
6366

67+
bool CudaDeviceSupportsBf16(const std::string& gpu_index) {
68+
std::vector<int> indices;
69+
try {
70+
indices = CSVToVector<int>(gpu_index);
71+
} catch (const std::exception&) {
72+
return false;
73+
}
74+
if (indices.size() != 1) {
75+
return false;
76+
}
77+
cudaDeviceProp properties{};
78+
if (cudaGetDeviceProperties(&properties, std::max(0, indices.front())) !=
79+
cudaSuccess) {
80+
return false;
81+
}
82+
// BF16 matrix kernels require Ampere (compute capability 8.0) or newer.
83+
return properties.major >= 8;
84+
}
85+
6486
} // namespace colmap

‎src/colmap/util/cuda.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#pragma once
44

5+
#include <string>
6+
57
namespace colmap {
68

79
int GetNumCudaDevices();
@@ -10,4 +12,13 @@ int FindBestCudaDevice();
1012

1113
void SetBestCudaDevice(int gpu_index);
1214

15+
// Returns whether the CUDA device selected by `gpu_index` supports BF16
16+
// matrix kernels, which require Ampere (compute capability 8.0) or newer.
17+
// `gpu_index` uses the single-device form parsed by CSVToVector (e.g. "0");
18+
// a negative index selects device 0, matching the ONNX provider behavior.
19+
// Returns false when `gpu_index` is invalid or the device cannot be queried,
20+
// so callers can safely fall back to FP32 whenever BF16 support is
21+
// unconfirmed.
22+
bool CudaDeviceSupportsBf16(const std::string& gpu_index);
23+
1324
} // namespace colmap

‎src/colmap/util/cuda_test.cc‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
3+
#include "colmap/util/cuda.h"
4+
5+
#include "colmap/util/cudacc.h"
6+
7+
#include <gtest/gtest.h>
8+
9+
namespace colmap {
10+
namespace {
11+
12+
TEST(CudaDeviceSupportsBf16Test, RejectsInvalidGpuIndex) {
13+
// No GPU needed: invalid input fails closed without querying a device.
14+
EXPECT_FALSE(CudaDeviceSupportsBf16(""));
15+
EXPECT_FALSE(CudaDeviceSupportsBf16("abc"));
16+
EXPECT_FALSE(CudaDeviceSupportsBf16("0,1"));
17+
EXPECT_FALSE(CudaDeviceSupportsBf16("0;1"));
18+
}
19+
20+
TEST(CudaDeviceSupportsBf16Test, MatchesDeviceProperties) {
21+
if (GetNumCudaDevices() == 0) GTEST_SKIP() << "CUDA device required";
22+
cudaDeviceProp properties{};
23+
ASSERT_EQ(cudaGetDeviceProperties(&properties, 0), cudaSuccess);
24+
EXPECT_EQ(CudaDeviceSupportsBf16("0"), properties.major >= 8);
25+
// A negative index selects device 0, matching the ONNX provider behavior.
26+
EXPECT_EQ(CudaDeviceSupportsBf16("-1"), properties.major >= 8);
27+
}
28+
29+
} // namespace
30+
} // namespace colmap

‎src/colmap/util/onnx.cc‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ void ONNXModel::CheckCudaCapabilityProbeAssignments() const {
295295
// ORT deliberately places shape/index manipulation on CPU. Reject
296296
// arithmetic fallback (in particular unsupported BF16 matrix kernels),
297297
// while permitting this small set of data-movement/metadata operators.
298+
// The assignment info reports operator types only, not input dtypes or
299+
// shapes, so this check cannot prove an allowlisted Gather/Concat/Slice
300+
// only moves index data rather than feature tensors -- it only bounds
301+
// the fallback to operators ORT uses for shape/index bookkeeping in
302+
// the LoMa BF16 graphs. Log allowed CPU nodes so a future ORT
303+
// partitioning change is diagnosable from the probe output.
298304
const bool shape_or_index =
299305
(domain.empty() || domain == "ai.onnx") &&
300306
(op == "Shape" || op == "Size" || op == "Gather" || op == "Squeeze" ||
@@ -305,6 +311,8 @@ void ONNXModel::CheckCudaCapabilityProbeAssignments() const {
305311
node.GetName() + "' (" + domain + ":" + op + ")";
306312
throw Ort::Exception(message.c_str(), ORT_NOT_IMPLEMENTED);
307313
}
314+
VLOG(2) << "CUDA capability probe allows CPU node '" << node.GetName()
315+
<< "' (" << domain << ":" << op << ")";
308316
}
309317
}
310318
if (!has_cuda_nodes) {

‎src/colmap/util/onnx.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ class ONNXModel {
7070
public:
7171
// A capability probe propagates initialization failures without logging an
7272
// error. CUDA probes permit CPU shape/index operators but require CUDA work
73-
// and reject CPU arithmetic fallback. Other accelerators and ONNX Runtime
73+
// and reject CPU arithmetic fallback. The probe inspects assigned operator
74+
// types only, not input dtypes. Other accelerators and ONNX Runtime
7475
// versions before 1.24 require the complete graph on the selected provider.
7576
ONNXModel(std::string model_path,
7677
int num_threads,

‎src/colmap/util/onnx_test.cc‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ TEST(CreateONNXScalarTensorTest, Nominal) {
197197
#if defined(COLMAP_CUDA_ENABLED) && ORT_API_VERSION >= 24
198198

199199
std::filesystem::path WriteMixedProbeModel() {
200-
// Dynamic MatMul plus CPU Shape; identity weights make inference exact.
200+
// ONNX IR v8: float input X [N, 2] -> MatMul with identity initializer
201+
// W [2, 2] -> float output Y [N, 2]; Shape(X) -> int64 output S [2].
202+
// The identity weights make inference exact for any batch size.
201203
constexpr char kModel[] =
202204
"\x08\x08\x3a\xa7\x01\x0a\x1d\x0a\x01\x58\x0a\x01"
203205
"\x57\x12\x01\x59\x1a\x0a\x67\x70\x75\x5f\x6d\x61"
@@ -220,7 +222,9 @@ std::filesystem::path WriteMixedProbeModel() {
220222
}
221223

222224
std::filesystem::path WriteCpuStringModel() {
223-
// CPU-only string computation, not shape/index bookkeeping.
225+
// ONNX IR v8: string input X [1] -> StringNormalizer (locale "C") -> string
226+
// output Y [N]. String computation has no CUDA kernel, so the probe must
227+
// reject it as CPU compute rather than shape/index bookkeeping.
224228
constexpr char kModel[] =
225229
"\x08\x08\x3a\x6b\x0a\x34\x0a\x01\x58\x12\x01\x59"
226230
"\x1a\x0a\x63\x70\x75\x5f\x73\x74\x72\x69\x6e\x67"

0 commit comments

Comments
 (0)