Mixed-precision BLAS
The recursive GEMM, triangular solve/multiply and SYRK methods of NextLA.MixedPrecision, in src/mxp/blas. Load them with using NextLA.MixedPrecision.
NextLA.recgemm! — Method
recgemm!(alpha, A, B, beta, C::FullMixedPrec;
parallel=size(C, 1) > RECGEMM_PARALLEL_THRESHOLD) -> CComputes C = alpha * A * B + beta * C in place, where C is an n × n FullMixedPrec, A is n × k and B is k × n for any k, else DimensionMismatch. It walks C's block tree: A is split by rows and B by columns at C's split, so each quadrant of C takes one product over the whole k, A[1:mid, :] * B[:, 1:mid] for C11. Off-diagonal blocks are updated by a GEMM and diagonal blocks recursively. A block stored with a scale factor s holds C / s, so its update uses alpha / s. Operands wider than a Float16 off-diagonal block are rounded to Float16 first, each with a scale, so the product runs on Float16 tensor cores with Float32 sums; that block is then updated in place (otherwise an off-diagonal block with less range than Float32 is updated in a Float32 copy) and stored back with its scale raised when the update took it past floatmax of its type; a leaf keeps its scale. This keeps the error within Float16's unit roundoff, which the Float16 blocks already cost. The Float32 GEMMs run in Float32; only inside the mixed-precision lu_recursive!, whose TF32 scope they inherit, may they run on TF32 tensor cores on CUDA. With parallel, the four quadrant updates of the top level run as concurrent tasks (@async): on a GPU each task has its own stream, so their kernels can overlap; on the CPU they take turns and leave the cores to BLAS.
C is the container itself: its transpose throws an ArgumentError (for Cᵀ, pass the transposed operands into C). An A or B without strides (Diagonal, Symmetric, a triangular wrapper, sparse, ...) is first copied to a dense matrix on its backend. Operands on another backend than C throw an ArgumentError.
Examples
julia> using NextLA.MixedPrecision, LinearAlgebra
julia> A = rand(1024, 512); B = rand(512, 1024); C = rand(1024, 1024);
julia> M = FullMixedPrec(C; precisions = DataType[Float32, Float64]);
julia> recgemm!(1.0, A, B, 0.5, M); # M = A * B + 0.5 * M
julia> isapprox(reconstruct_matrix(M), A * B + 0.5 * C; rtol = 1e-5)
trueNextLA.unified_rectrxm! — Method
unified_rectrxm!(side, uplo, trans, diag, alpha, func, A, B) -> B
unified_rectrxm!(side, uplo, trans, alpha, func, A, B) -> Bunified_rectrxm! for a mixed-precision container A: solves op(A) X = alpha B for func = 'S', or computes B = alpha op(A) B for func = 'M', on side side (B * op(A) for 'R'), in place in B. op is the identity for trans = 'N' and the transpose for 'T' or 'C'; uplo names the triangle of A that is read, and must be A.uplo for a SymmMixedPrec or TriMixedPrec, which store only that triangle. diag = 'U' takes the diagonal as ones, and the form without diag takes diag = 'N'. A is a FullMixedPrec, SymmMixedPrec or TriMixedPrec; for its transpose pass trans = 'T', not transpose(A), which throws an ArgumentError (a SymmMixedPrec is its own transpose, so transpose(A) is A). Any other side, uplo, trans, diag or func throws an ArgumentError, and a B whose size does not match A a DimensionMismatch.
Examples
julia> using NextLA.MixedPrecision, LinearAlgebra
julia> T = tril(rand(1024, 1024)) + 1024I; B = rand(1024, 512);
julia> M = TriMixedPrec(T, 'L'; precisions = DataType[Float32, Float64]);
julia> X = unified_rectrxm!('L', 'L', 'N', 'N', 1.0, 'S', M, copy(B)); # solve T * X = B
julia> isapprox(X, LowerTriangular(T) \ B; rtol = 1e-5)
trueNextLA.recsyrk! — Method
recsyrk!(alpha, A, beta, C::SymmMixedPrec) -> CComputes C = alpha * A * transpose(A) + beta * C in place on the stored triangle (C.uplo) of the n × n SymmMixedPrec C, where A is n × k. It walks C's block tree: each off-diagonal block takes one GEMM, and each leaf the dense recsyrk!. A Float64 leaf updated by Float16 or Float32 A instead takes A * transpose(A) in Float32 (summed in Float32) and adds it into the leaf in Float64. A block stored with a scale factor s holds C / s, so its update uses alpha / s; an off-diagonal block with less range than Float32 is updated in a Float32 copy (in place, still summed in Float32, when it and A are Float16) and stored back with its scale raised when the update took it past floatmax of its type. When the first diagonal block of the top level has more than RECSYRK_PARALLEL_THRESHOLD rows, the two diagonal halves run as concurrent tasks (@async), and a leaf follows the dense recsyrk! rule; the second half is never larger than the first and may be under the threshold. A with a row count other than C's throws a DimensionMismatch.
Examples
julia> using NextLA.MixedPrecision, LinearAlgebra
julia> A = rand(1024, 512); C = rand(1024, 1024); C = C + C';
julia> M = SymmMixedPrec(C, 'L'; precisions = DataType[Float32, Float64]);
julia> recsyrk!(1.0, A, 1.0, M); # M = A * A' + M, lower triangle
julia> isapprox(reconstruct_matrix(M), A * A' + C; rtol = 1e-5)
trueUsage examples
Each example stands on its own and ends with a check that prints true: the relative error of the mixed-precision result against the same computation in Float64, with a tolerance that matches the coarsest precision used.
GEMM and SYRK updates into mixed-precision containers
recgemm! updates a FullMixedPrec and recsyrk! the stored triangle of a SymmMixedPrec, from ordinary dense factors.
using NextLA, NextLA.MixedPrecision, LinearAlgebra
n, k = 2048, 512
A = rand(n, k)
B = rand(k, n)
C = rand(n, n)
F = FullMixedPrec(C; precisions = DataType[Float32, Float64])
recgemm!(-1.0, A, B, 1.0, F) # F = C - A * B
S = SymmMixedPrec(C + C', 'L'; precisions = DataType[Float32, Float64])
recsyrk!(1.0, A, 0.5, S) # S = A * A' + (C + C') / 2
relerr(X, Y) = norm(X - Y) / norm(Y)
relerr(reconstruct_matrix(F), C - A * B) < 1e-5 &&
relerr(reconstruct_matrix(S), A * A' + (C + C') / 2) < 1e-5
# output
true