Interface
This section lists the interface for structured matrices, that is the operations they need to implement to work in SINGD. It serves for internal purposes only. This is useful for developers that wish to add a new structured matrix class to the code that cannot be constructed with one of the available templates.
singd.structures.base.StructuredMatrix
Bases: ABC
Base class for structured matrices closed under addition and multiplication.
This base class defines the functions that need to be implemented to support a new structured matrix class with SINGD.
The minimum amount of work to add a new structured matrix class requires implementing the following methods:
to_dense
from_dense
All other operations will then use a naive implementation which internally re-constructs unstructured dense matrices. By default, these operations will trigger a warning which can be used to identify functions that can be implemented more efficiently using structure.
Note
You need to register tensors that represent parts of the represented
matrix using the register_tensor
method. This is similar to the
mechanism in PyTorch modules, which have a register_parameter
method.
It allows to support many operations out of the box.
Attributes:
-
WARN_NAIVE
(bool
) –Warn the user if a method falls back to a naive implementation of this base class. This indicates a method that should be implemented to save memory and run time by considering the represented structure. Default:
True
. -
WARN_NAIVE_EXCEPTIONS
(Set[str]
) –Set of methods that should not trigger a warning even if
WARN_NAIVE
isTrue
. This can be used to silence warnings for methods for which it is too complicated to leverage a specific structure and which should therefore call out to this class's implementation without performance warnings.
Initialize the structured matrix.
Source code in singd/structures/base.py
__add__
Add another matrix of same structure.
Parameters:
-
other
(StructuredMatrix
) –Another structured matrix which will be added.
Returns:
-
StructuredMatrix
–A structured matrix resulting from the addition.
Source code in singd/structures/base.py
__matmul__
Multiply onto a matrix (@ operator).
Parameters:
-
other
(Union[StructuredMatrix, Tensor]
) –Another matrix which will be multiplied onto. Can be represented by a PyTorch tensor or a structured matrix.
Returns:
-
Union[StructuredMatrix, Tensor]
–Result of the multiplication. If a PyTorch tensor was passed as argument,
-
Union[StructuredMatrix, Tensor]
–the result will be a PyTorch tensor. Otherwise, it will be a a structured
-
Union[StructuredMatrix, Tensor]
–matrix.
Source code in singd/structures/base.py
__mul__
Multiply with a scalar.
Parameters:
-
other
(float
) –A scalar that will be multiplied onto the structured matrix.
Returns:
-
StructuredMatrix
–The structured matrix, multiplied by the scalar.
Source code in singd/structures/base.py
add_
In-place addition with another structured matrix.
Parameters:
-
other
(StructuredMatrix
) –Another structured matrix which will be added in-place.
-
alpha
(float
, default:1.0
) –A scalar that will be multiplied onto
other
before adding it. Default:1.0
.
Returns:
-
StructuredMatrix
–Reference to the in-place updated matrix.
Source code in singd/structures/base.py
all_reduce
all_reduce(op: dist.ReduceOp = dist.ReduceOp.AVG, group: Union[dist.ProcessGroup, None] = None, async_op: bool = False) -> Union[None, Tuple[torch._C.Future, ...]]
Reduce the structured matrix across all devices.
This method only has to be implemented to support distributed data parallel training.
Parameters:
-
op
(ReduceOp
, default:AVG
) –The reduction operation to perform (default:
dist.ReduceOp.AVG
). -
group
(Union[ProcessGroup, None]
, default:None
) –The process group to work on. If
None
, the default process group will be used. -
async_op
(bool
, default:False
) –If
True
, this function will return atorch.distributed.Future
object. Otherwise, it will block until the reduction completes (default:False
).
Returns:
-
Union[None, Tuple[Future, ...]]
–If
async_op
isTrue
, a (tuple of)torch.distributed.Future
-
Union[None, Tuple[Future, ...]]
–object(s), else
None
.
Source code in singd/structures/base.py
average_trace
Compute the average trace of the represented matrix.
Returns:
-
Tensor
–The average trace of the represented matrix.
diag_add_
In-place add a value to the diagonal of the represented matrix.
Parameters:
-
value
(float
) –Value to add to the diagonal.
Returns:
-
StructuredMatrix
–A reference to the updated matrix.
Source code in singd/structures/base.py
eye
classmethod
eye(dim: int, dtype: Union[torch.dtype, None] = None, device: Union[torch.device, None] = None) -> StructuredMatrix
Create a structured matrix representing the identity matrix.
Parameters:
-
dim
(int
) –Dimension of the (square) matrix.
-
dtype
(Union[dtype, None]
, default:None
) –Optional data type of the matrix. If not specified, uses the default tensor type.
-
device
(Union[device, None]
, default:None
) –Optional device of the matrix. If not specified, uses the default tensor type.
Returns:
-
StructuredMatrix
–A structured matrix representing the identity matrix.
Source code in singd/structures/base.py
from_dense
abstractmethod
classmethod
Extract the represented structure from a dense symmetric matrix.
This will discard elements that are not part of the structure, even if they are non-zero.
Warning
We do not verify whether mat
is symmetric internally.
Parameters:
-
sym_mat
(Tensor
) –A symmetric dense matrix which will be converted into a structured one.
Returns:
-
StructuredMatrix
–Structured matrix.
Raises:
-
NotImplementedError
–Must be implemented by a child class.
Source code in singd/structures/base.py
from_inner
Extract the represented structure from self.T @ X @ X^T @ self
.
We can recycle terms by writing self.T @ X @ X^T @ self
as S @ S^T
with S := self.T @ X
.
Parameters:
-
X
(Union[Tensor, None]
, default:None
) –Optional arbitrary 2d tensor. If
None
,X = I
will be used.
Returns:
-
StructuredMatrix
–The structured matrix extracted from
self.T @ X @ X^T @ self
.
Source code in singd/structures/base.py
from_inner2
Extract the represented structure from self.T @ XXT @ self
.
Parameters:
-
XXT
(Tensor
) –2d square symmetric matrix.
Returns:
-
StructuredMatrix
–The structured matrix extracted from
self.T @ XXT @ self
.
Source code in singd/structures/base.py
infinity_vector_norm
Compute the infinity vector norm.
The infinity vector norm is the absolute value of the largest entry. Note that this is different from the infinity matrix norm, compare here and here.
Returns:
-
Tensor
–The matrix's infinity vector norm.
Source code in singd/structures/base.py
mul_
In-place multiplication with a scalar.
Parameters:
-
value
(float
) –A scalar that will be multiplied onto the structured matrix.
Returns:
-
StructuredMatrix
–Reference to the in-place updated matrix.
Source code in singd/structures/base.py
named_tensors
Yield all tensors that represent the matrix and their names.
Yields:
-
str
–A tuple of the tensor's name and the tensor itself.
Source code in singd/structures/base.py
register_tensor
Register a tensor that represents a part of the matrix structure.
Parameters:
-
tensor
(Tensor
) –A tensor that represents a part of the matrix structure.
-
name
(str
) –A name for the tensor. The tensor will be available under
self.name
.
Raises:
-
ValueError
–If the name is already in use.
Source code in singd/structures/base.py
rmatmat
Multiply the structured matrix's transpose onto a matrix (self.T @ mat
).
Parameters:
-
mat
(Tensor
) –A dense matrix that will be multiplied onto.
Returns:
-
Tensor
–A dense PyTorch tensor resulting from the multiplication.
Source code in singd/structures/base.py
to_dense
abstractmethod
Return a dense tensor representing the structured matrix.
Returns:
-
Tensor
–A dense PyTorch tensor representing the matrix.
Raises:
-
NotImplementedError
–Must be implemented by a child class.
Source code in singd/structures/base.py
zeros
classmethod
zeros(dim: int, dtype: Union[torch.dtype, None] = None, device: Union[torch.device, None] = None) -> StructuredMatrix
Create a structured matrix representing the zero matrix.
Parameters:
-
dim
(int
) –Dimension of the (square) matrix.
-
dtype
(Union[dtype, None]
, default:None
) –Optional data type of the matrix. If not specified, uses the default tensor type.
-
device
(Union[device, None]
, default:None
) –Optional device of the matrix. If not specified, uses the default tensor type.
Returns:
-
StructuredMatrix
–A structured matrix representing the zero matrix.