[sparse] Shape mismatch when doing matmul with semi-strutctured sparse and non-contiguous dense input
Labels: module: sparse, triaged

### 🐛 Describe the bug

When doing semi_structured sparse @ dense matmul, we sometimes see a shape error because PyTorch calculates the shape of the returned matrix wrong. It appears to be off by a factor of 4. 

This only happens when we run on non-contiguous inputs, which happens when we have a MLP layer and we sparsify both layers. 

```
======================================================================
ERROR: test_mlp_backend_cutlass_cuda (__main__.TestSparseSemiStructuredCUDA)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/jessecai/local/miniconda3/envs/pt-tutorial/lib/python3.10/site-packages/torch/testing/_internal/common_utils.py", line 2388, in wrapper
    method(*args, **kwargs)
  File "/home/jessecai/local/miniconda3/envs/pt-tutorial/lib/python3.10/site-packages/torch/testing/_internal/common_utils.py", line 2388, in wrapper
    method(*args, **kwargs)
  File "/home/jessecai/local/miniconda3/envs/pt-tutorial/lib/python3.10/site-packages/torch/testing/_internal/common_device_type.py", line 428, in instantiated_test
    raise rte
  File "/home/jessecai/local/miniconda3/envs/pt-tutorial/lib/python3.10/site-packages/torch/testing/_internal/common_device_type.py", line 415, in instantiated_test
    result = test(self, **param_kwargs)
  File "/home/jessecai/local/AA/pytorch/test/test_sparse_semi_structured.py", line 306, in test_mlp
    sparse_result = model(input)
  File "/home/jessecai/local/miniconda3/envs/pt-tutorial/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/jessecai/local/miniconda3/envs/pt-tutorial/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/jessecai/local/miniconda3/envs/pt-tutorial/lib/python3.10/site-packages/torch/nn/modules/container.py", line 215, in forward
    input = module(input)
  File "/home/jessecai/local/miniconda3/envs/pt-tutorial/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/jessecai/local/miniconda3/envs/pt-tutorial/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/jessecai/local/miniconda3/envs/pt-tutorial/lib/python3.10/site-packages/torch/nn/modules/linear.py", line 114, in forward    return F.linear(input, self.weight, self.bias)
RuntimeError: shape '[64, 768, 3072]' is invalid for input of size 37748736

To execute this test, run the following from the base repo dir:
     python test/test_sparse_semi_structured.py -k test_mlp_backend_cutlass_cuda
```

Code to reproduce:
```
SparseSemiStructuredTensor._FORCE_CUTLASS = backend == "cutlass"
input = torch.rand(64, 768, 768, device=device).half()
model = (
    nn.Sequential(
        nn.Linear(768, 3072),
        nn.Linear(3072, 768),
    )
    .half()
    .to(device)
)

for i in range(2):
    m, n = model[i].weight.shape
    mask = rand_sparse_semi_structured_mask(
        m, n, device=device, dtype=torch.bool
    )
    # set masked weight
    model[i].weight = nn.Parameter(model[i].weight * mask)

dense_result = model(input)

for i in range(2):
    model[i].weight = nn.Parameter(to_sparse_semi_structured(model[i].weight))

sparse_result = model(input)

assert torch.allclose(dense_result, sparse_result, rtol=1e-3, atol=1e-3)
```

### Versions

Collecting environment information...
PyTorch version: 2.2.0a0+git50054b1
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: CentOS Stream 9 (x86_64)
GCC version: (GCC) 11.4.1 20230605 (Red Hat 11.4.1-2)
Clang version: 16.0.6 (Red Hat 16.0.6-1.el9)
CMake version: version 3.26.4
Libc version: glibc-2.34

Python version: 3.10.12 (main, Jul  5 2023, 18:54:27) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.12.0-0_fbk16_zion_7661_geb00762ce6d2-x86_64-with-glibc2.34
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA PG509-210
GPU 1: NVIDIA PG509-210
GPU 2: NVIDIA PG509-210
GPU 3: NVIDIA PG509-210
GPU 4: NVIDIA PG509-210
GPU 5: NVIDIA PG509-210
GPU 6: NVIDIA PG509-210
GPU 7: NVIDIA PG509-210

Nvidia driver version: 525.105.17
cuDNN version: Probably one of the following:
/usr/lib64/libcudnn.so.8.8.1
/usr/lib64/libcudnn_adv_infer.so.8.8.1
/usr/lib64/libcudnn_adv_train.so.8.8.1
/usr/lib64/libcudnn_cnn_infer.so.8.8.1
/usr/lib64/libcudnn_cnn_train.so.8.8.1
/usr/lib64/libcudnn_ops_infer.so.8.8.1
/usr/lib64/libcudnn_ops_train.so.8.8.1
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn.so.8.0.5
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.0.5
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.0.5
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.0.5
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.0.5
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.0.5
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.0.5
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: False

CPU:
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Address sizes:                   46 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          192
On-line CPU(s) list:             0-191
Vendor ID:                       GenuineIntel
Model name:                      Intel(R) Xeon(R) Platinum 8339HC CPU @ 1.80GHz
CPU family:                      6
Model:                           85
Thread(s) per core:              2
Core(s) per socket:              24
Socket(s):                       4
Stepping:                        11
Frequency boost:                 enabled
CPU max MHz:                     1801.0000
CPU min MHz:                     800.0000
BogoMIPS:                        3600.00
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local avx512_bf16 dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req pku ospke avx512_vnni md_clear flush_l1d arch_capabilities
Virtualization:                  VT-x
L1d cache:                       3 MiB (96 instances)
L1i cache:                       3 MiB (96 instances)
L2 cache:                        96 MiB (96 instances)
L3 cache:                        132 MiB (4 instances)
NUMA node(s):                    4
NUMA node0 CPU(s):               0-23,96-119
NUMA node1 CPU(s):               24-47,120-143
NUMA node2 CPU(s):               48-71,144-167
NUMA node3 CPU(s):               72-95,168-191
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Enhanced IBRS, IBPB conditional, RSB filling
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected

Versions of relevant libraries:
[pip3] botorch==0.9.2
[pip3] flake8==6.0.0
[pip3] flake8-bugbear==23.3.23
[pip3] flake8-comprehensions==3.12.0
[pip3] flake8-executable==2.1.3
[pip3] flake8-logging-format==0.9.0
[pip3] flake8-pyi==23.3.1
[pip3] flake8-simplify==0.19.3
[pip3] gpytorch==1.11
[pip3] mypy==1.4.1
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.24.1
[pip3] pytorch-lightning==2.0.9
[pip3] pytorch-sphinx-theme==0.0.24
[pip3] pytorch-triton==2.1.0+6e4932cda8
[pip3] torch==2.1.0+cu121
[pip3] torchaudio==2.1.0+cu121
[pip3] torchdata==0.6.1
[pip3] torchmetrics==1.1.2
[pip3] torchmultimodal-nightly==2023.8.31
[pip3] torchrl==0.1.1
[pip3] torchtext==0.15.2
[pip3] torchvision==0.16.0+cu121
[pip3] torchx==0.5.0
[pip3] triton==2.1.0
[pip3] triton-nightly==2.1.0.dev20230822000928
[conda] blas                      1.0                         mkl  
[conda] botorch                   0.9.2                    pypi_0    pypi
[conda] cpuonly                   2.0                           0    pytorch-test
[conda] gpytorch                  1.11                     pypi_0    pypi
[conda] magma-cuda116             2.6.1                         1    pytorch
[conda] mkl                       2023.1.0         h213fc3f_46343  
[conda] mkl-include               2023.1.0         h06a4308_46343  
[conda] mkl-service               2.4.0           py310h5eee18b_1  
[conda] mkl_fft                   1.3.6           py310h1128e8f_1  
[conda] mkl_random                1.2.2           py310h1128e8f_1  
[conda] numpy                     1.24.1                   pypi_0    pypi
[conda] pytorch-cuda              12.1                 ha16c6d3_5    pytorch-test
[conda] pytorch-lightning         2.0.9                    pypi_0    pypi
[conda] pytorch-mutex             1.0                         cpu    pytorch-test
[conda] pytorch-sphinx-theme      0.0.24                    dev_0    <develop>
[conda] pytorch-triton            2.1.0+6e4932cda8          pypi_0    pypi
[conda] torch                     2.1.0+cu121              pypi_0    pypi
[conda] torchaudio                2.1.0+cu121              pypi_0    pypi
[conda] torchdata                 0.6.1                    pypi_0    pypi
[conda] torchmetrics              1.1.2                    pypi_0    pypi
[conda] torchmultimodal-nightly   2023.8.31                pypi_0    pypi
[conda] torchrl                   0.1.1                    pypi_0    pypi
[conda] torchtext                 0.15.2                   pypi_0    pypi
[conda] torchvision               0.16.0+cu121             pypi_0    pypi
[conda] torchx                    0.5.0                    pypi_0    pypi
[conda] triton                    2.1.0                    pypi_0    pypi
[conda] triton-nightly            2.1.0.dev20230822000928          pypi_0    pypi


cc @alexsamardzic @nikitaved @pearu @cpuhrsch @amjames @bhosmer