Repository navigation
Stop using cuBLAS nrm2 for single- and double-precision norms - #3633
Conversation
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.
|
Oof, this is my fault, sorry! |
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
Benchmark resultsCompared with aac556991, the merge base. 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.
All benchmarks
History: https://juliagpu.org/perf/?pkg=cuda |
cuBLAS'
nrm2mishandles subnormal elements, sonorm,normalize!andnormalizeofFloat32,Float64,ComplexF32andComplexF64arrays could be badly wrong for inputs that contain them. This PR stops usingnrm2for those types.Depending on the input and the version,
nrm2drops the contribution of subnormal elements or inflates it. Ratios ofnrm2to 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):nrm2/ true normfill(floatmin(T)/8, 2)fill(floatmin(T)/2, 2)fill(floatmin(T)/8, 10^6)(a normal norm)[floatmin(T)/8, 2floatmin(T)]fill(nextfloat(zero(T)), 4)ComplexF32[-2.42f-40 - 2.094507f-5im][complex(floatmin(T)/8, 1e-3)]cuBLAS 13.10 (CUDA 13.5) gets all of these right, but still drops the smallest
Float32subnormals (fill(nextfloat(0f0), 4)givesnextfloat(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(andLinearAlgebra.norm2) now falls through to GPUArrays' generic implementation. On an RTX 5080 it is as fast or faster thannrm2: 12-17 us for 10^6 elements, wherenrm2takes 15 us inFloat32and 72 us inFloat64, 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 withnrm2inFloat32, and 2.5x inFloat64on this GPU's slow FP64 units; small arrays take about 10 us longer.Float16andComplexF16keep usingnrm2, which computes inFloat32, where their elements are normal; I verified those with cuBLAS 13.4.The new tests in
lib/cublas/test/level1/core.jlfail with the previous methods on both cuBLAS 13.4 (74 of 96) and 13.10 (10 of 96), and pass with this change.libraries/cublasandgpuarrays/linalgpass with CUDA 13.2 and 13.5 on Julia 1.13, andlibraries/cublasandgpuarrays/linalg/normwith CUDA 13.2 on Julia 1.12.