Repository navigation
Conversation
drbh
requested review from
elibol,
kkraus14,
nihalpasham and
rparolin
as code owners
October 21, 2026 17:45
This branch has not been deployed
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.
hey guys! congrats on consolidating the repos!
this pr adds
CudaStream::borrow_raw, which wraps a rawCUstreamwithout taking ownership, so a host can launch cuda-oxide kernels on a stream that something else owns (eg. the current torch stream)the generated launchers on a
#[cuda_module]take&Arc<CudaStream>, and today the only way to get one isCudaContext::new_stream/default_stream/CudaStream::fork. dropping any of those destroys the stream, so there is no safe way to hand a framework's stream to aLoadedModule. we hit this building cuda-oxide kernels as python extensions for https://github.com/huggingface/kernels, where the stream comes fromTVMFFIEnvGetStream(torch via tvm-ffi)this follows the pattern
runtime.rsalready uses for external handles.Device,Stream,ModuleandFunctioneach have anunsafe fn borrow_rawtaking the raw*mut c_void, backed by anowned: boolfield thatDropchecks before releasing anything (added in6f3b354e7)cuda-rust/cuda-core/src/runtime.rs
Lines 726 to 733 in e027ac3
cuda-rust/cuda-core/src/runtime.rs
Lines 684 to 688 in e027ac3
CudaStream::borrow_rawis the same shape on the simt side: same*mut c_voidargument, same safety contract asStream::borrow_raw, and a crate privateownedfield. the existing constructors setowned: trueso nothing changes for them. a borrowed stream is not counted innum_streamsand itsDropreturns early.DeviceBuffer::from_raw_partsis the existing simt precedent for wrapping memory we do not own, this adds the stream equivalentnote: this does not add a
borrow_with_ownervariant likeruntime::Streamhas. happy to add it here if you want parity, we did not need it since the caller only holds the stream for the duration of one launchcargo test -p cuda-core --test borrow_raw cargo clippy -p cuda-core --all-targets -- -D warnings RUSTDOCFLAGS=-Dwarnings cargo doc -p cuda-core --no-depsa new test in
cuda-core/tests/borrow_raw.rsborrows and drops aCudaStream8 times and checks the source stream and context are still usable. also checked end to end on an rtx 4090 with a kernel loaded from python and launched on a torch side stream