Skip to content

BLAS: Account for subnormal rounding in NRM2 tests - #1423

Open
rmlarsen wants to merge 1 commit into
Reference-LAPACK:masterfrom
rmlarsen:blas-nrm2-subnormal-tests
Open

rmlarsen wants to merge 1 commit into
Reference-LAPACK:masterfrom
rmlarsen:blas-nrm2-subnormal-tests

Conversation

@rmlarsen

@rmlarsen rmlarsen commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Stop the NRM2 tests from rejecting correctly rounded subnormal results: floor the reference magnitude at SAFMIN in the error denominator, which takes a false-failure rate of 6.1e-5 per draw to zero over 2e7 draws, and add deterministic subnormal cases so the regression no longer rides on a lucky random draw.

The tests measure only relative error, so a subnormal result whose absolute error is one subnormal ulp is rejected outright. In #1382 an ARM64 OpenMP shared build reported ten DNRM2 failures at N=3 and INCX=0 while the tested BLAS sources were unchanged from master.

That count is the signature of the mechanism rather than a coincidence: at INCX=0 every IV row shares X(1), so a single unlucky draw of the random tail produces exactly ten reported failures. Sampling the same configuration on x86-64 — x = 2*SAFMIN*w with w uniform on (-1,1), compared against the reference DNRM2 — over 2e7 draws gives

SNRM != ZNRM 32% of draws, an ordinary one-subnormal-ulp disagreement
rejected by the current comparison 1229, i.e. 6.1e-5 per draw
rejected by the comparison in this PR 0, largest ratio 0.385 against THRESH = 10

So this is neither ARM-specific nor flush-to-zero; it is a rare lottery any platform can lose. It does gate CI: #1371's LAPACK_Test_Summary parses the .out files, and #1382's own rerun failed it with failing to pass the threshold: 10.

Fix. Use max(reference, SAFMIN) in the error denominator for all four real and complex norm tests. This is bit-identical to the current comparison for every normal result and supplies an absolute tolerance of order SAFMIN*ULP near underflow. It also replaces the overly permissive zero-reference branch: ZNRM == 0 happens only when the whole vector is exactly zero, in which case the SNRM == ZNRM fast path already claims it, so that branch was reachable only from a broken implementation. NaN and infinity checks remain in place.

Coverage. Add a fixed subnormal value and an unrandomized tail alongside every existing randomized case. The value has to sit in a narrow window: deep enough that one subnormal ulp is a relative error above THRESH*N*ULP, and shallow enough that SQRT(N)*x and NRM2(N,x,0) still round differently. Of 2*SAFMIN/k for k = 2, ..., 200000, only 13 values in double and 9 in single satisfy both, so the constants are commented rather than left bare, and a named parameter selects the unrandomized case instead of keying it to the end of the value list. The new cases fail the old comparison with test ratios of 3.2E+02 to 2.7E+03 in all four precisions and both index widths; each norm test now runs 1946 checks instead of 1610. No production BLAS or LAPACK routine changes.

Validation on a 13th Gen Intel(R) Core(TM) i7-13700HX (x86-64, WSL2) with GNU Fortran 13.3.0:

  • All eight Level 1 BLAS executables pass, 1946 norm checks each, in Release static, Release shared with -fopenmp -frecursive -fcheck=all, -O3 -march=native, and -O0 -g -fcheck=all, using both the default and _64 APIs. Output summaries were checked for numerical failures, since executable exit status alone does not detect them; LAPACK_Test_Summary is what turns them into a CI failure.
  • 200 repetitions per precision with zero failures, and 8e7 focused subnormal draws across the four routines whose largest test ratio is 0.385.
  • Mutation checks: reverting the comparison to the relative-only form fails 14, 14, 14 and 20 checks in S, D, C and Z; flushing nonzero subnormal norms to zero fails 104 to 128; perturbing normal norms by 1% fails about 1230 per precision.
  • git diff --check passes. Added fixed-form source lines fit within 72 columns.

Under a flush-to-zero build the new value itself flushes, so the deterministic case degenerates into the existing zero case and tests nothing rather than failing; -ffast-math shows only the pre-existing flush-to-zero failures, at twice the count because the new row duplicates the zero row.

ARM64/GNU 15.2 and other compiler configurations remain for upstream CI.

Related: #1382

Checklist

Floor the reference magnitude at SAFMIN when forming the NRM2 test
ratio. Subnormal results incur absolute rounding error of order
SAFMIN*ULP, which a purely relative tolerance can reject. Apply the
comparison to all four precisions and tighten the zero-reference case.

Add a fixed subnormal value and an unrandomized tail alongside every
existing randomized case. The value has to sit in a narrow window:
deep enough that one subnormal ulp is a relative error above
THRESH*N*ULP, and shallow enough that the reference and NRM2 still
round differently. The chosen values fail the old comparison with
test ratios of 3.2E+02 to 2.7E+03 against THRESH = 10, in all four
precisions and both index APIs. A named parameter selects the
unrandomized case so that it does not follow the end of the value
list. Each norm test now runs 1946 checks instead of 1610. No
production BLAS or LAPACK routine changes.

Validated on all eight Level 1 executables in Release static, Release
shared with OpenMP and -fcheck=all, -O3 -march=native, and
-O0 -g -fcheck=all; 200 repetitions per precision; 8E+07 focused
subnormal draws whose largest test ratio is 0.385; and mutations that
confirm flushed subnormal norms and 1% perturbed normal norms still
fail.
@rmlarsen
rmlarsen force-pushed the blas-nrm2-subnormal-tests branch from 172c6c4 to 0b0f51b Compare September 16, 2026 06:38
@codecov

codecov Bot commented Sep 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.36%. Comparing base (a6c6e74) to head (0b0f51b).
⚠️ Report is 2 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1423   +/-   ##
=======================================
  Coverage   69.36%   69.36%           
=======================================
  Files        6122     6122           
  Lines      486711   486723   +12     
  Branches    23268    23268           
=======================================
+ Hits       337584   337600   +16     
+ Misses     148689   148685    -4     
  Partials      438      438           
Components Coverage Δ
BLAS 97.94% <ø> (ø)
CBLAS 96.98% <ø> (ø)
LAPACK 82.38% <ø> (ø)
LAPACKE 2.17% <ø> (ø)
TMGLIB 55.69% <ø> (ø)
BLAS testing 88.36% <100.00%> (+0.03%) ⬆️
CBLAS testing 89.63% <ø> (ø)
LAPACK testing 82.20% <ø> (ø)
LAPACKE testing ∅ <ø> (∅)
Files with missing lines Coverage Δ
BLAS/TESTING/cblat1.f 89.22% <100.00%> (+0.45%) ⬆️
BLAS/TESTING/dblat1.f 91.12% <100.00%> (+0.27%) ⬆️
BLAS/TESTING/sblat1.f 92.23% <100.00%> (+0.27%) ⬆️
BLAS/TESTING/zblat1.f 89.22% <100.00%> (+0.45%) ⬆️

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a6c6e74...0b0f51b. Read the comment docs.

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.

1 participant