boundlab.expr.AffineSum#

class boundlab.expr.AffineSum[source]#

Bases: Expr

An expression representing a sum of linear operations applied to children.

Represents \(\sum_i \mathrm{op}_i(x_i)\) where each \(\mathrm{op}_i\) is a EinsumOp.

During construction, if a child is itself an AffineSum, its pairs are absorbed by composing the outer op with each inner op via @ (eager contraction). This ensures the expression tree is always flat — no AffineSum node ever has an AffineSum child.

pairs#

List of (op, child) tuples.

ops#

List of EinsumOp operators (convenience view).

Methods

__init__

Construct an AffineSum.

all_subnodes

Return a list of all sub-expressions in the DAG rooted at this expression, in topological order.

backward

Propagate weights backward: each child gets weights ∘ op_i.

bound_width

Compute the width of the bounds for this expression.

bound_width_reasons_breakdown

Compute the breakdown of the bound width by reason.

center

Compute the center of the bounds for this expression.

diag

expand

expand_on

flatten

flip

gather

get_const

Return the concrete tensor if self is a pure constant expression, else None.

is_symmetric_to_0

Return True if this expression is symmetric about zero, else False.

lb

Compute a lower bound for this expression.

max_bound_width

Compute the maximum width across all output dimensions.

mean

narrow

permute

repeat

replace_subnode_once

Return a new expression with the same structure but sub-expressions replaced by replace_fn.

reshape

roll

scatter

simplify_ops_

Recursively compute simplified ops for affine expressions.

split_const

Decompose this AffineSum into a constant part and a zero-constant AffineSum.

squeeze

sum

tile

to_string

Return string representation with child strings substituted.

transpose

ub

Compute an upper bound for this expression.

ublb

Compute both an upper bound and a lower bound for this expression.

uncertainty_reasons

Compute the breakdown of the bound width by reason, aggregated to total contributions.

unflatten

unsqueeze

with_children

Return a new AffineSum with the same ops but new children.

zeros_set

static __new__(cls, *pairs, const=None, **_kw)[source]#
__init__(*pairs, const=None)[source]#

Construct an AffineSum.

Parameters:

*pairs (tuple) – Sequence of (op, child) pairs where op is a EinsumOp and child is an Expr or torch.Tensor.

property shape: torch.Size#

The shape of the output(s) produced by this expression.

property children: tuple[Expr, ...]#

The child expressions that serve as inputs to this expression.

with_children(*new_children)[source]#

Return a new AffineSum with the same ops but new children.

backward(weights, direction)[source]#

Propagate weights backward: each child gets weights ∘ op_i.

Parameters:
  • weights – A EinsumOp accumulated weight.

  • direction (Literal['>=', '<=', '==']) – Bound direction (unused — Linear is always linear).

Returns:

(bias, [weights @ op_i for op_i in self.children_dict.values()]).

Return type:

tuple

to_string(*children_str)[source]#

Return string representation with child strings substituted.

simplify_ops_()[source]#

Recursively compute simplified ops for affine expressions.

split_const()[source]#

Decompose this AffineSum into a constant part and a zero-constant AffineSum.

property T: Expr#

Convenience for transpose of the last two dimensions.

__add__(other)#
__mul__(other)#

Element-wise multiplication (no broadcast).

all_subnodes()#

Return a list of all sub-expressions in the DAG rooted at this expression, in topological order.

bound_width()#

Compute the width of the bounds for this expression.

bound_width_reasons_breakdown()#

Compute the breakdown of the bound width by reason.

center()#

Compute the center of the bounds for this expression.

diag(diagonal=0)#
expand(*sizes)#
expand_on(dim, size)#
flatten(start_dim=0, end_dim=-1)#
flip(dims)#
gather(indices, dim=0)#
get_const()#

Return the concrete tensor if self is a pure constant expression, else None.

Works for ConstVal and any AffineSum that has no symbolic children.

is_symmetric_to_0()#

Return True if this expression is symmetric about zero, else False.

lb()#

Compute a lower bound for this expression.

max_bound_width()#

Compute the maximum width across all output dimensions.

mean(dim=None, keepdim=False)#
narrow(dim, start, length)#
permute(*dims)#
repeat(*sizes)#
replace_subnode_once(replace_fn)#

Return a new expression with the same structure but sub-expressions replaced by replace_fn.

reshape(*shape)#
roll(shifts, dims)#
scatter(indices, output_shape)#
squeeze(dim=None)#
sum(dim=None, keepdim=False)#
tile(*sizes)#
transpose(dim0, dim1)#
ub()#

Compute an upper bound for this expression.

ublb()#

Compute both an upper bound and a lower bound for this expression.

uncertainty_reasons()#

Compute the breakdown of the bound width by reason, aggregated to total contributions.

unflatten(dim, sizes)#
unsqueeze(dim)#
zeros_set(output_shape)#
id: int#

Unique identifier for the expression, used for topological sorting.

flags: ExprFlags#

Flags indicating expression properties for optimization.