Implementing structured matrices for use with autograd

I am wondering about how to best implement certain types of structured matrices in Pytorch. By “structured matrix,” I mean matrices that can behave as a FloatTensor (for example) in every way, but certain operations can be performed more efficiently. For example, matrix-vector multiplications and (approximate) linear solves with Toeplitz matrices are very fast. Other examples might include triangular matrices (to represent LU or Cholesky factorizations), or matrices that are the Kronecker product of smaller matrices.

I would additionally like to use these structured matrices in autograd: ideally it would be possible for the backward computation of some function to return a structured matrix, so that when earlier nodes in the graph use this structured matrix in their computations, the operations are efficient.

My first thought about how to accomplish this was to create, for example, ToeplitzFloatTensor, that extends FloatTensor and rewrites a few of the methods to handle the added structure efficiently.

However, Pytorch does not seem to easily allow extending the base Tensor classes. Or, at least, instances of classes extending FloatTensor cannot be used in a Variable without receiving an error:

RuntimeError: Variable data has to be a tensor…

Furthermore, adding such a class to torch._tensor_classes simply results in an error later down the line.

Is extending the base Tensor classes at all possible in Pytorch, or is there some other preferred pattern for making Tensors that represent structured matrices?


the short answer is that this is not really a simple effort, especially with autograd’s dependence on the base classes for type checks etc. We did do this with Sparse Tensors, but it wasn’t a trivial effort and we had to introduce C side base classes for sparse.

I’ll think about it and consult with the others and can get back to you.