Repository navigation
feat(rust): select the ONNX Runtime execution provider (CUDA) with a fallback policy and diagnostics - #25
Open
seo-rii wants to merge 2 commits into
Open
feat(rust): select the ONNX Runtime execution provider (CUDA) with a fallback policy and diagnostics#25seo-rii wants to merge 2 commits into
seo-rii wants to merge 2 commits into
Conversation
Trustmark always ran both sessions on ONNX Runtime's CPU provider, so a
host with an NVIDIA GPU could not use it without forking the crate.
Extend `SessionOptions` with `execution_provider` (`ExecutionProvider`,
default `Cpu`) and `fallback` (`ProviderFallback`, default `Error`). The
new opt-in `cuda` feature enables `ort/cuda`, which downloads ONNX
Runtime 1.22 built for CUDA 12, and lets `ExecutionProvider::Cuda {
device_id }` register the CUDA provider. If the requested provider can't
be used (feature missing, CUDA/cuDNN libraries missing, bad device, or
session creation failing with it), `ProviderFallback::Error` returns the
new `Error::ExecutionProvider` and `ProviderFallback::Cpu` loads the
model on the CPU instead. A model that fails to load on the CPU as well
is reported as the usual `Error::Ort`, not blamed on the provider.
`Trustmark::encoder_provider()` and `decoder_provider()` return a
`ProviderInfo` with the requested provider, the one the session was
created with, and the fallback reason. ONNX Runtime does not expose
per-node placement through its API, so this does not claim every node
runs on the provider; the docs point to `ORT_LOG=verbose` for that.
`Trustmark::new` and `SessionOptions::default()` keep their behaviour
(CPU, 8 intra-op threads), and the session Mutexes are unchanged. The
session construction moves to a new `session` module.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 21, 2026
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.
Problem
The crate always runs on ONNX Runtime's CPU provider. There was no way to request a GPU, to choose what happens when the GPU can't be used, or to find out which provider actually ran.
Change
SessionOptionsgains two fields; it is stillCopy/Eq, and defaults are unchanged:execution_provider: ExecutionProvider, defaultCpu;fallback: ProviderFallback, defaultError.ExecutionProvider { Cpu, Cuda { device_id } }is#[non_exhaustive]and implementsDisplay.cuda = ["ort/cuda"].Cudafails when the model loads, with a message that names the feature.ProviderFallback { Error, Cpu }chooses between failing and falling back to the CPU when the provider can't be registered.Trustmark::encoder_provider()/decoder_provider()returnProviderInfo { requested, registered, fallback_reason }.Error::ExecutionProvider { provider, source }.Error::Ort.registeredmeans the provider was registered. It does not prove every node runs on it; ORT only logs node placement. The README explains how to see it withORT_LOG=verboseand a tracing subscriber.non_exhaustiveleaves room for them.GPU verification (NVIDIA A100 80GB, driver 580.105.08)
nvidia-*-cu12wheels viaLD_LIBRARY_PATH. The binaries were built with--features cuda(ORT 1.22, CUDA 12 build).--include-ignored.CUDAExecutionProvider" and "All nodes placed on [CUDAExecutionProvider]" (52 encoder nodes, 125 decoder nodes);MemcpyTransformermodified 0 nodes.nvidia-smi: the bench process held 778 MiB in 45 of 48 samples.Timings (median of 20 warm runs, Q/BCH_5, same host; CPU with 8 intra-op threads):
Deployment note (added to the README): a standalone binary does not find
libonnxruntime_providers_shared.sonext to itself. It must be onLD_LIBRARY_PATH;cargo runandcargo testset this up automatically.Local checks
cargo fmt --checkpasses.-D warningspasses, both with default features and with--all-features.cargo test --workspace: 29 lib tests plus 2 doctests pass.--features cudaon a machine without a GPU: 28 pass and the 3 GPU tests are ignored. The fallback test hit a real registration failure (missinglibcublasLt.so.12) and fell back as expected.cargo docandcargo publish --dry-run --locked(default features) pass, and so does the MSRV 1.88 check, also with--features cuda --lib.Compatibility
Errorvariant breaks exhaustive matches downstream, becauseErroris not#[non_exhaustive]. The next release should therefore be 0.5.0.--all-features, so it will now also download the ~170 MB CUDA build of ORT.🤖 Generated with Claude Code