Skip to main content

Graph

Struct Graph 

Source
pub struct Graph<T, F> { /* private fields */ }
Expand description

A captured computation.

capture records a closure once; replay then re-runs it as a single dispatch on backends with hardware graph support (CUDA/HIP — collapsing hundreds of kernel launches into one), or simply re-executes the closure everywhere else. The API is identical either way; only the speed differs.

The recorded graph replays against the exact device buffers used during capture, so the closure must read its inputs from, and write its outputs to, stable buffers held across the loop. Refresh those inputs in place with Tensor::inplace-style writes before each replay.

§Safety

The graph has no explicit input/state/output signature: whatever buffers the closure touched during capture are what every replay reads and writes, with nothing tracking them afterwards. replay is therefore unsafe — see its safety contract. Safe, structured APIs can be layered on top of this mechanism for specific workloads (e.g. a decode step with pinned KV-cache and token buffers).

Implementations§

Source§

impl<T, F> Graph<T, F>
where F: FnMut() -> T,

Source

pub unsafe fn replay(&mut self) -> &T

Re-run the captured computation and return its output.

On a captured graph this is one dispatch replaying the recorded launches against their original buffers — so write fresh inputs into those buffers first. On the fallback path it re-executes the closure. The returned reference is the output produced during capture (whose buffer the replay just overwrote), or the fresh output on the fallback path.

§Safety

On the hardware path this dispatches the recorded kernels against the raw device buffers the closure touched during capture, with nothing checking they are still valid. The caller must guarantee, until the replay’s work completes (e.g. it is followed by a read of the output or a device sync):

  • Liveness — every tensor the captured closure read or wrote still exists. Dropping one frees its buffer for reuse by other allocations, and a later replay would read or overwrite whatever now lives there. Tensors owned by the closure (or by self, like the output) are kept alive automatically; tensors the closure only borrowed must outlive the replays.
  • No concurrent use — no other stream or thread reads or writes a tensor shared with the graph while the replay executes; the replay is only ordered against work on its own capture stream.
  • Same-stream refreshes — input refreshes and output reads are issued on the stream the graph was captured on (the same device thread/client), so they order correctly against the replay rather than racing it with stale or torn data.

On the fallback path (no hardware graph) this simply re-runs the closure and is trivially safe.

Source

pub fn output(&self) -> &T

The output tensor(s) the graph writes to — stable across replays on the hardware path (the same buffer is overwritten each time).

Source

pub fn is_hardware(&self) -> bool

Whether this graph replays as a hardware dispatch (true) or by re-running the closure (false).

Auto Trait Implementations§

§

impl<T, F> Freeze for Graph<T, F>
where T: Freeze, F: Freeze,

§

impl<T, F> RefUnwindSafe for Graph<T, F>

§

impl<T, F> Send for Graph<T, F>
where T: Send, F: Send,

§

impl<T, F> Sync for Graph<T, F>
where T: Sync, F: Sync,

§

impl<T, F> Unpin for Graph<T, F>
where T: Unpin, F: Unpin,

§

impl<T, F> UnsafeUnpin for Graph<T, F>
where T: UnsafeUnpin, F: UnsafeUnpin,

§

impl<T, F> UnwindSafe for Graph<T, F>
where T: UnwindSafe, F: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.