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 rounding models such as the noncomputable
FloatLib.Floats.Formats.Flocq.NF, whose arithmetic uses Flocq.round. Lean gives builtin Float
a logical model, but connecting that model and its runtime implementation to these approximation
bounds requires separate per-operation proofs.
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 #
Autograd.Algebra.TensorPackandEList: heterogeneous contexts and aligned error vectors.approxTensorandapproxCtx: 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 array only after propagation has
finished.
Instances For
Flattening an error list to an array keeps one entry per shape.
Tensor-level approximation under a toSpec : α → ℝ mapping.
Instances For
Scoped notation for tensor approximation (approxTensor).
open scoped RuntimeApprox
spec ≈ᵀ[toSpec] runtime : eps
Instances For
Monotonicity of approxTensorWithTol: if you only loosen tolerances, an approximation stays valid.
approxTensorWithTol specialized to an absolute-only tolerance is equivalent to approxTensor.
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 approxTensorWithTol 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
τ.
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 approxTensor 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 (approxTensor) with error at most bound.
- forwardSpec : TorchLean.TensorPack Spec.SpecScalar Γ → Spec.SpecTensor τ
Specification-level semantics of this node.
- forwardRuntime : TorchLean.TensorPack α Γ → TorchLean.Tensor α τ
Runtime semantics of this node.
- bound : EList Γ → TorchLean.TensorPack α Γ → Spec.SpecScalar
Error bound computed from the current context bounds and runtime values.
- sound (xS : TorchLean.TensorPack Spec.SpecScalar Γ) (xR : TorchLean.TensorPack α Γ) (eps : EList Γ) : approxCtx toSpec xS xR eps → approxTensor 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} [TorchLean.Storage α] {toSpec : α → Spec.SpecScalar} {Γ : List Spec.Shape} : FwdGraph toSpec Γ []
- snoc {α : Type} [TorchLean.Storage α] {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
theorem (FwdNode.sound) and then extend the context approximation via approxCtx_snoc.