Repository: pytorch
Issue: Bug of `svd_lowrank` when passing parameter `M`
Labels: triaged,module: linear algebra

### 🐛 Describe the bug

The documentation of `svd_lowrank` suggests to pass the parameter `M` with shape: [*, 1, n] . However it will cause error like this:

```python
import torch

a = torch.tensor([[-0.05817481,  0.03267088,  0.02440962, -0.03175439],
        [ 0.32828987, -0.17132112, -0.13449055,  0.16437443],
        [ 0.05635244, -0.00889741, -0.01796546,  0.00491459],
        [ 0.20856347, -0.10837366, -0.08532560,  0.10389686],
        [-0.40811522,  0.18267809,  0.15962810, -0.16992001],
        [-0.04855471,  0.05635455,  0.02763447, -0.05954668],
        [ 0.07595293, -0.04992681, -0.03368455,  0.04971954],
        [ 0.10797926, -0.01546850, -0.03402987,  0.00762189],
        [ 0.01843280, -0.03143222, -0.01299692,  0.03400972],
        [-0.14565435,  0.05855532,  0.05531243, -0.05309846],
        [ 0.12740093, -0.08581684, -0.05701843,  0.08575096],
        [ 0.01459464, -0.04714232, -0.01584659,  0.05221082],
        [-0.08250186,  0.02841545,  0.03014396, -0.02467814],
        [ 0.16549724, -0.07670472, -0.06538728,  0.07188835],
        [ 0.38010505, -0.20409320, -0.15714863,  0.19682987],
        [-0.00852721, -0.03331695, -0.00593512,  0.03863540],
        [ 0.14721412, -0.08573240, -0.06253293,  0.08382912]])
torch.svd_lowrank(a, 2, M=a.mean(-2,keepdim=True))

# ---------------------------------------------------------------------------
# RuntimeError                              Traceback (most recent call last)
# Cell In[3], line 18
#       1 a = torch.tensor([[-0.05817481,  0.03267088,  0.02440962, -0.03175439],
#       2         [ 0.32828987, -0.17132112, -0.13449055,  0.16437443],
#       3         [ 0.05635244, -0.00889741, -0.01796546,  0.00491459],
#    (...)
#      16         [-0.00852721, -0.03331695, -0.00593512,  0.03863540],
#      17         [ 0.14721412, -0.08573240, -0.06253293,  0.08382912]])
# ---> 18 torch.svd_lowrank(a, 2, M=a.mean(-2,keepdim=True))
# 
# File my_path\lib\site-packages\torch\_lowrank.py:137, in svd_lowrank(A, q, niter, M)
#     131     if not set(map(type, tensor_ops)).issubset(
#     132         (torch.Tensor, type(None))
#     133     ) and has_torch_function(tensor_ops):
#     134         return handle_torch_function(
#     135             svd_lowrank, tensor_ops, A, q=q, niter=niter, M=M
#     136         )
# --> 137 return _svd_lowrank(A, q=q, niter=niter, M=M)
# 
# File my_path\lib\site-packages\torch\_lowrank.py:161, in _svd_lowrank(A, q, niter, M)
#     155 # Algorithm 5.1 in Halko et al 2009, slightly modified to reduce
#     156 # the number conjugate and transpose operations
#     157 if m < n or n > q:
#     158     # computing the SVD approximation of a transpose in
#     159     # order to keep B shape minimal (the m < n case) or the V
#     160     # shape small (the n > q case)
# --> 161     Q = get_approximate_basis(A_t, q, niter=niter, M=M_t)
#     162     Q_c = _utils.conjugate(Q)
#     163     if M is None:
# 
# File my_path\lib\site-packages\torch\_lowrank.py:76, in get_approximate_basis(A, q, niter, M)
#      74 else:
#      75     M_H = _utils.transjugate(M)
# ---> 76     Q = torch.linalg.qr(matmul(A, R) - matmul(M, R)).Q
#      77     for i in range(niter):
#      78         Q = torch.linalg.qr(matmul(A_H, Q) - matmul(M_H, Q)).Q
# 
# File my_path\lib\site-packages\torch\_linalg_utils.py:43, in matmul(A, B)
#      41 if is_sparse(A):
#      42     return torch.sparse.mm(A, B)
# ---> 43 return torch.matmul(A, B)
# 
# RuntimeError: mat1 and mat2 shapes cannot be multiplied (4x1 and 17x2)
```

The reason is that in the code `Q = torch.linalg.qr(matmul(A, R) - matmul(M, R)).Q` in `get_approximate_basis(A, q, niter, M)`, `M` does not have the same shape with `A`.

### Versions

PyTorch version: 1.13.1+cu116
Is debug build: False
CUDA used to build PyTorch: 11.6
ROCM used to build PyTorch: N/A

OS: Microsoft Windows 10 家庭中文版
GCC version: (x86_64-win32-seh-rev0, Built by MinGW-W64 project) 12.2.0
Clang version: 12.0.0
CMake version: version 3.26.4
Libc version: N/A

Python version: 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] (64-bit runtime)
Python platform: Windows-10-10.0.19045-SP0
Is CUDA available: True
CUDA runtime version: 11.6.55
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3060 Laptop GPU
Nvidia driver version: 527.99
cuDNN version: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\bin\cudnn_ops_train64_8.dll
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture=9
CurrentClockSpeed=3201
DeviceID=CPU0
Family=107
L2CacheSize=4096
L2CacheSpeed=
Manufacturer=AuthenticAMD
MaxClockSpeed=3201
Name=AMD Ryzen 7 5800H with Radeon Graphics
ProcessorType=3
Revision=20480

Versions of relevant libraries:
[pip3] numpy==1.23.1
[pip3] onnx==1.15.0
[pip3] paddle2onnx==1.0.6
[pip3] torch==1.13.1+cu116
[pip3] torchaudio==0.13.1+cu116
[pip3] torchlibrosa==0.1.0
[pip3] torchvision==0.14.1+cu116
[conda] Could not collect

cc @jianyuh @nikitaved @pearu @mruberry @walterddr @xwang233 @Lezcano