Repository navigation
Fix MSVC versions of complex power and dot helpers in f2c-translated LAPACK - #6286
Conversation
…LAPACK The _MSC_VER branches of the helpers in the C-translated LAPACK sources did not implement complex arithmetic correctly: - zpow_ui (and cpow_ui outside clahqr/chgeqz/claesy) multiplied real and imaginary parts element-wise, and inverted them element-wise for negative exponents. ZLAHQR uses pow_zi for its shift, so for real input with complex-conjugate eigenvalues the shift degenerates, ZLADIV divides by zero and ZLAHQR/ZHSEQR/ZGEEV return INFO > 0 with NaN eigenvalues. ZHGEQZ (ZGGEV/ZGGES) is affected the same way. - zdotu_/cdotu_ used xi*yi instead of xi*yr in the imaginary part, giving wrong results (with INFO = 0) from xTRSYL, xSYTRI, xSPTRI and others in both precisions. - the strided branch of cdotc_ had the same yi/yr mixup (e.g. CLAPLL with INCX > 1). Replace all copies of these MSVC helpers with one canonical version that uses _Cmulcc/_FCmulcc and _Cbuild/_FCbuild, including the unused copies so they cannot be picked up broken later. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…w_ui For negative exponents, compute 1/x with Smith's scaled division so that inputs such as 1e200 or 1e-200 give the representable result instead of 0 or NaN, and take the exponent magnitude in an unsigned long long without evaluating -n, so the most negative integer and ILP64 exponents are handled. All current callers use n = 2, so this does not change results there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks - I had begun another round of cleanup with #6061 but ran out of time after that, so lets merge this and remove the unused/unreachable code where most of your changes ended up later. (There is no reason for something like the timing function DSECND to ever want to do complex arithmetic, but way back when I did the initial mass translation, it somehow made sense to my former self to copy the definitions into each individual file instead of adding a dependency on a single f2c-like header) |
|
@martin-frbg Thanks for the quick review, if you would like I don't mind opening a clean-up PR as well |
|
If there's an easy way to Claude this ? For instance, I believe only C/ZHGEQZ, C/ZLAESY and C/ZLAHQR actually call their respective "pow" function, only ?LATBS, ?LATPS, ?LATRS, ?SPTRI, ?SYTRI, ?SYTRI_ROOK and ?TRSYL use the respective DOTU. (DOTC seems to have 24 actual callers each. TESTING/MATGEN only has ?LAGHE and ?LAGSY calling DOTC, but cleaning up all this by hand is more time consuming than I'd like it to be. |
|
Should be relatively easy, Claude already identified the dead code when I did the pass for this PR. Let me take a look |
Roll host-blas forward from 18638c7 (v0.3.31+106) to OpenBLAS develop 93a2969 (1,448 commits). It includes the upstream fix for the MSVC complex helpers in the C-translated LAPACK (OpenMathLib/OpenBLAS#6286, plus the follow-up cleanup in #6290), so the 0002 patch is no longer needed. Patch 0001 still applies (6-line offset). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
## Motivation JIRA ID : ROCM-34127 On Windows, complex128 `torch.linalg.eig`/`eigvals` fail on CPU with `LinAlgError: ... (error code: 2)`. The cause is a bug in OpenBLAS's C-translated LAPACK, which our host-blas build uses (`rocm-openblas.dll`). It is now fixed upstream, so this bumps host-blas to pick up the fix. ## Technical Details - Bumps OpenBLAS from `18638c7` (v0.3.31+106) to `develop` at `93a2969` (1,448 commits). That includes the fix (OpenMathLib/OpenBLAS#6286) and a follow-up cleanup (OpenMathLib/OpenBLAS#6290). - Patch `0001` is kept; it still applies. - An earlier revision of this PR carried the fix as a host-blas patch; it is no longer needed. The new source archive still needs to be uploaded to `rocm-third-party-deps` as `OpenBLAS-93a2969.tar.gz`. Until then, CI cannot fetch the sources. ## Test Plan - Built the new OpenBLAS with TheRock's host-blas CMake on Linux, LP64 and ILP64. - Checked the affected LAPACK routines directly against numpy/scipy. - The fix was validated on Windows with the earlier patch revision, with the patched DLL swapped into `torch 2.15.0+rocm10.3.0a20261105`. ## Test Result | | Before | After | |---|---|---| | LAPACK routine checks | 34/56 (Windows) | 56/56 (Windows with the fix as a patch; Linux with the new pin) | | `test/torch_np/numpy_tests/linalg/test_linalg.py` (Windows) | 2 failed | all pass | | Linux host-blas build, LP64 and ILP64 | — | builds and installs | The Windows build of the new pin will run in CI once the archive is uploaded. ## Submission Checklist - [x] Look over the contributing guidelines at https://github.com/ROCm/TheRock/blob/main/CONTRIBUTING.md. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Follow-up to #5574 (from #5562), which fixed the MSVC
z_div/c_divmacros used by the C-translated LAPACK. Other hand-written_MSC_VERcomplex helpers in the same per-file headers are still wrong, and some of them are on hot paths:zpow_uimultiplies real and imaginary parts element-wise instead of doing complex multiplication.ZLAHQRusespow_zifor its shift, so for real-valued input with complex-conjugate eigenvalues the shift degenerates andZLADIVdivides by zero.ZLAHQR/ZHSEQR/ZGEEVthen returnINFO > 0; for[[1, 0.5], [-1, 1]],ZGEEVreturnsINFO = 2with NaN eigenvalues.ZHGEQZuses it the same way, soZGGEV/ZGGESare affected. Mostcpow_uicopies have the same element-wise multiply; the three that are used (clahqr.c,chgeqz.c,claesy.c) multiply correctly but still invert element-wise forn < 0.zdotu_/cdotu_usexi*yiinstead ofxi*yrin the imaginary part, in both stride branches. Their callers includexTRSYL,xSYTRI,xSYTRI_ROOK,xSPTRIandxLATRS/xLATBS/xLATPS;xTRSYLandxSYTRIwere confirmed to return wrong results withINFO = 0.cdotc_has the sameyi/yrmixup in its strided branch (e.g.CLAPLLwithINCX > 1).The variants differ from file to file (some copies of
zdotc_were already correct), so this replaces the_MSC_VERbranch of all six helpers in every file (493 files, used and unused copies) with one canonical version built on_Cmulcc/_FCmulccand_Cbuild/_FCbuild, asz_divalready does. For negative exponents, the pow helpers use a scaled reciprocal and take the exponent magnitude asunsigned long long. No current caller uses a negative exponent, but the unscaled form would overflow or underflow at large or small magnitudes. The non-MSVC branches and everything outside these helper bodies are byte-for-byte unchanged. The files were rewritten by a small script, so a different formatting is easy to regenerate if preferred.Testing
Windows testing was done with clang-cl 19 / Windows SDK 26100,
-DC_LAPACK=ON -DTARGET=HASWELL -DDYNAMIC_ARCH=OFF(same settings as #5562), on the OpenBLAS commit used by AMD's TheRock (18638c7), with an equivalent patch to the 59 files that actually call these helpers. I have not built this exact branch on Windows. The routines were called directly through ctypes and checked against residuals or numpy/scipy references:zlahqron real matrices with complex eigenvalues (n=2..40)INFO=2..38zgeev,[[1, 0.5], [-1, 1]]INFO=2zggev, real pair with complex eigenvaluesINFO=2/6ztrsyl/ctrsyl(N/C combinations)zsytri/csytri(U/L)‖A·A⁻¹−I‖1-6clapllwithINCX=INCY=2PyTorch's
test/torch_np/numpy_tests/linalg/test_linalg.py(TestEig/TestEigvalscomplex128) andtest_eig_numpy/test_eigvals_numpycomplex128 intest/test_linalg.pygo from failing to passing. No other tests in the CPU subset changed.For this branch, a standalone gcc build of the new helper bodies (with emulated
_Fcomplex/_Dcomplex) checks pow for n = -3..5, reciprocals of 1e±200 / 1e±30 (where the unscaled form returns 0 or NaN), the most negative exponent, anddotc/dotuwith stride 1 and 2 in both precisions.I have not run the
lapack-netlibTESTING drivers on Windows. As far as I can tell, no CI job builds the translated LAPACK (C_LAPACK=ON) with MSVC or clang-cl, which is likely how this went unnoticed.Separately (not in this PR): some files still carry an older form of the
c_div/z_divmacros (e.g.INSTALL/dsecnd_NONE.c), so #5574 did not reach every copy.🤖 Generated with Claude Code