Repository: pytorch
Issue: No dtype check for zero sparse tensor!
Labels: module: sparse,triaged,Stale

## 🐛 Bug

Similar with [issue](https://github.com/pytorch/pytorch/issues/69518), I find that `torch.sparse.mm`, `torch.mm`, `torch.mv`, `torch.matmul`, `torch.addmm`, `torch.bmm` and `torch.sparse.addmm` should check the dtype of zero sparse tensor, but they don't.

This behavior is needed, because for normal tensor, they will do the dtype check no matter whether the tensor is zero. Besides, after you change the value of zero sparse tensor, you will fail when you call the last API again, which is so confusing.
```python
import torch
zero_tensor = torch.tensor([[0, 0], [0, 0]], dtype=torch.int64)
sparse_zero_tensor = zero_tensor.to_sparse()
mat1 = torch.tensor([[1, 2], [3, 4]], dtype=torch.int32)
torch.mm(sparse_zero_tensor, mat1)
# succeed
sparse_zero_tensor = sparse_zero_tensor + torch.tensor([[0, 1], [0, 0]], dtype=torch.int64).to_sparse()
torch.mm(sparse_zero_tensor, mat1)
# fail
```

Interestingly, `torch.smm` and `torch.sspaddmm` do check the dtype of zero sparse tensor.

## To Reproduce

For `torch.sparse.mm`, `torch.mm`, `torch.matmul`:
```python
import torch

zero_tensor = torch.tensor([[0, 0], [0, 0]], dtype=torch.int64)
sparse_zero_tensor = zero_tensor.to_sparse()
mat1 = torch.tensor([[1, 2], [3, 4]], dtype=torch.int32)

torch.mm(sparse_zero_tensor, mat1)
# succeed
torch.mm(zero_tensor, mat1)
# RuntimeError: expected scalar type Long but found Int
```

For `torch.mv`:
```python
import torch

zero_tensor = torch.tensor([[0, 0], [0, 0]], dtype=torch.int64)
sparse_zero_tensor = zero_tensor.to_sparse()

mat1 = torch.tensor([1, 2], dtype=torch.int32)
torch.mv(sparse_zero_tensor, mat1)
# succeed
torch.mv(zero_tensor, mat1)
# RuntimeError: expected scalar type Long but found Int
```

For `torch.addmm`, `torch.sparse.addmm`, 
```python
import torch

zero_tensor = torch.tensor([[0, 0], [0, 0]], dtype=torch.int64)
sparse_zero_tensor = zero_tensor.to_sparse()

mat1 = torch.tensor([[1, 2], [3, 4]], dtype=torch.int32)
torch.addmm(mat1, sparse_zero_tensor, mat1)
# succeed
torch.addmm(mat1, zero_tensor, mat1)
# RuntimeError: expected scalar type Int but found Long
```

For `torch.bmm`
```python
zero_tensor = torch.randint(0, 1, [1,1,1], dtype=torch.int64)
sparse_zero_tensor = zero_tensor.to_sparse()
mat1 = torch.randint(0, 10, [1, 1, 1], dtype=torch.int32)
mat2 = torch.randint(0, 10, [1, 1, 1], dtype=torch.int32)
torch.bmm(sparse_zero_tensor, mat1)
# succeed
torch.bmm(zero_tensor, mat1)
# RuntimeError: expected scalar type Long but found Int
```

Interestingly, for `torch.smm` and `torch.sspaddmm`
```python
zero_tensor = torch.tensor([[0, 0], [0, 0]], dtype=torch.int64)
sparse_zero_tensor = zero_tensor.to_sparse()

mat1 = torch.tensor([[1, 2], [3, 4]], dtype=torch.int32)

torch.smm(sparse_zero_tensor, mat1)
# RuntimeError: expected scalar type Long but found Int
torch.sspaddmm(sparse_zero_tensor, sparse_zero_tensor, mat1)
# RuntimeError: expected scalar type Long but found Int
```

## Expected behavior

`torch.sparse.mm`, `torch.mm`, `torch.mv`, `torch.matmul`, `torch.addmm`, `torch.bmm` and `torch.sparse.addmm` should check the dtype of zero sparse tensor.

## Environment

 - PyTorch Version (e.g., 1.0): 1.10.0
 - OS (e.g., Linux): MacOS
 - How you installed PyTorch (`conda`, `pip`, source): pip
 - Build command you used (if compiling from source): none
 - Python version: 3.9.6
 - CUDA/cuDNN version: none
 - GPU models and configuration: none
 - Any other relevant information: none



cc @nikitaved @pearu @cpuhrsch @IvanYashchuk