GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Fix the JOBR = 'T' path of xGESVDQ: wrong TRANS in the real routines, invalid read in all four by rmlarsen · Pull Request #1513 · Reference-LAPACK/lapack Conversation
Contributor
AI disclosure: This PR was prepared with Claude Code. Summary The path of
Description Both defects are in
I went through the rest of this path once more (the copy of R**T into U, Fix
Nothing else changes; the values in V(NR+1:N,1:NR) were overwritten before being used. Reproducer For commit 1, run on master and with the fix (a rank-1 8-by-6 matrix, JOBA = 'A', JOBR = 'T', JOBU = JOBV = 'A'): program repro implicit none integer, parameter :: m = 8, n = 6 double precision :: a(m,n), a0(m,n), s(n), u(m,m), vt(n,n), x(m), y(n) double precision :: work(1000), rwork(10), q(2), res, ou, ov integer :: iwork(100), info, numrank, i, j, iq(2) do i = 1, m; x(i) = 1d0 + 0.1d0*i; end do do j = 1, n; y(j) = 1d0 - 0.1d0*j; end do do j = 1, n; a(:,j) = x*y(j); end do ! rank 1 a0 = a ! workspace query: JOBA='A', JOBR='T', JOBU='A', JOBV='A' call dgesvdq('A','N','T','A','A',m,n,a,m,s,u,m,vt,n,numrank,iq,-1,q,-1,rwork,-1,info) print '(a,i3,a,2f8.1)', 'query: info =', info, ', lwork =', q call dgesvdq('A','N','T','A','A',m,n,a,m,s,u,m,vt,n,numrank,iwork,100,work,1000,rwork,10,info) print '(a,i3,a,i3)', 'solve: info =', info, ', numrank =', numrank do j = 1, n; do i = 1, m a0(i,j) = a0(i,j) - sum(u(i,1:n)*s*vt(1:n,j)) end do; end do res = maxval(abs(a0)) ou = 0; ov = 0 do i = 1, m; do j = 1, m ou = max(ou, abs(dot_product(u(:,i),u(:,j)) - merge(1d0,0d0,i==j))) end do; end do do i = 1, n; do j = 1, n ov = max(ov, abs(dot_product(vt(i,:),vt(j,:)) - merge(1d0,0d0,i==j))) end do; end do print '(a,3es10.2)', 'max|A-U S VT|, |U^TU-I|, |VT VT^T-I| =', res, ou, ov end program On master the program stops at the query (and in the solve, if the query call is removed): With commit 1: For commit 2, a second program ( Regression test Commit 1. On the parent commit 236 to 248 of the 16340 DBD and SBD driver tests fail per run (tests 40 and 43), with Commit 2. The suite cannot detect an invalid read, so the validation is the program above. Test suite The full test suite passes, configured as in CI ( Checklist
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsDGESVDQ and SGESVDQ call DORMQR/SORMQR with TRANS = 'C' in two places:
the workspace query for JOBR = 'T' with all of U and V requested, and
the computation that assembles V when JOBR = 'T', JOBV = 'A', R has been
truncated to the numerical rank NR and 2*NR <= N. The real xORMQR
accepts only 'N' or 'T', so it reports argument 2 through XERBLA, and
the reference XERBLA stops the program. The complex routines call
xUNMQR, for which 'C' is valid, and the real code was evidently
transcribed from them.
Both paths were therefore never executable. DGESVDQ('A','N','T','A',
'A',8,6,...) with LWORK = -1 stops with "On entry to DORMQR parameter
number 2 had an illegal value", and so does a solve on an 8-by-6 matrix
of rank 1, where JOBA = 'A' truncates R to NR = 1. With JOBA = 'H' or
'E' no truncation is attempted, so NR = N and the second call is never
reached.
Use 'T', the real equivalent of the conjugate transpose. With it both
paths run and give a correct SVD: for the rank-1 8-by-6 matrix the
residual max|A - U S VT| is 4e-16 and |U**T U - I|, |VT VT**T - I| are
7e-16 and 2e-16. Other inputs behave exactly as before.
Test: DDRVBD and SDRVBD now also call xGESVDQ with JOBA = 'A' and
JOBR = 'T' on a matrix whose first N/2 columns are repeated to fill it,
so that its rank is at most N/2. The call is first a workspace query,
whose result must be consistent and must not make any routine call
XERBLA (xGESVDQ does not check the INFO of its inner xORMQR query, so
this is detected through the LERR flag of the test XERBLA). The solve
then gets the queried minimal LWORK in one IWS iteration and the queried
optimal LWORK in another, with a guard region after the workspace.
Tests 40-43 check the residual, the orthogonality of U and VT, and the
ordering of S, the guard, the flags and the query values. The old
driver only called xGESVDQ('H','N','N','A','A'), which never takes
either path. On the parent commit 236 to 248 of the 16340 DBD (SBD)
driver tests fail per run, tests 40 and 43, with XERBLA reporting DORMQR
(SORMQR) parameter 2; with this change all 16340 pass in both
precisions. Restoring 'C' in the query call of DGESVDQ alone fails 140 tests (43).
Co-Authored-By: Claude Fable 5.2 <[email protected]>
When JOBR = 'T', JOBV = 'A', R has been truncated to the numerical rank
NR and 2*NR <= N, xGESVDQ computes a QR factorization of the N-by-NR
matrix holding R**T (or R**H) in U(1:N,NR+1:2*NR), and copies the
triangular factor, which occupies U(1:NR,NR+1:2*NR), into V with
DO 1143 p = 1, NR
DO 1144 q = 1, N
V(q,p) = U(p,NR+q)
The triangle is NR-by-NR, but q runs to N, so the loop also reads
U(1:NR,2*NR+1:NR+N). For JOBU = 'S', 'R', 'F' or 'U', U has N columns,
and columns N+1 to NR+N do not exist; for JOBU = 'A' they do not exist
whenever NR+N > M. The values read are copied into V(NR+1:N,1:NR),
which xGESVD does not use and xLASET overwrites afterwards, so results
are unaffected, but it is an invalid read. Valgrind reports, for ZGESVDQ on a rank-1 8-by-6 matrix with JOBU = 'S'
and U allocated 8-by-6, "Invalid read of size 8 ... Address is 0 bytes
after a block of size 768", and AddressSanitizer reports a
heap-buffer-overflow READ in DGESVDQ for the same case. The
complex routines reach this path on master; the real routines can reach
it since the previous commit.
Bound the inner loop by NR, in all four precisions (CONJG is kept in the
complex ones). Nothing else in this path reads or writes outside the
arrays: the neighboring loops over R**T and the transposition loops
already stop at NR or N as their matrices require, and the non-
transposed twin copies the triangle with xLACPY( 'L', NR, NR, ... ).
Results are unchanged; V(NR+1:N,1:NR) held values that were overwritten
before use.
Test: the suite cannot detect an invalid read, so the fix was checked
with AddressSanitizer (DGESVDQ) and valgrind (ZGESVDQ) on rank-1 6-by-6
and 8-by-6 matrices with JOBU = 'A' and 'S', U allocated with exactly
the size the routine documents: errors on the parent, none with this
change, and identical singular values. CDRVBD and ZDRVBD now also run
xGESVDQ with JOBA = 'A', JOBR = 'T' on a matrix of rank at most N/2,
as DDRVBD and SDRVBD do, with a workspace query, the queried minimal
and optimal LWORK and a guard region; they pass on the parent and with
this change, since they only show that the path computes a correct SVD.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
There was an error while loading. Please reload this page.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@ ## master #1513 +/- ## ========================================== + Coverage 70.63% 70.76% +0.12% ========================================== Files 6243 6243 Lines 501290 501652 +362 Branches 23516 23516 ========================================== + Hits 354100 355006 +906 + Misses 146327 145783 -544 Partials 863 863
... and 20 files with indirect coverage changes Continue to review full report in Codecov by Harness.
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| $ LSWORK = LOPT | ||
| LG = MIN( 8, LWORK-LSWORK ) | ||
| DO 198 J = LSWORK + 1, LSWORK + LG | ||
| WORK( J ) = DCMPLX( GUARD, GUARD ) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
DCMPLX is a non-standard intrinsic. 773fd2f ("TESTING: replace non-standard double complex intrinsics") removed every use from TESTING because NAG Fortran only accepts it under -dcfuns, and this reintroduces it (here and line 1340). The NAG jobs are skipped on fork PRs (they finish in a few seconds here) but do build on master, so this would only show up after merge. Use CMPLX( ..., KIND=... ) with a kind parameter, as that commit did.
Found in an AI-assisted self-review (Claude Code).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 40704ff: both uses are now CMPLX( ..., KIND=DP ).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
The test added on this branch builds its double complex values with DCMPLX, a VAX extension that NAG Fortran only accepts under -dcfuns. 773fd2f removed every such use from TESTING, and the NAG jobs do not run on pull requests from forks, so the build would only break after the merge. Use CMPLX( ..., KIND=DP ) with a DP kind parameter, as that commit did. The values are the same; the complex16 LAPACK tests of this branch run 1154029 tests with no failures. Co-Authored-By: Claude Fable 5.2 <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.