torch.lobpcg producing different largest eigenvalue than scipy and np.linalg.eig
Labels: triaged, module: numpy, module: linear algebra

### 🐛 Describe the bug

I have tried to use [lobpcg](https://pytorch.org/docs/stable/generated/torch.lobpcg.html) to obtain the largest eigenvalue of a of a symmetric [generalized eigenvalue problem](https://en.wikipedia.org/wiki/Generalized_eigenvalue_problem) defined by the pair of matrices (A,B). In most of the cases, it obtains the same value as the one obtained using [scipy.sparse.linalg.lobpcg](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.linalg.lobpcg.html) or applying [np.linalg.eig]() to B^-1A

However, there are instances where `lobpcg` fails to generate the same answer as `scipy` or `eig`. The code to reproduce this error is this one: 


```python
import numpy as np
import torch
import scipy

dim=3

while True:
    print("\n\n-------")

    #Random generation of matrices A and B
    tmp = np.random.uniform(-1, 1, (dim, dim))
    A=(tmp+tmp.T)/2.0  #A is symmetric by construction

    tmp = np.random.uniform(-1, 1, (dim, dim))
    B = np.dot(tmp, tmp.transpose()) + np.eye(dim) #B is symmetric positive definite by construction

    print(f"A={A}\n")
    print(f"B={B}\n")

    X=np.random.rand(A.shape[0], 1) #Initial guess for the eigenvector

    # Using lobpcg
    lambda_lobpcg, _ = torch.lobpcg(A=torch.from_numpy(A), k=1, B=torch.from_numpy(B), niter=-1, X=torch.from_numpy(X))
    lambda_lobpcg = lambda_lobpcg.item()
    print(f"lambda_lobpcg={lambda_lobpcg}")

    # Using Scipy
    lambda_scipy, _ =scipy.sparse.linalg.lobpcg(A=A, B=B, X=X, maxiter=10000)
    lambda_scipy=lambda_scipy[0]
    print(f"lambda_scipy={lambda_scipy}")

    # Using normal eigendecomposition
    all_lambdas, _=np.linalg.eig(np.linalg.inv(B)@A); 
    # print(f"all_lambdas={all_lambdas}")
    lambda_eig=np.max(all_lambdas)
    print(f"lambda_eig={lambda_eig}")

    assert abs(lambda_lobpcg-lambda_eig)<1e-6
    assert abs(lambda_scipy-lambda_eig)<1e-6
```


When I run this code, after ~2 seconds it finds a pair of matrices (A,B) for which the result of `lobpcg` is different than the other two methods

```
A=[[-0.90404846  0.33497974 -0.47530819]
 [ 0.33497974 -0.70714823 -0.34896375]
 [-0.47530819 -0.34896375 -0.25162643]]

B=[[ 1.51871581e+00 -8.51033541e-05 -1.49293047e-02]
 [-8.51033541e-05  1.16174545e+00 -1.42144256e-01]
 [-1.49293047e-02 -1.42144256e-01  2.73072664e+00]]

lambda_lobpcg=-0.560304480425443
lambda_scipy=0.10388536097610737
lambda_eig=0.10388536097610745

---
test_bug_logpcg.py 39 <module>
assert abs(lambda_lobpcg-lambda_eig)<1e-6

AssertionError
```


### Versions

Collecting environment information...
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: Ubuntu 20.04.5 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: Could not collect
CMake version: version 3.16.3
Libc version: glibc-2.31

Python version: 3.8.10 (default, Mar 13 2023, 10:26:41)  [GCC 9.4.0] (64-bit runtime)
Python platform: Linux-5.15.0-71-generic-x86_64-with-glibc2.29
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 4080
Nvidia driver version: 525.105.17
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   46 bits physical, 48 bits virtual
CPU(s):                          24
On-line CPU(s) list:             0-23
Thread(s) per core:              1
Core(s) per socket:              16
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           151
Model name:                      12th Gen Intel(R) Core(TM) i9-12900
Stepping:                        2
CPU MHz:                         2400.000
CPU max MHz:                     5100.0000
CPU min MHz:                     800.0000
BogoMIPS:                        4838.40
Virtualization:                  VT-x
L1d cache:                       384 KiB
L1i cache:                       256 KiB
L2 cache:                        10 MiB
NUMA node0 CPU(s):               0-23
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Not affected
Vulnerability Retbleed:          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, PBRSB-eIBRS SW sequence
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected
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 tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req umip pku ospke waitpkg gfni vaes vpclmulqdq tme rdpid movdiri movdir64b fsrm md_clear serialize pconfig arch_lbr flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] numpy==1.24.2
[pip3] torch==1.13.1+cu116
[pip3] torchaudio==0.13.1+cu116
[pip3] torchvision==0.14.1+cu116
[conda] Could not collect


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