boundlab.linearop.EinsumOp#
- class boundlab.linearop.EinsumOp[source]#
Bases:
LinearOpA linear operator defined by an Einstein summation with a fixed tensor.
Depending on
input_dimsandoutput_dims, this can express contraction (dot-product-like behavior), Hadamard-style elementwise multiplication, and dimension expansion.Methods
Initialize an EinsumOp.
Return a LinearOp representing the element-wise absolute value of this EinsumOp.
Apply the transposed linear function to an input tensor.
Materialize Jacobian via batched forward/backward application.
Apply the original linear function to an input tensor.
Create an EinsumOp that fully contracts over the specified input dimension.
Create an EinsumOp for Hadamard-style multiplication with
tensor.Check if this EinsumOp fully contracts all input dimensions (output_dims & input_dims == ø).
Check whether this EinsumOp performs no contraction over input dims.
Check if this EinsumOp doesn't introduce new dimensions (output_dims - input_dims == ø).
Check if this EinsumOp is effectively a tensordot (no elementwise multiplication).
Return an explicit Jacobian for full-layout
EinsumOpinstances.Materialize this LinearOp as an explicit Jacobian tensor.
Accumulate this operator's Jacobian into
srcefficiently.Return a LinearOp that sums over the input dimensions of this EinsumOp.
Return a LinearOp that sums over the output dimensions of this EinsumOp.
Apply the transposed linear function to an input tensor, supporting additional leading dimensions for batching.
Apply the original linear function to an input tensor, supporting additional trailing dimensions for batching.
- __init__(tensor, input_dims, output_dims, name=None)[source]#
Initialize an EinsumOp.
This operator behaves like contraction on
input_dims - output_dims, elementwise multiplication oninput_dims & output_dims, and expansion onoutput_dims - input_dims.- Parameters:
tensor (torch.Tensor) – The fixed tensor used in the Einstein summation.
input_dims (list[int]) – A list of dimensions of tensor that correspond to the input tensor dimensions.
output_dims (list[int]) – A list of dimensions of tensor that correspond to the output tensor dimensions.
name – Optional name for display purposes.
- vforward(x)[source]#
Apply the original linear function to an input tensor, supporting additional trailing dimensions for batching.
- vbackward(grad)[source]#
Apply the transposed linear function to an input tensor, supporting additional leading dimensions for batching.
- is_full()[source]#
Check if this EinsumOp fully contracts all input dimensions (output_dims & input_dims == ø).
- is_hardmard()[source]#
Check whether this EinsumOp performs no contraction over input dims.
Note
The method name keeps the historical
hardmardspelling for backward compatibility.
- is_non_expanding()[source]#
Check if this EinsumOp doesn’t introduce new dimensions (output_dims - input_dims == ø).
- is_tensordot()[source]#
Check if this EinsumOp is effectively a tensordot (no elementwise multiplication).
- static from_hardmard(tensor, n_input_dims, name=None)[source]#
Create an EinsumOp for Hadamard-style multiplication with
tensor.Note
The constructor name keeps the historical
hardmardspelling for backward compatibility.
- static from_full(tensor, input_dim, name=None)[source]#
Create an EinsumOp that fully contracts over the specified input dimension.
- __add__(other)[source]#
Add this EinsumOp to another LinearOp, returning a new LinearOp representing the sum.
- jacobian()[source]#
Return an explicit Jacobian for full-layout
EinsumOpinstances.- Returns:
A tensor with shape
[*output_shape, *input_shape]when the operator is in full representation (is_full()). Otherwise returnsNotImplemented.- Return type:
- jacobian_scatter(src)[source]#
Accumulate this operator’s Jacobian into
srcefficiently.- Parameters:
src (torch.Tensor) – Accumulator tensor with shape
[*output_shape, *input_shape].- Returns:
A tensor with the same shape as
srcequal tosrc + jacobian(self).- Return type:
Notes
For full-layout operators, this reduces to direct addition. For compressed/masked layouts, this method performs a structured diagonal-scatter update that avoids full Jacobian expansion in the common case.
- __call__(x)#
Apply this LinearOp to an expression, returning a Linear.
- force_jacobian()#
Materialize Jacobian via batched forward/backward application.
This fallback constructs an identity basis and applies either
vforward()orvbackward()depending on whether the input or output side is smaller.- Returns:
A dense Jacobian tensor with shape
[*output_shape, *input_shape].
Notes
This may be expensive in time and memory and is mainly intended for debugging, validation, or rare paths that require explicit Jacobians.
- jacobian_op()#
Materialize this LinearOp as an explicit Jacobian tensor.
- Returns:
A tensor with shape
[*output_shape, *input_shape]representing the Jacobian of this LinearOp.- Return type:
Notes
This may be expensive in time and memory and is mainly intended for debugging, validation, or rare paths that require explicit Jacobians.
- input_shape: torch.Size#
Expected input tensor shape.
- output_shape: torch.Size#
Computed output tensor shape.