pub struct ExprGraph { /* private fields */ }Expand description
Arena-based expression graph with structural interning.
Identical subexpressions always return the same ExprId — this gives
automatic common subexpression elimination (CSE) for free.
Implementations§
Source§impl ExprGraph
impl ExprGraph
Sourcepub fn to_c_f64(&self, outputs: &[ExprId], n_inputs: usize) -> ComputeKernel
pub fn to_c_f64(&self, outputs: &[ExprId], n_inputs: usize) -> ComputeKernel
Generate a plain-C kernel in f64: void k0(const double* inputs, double* outputs, int count). Literals keep full precision.
Source§impl ExprGraph
impl ExprGraph
Sourcepub fn compile(&self, expr: ExprId) -> CompiledExpr
pub fn compile(&self, expr: ExprId) -> CompiledExpr
Compile a single expression to a closure &[f64] -> f64.
Shared subexpressions (from interning) are computed once. Dead nodes (not reachable from output) are skipped.
Sourcepub fn compile_many(&self, exprs: &[ExprId]) -> CompiledMany
pub fn compile_many(&self, exprs: &[ExprId]) -> CompiledMany
Compile multiple output expressions to a single closure.
Writes results into the output slice.
Source§impl ExprGraph
impl ExprGraph
Source§impl ExprGraph
impl ExprGraph
Sourcepub fn nodes_slice(&self) -> &[Node]
pub fn nodes_slice(&self) -> &[Node]
Read-only access to the node arena for serialization.
Source§impl ExprGraph
impl ExprGraph
Sourcepub fn vjp(
&mut self,
outputs: &[ExprId],
seeds: &[ExprId],
n_vars: usize,
) -> Vec<ExprId>
pub fn vjp( &mut self, outputs: &[ExprId], seeds: &[ExprId], n_vars: usize, ) -> Vec<ExprId>
The vector-Jacobian product seedᵀ ∂outputs/∂vars as new
expressions in this graph: entry k is Σ_i seeds[i] ∂outputs[i]/∂Var(k)
for k < n_vars. One reverse sweep over the live nodes, so the cost
is a small multiple of evaluating outputs whatever n_vars is
(forward diff is one sweep per variable). Seeds are
expressions too (typically fresh Vars past the inputs), so one
compiled graph serves every seed. Select passes gradient to the
taken branch only, as diff does.
Source§impl ExprGraph
impl ExprGraph
Source§impl ExprGraph
impl ExprGraph
Sourcepub fn to_wgsl_split(
&self,
outputs: &[ExprId],
n_inputs: usize,
max_nodes: usize,
) -> SplitKernels
pub fn to_wgsl_split( &self, outputs: &[ExprId], n_inputs: usize, max_nodes: usize, ) -> SplitKernels
to_kernel for WGSL, cut into kernels of at most
max_nodes evaluated nodes each (topological order); a value used
outside the kernel that computes it goes through scratch.
Sourcepub fn to_wgsl_split_f64(
&self,
outputs: &[ExprId],
n_inputs: usize,
max_nodes: usize,
) -> SplitKernels
pub fn to_wgsl_split_f64( &self, outputs: &[ExprId], n_inputs: usize, max_nodes: usize, ) -> SplitKernels
to_wgsl_split in f64 (buffers, scratch and
arithmetic), for a device with wgpu’s SHADER_F64. Vulkan has no f64
sin/exp2/log2/atan2, so those come from WGSL helpers accurate
to a few ulp. A buffer of n values takes 2n f32 slots.
Source§impl ExprGraph
impl ExprGraph
Sourcepub fn to_wgsl(&self, outputs: &[ExprId], n_inputs: usize) -> WgslKernel
pub fn to_wgsl(&self, outputs: &[ExprId], n_inputs: usize) -> WgslKernel
Generate a WGSL compute shader that evaluates expressions in parallel.
Each work item reads n_inputs values and writes outputs.len() values.
The generated shader uses f32 (GPU native). Shared subexpressions are
computed once per thread.
The caller handles device/pipeline/dispatch (no wgpu dependency here).