Repository: pytorch
Issue: torch.bmm backward with sparse input
Labels: module: sparse,triaged

### 🐛 Describe the bug

Based on the torch.sparse documentation it sounds like `torch.bmm` supports forward and backward for sparse tensors (but the gradients themselves will be dense). During the backward pass I'm getting the error shown below. Is this expected (in which case this is a feature request) or is this a bug?

```python
import torch
a = torch.rand(2, 3, 3).to_sparse().requires_grad_(True)
b = torch.rand(2, 3, 3)
c = torch.bmm(a, b)
loss = c.sum()
loss.backward()
```

gives

```text
Traceback (most recent call last):
  File "/home/test_bmm.py", line 7, in <module>
    loss.backward()
  File "/home/cmccarth/.conda/envs/test/lib/python3.8/site-packages/torch/_tensor.py", line 255, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
  File "/home/cmccarth/.conda/envs/test/lib/python3.8/site-packages/torch/autograd/__init__.py", line 147, in backward
    Variable._execution_engine.run_backward(
RuntimeError: Function BmmBackward0 returned an invalid gradient at index 0 - expected type TensorOptions(dtype=float, device=cpu, layout=Sparse, requires_grad=false (default), pinned_memory=false (default), memory_format=(nullopt)) but got TensorOptions(dtype=float, device=cpu, layout=Strided, requires_grad=false (default), pinned_memory=false (default), memory_format=(nullopt))
```

Thank you for your help!

### Versions

```text
Collecting environment information...
PyTorch version: 1.9.1+cu111
Is debug build: False
CUDA used to build PyTorch: 11.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.2 LTS (x86_64)
GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Clang version: 10.0.0-4ubuntu1 
CMake version: version 3.16.3
Libc version: glibc-2.31

Python version: 3.8.12 | packaged by conda-forge | (default, Oct 12 2021, 21:57:06)  [GCC 9.4.0] (64-bit runtime)
Python platform: Linux-5.11.0-46-generic-x86_64-with-glibc2.10
Is CUDA available: True
CUDA runtime version: 11.3.58
GPU models and configuration: 
GPU 0: NVIDIA GeForce GTX TITAN X
GPU 1: NVIDIA GeForce GTX TITAN X

Nvidia driver version: 495.46
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.1.1
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.1.1
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.1.1
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.1.1
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.1.1
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.1.1
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.1.1
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn.so.8
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_adv_train.so.8
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_ops_train.so.8
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn.so.8
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_adv_train.so.8
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_ops_train.so.8
/usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn.so.8
/usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8
/usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_adv_train.so.8
/usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8
/usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8
/usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8
/usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_ops_train.so.8
/usr/local/cuda-11.5/targets/x86_64-linux/lib/libcudnn.so.8
/usr/local/cuda-11.5/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8
/usr/local/cuda-11.5/targets/x86_64-linux/lib/libcudnn_adv_train.so.8
/usr/local/cuda-11.5/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8
/usr/local/cuda-11.5/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8
/usr/local/cuda-11.5/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8
/usr/local/cuda-11.5/targets/x86_64-linux/lib/libcudnn_ops_train.so.8
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] mypy==0.931
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.22.1
[pip3] torch==1.9.1+cu111
[pip3] torchmetrics==0.5.0
[pip3] torchvision==0.10.1+cu111
[conda] cudatoolkit               11.1.74              h6bb024c_0    nvidia
[conda] mypy                      0.931                    pypi_0    pypi
[conda] numpy                     1.22.1                   pypi_0    pypi
[conda] torch                     1.9.1+cu111              pypi_0    pypi
[conda] torchvision               0.10.1+cu111             pypi_0    pypi
```

cc @nikitaved @pearu @cpuhrsch