|
| 1 | +from typing import List, Optional, Tuple, Union |
| 2 | + |
| 3 | +import torch |
| 4 | +from jaxtyping import Bool, Float |
| 5 | +from torch import Tensor |
| 6 | + |
| 7 | +from ._linear_operator import _is_noop_index, IndexType, LinearOperator |
| 8 | + |
| 9 | + |
| 10 | +class MaskedLinearOperator(LinearOperator): |
| 11 | + r""" |
| 12 | + A :obj:`~linear_operator.operators.LinearOperator` that applies a mask to the rows and columns of a base |
| 13 | + :obj:`~linear_operator.operators.LinearOperator`. |
| 14 | + """ |
| 15 | + |
| 16 | + def __init__( |
| 17 | + self, |
| 18 | + base: Float[LinearOperator, "*batch M0 N0"], |
| 19 | + row_mask: Bool[Tensor, "M0"], |
| 20 | + col_mask: Bool[Tensor, "N0"], |
| 21 | + ): |
| 22 | + r""" |
| 23 | + Create a new :obj:`~linear_operator.operators.MaskedLinearOperator` that applies a mask to the rows and columns |
| 24 | + of a base :obj:`~linear_operator.operators.LinearOperator`. |
| 25 | +
|
| 26 | + :param base: The base :obj:`~linear_operator.operators.LinearOperator`. |
| 27 | + :param row_mask: A :obj:`torch.BoolTensor` containing the mask to apply to the rows. |
| 28 | + :param col_mask: A :obj:`torch.BoolTensor` containing the mask to apply to the columns. |
| 29 | + """ |
| 30 | + super().__init__(base, row_mask, col_mask) |
| 31 | + self.base = base |
| 32 | + self.row_mask = row_mask |
| 33 | + self.col_mask = col_mask |
| 34 | + self.row_eq_col_mask = torch.equal(row_mask, col_mask) |
| 35 | + |
| 36 | + @staticmethod |
| 37 | + def _expand(tensor: Float[Tensor, "*batch N C"], mask: Bool[Tensor, "N0"]) -> Float[Tensor, "*batch N0 C"]: |
| 38 | + res = torch.zeros( |
| 39 | + *tensor.shape[:-2], |
| 40 | + mask.size(-1), |
| 41 | + tensor.size(-1), |
| 42 | + device=tensor.device, |
| 43 | + dtype=tensor.dtype, |
| 44 | + ) |
| 45 | + res[..., mask, :] = tensor |
| 46 | + return res |
| 47 | + |
| 48 | + def _matmul( |
| 49 | + self: Float[LinearOperator, "*batch M N"], |
| 50 | + rhs: Union[Float[torch.Tensor, "*batch2 N C"], Float[torch.Tensor, "*batch2 N"]], |
| 51 | + ) -> Union[Float[torch.Tensor, "... M C"], Float[torch.Tensor, "... M"]]: |
| 52 | + rhs_expanded = self._expand(rhs, self.col_mask) |
| 53 | + res_expanded = self.base.matmul(rhs_expanded) |
| 54 | + res = res_expanded[..., self.row_mask, :] |
| 55 | + |
| 56 | + return res |
| 57 | + |
| 58 | + def _t_matmul( |
| 59 | + self: Float[LinearOperator, "*batch M N"], |
| 60 | + rhs: Union[Float[Tensor, "*batch2 M P"], Float[LinearOperator, "*batch2 M P"]], |
| 61 | + ) -> Union[Float[LinearOperator, "... N P"], Float[Tensor, "... N P"]]: |
| 62 | + rhs_expanded = self._expand(rhs, self.row_mask) |
| 63 | + res_expanded = self.base.t_matmul(rhs_expanded) |
| 64 | + res = res_expanded[..., self.col_mask, :] |
| 65 | + return res |
| 66 | + |
| 67 | + def _size(self) -> torch.Size: |
| 68 | + return torch.Size( |
| 69 | + (*self.base.size()[:-2], torch.count_nonzero(self.row_mask), torch.count_nonzero(self.col_mask)) |
| 70 | + ) |
| 71 | + |
| 72 | + def _transpose_nonbatch(self: Float[LinearOperator, "*batch M N"]) -> Float[LinearOperator, "*batch N M"]: |
| 73 | + return self.__class__(self.base.mT, self.col_mask, self.row_mask) |
| 74 | + |
| 75 | + def _diagonal(self: Float[LinearOperator, "... M N"]) -> Float[torch.Tensor, "... N"]: |
| 76 | + if not self.row_eq_col_mask: |
| 77 | + raise NotImplementedError() |
| 78 | + diag = self.base.diagonal() |
| 79 | + return diag[..., self.row_mask] |
| 80 | + |
| 81 | + def to_dense(self: Float[LinearOperator, "*batch M N"]) -> Float[Tensor, "*batch M N"]: |
| 82 | + full_dense = self.base.to_dense() |
| 83 | + return full_dense[..., self.row_mask, :][..., :, self.col_mask] |
| 84 | + |
| 85 | + def _bilinear_derivative(self, left_vecs: Tensor, right_vecs: Tensor) -> Tuple[Optional[Tensor], ...]: |
| 86 | + left_vecs = self._expand(left_vecs, self.row_mask) |
| 87 | + right_vecs = self._expand(right_vecs, self.col_mask) |
| 88 | + return self.base._bilinear_derivative(left_vecs, right_vecs) + (None, None) |
| 89 | + |
| 90 | + def _expand_batch( |
| 91 | + self: Float[LinearOperator, "... M N"], batch_shape: Union[torch.Size, List[int]] |
| 92 | + ) -> Float[LinearOperator, "... M N"]: |
| 93 | + return self.__class__(self.base._expand_batch(batch_shape), self.row_mask, self.col_mask) |
| 94 | + |
| 95 | + def _unsqueeze_batch(self, dim: int) -> LinearOperator: |
| 96 | + return self.__class__(self.base._unsqueeze_batch(dim), self.row_mask, self.col_mask) |
| 97 | + |
| 98 | + def _getitem(self, row_index: IndexType, col_index: IndexType, *batch_indices: IndexType) -> LinearOperator: |
| 99 | + if _is_noop_index(row_index) and _is_noop_index(col_index): |
| 100 | + if len(batch_indices): |
| 101 | + return self.__class__(self.base[batch_indices], self.row_mask, self.col_mask) |
| 102 | + else: |
| 103 | + return self |
| 104 | + else: |
| 105 | + return super()._getitem(row_index, col_index, *batch_indices) |
| 106 | + |
| 107 | + def _get_indices(self, row_index: IndexType, col_index: IndexType, *batch_indices: IndexType) -> torch.Tensor: |
| 108 | + row_mapping = torch.arange(self.base.size(-2), device=self.base.device)[self.row_mask] |
| 109 | + col_mapping = torch.arange(self.base.size(-1), device=self.base.device)[self.col_mask] |
| 110 | + return self.base._get_indices(row_mapping[row_index], col_mapping[col_index], *batch_indices) |
| 111 | + |
| 112 | + def _permute_batch(self, *dims: int) -> LinearOperator: |
| 113 | + return self.__class__(self.base._permute_batch(*dims), self.row_mask, self.col_mask) |
0 commit comments