Skip to content

graph #

Replayable execution plans

vtl.graph is an explicit, replayable execution-plan slice for #740. It does not auto-capture eager calls; callers record the supported operations once and replay them for repeated workloads.

mut plan := graph.new_plan[f64]([1024])!
plan.add_scalar(1.0)!
plan.multiply_scalar(0.5)!
plan.optimize()
output := plan.run(input)!
  • Plan[T] is bound to a fixed input shape; run validates shape before replay and matches the equivalent eager scalar operations.
  • Supported steps are add_scalar and multiply_scalar.
  • optimize() groups adjacent same-kind steps into one replay step (additions sum, multiplications multiply for inspection) and reports how many steps were removed. The original per-step scalars stay in replay order.
  • run calls the shared vtl.fused_scalar_chain kernel: it allocates one output tensor and updates it in place. The eager *_scalar_promoted helpers allocate a new tensor (and scalar tensor) per operation, so fused replay removes those intermediate allocations while preserving per-step casts and floating-point rounding.
  • forward_variable replays a plan on an autograd.Variable for inference and returns a fresh non-grad variable on the same context. Gradient-aware capture stays on the eager autograd path.
  • plan_layer wraps a plan as a vtl.nn layer (no trainable parameters), so a plan can be used in Sequential via sequential_from_ctx_with_layers.

Benchmark (build with -prod, then run the binary):

v -prod -o graph_plan_bench benchmarks/graph_plan_bench.v
./graph_plan_bench

It reports eager vs fused replay time for 64 scalar operations over a [1024] tensor; one measured -prod run on this host gave eager=175ms fused_plan=12ms (14.58x, 200 reps). That is one machine, shape and operation chain, not general parity.

Run the focused tests with:

v test tests/graph/plan_test.v

fn new_plan #

fn new_plan[T](input_shape []int) !&Plan[T]

new_plan creates an empty plan for a fixed input shape.

fn plan_layer #

fn plan_layer[T](plan &Plan[T]) types.Layer[T]

plan_layer wraps a plan as a vtl.nn layer so fused plans can be placed in a Sequential alongside ordinary layers. The wrapper owns no parameters; the plan is inference-only, matching forward_variable.

fn StepKind.from #

fn StepKind.from[W](input W) !StepKind

from returns the StepKind value of an integer or of a field name, or an error if there is none.

fn (Plan[T]) add_scalar #

fn (mut p Plan[T]) add_scalar(value f64) !

add_scalar appends scalar addition.

fn (Plan[T]) multiply_scalar #

fn (mut p Plan[T]) multiply_scalar(value f64) !

multiply_scalar appends scalar multiplication.

fn (Plan[T]) step_count #

fn (p &Plan[T]) step_count() int

step_count reports the number of replayable steps.

fn (Plan[T]) steps_copy #

fn (p &Plan[T]) steps_copy() []Step

steps_copy returns a copy of the recorded steps for inspection.

fn (Plan[T]) optimize #

fn (mut p Plan[T]) optimize() int

optimize groups adjacent steps of the same kind into a single replay step. Addition chains combine by addition and multiplication chains by multiplication for the inspected value, while the original scalars stay in order for replay. It returns the number of removed steps.

fn (Plan[T]) run #

fn (p &Plan[T]) run(input &vtl.Tensor[T]) !&vtl.Tensor[T]

run validates the input shape and replays the plan through the fused vtl kernel: one output tensor is allocated and updated in place instead of one tensor per scalar. Results match the equivalent eager scalar operations in the same order, including per-step scalar conversion and floating-point rounding.

fn (Plan[T]) forward_variable #

fn (p &Plan[T]) forward_variable(input &autograd.Variable[T]) !&autograd.Variable[T]

forward_variable replays a plan on an autograd Variable for inference. The result is a fresh Variable on the input's context with requires_grad false: gradient-aware capture remains on the eager autograd path.

enum StepKind #

enum StepKind {
	add_scalar
	multiply_scalar
}

StepKind identifies a replayable elementwise operation. This initial slice covers scalar arithmetic chains, which can be fused into one replay step without dropping the original per-step scalars or their evaluation order.

struct Plan #

struct Plan[T] {
mut:
	steps []Step
pub:
	input_shape []int
}

Plan is an explicit, replayable execution graph over a fixed input shape. It is pure-tensor only: autograd variables must stay on the eager path until gradient-aware capture is implemented.

struct Step #

struct Step {
pub:
	kind   StepKind
	value  f64
	values []f64
}

Step records one operation in a plan. value is the f64 fold of the scalars, exposed for inspection; values holds the original scalars in replay order so each cast and rounding step is preserved.