Skip to content

Graceful CUDA fallback for stub/broken CUDA libraries - #50

Merged
raphaelsty merged 3 commits into
mainfrom
fix/cuda-graceful-fallback
Mar 18, 2026
Merged

raphaelsty merged 3 commits into
mainfrom
fix/cuda-graceful-fallback

Conversation

@raphaelsty

Copy link
Copy Markdown
Contributor

Summary

  • Wrap all CUDA initialization paths (cudarc, ONNX Runtime CUDA EP, FastKMeansCuda) in catch_unwind to trap panics from stub/incomplete CUDA libraries
  • Upgrade cudarc from 0.12 to 0.19.3 with dynamic-loading (dlopen at runtime instead of link-time)
  • Add CUDA_BROKEN flag with clear_cuda_broken() API so the cached state can be reset after the user repairs their CUDA environment
  • Replace OnceLock<Option<CudaContext>> with OnceLock<Mutex<Option<Arc<CudaContext>>>> to support resetting
  • Use kmeans_double_chunked directly for CPU fallback to avoid re-entering CUDA via FastKMeans::train()
  • Add build.rs to detect CUDA toolkit at build time

Context

On systems with stub CUDA libraries (e.g., container images with /usr/lib/x86_64-linux-gnu/libcuda.so.1 but no real driver), cudarc panics with undefined symbol: cuArray3DCreate_v2 instead of returning an error. This crashed colgrep on startup.

Depends on: lightonai/fastkmeans-rs#5

Test plan

  • cargo check --features cuda compiles
  • cargo check (no CUDA) compiles
  • colgrep runs successfully on a system with stub CUDA libraries (falls back to CPU)
  • Verify CUDA acceleration still works on a system with proper CUDA drivers
  • Verify clear_cuda_broken() allows re-initialization after fixing CUDA

🤖 Generated with Claude Code

Raphael Sourty and others added 3 commits March 18, 2026 12:25
On systems with stub CUDA libraries (e.g., incomplete driver installs),
cudarc panics with missing symbol errors instead of returning errors.
This causes colgrep and other tools to crash on startup.

Changes:
- Upgrade cudarc from 0.12 to 0.19.3 with dynamic-loading (dlopen)
- Wrap all CUDA initialization paths in catch_unwind to trap panics
- Add CUDA_BROKEN flag with clear_cuda_broken() to allow retrying after
  the user repairs their CUDA environment
- Replace OnceLock<Option<CudaContext>> with OnceLock<Mutex<Option<Arc>>>
  so the cached context can be reset when clearing the broken flag
- Protect ONNX Runtime CUDA provider init with catch_unwind
- Use kmeans_double_chunked directly for CPU fallback in k-means to
  avoid FastKMeans::train() which re-enters CUDA when the feature is on
- Add build.rs to detect CUDA toolkit at build time

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@raphaelsty
raphaelsty merged commit 8822173 into main Mar 18, 2026
19 checks passed
@raphaelsty raphaelsty mentioned this pull request Aug 25, 2026
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