Can torch.sparse.mm support the multiplication of two bool vectors

I want to multiply two bool sparse matrices. If the two matrices become dense, it will take up a lot of space. How to multiply two bool sparse matrices?

Hello,
torch.sparse.mm currently does not support the multiplication of boolean matrices and will fail with

RuntimeError: "sparse_matmul" not implemented for 'Bool'

Therefore, operation such as:

import torch

a = torch.tensor([[1., 0, 1], [0, 1, 0]], requires_grad=False, dtype=bool).to_sparse()
b = torch.tensor([[0, 1.], [1, 0], [0, 0]], requires_grad=False, dtype=bool).to_sparse()
y = torch.sparse.mm(a, b)

will fail.

However, you could work around this by casting your bool tensor to a different type, multiplying the tensors, and casting back to bool. For example:

import torch

a = torch.tensor([[1., 0, 1], [0, 1, 0]], requires_grad=False, dtype=bool).to_sparse()
b = torch.tensor([[0, 1.], [1, 0], [0, 0]], requires_grad=False, dtype=bool).to_sparse()
y = torch.sparse.mm(a.type(torch.FloatTensor), b.type(torch.FloatTensor)).type(torch.BoolTensor)

Of course, this requires more memory than if the operation was supported for boolean matrices. It could, however, solve your issue if your matrices are sufficiently sparse.