linear-operator class calls nonexistent PyTorch method

3 hours ago 2
ARTICLE AD BOX

I'm using the linear-operator Python library. I create a BlockDiagLinearOperator, whose base_linear_op is a (10000, 2, 2) tensor. Calling its _diagonal() method calls self.base_linear_op._diagonal(), but PyTorch Tensors don't have such a method. It seems _diagonal() assumes base_linear_op is itself a LinearOperator. I believe this is an oversight in implementation, or I'm misunderstanding. Any clarification is appreciated!

Reproducable Snippet:

C = torch.randn(10000, 2, 2) op = linear_operators.operators.BlockDiagLinearOperator(C) op._diagonal()

Stack Trace:

AttributeError Traceback (most recent call last) Cell In[51], line 1 ----> 3 op._diagonal() File ~/venv/lib64/python3.13/site-packages/linear_operator/operators/_linear_operator.py:1419, in LinearOperator.diagonal(self, offset, dim1, dim2) 1417 elif not self.is_square: 1418 raise RuntimeError("LinearOperator#diagonal is only implemented for square operators.") -> 1419 return self._diagonal() File ~/venv/lib64/python3.13/site-packages/linear_operator/operators/block_diag_linear_operator.py:94, in BlockDiagLinearOperator._diagonal(self) 93 def _diagonal(self: Float[LinearOperator, "... M N"]) -> Float[torch.Tensor, "... N"]: ---> 94 res = self.base_linear_op._diagonal().contiguous() 95 return res.view(*self.batch_shape, self.size(-1)) AttributeError: 'Tensor' object has no attribute '_diagonal'
Read Entire Article