Skip to content

Fix MSVC versions of complex power and dot helpers in f2c-translated LAPACK - #6286

Merged
martin-frbg merged 2 commits into
OpenMathLib:developfrom
sstamenk:fix-msvc-complex-pow-dot
Nov 27, 2026
Merged

martin-frbg merged 2 commits into
OpenMathLib:developfrom
sstamenk:fix-msvc-complex-pow-dot

Conversation

@sstamenk

@sstamenk sstamenk commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #5574 (from #5562), which fixed the MSVC z_div/c_div macros used by the C-translated LAPACK. Other hand-written _MSC_VER complex helpers in the same per-file headers are still wrong, and some of them are on hot paths:

  • zpow_ui multiplies real and imaginary parts element-wise instead of doing complex multiplication. ZLAHQR uses pow_zi for its shift, so for real-valued input with complex-conjugate eigenvalues the shift degenerates and ZLADIV divides by zero. ZLAHQR/ZHSEQR/ZGEEV then return INFO > 0; for [[1, 0.5], [-1, 1]], ZGEEV returns INFO = 2 with NaN eigenvalues. ZHGEQZ uses it the same way, so ZGGEV/ZGGES are affected. Most cpow_ui copies have the same element-wise multiply; the three that are used (clahqr.c, chgeqz.c, claesy.c) multiply correctly but still invert element-wise for n < 0.
  • zdotu_/cdotu_ use xi*yi instead of xi*yr in the imaginary part, in both stride branches. Their callers include xTRSYL, xSYTRI, xSYTRI_ROOK, xSPTRI and xLATRS/xLATBS/xLATPS; xTRSYL and xSYTRI were confirmed to return wrong results with INFO = 0.
  • cdotc_ has the same yi/yr mixup in its strided branch (e.g. CLAPLL with INCX > 1).

The variants differ from file to file (some copies of zdotc_ were already correct), so this replaces the _MSC_VER branch of all six helpers in every file (493 files, used and unused copies) with one canonical version built on _Cmulcc/_FCmulcc and _Cbuild/_FCbuild, as z_div already does. For negative exponents, the pow helpers use a scaled reciprocal and take the exponent magnitude as unsigned 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:

before after
zlahqr on real matrices with complex eigenvalues (n=2..40) INFO=2..38 pass
zgeev, [[1, 0.5], [-1, 1]] INFO=2 pass
zggev, real pair with complex eigenvalues INFO=2 / 6 pass
ztrsyl/ctrsyl (N/C combinations) relative residual 0.17-0.71 ~1e-16 / ~1e-7
zsytri/csytri (U/L) ‖A·A⁻¹−I‖ 1-6 pass
clapll with INCX=INCY=2 16% error pass
total, 56 checks over both precisions 34/56 56/56

PyTorch's test/torch_np/numpy_tests/linalg/test_linalg.py (TestEig/TestEigvals complex128) and test_eig_numpy/test_eigvals_numpy complex128 in test/test_linalg.py go 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, and dotc/dotu with stride 1 and 2 in both precisions.

I have not run the lapack-netlib TESTING 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_div macros (e.g. INSTALL/dsecnd_NONE.c), so #5574 did not reach every copy.

🤖 Generated with Claude Code

sstamenk and others added 2 commits November 26, 2026 08:17
…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>
@sstamenk
sstamenk marked this pull request as ready for review November 26, 2026 08:41
@martin-frbg martin-frbg modified the milestone: 0.3.35 Nov 27, 2026
@martin-frbg

Copy link
Copy Markdown
Collaborator

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 martin-frbg added this to the 0.3.35 milestone Nov 27, 2026
@martin-frbg
martin-frbg merged commit a7256cc into OpenMathLib:develop Nov 27, 2026
96 of 110 checks passed
@sstamenk

Copy link
Copy Markdown
Contributor Author

@martin-frbg Thanks for the quick review, if you would like I don't mind opening a clean-up PR as well

@sstamenk
sstamenk deleted the fix-msvc-complex-pow-dot branch November 27, 2026 14:14
@martin-frbg

Copy link
Copy Markdown
Collaborator

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.

@sstamenk

Copy link
Copy Markdown
Contributor Author

Should be relatively easy, Claude already identified the dead code when I did the pass for this PR. Let me take a look

sstamenk added a commit to ROCm/TheRock that referenced this pull request Nov 30, 2026
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>
sstamenk added a commit to ROCm/TheRock that referenced this pull request Nov 30, 2026
## 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants