Skip to content

Commit 340f948

Browse files
Fix native library loader silently failing when CUDA DLLs are missing
Change all 'ok = TryLoadNativeLibraryByName(...)' calls to 'ok &= ...' so that failures accumulate instead of being overwritten by subsequent successful loads. Initialize 'ok = true' before the loading chain. Previously, each load call overwrote the result of the previous one, so if an early CUDA dependency (e.g. cudnn_adv64_9) failed to load but LibTorchSharp succeeded, 'ok' would be true. This caused: - nativeBackendCudaLoaded set to true despite missing dependencies - The fallback loading path was skipped - The diagnostic trace (StringBuilder) was discarded - Subsequent load attempts were skipped entirely - CUDA operations failed later with cryptic errors Now any single load failure keeps 'ok' as false, ensuring the fallback path is attempted and the full diagnostic trace is preserved in error messages. Fixes #1545 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 5782540 commit 340f948

1 file changed

Lines changed: 19 additions & 19 deletions

File tree

src/TorchSharp/Torch.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private static void LoadNativeBackend(bool useCudaBackend, out StringBuilder? tr
116116
trace = null;
117117

118118
if (!alreadyLoaded) {
119-
bool ok;
119+
bool ok = true;
120120
trace = new StringBuilder();
121121
trace.AppendLine($"");
122122
trace.AppendLine($"TorchSharp: LoadNativeBackend: Initialising native backend, useCudaBackend = {useCudaBackend}");
@@ -134,27 +134,27 @@ private static void LoadNativeBackend(bool useCudaBackend, out StringBuilder? tr
134134
// Preloading these DLLs on windows seems to iron out problems where one native DLL
135135
// requests a load of another through dynamic linking techniques.
136136
//
137-
ok = TryLoadNativeLibraryByName("cudnn_adv64_9", typeof(torch).Assembly, trace);
138-
ok = TryLoadNativeLibraryByName("cudnn_cnn64_9", typeof(torch).Assembly, trace);
139-
ok = TryLoadNativeLibraryByName("cudnn_ops64_9", typeof(torch).Assembly, trace);
140-
ok = TryLoadNativeLibraryByName("cudnn_graph64_9.dll", typeof(torch).Assembly, trace);
141-
ok = TryLoadNativeLibraryByName("cudnn_heuristic64_9.dll", typeof(torch).Assembly, trace);
142-
ok = TryLoadNativeLibraryByName("cudnn_engines_precompiled64_9.dll", typeof(torch).Assembly, trace);
143-
ok = TryLoadNativeLibraryByName("cudnn_engines_runtime_compiled64_9.dll", typeof(torch).Assembly, trace);
144-
ok = TryLoadNativeLibraryByName("nvrtc-builtins64_128", typeof(torch).Assembly, trace);
145-
ok = TryLoadNativeLibraryByName("caffe2_nvrtc", typeof(torch).Assembly, trace);
146-
ok = TryLoadNativeLibraryByName("nvrtc64_120_0", typeof(torch).Assembly, trace);
147-
ok = TryLoadNativeLibraryByName("cublasLt64_12", typeof(torch).Assembly, trace);
148-
ok = TryLoadNativeLibraryByName("cufft64_11", typeof(torch).Assembly, trace);
149-
ok = TryLoadNativeLibraryByName("cusparse64_12", typeof(torch).Assembly, trace);
150-
ok = TryLoadNativeLibraryByName("cusolver64_11", typeof(torch).Assembly, trace);
137+
ok &= TryLoadNativeLibraryByName("cudnn_adv64_9", typeof(torch).Assembly, trace);
138+
ok &= TryLoadNativeLibraryByName("cudnn_cnn64_9", typeof(torch).Assembly, trace);
139+
ok &= TryLoadNativeLibraryByName("cudnn_ops64_9", typeof(torch).Assembly, trace);
140+
ok &= TryLoadNativeLibraryByName("cudnn_graph64_9.dll", typeof(torch).Assembly, trace);
141+
ok &= TryLoadNativeLibraryByName("cudnn_heuristic64_9.dll", typeof(torch).Assembly, trace);
142+
ok &= TryLoadNativeLibraryByName("cudnn_engines_precompiled64_9.dll", typeof(torch).Assembly, trace);
143+
ok &= TryLoadNativeLibraryByName("cudnn_engines_runtime_compiled64_9.dll", typeof(torch).Assembly, trace);
144+
ok &= TryLoadNativeLibraryByName("nvrtc-builtins64_128", typeof(torch).Assembly, trace);
145+
ok &= TryLoadNativeLibraryByName("caffe2_nvrtc", typeof(torch).Assembly, trace);
146+
ok &= TryLoadNativeLibraryByName("nvrtc64_120_0", typeof(torch).Assembly, trace);
147+
ok &= TryLoadNativeLibraryByName("cublasLt64_12", typeof(torch).Assembly, trace);
148+
ok &= TryLoadNativeLibraryByName("cufft64_11", typeof(torch).Assembly, trace);
149+
ok &= TryLoadNativeLibraryByName("cusparse64_12", typeof(torch).Assembly, trace);
150+
ok &= TryLoadNativeLibraryByName("cusolver64_11", typeof(torch).Assembly, trace);
151151
}
152152

153-
ok = TryLoadNativeLibraryByName("torch_cuda", typeof(torch).Assembly, trace);
154-
ok = TryLoadNativeLibraryByName("LibTorchSharp", typeof(torch).Assembly, trace);
153+
ok &= TryLoadNativeLibraryByName("torch_cuda", typeof(torch).Assembly, trace);
154+
ok &= TryLoadNativeLibraryByName("LibTorchSharp", typeof(torch).Assembly, trace);
155155
} else {
156-
ok = TryLoadNativeLibraryByName("torch_cpu", typeof(torch).Assembly, trace);
157-
ok = TryLoadNativeLibraryByName("LibTorchSharp", typeof(torch).Assembly, trace);
156+
ok &= TryLoadNativeLibraryByName("torch_cpu", typeof(torch).Assembly, trace);
157+
ok &= TryLoadNativeLibraryByName("LibTorchSharp", typeof(torch).Assembly, trace);
158158
}
159159

160160
trace.AppendLine($" Result from regular native load of LibTorchSharp is {ok}");

0 commit comments

Comments
 (0)