Skip to content

Stop using cuBLAS nrm2 for single- and double-precision norms - #3633

Merged
maleadt merged 1 commit into
mainfrom
tb/nrm2-subnormal
Nov 25, 2026
Merged

maleadt merged 1 commit into
mainfrom
tb/nrm2-subnormal

Conversation

@maleadt

@maleadt maleadt commented Nov 25, 2026

Copy link
Copy Markdown
Member

cuBLAS' nrm2 mishandles subnormal elements, so norm, normalize! and normalize of Float32, Float64, ComplexF32 and ComplexF64 arrays could be badly wrong for inputs that contain them. This PR stops using nrm2 for those types.

Depending on the input and the version, nrm2 drops the contribution of subnormal elements or inflates it. Ratios of nrm2 to the true norm with cuBLAS 12.9.1 through 13.8 (CUDA 13.2's cuBLAS 13.4 measured on an RTX 5080; 12.8.4 behaves the same):

input nrm2 / true norm
fill(floatmin(T)/8, 2) 0.0028
fill(floatmin(T)/2, 2) 0.18
fill(floatmin(T)/8, 10^6) (a normal norm) 0.0625
[floatmin(T)/8, 2floatmin(T)] 0.998
fill(nextfloat(zero(T)), 4) 0
ComplexF32[-2.42f-40 - 2.094507f-5im] 1.0002
[complex(floatmin(T)/8, 1e-3)] 1.0078
~10^6 subnormal components and one of 1e-3, ComplexF32 / ComplexF64 1.35 / 1.03

cuBLAS 13.10 (CUDA 13.5) gets all of these right, but still drops the smallest Float32 subnormals (fill(nextfloat(0f0), 4) gives nextfloat(0f0) instead of twice that), so there's no version to gate on.

This is what makes GPUArrays' new subnormal tests (gpuarrays/linalg/norm, linalg.jl:1018) fail on CI runners with an older local toolkit, e.g. the "Julia 1.13" job of build 8681 for #3627, on a runner with CUDA 13.2, while runners with CUDA 13.5 pass.

  • norm (and LinearAlgebra.norm2) now falls through to GPUArrays' generic implementation. On an RTX 5080 it is as fast or faster than nrm2: 12-17 us for 10^6 elements, where nrm2 takes 15 us in Float32 and 72 us in Float64, and on par for 10^8 elements.
  • normalize! still keeps the norm on the GPU (and remains capturable in a graph), now with two reductions: the largest component, then the sum of squares scaled by it with exact powers of two. The division happens in those scaled coordinates when the norm isn't a normal number. For 10^8 elements that is about 30% slower than with nrm2 in Float32, and 2.5x in Float64 on this GPU's slow FP64 units; small arrays take about 10 us longer.
  • Float16 and ComplexF16 keep using nrm2, which computes in Float32, where their elements are normal; I verified those with cuBLAS 13.4.

The new tests in lib/cublas/test/level1/core.jl fail with the previous methods on both cuBLAS 13.4 (74 of 96) and 13.10 (10 of 96), and pass with this change. libraries/cublas and gpuarrays/linalg pass with CUDA 13.2 and 13.5 on Julia 1.13, and libraries/cublas and gpuarrays/linalg/norm with CUDA 13.2 on Julia 1.12.

cuBLAS' nrm2 mishandles subnormal elements. Depending on the input and
the version, it drops their contribution, e.g. 0.0028x the true norm
for `fill(floatmin(T)/8, 2)` and 0 for `fill(nextfloat(zero(T)), 4)`
with cuBLAS 12.9.1 to 13.8, or inflates it: with a million subnormal
components next to a component of 1e-3, the result is 35% too large
for ComplexF32 and 3% for ComplexF64. Every version tried, 12.5 to
13.10, is affected; 13.10 still drops the smallest Float32
subnormals. GPUArrays' new subnormal norm tests therefore fail on CI
runners with an older toolkit.

Use GPUArrays' generic norm instead. On an RTX 5080 it is as fast or
faster: 12-17 us for 10^6 elements, where nrm2 takes 15 us (Float32)
to 72 us (Float64). normalize! keeps the norm on the device with two
reductions, for the largest component and the scaled sum of squares.
For 10^8 elements, that makes it about 30% slower than with nrm2 in
Float32 and 2.5x in Float64 (on this GPU's slow FP64 units), and it
adds about 10 us for small arrays. The half-precision variants keep using nrm2, which
computes in single precision, where their elements are normal.
@kshyatt

kshyatt commented Nov 25, 2026

Copy link
Copy Markdown
Member

Oof, this is my fault, sorry!

@maleadt
maleadt merged commit a767acb into main Nov 25, 2026
4 of 9 checks passed
@maleadt
maleadt deleted the tb/nrm2-subnormal branch November 25, 2026 22:00
@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.88%. Comparing base (aac5569) to head (95e4236).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
lib/cublas/src/linalg.jl 90.90% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3633      +/-   ##
==========================================
- Coverage   90.39%   88.88%   -1.51%     
==========================================
  Files         201      200       -1     
  Lines       22650    21670     -980     
==========================================
- Hits        20474    19262    -1212     
- Misses       2176     2408     +232     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

Copy link
Copy Markdown
Contributor

Benchmark results

Compared with aac556991, the merge base.
The environment differs from the baseline's: Driver 595.71.5 → 620.70.10, OS Linux 6.8.0-124-generic → Linux 6.8.0-151-generic.

Times are the minimum time of every benchmark. A change is unusual when it exceeds both three times the benchmark's typical change between consecutive runs on the default branch and a minimum per category (performance 3%, latency 5%, applications 3%, other 3%). A benchmark that starts or stops allocating is listed however few the allocations. A single run can still be a fluke, so rerun before drawing conclusions.

Geometric mean Unusual: slower Unusual: faster Compared
Performance +1.1% 0 0 79
Latency −5.6% 0 2 3
Applications +0.0% 0 0 6
Benchmark Baseline This PR Change
Latency
ttfp 4.59 s 4.29 s −6.5% 🟢 faster
import 4.19 s 3.92 s −6.5% 🟢 faster
All benchmarks
Benchmark Baseline This PR Change
Performance
array/accumulate/Float32/1d 43.5 µs 44.2 µs +1.7%
array/accumulate/Float32/dims=1 30.1 µs 31.2 µs +3.7%
array/accumulate/Float32/dims=1L 96.1 µs 96.8 µs +0.7%
array/accumulate/Float32/dims=2 47.2 µs 49.0 µs +3.7%
array/accumulate/Float32/dims=2L 370 µs 366 µs −1.1%
array/accumulate/Int64/1d 67.3 µs 68.3 µs +1.5%
array/accumulate/Int64/dims=1 40.4 µs 41.3 µs +2.3%
array/accumulate/Int64/dims=1L 139 µs 140 µs +0.2%
array/accumulate/Int64/dims=2 70.5 µs 71.1 µs +0.8%
array/accumulate/Int64/dims=2L 770 µs 762 µs −1.0%
array/broadcast 10.6 µs 10.6 µs −0.1%
array/broadcast launch 5.89 µs 5.78 µs −2.0%
array/construct 1.30 µs 1.33 µs +2.2%
array/copy 17.6 µs 17.5 µs −0.4%
array/copyto!/cpu_to_gpu 208 µs 207 µs −0.6%
array/copyto!/gpu_to_cpu 240 µs 240 µs −0.1%
array/copyto!/gpu_to_gpu 12.2 µs 12.1 µs −0.8%
array/iteration/findall/bool 47.8 µs 48.4 µs +1.2%
array/iteration/findall/int 56.6 µs 57.7 µs +1.9%
array/iteration/findfirst/bool 31.6 µs 32.0 µs +1.0%
array/iteration/findfirst/int 33.1 µs 33.5 µs +1.1%
array/iteration/findmin/1d 62.2 µs 62.9 µs +1.1%
array/iteration/findmin/2d 39.8 µs 40.5 µs +1.7%
array/iteration/logical 47.0 µs 48.4 µs +3.0%
array/iteration/scalar 58.0 µs 57.4 µs −1.0%
array/permutedims/2d 55.5 µs 55.3 µs −0.3%
array/permutedims/3d 51.3 µs 52.1 µs +1.5%
array/permutedims/4d 42.9 µs 43.3 µs +1.0%
array/random/rand!/Float32 9.65 µs 9.62 µs −0.3%
array/random/rand!/Int64 16.3 µs 16.3 µs −0.3%
array/random/rand/Float32 11.4 µs 11.8 µs +3.3%
array/random/rand/Int64 20.6 µs 20.6 µs −0.1%
array/random/randn!/Float32 29.1 µs 29.5 µs +1.1%
array/random/randn/Float32 38.9 µs 39.3 µs +0.9%
array/reductions/mapreduce/Float32/1d 29.6 µs 30.4 µs +2.8%
array/reductions/mapreduce/Float32/dims=1 17.7 µs 17.7 µs +0.2%
array/reductions/mapreduce/Float32/dims=1L 54.3 µs 54.3 µs −0.0%
array/reductions/mapreduce/Float32/dims=2 24.0 µs 25.6 µs +6.7%
array/reductions/mapreduce/Float32/dims=2L 60.6 µs 61.4 µs +1.3%
array/reductions/mapreduce/Int64/1d 30.7 µs 31.1 µs +1.3%
array/reductions/mapreduce/Int64/dims=1 19.5 µs 20.0 µs +2.4%
array/reductions/mapreduce/Int64/dims=1L 92.7 µs 93.0 µs +0.4%
array/reductions/mapreduce/Int64/dims=2 26.4 µs 27.5 µs +4.2%
array/reductions/mapreduce/Int64/dims=2L 96.2 µs 96.5 µs +0.3%
array/reductions/reduce/Float32/1d 29.4 µs 30.0 µs +2.2%
array/reductions/reduce/Float32/dims=1 17.6 µs 17.9 µs +2.0%
array/reductions/reduce/Float32/dims=1L 53.9 µs 54.3 µs +0.7%
array/reductions/reduce/Float32/dims=2 24.1 µs 25.1 µs +3.9%
array/reductions/reduce/Float32/dims=2L 60.5 µs 61.4 µs +1.4%
array/reductions/reduce/Int64/1d 30.6 µs 31.1 µs +1.9%
array/reductions/reduce/Int64/dims=1 19.7 µs 19.9 µs +0.8%
array/reductions/reduce/Int64/dims=1L 92.7 µs 92.8 µs +0.1%
array/reductions/reduce/Int64/dims=2 26.8 µs 27.0 µs +0.8%
array/reductions/reduce/Int64/dims=2L 96.2 µs 96.6 µs +0.4%
array/reverse/1d 19.3 µs 19.1 µs −0.8%
array/reverse/1d_inplace 11.9 µs 11.9 µs −0.2%
array/reverse/1dL 73.2 µs 73.2 µs +0.0%
array/reverse/1dL_inplace 68.6 µs 68.5 µs −0.1%
array/reverse/2d 23.0 µs 23.1 µs +0.7%
array/reverse/2d_inplace 15.0 µs 15.1 µs +0.5%
array/reverse/2dL 92.5 µs 92.6 µs +0.1%
array/reverse/2dL_inplace 69.4 µs 69.5 µs +0.2%
array/sorting/1d 825 µs 826 µs +0.1%
array/sorting/2d 248 µs 247 µs −0.1%
array/sorting/by 966 µs 967 µs +0.1%
cuda/graph/capture 24.5 µs 25.7 µs +4.9%
cuda/graph/eager 25.0 µs 24.5 µs −2.1%
cuda/graph/launch 16.5 µs 16.6 µs +0.2%
cuda/synchronization/context/auto 1.91 µs 1.85 µs −3.2%
cuda/synchronization/context/blocking 660 ns 663 ns +0.4%
cuda/synchronization/context/nonblocking 4.28 µs 6.05 µs +41.4%
cuda/synchronization/stream/auto 583 ns 580 ns −0.6%
cuda/synchronization/stream/blocking 542 ns 532 ns −1.8%
cuda/synchronization/stream/nonblocking 6.77 µs 6.39 µs −5.6%
kernel/indexing 11.7 µs 11.6 µs −0.7%
kernel/indexing_checked 12.3 µs 12.2 µs −0.1%
kernel/launch 1.46 µs 1.42 µs −2.5%
kernel/occupancy 30.0 ns 30.0 ns −0.0%
kernel/rand 16.2 µs 16.2 µs −0.2%
Latency
import 4.19 s 3.92 s −6.5% 🟢 faster
precompile 6.43 s 6.18 s −3.9%
ttfp 4.59 s 4.29 s −6.5% 🟢 faster
Applications
byval/reference 145 µs 145 µs −0.1%
byval/slices=1 146 µs 146 µs +0.0%
byval/slices=2 286 µs 286 µs −0.1%
byval/slices=3 426 µs 426 µs +0.1%
cudadevrt 104 µs 104 µs +0.1%
volumerhs 10.3 ms 10.3 ms +0.0%

History: https://juliagpu.org/perf/?pkg=cuda

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