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.
Return a new EinsumOp with additional (input_dim, output_dim) pairs multiplied together.
Apply the transposed linear function to an input tensor.
Materialize this LinearOp as an explicit Jacobian 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.Convert a ScalarOp to an EinsumOp.
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.Add this operator's Jacobian contribution into an existing tensor.
Return a LinearOp that computes the norm over the input dimensions of this EinsumOp.
Return a LinearOp that computes the norm over the output dimensions of this EinsumOp.
Return a new EinsumOp with some (input_dim, output_dim) pairs no longer multiplied together.
Drop
input_dims[idx]whose corresponding tensor dim has size 1.Drop
output_dims[idx]whose corresponding tensor dim has size 1.Return a LinearOp that sums over the input dimensions of this EinsumOp.
Return a LinearOp that sums over the output dimensions of this EinsumOp.
Insert a new size-1 input dim at position
idx.Insert a new size-1 output dim at position
idx.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.
Return a new EinsumOp with the same input/output dims but a different tensor.
- __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=None, 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:
- norm_input(p=1)[source]#
Return a LinearOp that computes the norm over the input dimensions of this EinsumOp.
- norm_output(p=1)[source]#
Return a LinearOp that computes the norm over the output dimensions of this EinsumOp.
- property mul_conditions: EQCondition#
Return a list of (input_dim, output_dim) pairs that are multiplied together in this EinsumOp.
- add_conditions(target)[source]#
Return a new EinsumOp with additional (input_dim, output_dim) pairs multiplied together.
- remove_conditions(target)[source]#
Return a new EinsumOp with some (input_dim, output_dim) pairs no longer multiplied together.
- with_tensor(tensor)[source]#
Return a new EinsumOp with the same input/output dims but a different tensor.
- __call__(x)#
Apply this LinearOp to an expression, returning a Linear.
- einsum_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.
- 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_scatter(src)#
Add this operator’s Jacobian contribution into an existing tensor.
- Parameters:
src (torch.Tensor) – A tensor with Jacobian layout
[*output_shape, *input_shape]that acts as the accumulation buffer.- Returns:
A tensor with the same shape as
srccontainingsrc + jacobian(self).- Return type:
Notes
Subclasses may override this to implement structured/sparse updates without materializing the full Jacobian first.
- input_shape: torch.Size#
Expected input tensor shape.
- output_shape: torch.Size#
Computed output tensor shape.