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
impl TrainingProgram
Sourcepub fn capture(
runtime: &Runtime,
optimizer: &mut AdamW,
step: impl FnOnce() -> Result<Matrix>,
) -> Result<Self>
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.
Sourcepub fn capture_scaled(
runtime: &Runtime,
optimizer: &mut AdamW,
scaler: &mut GradScaler,
step: impl FnOnce(&GradScaler) -> Result<Matrix>,
) -> Result<Self>
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.
Sourcepub fn upload_input<T: Element>(
&mut self,
captured: &Matrix,
values: &[T],
) -> Result<()>
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.
Sourcepub fn bind_input(
&mut self,
captured: &Matrix,
replacement: &Matrix,
) -> Result<()>
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.
Sourcepub fn replay(
&mut self,
runtime: &Runtime,
optimizer: &mut AdamW,
) -> Result<Event>
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.
Sourcepub fn replay_timed(
&mut self,
runtime: &Runtime,
optimizer: &mut AdamW,
) -> Result<Event>
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.
Sourcepub fn replay_and_wait(
&mut self,
runtime: &Runtime,
optimizer: &mut AdamW,
) -> Result<f32>
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.
Sourcepub fn replay_timed_and_wait(
&mut self,
runtime: &Runtime,
optimizer: &mut AdamW,
) -> Result<(f32, Duration)>
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.
Sourcepub fn diagnostics(&self) -> ExecutionPlanDiagnostics
pub fn diagnostics(&self) -> ExecutionPlanDiagnostics
Return graph, command-cache, submission, and input-upload diagnostics.
Sourcepub const fn semantic_graph(&self) -> &SemanticGraph
pub const fn semantic_graph(&self) -> &SemanticGraph
Return the captured backend-independent training graph.
Sourcepub fn semantic_debug_report_json(&self, name: &str) -> String
pub fn semantic_debug_report_json(&self, name: &str) -> String
Return the backend-independent captured semantic graph as deterministic JSON.
Sourcepub fn debug_report_json(&self, name: &str) -> String
pub fn debug_report_json(&self, name: &str) -> String
Return the normalized captured executable graph as deterministic JSON.
Sourcepub const fn replay_count(&self) -> u32
pub const fn replay_count(&self) -> u32
Return how many training steps this program submitted.
Sourcepub fn is_complete(&self) -> Result<bool>
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.
Sourcepub fn wait(&self) -> Result<()>
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.
Sourcepub fn reset(self) -> Result<()>
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.
Sourcepub fn compilation_debug_report_json(&self, name: &str) -> String
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.
Sourcepub const fn compilation_stages(&self) -> &[TrainingCompilationStageRecord]
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.