Skip to main content

TrainingProgram

Struct TrainingProgram 

Source
pub struct TrainingProgram { /* private fields */ }
Expand description

Fixed-shape forward, backward, and AdamW program captured for Vulkan replay.

The first capture records semantic work without executing it. Replays use one cached command buffer and graph-resident optimizer state; no forward, autograd, or optimizer graph is rebuilt on the host.

Implementations§

Source§

impl TrainingProgram

Source

pub fn capture( runtime: &Runtime, optimizer: &mut AdamW, step: impl FnOnce() -> Result<Matrix>, ) -> Result<Self>

Capture one complete fixed-shape forward/backward/AdamW step.

step must construct and return a scalar F32 loss and record its backward pass. This method clears existing gradients and records the optimizer update after step returns. Capture performs no submission or waiting.

§Errors

Returns an error from execution capture, step, backward/optimizer recording, or when the returned value is not a scalar F32 loss or every optimizer parameter did not receive a stable gradient.

Source

pub fn capture_scaled( runtime: &Runtime, optimizer: &mut AdamW, scaler: &mut GradScaler, step: impl FnOnce(&GradScaler) -> Result<Matrix>, ) -> Result<Self>

Capture one complete fixed-shape scaled forward/backward/AdamW step under a GradScaler.

The step closure owns forward and backward exactly as in Self::capture, with one contract change: it must scale the loss with scale_loss on the scaler it receives before tape.backward and return the unscaled scalar F32 loss for observability. The closure receives the prepared scaler rather than capturing it so the same value can be mutably prepared and immutably consulted in one call. This method then records the remainder of the scaler contract inside the same program — gradient unscaling, the finite check, device-side scale bookkeeping, and the gated optimizer update — so replaying needs no per-step host readback: overflow skips the update on-device exactly as a host-gated step_optimizer false return does, and scale growth/backoff advance in device state per replay.

This method prepares the scaler on the first capture and retains its device state for recapture on the same Runtime. Recapture reads the applied optimizer step at a completed boundary for bias correction. Complete prior replay work before recapturing.

§Errors

Returns the scaler preparation, gate binding, execution capture, step, unscale, or optimizer recording errors, or the scalar-loss validation error of Self::capture.

Source

pub fn upload_input<T: Element>( &mut self, captured: &Matrix, values: &[T], ) -> Result<()>

Upload a new fixed-shape host batch into a captured input slot.

The slot rotates through a bounded triple-buffered staging ring: each upload writes the next ring buffer and rebinds it, so a pending prior replay stalls only while its submission still consumes the target ring buffer. Each distinct ring binding records one command on first use and reuses the cached command afterwards. The captured Matrix’s own buffer mirrors each upload, so the stable value remains coherent for host reads.

§Errors

Returns the input-slot validation, completion, or upload error.

Source

pub fn bind_input( &mut self, captured: &Matrix, replacement: &Matrix, ) -> Result<()>

Rebind a captured input slot to a different same-shape Matrix without waiting for in-flight replays.

Unlike TrainingProgram::upload_input, rebinding does not wait for the pending replay because it never overwrites the buffer the GPU still reads. Rotating through a bounded set of input Matrices therefore pipelines uploads with replays; each distinct binding set pays one command record on its first submission and reuses the cached command afterwards.

§Errors

Returns the input-slot validation error.

Source

pub fn replay( &mut self, runtime: &Runtime, optimizer: &mut AdamW, ) -> Result<Event>

Submit one replay without waiting.

Same-queue timeline ordering serializes repeated updates. Call TrainingProgram::upload_input before changing a reused input slot; it owns the required completion wait.

§Errors

Returns an error when the runtime or optimizer differs, stable captured state was replaced, a counter is exhausted, or submission fails.

Source

pub fn replay_timed( &mut self, runtime: &Runtime, optimizer: &mut AdamW, ) -> Result<Event>

Submit one replay with device timestamps around the complete program.

§Errors

Returns the errors from TrainingProgram::replay, or MissingCapability when the selected queue cannot provide timestamps.

Source

pub fn replay_and_wait( &mut self, runtime: &Runtime, optimizer: &mut AdamW, ) -> Result<f32>

Submit one replay, wait for its exact completion, and read its scalar loss.

§Errors

Returns the replay, completion, or loss-read error.

Source

pub fn replay_timed_and_wait( &mut self, runtime: &Runtime, optimizer: &mut AdamW, ) -> Result<(f32, Duration)>

Submit one timed replay, wait, and return its scalar loss and device time.

§Errors

Returns the timed replay, completion, timestamp, or loss-read error.

Source

pub const fn loss(&self) -> &Matrix

Return the captured scalar loss value.

Source

pub fn diagnostics(&self) -> ExecutionPlanDiagnostics

Return graph, command-cache, submission, and input-upload diagnostics.

Source

pub const fn semantic_graph(&self) -> &SemanticGraph

Return the captured backend-independent training graph.

Source

pub fn semantic_debug_report_json(&self, name: &str) -> String

Return the backend-independent captured semantic graph as deterministic JSON.

Source

pub fn debug_report_json(&self, name: &str) -> String

Return the normalized captured executable graph as deterministic JSON.

Source

pub const fn replay_count(&self) -> u32

Return how many training steps this program submitted.

Source

pub fn is_complete(&self) -> Result<bool>

Return whether the most recently submitted training replay has completed.

A captured program that has not replayed is complete.

§Errors

Returns an error when Vulkan cannot query the originating timeline.

Source

pub fn wait(&self) -> Result<()>

Wait for the most recently submitted replay and host retirement.

§Errors

Returns an error when Vulkan completion or host retirement waiting fails.

Source

pub fn reset(self) -> Result<()>

Wait for pending replay and consume the program, releasing captured state.

Capture constructs a new Rust value, so consuming reset replaces the C++ donor’s reusable empty shell. Ordinary drop remains non-blocking.

§Errors

Returns an error from TrainingProgram::wait. Submitted resources remain protected by runtime retirement if the wait fails.

Source

pub fn compilation_debug_report_json(&self, name: &str) -> String

Return the donor-compatible deterministic compilation report as JSON.

The report contains no Vulkan handles, addresses, descriptors, or mutable routing controls. It records capture-time stages, private DNN analysis, semantic lowering provenance, and logical memory-planning evidence.

Source

pub const fn compilation_stages(&self) -> &[TrainingCompilationStageRecord]

Return immutable stage-by-stage evidence from the capture transaction.

Explicit capture records commands lazily, so its command-recording stage is NotRun. Automatic crate::ml::Training capture precompiles the reusable command before committing its preserved source recording.

Auto Trait Implementations§

§

impl !Freeze for TrainingProgram

§

impl !RefUnwindSafe for TrainingProgram

§

impl !Send for TrainingProgram

§

impl !Sync for TrainingProgram

§

impl !UnwindSafe for TrainingProgram

§

impl Forget for TrainingProgram

§

impl Move for TrainingProgram

§

impl Unpin for TrainingProgram
where Rc<RefCell<RuntimeState>>: Unpin, *const u8 is !null: Unpin, &'static str: Unpin, Arc<BufferInner>: Unpin, Arc<DecodedPlaneCopy>: Unpin, Arc<RetainedDecode>: Unpin, Arc<EncodePlaneCopy>: Unpin, Arc<RetainedEncode>: Unpin, Rc<StorageInner>: Unpin, Arc<Device>: Unpin, Arc<RetirementCompletion>: Unpin, Arc<WorkerControl>: Unpin, Arc<TimestampPairInner>: Unpin, Arc<Mutex<Vec<Arc<DecodeStatus>>>>: Unpin, Arc<Mutex<Vec<Arc<EncodeReceipt>>>>: Unpin, Arc<ReusableCommandBufferInner>: Unpin, Rc<MatrixSemantic>: Unpin, Rc<()>: Unpin,

§

impl UnsafeUnpin for TrainingProgram
where *const RcInner<RefCell<RuntimeState>> is !null: UnsafeUnpin, PhantomData<RcInner<RefCell<RuntimeState>>>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<GraphNode>: UnsafeUnpin, PhantomData<Vec<BufferHazard>>: UnsafeUnpin, PhantomData<u32>: UnsafeUnpin, PhantomData<Storage>: UnsafeUnpin, PhantomData<SemanticValueDesc>: UnsafeUnpin, PhantomData<SemanticOpDesc>: UnsafeUnpin, PhantomData<SemanticAutogradDesc>: UnsafeUnpin, PhantomData<DnnValueDesc>: UnsafeUnpin, PhantomData<DnnOpDesc>: UnsafeUnpin, PhantomData<DnnPartition>: UnsafeUnpin, PhantomData<MatrixInputBinding>: UnsafeUnpin, PhantomData<ExecutionBindingDesc>: UnsafeUnpin, PhantomData<CapturedResourceDesc>: UnsafeUnpin, PhantomData<SemanticStorageBinding>: UnsafeUnpin, PhantomData<u8>: UnsafeUnpin, PhantomData<(Vec<u32>, Vec<ReusableCommandBuffer>)>: UnsafeUnpin, *const ArcInner<Device> is !null: UnsafeUnpin, PhantomData<ArcInner<Device>>: UnsafeUnpin, *const ArcInner<RetirementCompletion> is !null: UnsafeUnpin, PhantomData<ArcInner<RetirementCompletion>>: UnsafeUnpin, *const ArcInner<WorkerControl> is !null: UnsafeUnpin, PhantomData<ArcInner<WorkerControl>>: UnsafeUnpin, *const ArcInner<TimestampPairInner> is !null: UnsafeUnpin, PhantomData<ArcInner<TimestampPairInner>>: UnsafeUnpin, *const ArcInner<Mutex<Vec<Arc<DecodeStatus>>>> is !null: UnsafeUnpin, PhantomData<ArcInner<Mutex<Vec<Arc<DecodeStatus>>>>>: UnsafeUnpin, *const ArcInner<Mutex<Vec<Arc<EncodeReceipt>>>> is !null: UnsafeUnpin, PhantomData<ArcInner<Mutex<Vec<Arc<EncodeReceipt>>>>>: UnsafeUnpin, *const RcInner<StorageInner> is !null: UnsafeUnpin, PhantomData<RcInner<StorageInner>>: UnsafeUnpin, PhantomData<usize>: UnsafeUnpin, *const RcInner<MatrixSemantic> is !null: UnsafeUnpin, PhantomData<RcInner<MatrixSemantic>>: UnsafeUnpin, PhantomData<Rc<()>>: UnsafeUnpin, PhantomData<u64>: UnsafeUnpin, PhantomData<(u64, u64)>: UnsafeUnpin,

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<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

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<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

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

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.