Session is the runtime object produced after compiling a Graph. It owns the GPU context, allocates all buffers, compiles GPU pipelines, and executes the pre-built dispatch sequence on each call to step() or run().
You do not construct a Session directly. Use one of the builder functions:
Builder functions
build_session
- Optimizes the forward graph (SwiGLU fusion, MatMul+Add fusion, etc.)
- Runs autodiff on the optimized forward graph to produce the full forward+backward graph
- Optimizes the full graph (fuses backward MatMul+Add, etc.)
- Compiles the optimized graph to an
ExecutionPlan - Allocates all GPU buffers and compiles GPU pipelines
build_inference_session
GroupNorm + SiLU → GroupNormSilu).
build_session_cached
build_session, but loads the compiled ExecutionPlan from a cache file if the graph hash matches. Saves the plan after compilation if no cache exists. Useful for skipping the compilation overhead on repeated runs.
The forward-pass computation graph.
Path to read from / write to (e.g.
"model.plan.ron").build_session_with_report
build_session, but also returns an OptimizeReport describing what the e-graph optimizer did.
MemorySummary
Total bytes across all allocated GPU buffers.
Bytes used by Adam first- and second-moment buffers (0 if using SGD).
Number of individual GPU buffers.
Size of the largest single buffer.
MemorySummary implements Display:
Session methods
Data upload
set_input
f32 data into the named input buffer. Call this before each step() or run().
Must match the name passed to
graph.input() when building the graph.Flat slice of values. Length must match the number of elements in the named input.
set_parameter
f32 data into a named parameter buffer. Use this to initialize weights before training, or to restore a checkpoint manually.
Must match the name passed to
graph.parameter().Flat slice of initial values.
If the parameter feeds a derived (concatenated) weight created by the optimizer,
set_parameter writes the source data into the correct column offset of the derived buffer automatically.get_parameter / read_param
Parameter name.
Destination slice. Must be large enough to hold all parameter elements.
Optimizer configuration
set_learning_rate
step(). When set, step() appends all SGD parameter updates to the same GPU submission as the forward+backward pass — eliminating the overhead of a separate sgd_step() call.
Learning rate.
set_adam
step(). Analogous to set_learning_rate for SGD.
Learning rate.
First moment decay (typically
0.9).Second moment decay (typically
0.999).Numerical stability constant (typically
1e-8).Execution
step
wait() before reading results.
wait
run
For inference graphs, use
step() as the forward execution method. The session does not have a separate run() method — step() works for both training and inference sessions.Reading outputs
read_loss
wait().
Returns 0.0 if the graph has no loss output.
read_output_by_index
0 is the primary output. Higher indices correspond to additional outputs set via graph.set_outputs().
Output index (0-based).
Destination buffer.
num_outputs
Inspection
plan
memory_summary
MemorySummary with GPU memory statistics for this session.