You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In commit 4174d8d, SORCSD2BY1 computes an inaccurate result with a backward error of ||Q - U Σ V^T|| ≈ 10^-5 when it should be at most ε ||Q|| ≈ 10^-7. Input with m = 3, n = 2, p = 2 (Q11 ∈ ℝ(p, n), Q21 ∈ ℝ(m - p, n)):
The Python code below demonstrates the problem. It requires Python 3, NumPy, and a shared LAPACK library.
Call it with python3 lapack-issue-917.py and optionally pass the path to the LAPACK shared library. Example (the first execution uses the system's LAPACK installation, the second execution uses my LAPACK build of the latest master branch code):
The patch closing #634 added the assumption that the input vector x of xORBDB6 has unit norm. This may not be the case when xORBDB6 is called for the first time inside xORBDB5 (the only xORBDB6-caller in LAPACK).
[Given a vector x and a matrix with orthonormal columns Q, xORBDB6 returns the components of x orthogonal to the range of Q or any other vector orthogonal to ran(Q) if x lies within ran(Q).]
For example when calling SORCSD2BY1 with the arguments in the first post, the SORBDB6 arguments are as follows:
x = [-7.45058060E-08; -1.51934143E-09]
Q = [0.999999702; -7.29401961E-09]
Given the following 2x1 matrix with orthonormal columns Q1 = [1 - ε / 2; 0]
and the following unit-norm vector x1:
x0 = [6399 / 8192 ε; 1 / 128 ε]
x1 = x0 / ‖x0‖
SORBDB5 computes the output y = [1.2e-5; 1] with ‖Q1^T y‖ ≈ 100 ε. This is clearly inaccurate (the factor 100 should instead be small in modulus).
Call the Python code below as follows (NumPy required):
python3 lapack-issue-917-sorbdb5.py # uses system LAPACK
python3 lapack-issue-917-sorbdb5.py /path/to/lapack.so # uses the LAPACK library at the provided location
Python code calling SORBDB5:
#!/usr/bin/python3# This file calls SORBDB5 with a unit-norm vector `x` and a matrix `Q` with a# single unit-norm column as parameters. The vector `x` is overwritten by the# SORBDB5 output vector `x` which is supposed to be orthogonal to the range of# `Q`.importctypesimportctypes.utilfromctypesimportbyref, c_char, c_float, c_int32, c_void_p, POINTERimportsysimportnumpyasnpimportnumpy.linalgasladefmain():
iflen(sys.argv) notin [1, 2]:
return"usage: python3 %s [path to LAPACK library]"% (sys.argv[0], )
iflen(sys.argv) >=2:
lapack_library=sys.argv[1]
else:
lapack_library=ctypes.util.find_library("lapack")
print("name of LAPACK library:", lapack_library)
lapack=ctypes.CDLL(lapack_library, use_errno=True)
lapack.sorbdb5_.restype=None# an alias for the _F_ortran integer typef_int=ctypes.c_int32lapack.sorcsd2by1_.argtypes= [
POINTER(f_int),
POINTER(f_int),
POINTER(f_int),
# X1POINTER(c_float),
POINTER(f_int),
# X2POINTER(c_float),
POINTER(f_int),
# Q1POINTER(c_float),
POINTER(f_int),
# Q2POINTER(c_float),
POINTER(f_int),
# WORKPOINTER(c_float),
POINTER(f_int),
# INFOPOINTER(f_int),
]
eps=np.finfo(np.float32).epsnan=np.float32(np.nan)
m1=2m2=0n=1x1=np.array([6399/8192*eps, 1/128*eps], dtype=np.float32)
x1=x1/la.norm(x1)
x2=np.array([], dtype=np.float32)
q1=np.array(
[
[1-eps/2.],
[0],
],
dtype=np.float32,
order="F",
).reshape([2, 1])
assertnp.abs(1-np.dot(q1.T, q1)) <=epsq2=np.array([[]], dtype=np.float32, order="F").reshape((0, 1))
work=np.full(256, nan)
info=f_int(0)
print("vector x")
print(x1)
print("matrix with orthonormal columns Q")
print(q1)
intref=lambdan: byref(f_int(n))
lapack.sorbdb5_(
intref(m1),
intref(m2),
intref(n),
x1.ctypes.data_as(POINTER(c_float)),
intref(1),
x2.ctypes.data_as(POINTER(c_float)),
intref(1),
q1.ctypes.data_as(POINTER(c_float)),
intref(q1.shape[0]),
q2.ctypes.data_as(POINTER(c_float)),
intref(max(1, q2.shape[0])), # ldq2 must be nonnegativework.ctypes.data_as(POINTER(c_float)),
intref(work.shape[0]),
byref(info),
)
assertinfo.value==0x1=x1/la.norm(x1)
tol=2*epsdelta=la.norm(np.dot(q1.T, x1))
print("tolerance: %.2e"% (tol, ))
print("computed vector orthogonal to ran(Q):")
print(x1)
ifdelta<=tol:
outcome="ok"else:
outcome="FAIL (error is %.3f times epsilon)\n"% (delta/eps,)
print()
print(outcome)
if__name__=="__main__":
sys.exit(main())
xORCSD2BY1/xUNBDB6 calls xORBDB6/xUNBDB6 indirectly. Given a vector x and a unit-norm vector q, xORBDB6 computes a vector orthogonal to q with at most two iterations of the Gram-Schmidt method. The subroutine terminates immediately if
∥y∥₂ ≥ α ∥x∥₂,
where y is the result of one round of the Gram-Schmidt method
y ≔ x - q (q^* x)
and 0 < α < 1 is a magic constant. The assumption is that α is sufficiently large to ensure that y is numerically orthogonal to q. This is not the case with the current value of α.
Implementation
In the current code α = 0.1 and this value is provably too small. Let x ≔ [1; β] and let q = [1 - ε; ε]. We are looking for the value of β maximizing the inner product of q and y subject to ∥y∥₂ ≥ α ∥x∥₂. With a bit of calculation one arrives at the equations
q^* y = ε (α + β)
and
α² (1 + β²) ≈ β² - 2 ε β
Solving for β one arrives at β ≈ 0.1005 and ∣q^* y∣ ≈ 20.9 ε. y is clearly not numerically orthogonal to q.
Next we can reverse the question and ask for a value of α subject to ∣q^* y∣ ≤ 2 ε or equivalently β ≈ 1. The solution is α ≈ 1 / √2. This is clearly much larger than the current value 0.1.
Note
I do not claim that the values above maximize the required value of α; they were just derived from the input in the issue description and modified to yield nice formula.
Demo Code
The script below allows you to play with the values:
#!/usr/bin/python3importnumpyasnpimportnumpy.linalgaslaeps=np.finfo(np.float32).epsalpha=0.1beta=1/ (1-alpha**2) *eps+alpha/np.sqrt(1-alpha**2)
# slightly grow beta to avoid failing vector norm test norm(y) / norm(x) belowbeta= (1+eps) *betaprint("beta =", beta)
x=np.array([1, beta], dtype=np.float32)
q=np.array([1-eps, eps], dtype=np.float32)
assertnp.abs(1-la.norm(q)) <=epsy=x-np.dot(q, np.dot(q.T, x))
# check that the early termination condition is fulfilledassertla.norm(y) >=alpha*la.norm(x)
print("norm(y) / norm(x) =", la.norm(y) /la.norm(x))
print(" q^* y =", np.dot(q.T, y) /la.norm(y))
Sorry our posts criss-crossed. I was answering on the other thread at: #928 (comment)
A similar analysis for two vectors to what you are doing is done by Beresford Parlett in his book "The Symmetric Eigenvalue Problem" published in 1980. Credits for the algorithm is Kahan and Parlett and the algorithm for two vectors gave the moniker of "Twice is enough".
Taking an ALPHA small such as 1/10 should not be too much of an issue in this case. (Only 10x loss of accuracy at most.)
As stated earlier, xORBDB6 assumes unit-norm input vectors x. When this assumption is removed (i.e., xORBDB6 computes the norm of x before performing orthogonalization), then the Python code in the second post still fails with the larger ALPHA = 0.1 value. That is, three layers higher in the call stack the loss of accuracy is detectable by a random test problem.
For this reason I think ALPHA is too small even for practitioners and should be set to a safe value instead or an unconditional second round of orthogonalization should be executed.
Having skimmed the GLR paper, I wonder if ALPHA = 1 / (4 √2) is safe because according to Inequality (2.5) the loss of orthogonality in the Kahan-Parlett method is bounded by
|p · q| ≤ σ ϕ(m, n) ε,
where σ = 1 / ALPHA. In the case of unconditional execution of two orthogonalization rounds, the loss of orthogonality is bounded by (3.21):
∥I - Q^* Q∥₂ ≤ 2 √2n ϕ(m, n) ε
For n = 2, σ = 4 √2 yields the same error bound (according to my understanding the polynomial ϕ(m, n) is the same in both cases).
@langou, please let me know if and how you want me to make the orthogonalization safer.
The input in the C code below triggers SORCSD2BY1 to return a matrix with infinite values (when the matrix should be orthogonal). The issue is present at least since v3.9.1 (commit 77a0ceb). The cause is xORBDB5 returning a vector very small in norm (much smaller than ε). The causes problems when computing Householder reflectors in SLARFGP in the xORBDB5 caller code. This subroutine does possess a branch for handling underflows but this branch is not taken because ABS(TAU) = 2 * SMLNUM.
The demonstration code (compile with cc -Wextra -Wall -std=c99 -pedantic -llapack) prints the contents of the matrix U1 computed by SORCSD2BY1. This matrix should be orthogonal. On the author's computer and with LAPACK 3.9.1, the output is instead
U1 -inf +1.00e+00
U1 -inf -inf
Starting with version 3.11, the output is
U1 -8.88e-16 +1.00e+00
U1 -1.00e+00 -8.88e-16
Version 3.11 accidentally fixes the issue. In version 3.11 (specifically in #647) an assumption was added to xORBDB6 that the input vector has unit norm. This is most likely not the case when xORBDB5 calls xORBDB6 for the first time in line 218 but this is the only execution path returning nonzero vectors with a very small norm.
Demonstration code (NB it calls SORCSD2BY1 instad of SORBDB3 because it conveniently forms U1 from the elementary Householder reflectors):
Activity
christoph-conrads commentedon Oct 26, 2023
The Python code below demonstrates the problem. It requires Python 3, NumPy, and a shared LAPACK library.
Call it with
python3 lapack-issue-917.pyand optionally pass the path to the LAPACK shared library. Example (the first execution uses the system's LAPACK installation, the second execution uses my LAPACK build of the latest master branch code):christoph-conrads commentedon Nov 2, 2023
The patch closing #634 added the assumption that the input vector
xof xORBDB6 has unit norm. This may not be the case when xORBDB6 is called for the first time inside xORBDB5 (the only xORBDB6-caller in LAPACK).[Given a vector
xand a matrix with orthonormal columnsQ, xORBDB6 returns the components ofxorthogonal to the range ofQor any other vector orthogonal toran(Q)ifxlies withinran(Q).]For example when calling SORCSD2BY1 with the arguments in the first post, the SORBDB6 arguments are as follows:
xdoes evidently not have unit norm.christoph-conrads commentedon Nov 3, 2023
Given the following 2x1 matrix with orthonormal columns
Q1 = [1 - ε / 2; 0]and the following unit-norm vector
x1:SORBDB5 computes the output
y = [1.2e-5; 1]with‖Q1^T y‖ ≈ 100 ε. This is clearly inaccurate (the factor 100 should instead be small in modulus).Call the Python code below as follows (NumPy required):
Python code calling SORBDB5:
christoph-conrads commentedon Nov 7, 2023
@langou,
ALPHAis provably too small.Context
xORCSD2BY1/xUNBDB6 calls xORBDB6/xUNBDB6 indirectly. Given a vector
xand a unit-norm vectorq, xORBDB6 computes a vector orthogonal toqwith at most two iterations of the Gram-Schmidt method. The subroutine terminates immediately ifwhere
yis the result of one round of the Gram-Schmidt methodand 0 < α < 1 is a magic constant. The assumption is that α is sufficiently large to ensure that
yis numerically orthogonal toq. This is not the case with the current value of α.Implementation
In the current code
α = 0.1and this value is provably too small. Letx ≔ [1; β]and letq = [1 - ε; ε]. We are looking for the value ofβmaximizing the inner product ofqandysubject to∥y∥₂ ≥ α ∥x∥₂. With a bit of calculation one arrives at the equationsand
Solving for β one arrives at β ≈ 0.1005 and
∣q^* y∣ ≈ 20.9 ε.yis clearly not numerically orthogonal toq.Next we can reverse the question and ask for a value of α subject to
∣q^* y∣ ≤ 2 εor equivalentlyβ ≈ 1. The solution isα ≈ 1 / √2. This is clearly much larger than the current value 0.1.Note
I do not claim that the values above maximize the required value of α; they were just derived from the input in the issue description and modified to yield nice formula.
Demo Code
The script below allows you to play with the values:
langou commentedon Nov 7, 2023
Thanks @ConradS.
Sorry our posts criss-crossed. I was answering on the other thread at:
#928 (comment)
A similar analysis for two vectors to what you are doing is done by Beresford Parlett in his book "The Symmetric Eigenvalue Problem" published in 1980. Credits for the algorithm is Kahan and Parlett and the algorithm for two vectors gave the moniker of "Twice is enough".
christoph-conrads commentedon Nov 8, 2023
(I am taking the liberty to continue the discussion here.)
Thank you for this hint. I can now see in the Giraud/Langou/Rozložnı́k paper "On the round-off error analysis of the Gram-Schmidt algorithm with
reorthogonalization" where this magic constant is discussed.
As stated earlier, xORBDB6 assumes unit-norm input vectors
x. When this assumption is removed (i.e., xORBDB6 computes the norm ofxbefore performing orthogonalization), then the Python code in the second post still fails with the largerALPHA = 0.1value. That is, three layers higher in the call stack the loss of accuracy is detectable by a random test problem.For this reason I think
ALPHAis too small even for practitioners and should be set to a safe value instead or an unconditional second round of orthogonalization should be executed.Having skimmed the GLR paper, I wonder if
ALPHA = 1 / (4 √2)is safe because according to Inequality (2.5) the loss of orthogonality in the Kahan-Parlett method is bounded bywhere
σ = 1 / ALPHA. In the case of unconditional execution of two orthogonalization rounds, the loss of orthogonality is bounded by (3.21):For
n = 2,σ = 4 √2yields the same error bound (according to my understanding the polynomial ϕ(m, n) is the same in both cases).@langou, please let me know if and how you want me to make the orthogonalization safer.
christoph-conrads commentedon Nov 9, 2023
The input in the C code below triggers SORCSD2BY1 to return a matrix with infinite values (when the matrix should be orthogonal). The issue is present at least since v3.9.1 (commit 77a0ceb). The cause is xORBDB5 returning a vector very small in norm (much smaller than ε). The causes problems when computing Householder reflectors in SLARFGP in the xORBDB5 caller code. This subroutine does possess a branch for handling underflows but this branch is not taken because
ABS(TAU) = 2 * SMLNUM.lapack/SRC/slarfgp.f
Line 200 in 8ecaaf9
The demonstration code (compile with
cc -Wextra -Wall -std=c99 -pedantic -llapack) prints the contents of the matrixU1computed by SORCSD2BY1. This matrix should be orthogonal. On the author's computer and with LAPACK 3.9.1, the output is insteadStarting with version 3.11, the output is
Version 3.11 accidentally fixes the issue. In version 3.11 (specifically in #647) an assumption was added to xORBDB6 that the input vector has unit norm. This is most likely not the case when xORBDB5 calls xORBDB6 for the first time in line 218 but this is the only execution path returning nonzero vectors with a very small norm.
Demonstration code (NB it calls SORCSD2BY1 instad of SORBDB3 because it conveniently forms U1 from the elementary Householder reflectors):