ForwardApprox #
Forward (runtime→spec) approximation framework.
This file is backend-agnostic: it proves that approximation bounds compose over a tape/SSA-style graph, assuming each node provides a local forward approximation lemma.
It is intended to be instantiated by proof-relevant runtimes such as rounding models
(NF / neural_round). Lean's builtin Float is treated as trusted (see
NN/Runtime/Scalar.lean).
What you get #
FwdGraph.eval_approx: an end-to-end theorem saying that if the runtime input context is within an explicit per-entry error budget of the spec input context, then runtime graph evaluation is within a computable propagated error budget of the spec evaluation.
Reading guide #
TListandEList: heterogeneous runtime/spec contexts and aligned error vectors.approxTandapproxCtx: the approximation predicates for a single tensor and a whole context.Idx: a typed index into a context (so graph nodes can refer to earlier values safely).FwdNode/FwdGraph: local approximation lemmas and their composition over a snoc-list DAG.
PyTorch correspondence / citations #
This is conceptually similar to the “graph of ops” view behind PyTorch Autograd (and tooling like
torch.fx), except that our graph nodes carry proof-relevant approximation bounds that can be
composed into an end-to-end theorem.
https://pytorch.org/docs/stable/autograd.html
https://pytorch.org/docs/stable/fx.html
Scalar error bounds aligned with a context shape list.
- nil : EList []
- cons {s : Spec.Shape} {ss : List Spec.Shape} : ℝ → EList ss → EList (s :: ss)
Instances For
Cast an error list along an equality of shape lists.
Instances For
Casting along rfl is the identity.
EList.cast composes as expected.
Append a scalar error bound to the end of an error list.
Instances For
Split an error list aligned with ss ++ [τ] into its prefix and last scalar.
Instances For
Get the ith scalar bound from an error list.
Instances For
Erase shape indices while retaining bounds in context order.
This is the reporting boundary used by architecture-independent numerical traces. Proofs continue
to use EList; UI and serialization code receives an ordinary list only after propagation has
finished.
Instances For
Tensor-level approximation under a toSpec : α → ℝ mapping.
Instances For
Scoped notation for tensor approximation (approxT).
open scoped RuntimeApprox
spec ≈ᵀ[toSpec] runtime : eps
Instances For
Monotonicity of approxTTol: if you only loosen tolerances, an approximation stays valid.
approxTTol specialized to an absolute-only tolerance is equivalent to approxT.
This is mostly a convenience lemma for switching between the "tolerance" API and the plain
eps : ℝ API.
Context-level approximation with a per-entry error list.
Instances For
Scoped notation for approxCtx.
open scoped RuntimeApprox
-- `ΓS` is an approximate view of `ΓR` with per-entry bounds `eps`:
ΓS ≈ᶜ[toSpec] ΓR : eps
The notation is scoped to avoid polluting unrelated code.
Instances For
Transport a context approximation across an equality of shape lists.
This is used any time we need to reassociate Γ ++ ss type indices (casts are unavoidable in this
List Shape-indexed encoding).
Extend a context approximation by appending one more approximated tensor.
This is the core "composition" step used when evaluating a snoc-graph: if the previous context is
approximated, and the new node output is approximated with some bound e, then the extended context
is approximated with the extended error list.
Extract a single entry approximation from approxCtx.
approxCtx_get expressed in terms of approxTTol with an absolute-only tolerance.
Many downstream theorems are stated using a tolerance record (ApproxTol) rather than a bare
eps : ℝ. For absolute-only bounds, this lemma gives the bridge.
Split a context approximation for ss ++ [τ] into:
- a prefix context approximation for
ss, and - a single-tensor approximation for the last entry of shape
τ.
An index into a heterogeneous context, carrying a proof of the expected shape.
Position in the heterogeneous context.
Proof that the selected context entry has shape
s.
Instances For
Typed lookup from a heterogeneous context TList α Γ using an index Idx Γ s.
Instances For
Lookup the epsilon entry associated to an index Idx Γ s.
Instances For
Context approximation implies approximation of any indexed entry.
Informally: if every tensor in the runtime context is close to its spec counterpart (with an
aligned error list eps), then reading any entry idx : Idx Γ s yields an approxT fact with the
corresponding scalar bound getIdxEps eps idx.
A single SSA/DAG node with a local forward approximation lemma.
Fields:
forwardSpec/forwardRuntime: the spec vs runtime semantics of the node.bound: computes an explicit scalar error bound for the node’s output, given current context bounds and the runtime context (to allow data-dependent bounds).sound: the “local theorem” that justifiesbound.
Informally: if the whole input context is approximated (approxCtx), then this node’s output is
approximated (approxT) with error at most bound.
- forwardSpec : TList Spec.SpecScalar Γ → Spec.SpecTensor τ
Specification-level semantics of this node.
- forwardRuntime : TList α Γ → Spec.Tensor α τ
Runtime semantics of this node.
- bound : EList Γ → TList α Γ → Spec.SpecScalar
Error bound computed from the current context bounds and runtime values.
- sound (xS : TList Spec.SpecScalar Γ) (xR : TList α Γ) (eps : EList Γ) : approxCtx toSpec xS xR eps → approxT toSpec (self.forwardSpec xS) (self.forwardRuntime xR) (self.bound eps xR)
Local approximation theorem for this node.
Instances For
Forward-only SSA/DAG graph (nodes appended in topological order).
The type parameter ss : List Shape tracks the shapes of intermediate values produced by the
graph; evaluating a graph returns an extended context of shape list Γ ++ ss.
- nil {α : Type} {toSpec : α → Spec.SpecScalar} {Γ : List Spec.Shape} : FwdGraph toSpec Γ []
- snoc {α : Type} {toSpec : α → Spec.SpecScalar} {Γ ss : List Spec.Shape} {τ : Spec.Shape} : FwdGraph toSpec Γ ss → FwdNode toSpec (Γ ++ ss) τ → FwdGraph toSpec Γ (ss ++ [τ])
Instances For
Evaluate a forward graph in the spec semantics.
Result type: an extended context Γ ++ ss containing the original inputs and all intermediate
values produced by the graph.
Instances For
Evaluate a forward graph in the runtime semantics.
This mirrors evalSpec, but uses the backend α tensors and the node runtime closures.
Instances For
Propagate an input error list epsIn through the whole graph, producing output bounds for
Γ ++ ss.
Each node can compute its own output bound from the current context bounds and the runtime context;
evalBounds just composes those local transformers over the snoc-list DAG.
Instances For
End-to-end forward approximation theorem for FwdGraph.
Informally:
assume every input tensor in the runtime context xR is within the provided per-entry bounds
epsIn of the corresponding spec tensor in xS. Then evaluating the whole graph preserves that
approximation relation, with output bounds given by evalBounds.
Proof idea: induction over the snoc-list graph; at each step, apply the node's local bound/soundness
lemma (FwdNode.sound) and then extend the context approximation via approxCtx_snoc.