Skip to main content

Module backend_default_walk

Module backend_default_walk 

Source
Expand description

Shared default impls for the Backend Contract trait.

The Contract surface is thirty typed per-op methods (super::Backend::add, super::Backend::matmul, …) plus super::Backend::execute (&GraphProto, HashMapHashMap). Each side has a default body that calls into the other so backend authors override whichever side is natural:

  • Override per-op methods (e.g. CpuBackend over ndarray): each Contract method is a direct kernel call. The default execute walks the graph node-by-node and dispatches through the overridden per-op methods.

  • Override execute (e.g. a graph-compiling backend like Burn): the whole GraphProto body is handed to the native execution engine. The per-op defaults wrap a single-node GraphProto and call back into execute.

Pathological case: a backend that overrides neither side stack-overflows — every add call walks into a single-node graph that calls add again. Document loudly on the trait.

This module also encodes the attribute conventions every BackendSubgraph_* carrier uses for primitive ops with attributes (ReduceSum.axes, Reshape.shape, Cast.to, …). Per-op defaults call ints_attr / int_attr / tensor_attr to encode; the walker calls attr_ints / attr_int / attr_tensor to decode. ONNX-style names are preserved (axes, keepdims, shape, perm, axis, to, value).

Enums§

BackendWalkError
Failures the default walker surfaces when handed a malformed GraphProto body or when a Backend::execute impl violates its output-name contract. Required From bound on Backend::Error makes the walker fail with a typed error instead of panic!-ing on peer-supplied or buggy input.

Functions§

execute_graph_via_per_op
Default body for Backend::execute — walks graph.node in topological order, dispatching each through the typed per-op methods on backend. The implementation is a tight linear scan: ONNX guarantees graph.node is topologically ordered, so no petgraph / explicit ordering is needed.
execute_multi
Default body for multi-output per-op methods (Backend::split today). Builds a one-node GraphProto with output_count positionally-named outputs, calls Backend::execute, and extracts the outputs in declared order.
execute_single
Default body for every per-op method on Backend — wraps the op in a one-node GraphProto and routes through Backend::execute. Backends that override execute natively (graph-compiling backends) get the per-op surface free; backends that override the per-op methods directly bypass this helper entirely.
int_attr
Build an AttributeProto of type INT (per ONNX [AttributeType::Int]). Used by the Contract per-op defaults for scalar i64 attributes (axis, to, keepdims).
ints_attr
Build an AttributeProto of type INTS. Used for vector attributes (axes, shape, perm, starts, ends, steps, split).
tensor_attr
Build an AttributeProto of type TENSOR. Used for Constant’s value attribute.