boundlab.Expr#
- class boundlab.Expr[source]#
Bases:
ABCAbstract base of every BoundLab expression component.
Subclasses implement the primitives below; the base class derives the whole tensor-algebra surface (
+,-,*,@, reductions, conversion, concretization) from them. All linear structure is funnelled througheinsum(), so a component only has to know how a linear map acts on its own representation to be sound under every derived operation.Methods
__init__Bound on the magnitude: \(\max(|lb|, ub) \ge \sup |x|\).
Same-class addition;
__add__dispatches here when classes match and otherwise groups the addends in anExprGroup.Center/halfwidth form:
c = (ub + lb) / 2,w = (ub - lb) / 2.The set of component classes present in this value.
Hook: build an instance of
clsrepresenting exactlyexpr, orNonewhen this class cannot.Hook: convert
selfintoexpr_type, orNonewhen this class does not know how.Apply the linear map described by an integer-label einsum.
Lift a tensor/scalar into a broadcast
Bias; passExprthrough.Sound elementwise lower bound on every concrete value represented.
(lb, ub)in one call; components override it when computing both at once is cheaper than two passes.Split into
(matching, rest)so thatmatching + rest == self.Convert to another component class, exactly.
Collapse to the box hull
Bias(c) + Noise(w).Diagnostics as a
TensorFormat.Sound elementwise upper bound on every concrete value represented.
- abstract property shape_dtype: ShapeDtype#
Allocation-free
(shape, dtype)metadata of the value.
- abstractmethod einsum(subscripts, *operands)[source]#
Apply the linear map described by an integer-label einsum.
subscriptsholds one label tuple per input followed by the output labels (seeboundlab.utils.einsum_parser()). This is the single linear primitive:__mul__,__matmul__,sumandmeanall lower to it, so implementing it soundly makes every derived linear operation sound.
- abstractmethod add(other)[source]#
Same-class addition;
__add__dispatches here when classes match and otherwise groups the addends in anExprGroup.
- lbub()[source]#
(lb, ub)in one call; components override it when computing both at once is cheaper than two passes.
- final to_intervals(name='')[source]#
Collapse to the box hull
Bias(c) + Noise(w).Sound but lossy: every correlation between error symbols is dropped, so downstream cancellation (
x - x = 0) no longer happens.
- static from_exprlike(expr, shape)[source]#
Lift a tensor/scalar into a broadcast
Bias; passExprthrough.
- torch_print(group='reason')[source]#
Diagnostics as a
TensorFormat.The result is safe to hand to
Torch diagnostic outputunder tracing: only plain arrays cross the callback boundary, never expression metadata (which may hold tracers, e.g. reason weights).
- classmethod convert_from(expr)[source]#
Hook: build an instance of
clsrepresenting exactlyexpr, orNonewhen this class cannot. One half ofto().
- convert_to(expr_type)[source]#
Hook: convert
selfintoexpr_type, orNonewhen this class does not know how. The other half ofto().
- final to(expr_type)[source]#
Convert to another component class, exactly.
Identity short-circuits; otherwise the target’s
convert_fromis tried, then this class’sconvert_to. RaisesTypeErrorwhen neither side knows the conversion — conversions never approximate.