CurrentModule = NextLA.MixedPrecision
The NextLA.MixedPrecision submodule stores a matrix as a tree of blocks, each level in its own
floating-point type, and runs recursive GEMM, triangular solve/multiply, SYRK, Cholesky and LU
directly on that tree. Off-diagonal blocks, which usually carry less of the matrix’s norm, can be
stored in Float16, BFloat16 or even 8-bit floats, while the diagonal leaves stay in Float32
or Float64.
The submodule is not re-exported by NextLA. Load it explicitly:
using NextLA
using NextLA.MixedPrecision
All containers split an n × n matrix the same way: at n ÷ 2 when n is a power of two, and
otherwise at the largest power of two below n. The diagonal blocks A11 and A22 are split
again; the off-diagonal blocks are stored as dense arrays.
level 0 after level 1
┌─────────┬─────────┐ ┌────┬────┬─────────┐
│ │ │ │ L │ o1 │ │
│ A11 │ A12 │ ├────┼────┤ o0 │
│ │ │ │ o1 │ L │ │
├─────────┼─────────┤ → ├────┴────┼────┬────┤
│ │ │ │ │ L │ o1 │
│ A21 │ A22 │ │ o0 ├────┼────┤
│ │ │ │ │ o1 │ L │
└─────────┴─────────┘ └─────────┴────┴────┘
o0: precisions[1] o1: precisions[2]
L: leaves, precisions[end]
The precisions vector gives one type per level, from the top down:
precisions[1] is used for the off-diagonal blocks at level 0,precisions[2] for the off-diagonal blocks at level 1, and so on,precisions[end] is used for the leaves (the diagonal blocks where recursion stops) and is the
container’s element type T_Base.Recursion stops when one precision is left or a block is 1 × 1. So for a FullMixedPrec,
precisions = DataType[Float16, Float32, Float64] stores the two largest off-diagonal blocks in
Float16, the next four in Float32, and four Float64 leaves of size about n/4. A
SymmMixedPrec or TriMixedPrec stores one off-diagonal block per split: one in Float16 and two
in Float32.
precisionsFloat8_E5M2, BFloat16, Float16, Float32, Float64.precisions[end] must be Float32 or Float64.| Container | Stores | Typical use |
|---|---|---|
FullMixedPrec |
a dense square matrix (A11, A12, A21, A22) |
GEMM output, LU |
SymmMixedPrec |
one triangle of a symmetric matrix | SYRK output, Cholesky |
TriMixedPrec |
one triangle of a triangular matrix | triangular solve and multiply |
TiledTriMixedPrec |
a triangular matrix as a flat grid of tiles | tile-based algorithms |
All of them are AbstractMatrix subtypes, so size, M[i, j] and eltype work, and
reconstruct_matrix copies the container back into a dense matrix of type T_Base.
transpose(M) and M' return a lazy transposed wrapper, except for a SymmMixedPrec, which is its
own transpose: they return M itself.
Each example on this page stands on its own and ends with a check that prints true. They are run
as doctests every time the documentation is built.
using NextLA, NextLA.MixedPrecision
A = rand(1024, 1024)
F = FullMixedPrec(A; precisions = DataType[Float16, Float64]) # off-diagonal Float16, leaves Float64
B = reconstruct_matrix(F) # back to a dense Float64 matrix
isapprox(B, A; rtol = 1e-3) # equal up to Float16 rounding
# output
true
Narrow types have little range: floatmax(Float16) is 65504. To store large values, every block
carries a Float32 scale factor s. A block whose largest magnitude exceeds floatmax of its
storage type is stored as A_block / s, and reads return stored * s. Unscaled blocks have
s = 1.
Float32.floatmax(Float32) throws an ArgumentError, when the
container is built and when an update would raise the scale that far.Float32, and solves of an
off-diagonal block narrower than the leaves, raise the block’s scale when the result passes the
range of its type. Leaves and wider blocks keep their scale, and an update whose result overflows
the Float32 working copy itself is stored as Inf without an error.lu_recursive! and potrf_recursive! must be unscaled: one scale
cannot hold both L and U, and a Cholesky factor does not scale with its block. A scaled leaf
throws an ArgumentError.The same block quantization is available on its own:
using NextLA.MixedPrecision
A = 1e6 .* rand(1024, 1024) # too large for Float16 (max 65504)
Q, s = quantize(A, Float16) # Q = A / s in Float16, s is a Float32 scale
isapprox(dequantize(Q, s, Float64), A; rtol = 1e-3)
# output
true
adaptive_precisions picks the coarsest type per level that keeps the error below a target
epsilon, from the norms of the off-diagonal blocks at each level relative to the whole matrix:
using NextLA.MixedPrecision, LinearAlgebra
A = rand(1024, 1024)
S = A * A' + 1024I
U = DataType[Float16, Float32, Float64] # candidate types
p = adaptive_precisions(S, U, 16, 1e-8; uplo = 'L')
M = SymmMixedPrec(S, 'L'; precisions = p) # p is ready to use as `precisions`
p[end] # the leaves keep the input's precision
# output
Float64
U lists the candidate types in any order.n_min (here 16) stops splitting before any block would be smaller than this.epsilon is the target accuracy; a larger epsilon allows coarser types.uplo is 'L' or 'U' for a symmetric or triangular matrix (only that triangle is read), and
'F' for a full one.The leaf type is the coarsest type in U that is at least as fine as eltype(A) and has the range
of Float32, so the result is always a valid precisions argument when U contains Float32 or
Float64.
The routines below extend the dense NextLA functions of the same name with methods for the
containers. They walk the block tree, call the dense routines on the leaves and GEMM or a
triangular solve on the off-diagonal blocks. recgemm!, recsyrk! and
lu_recursive! run independent parts as concurrent tasks for large matrices: on a GPU each
task has its own stream, so their kernels can overlap; on the CPU they take turns, so they do not
compete with BLAS’s own threads.
| Routine | Container argument | Computes |
|---|---|---|
recgemm!(α, A, B, β, C) |
C::FullMixedPrec |
C = αAB + βC |
recsyrk!(α, A, β, C) |
C::SymmMixedPrec |
C = αAAᵀ + βC on C.uplo |
potrf_recursive!(A) |
A::SymmMixedPrec |
Cholesky, in place |
lu_recursive!(A) |
A::FullMixedPrec |
unpivoted LU, in place |
unified_rectrxm!(side, uplo, trans, α, func, A, B) |
A::FullMixedPrec, SymmMixedPrec, TriMixedPrec |
triangular solve ('S') or multiply ('M') |
With an off-diagonal block narrower than Float32, recgemm! (and so the updates of
lu_recursive!) first rounds wider operands to the block’s type, each with a scale, so the
product’s error is that of the block’s storage. A Float16 block is then updated in place with
Float32 sums, on tensor cores on a GPU, and its scale is raised if the result overflows. On Metal
and oneAPI, which have no mixed-type GEMM, the operands are not rounded and the product runs in
Float32. When the top-level off-diagonal blocks are narrower than Float32, the Float32 GEMMs of
recgemm! and lu_recursive! may run on TF32 tensor cores on CUDA, which keeps the error at the
level the Float16 blocks already cost.
recsyrk! updates a Float64 leaf from Float16 or Float32 rows by forming the product in
Float32 (summed in Float32) and adding it into the leaf in Float64; the leaf itself is never
rounded. This runs at the speed of a Float32 update but adds Float32 rounding to it. In a
Cholesky with Float64 leaves directly under Float32 blocks (precisions = [Float32, Float64]),
the error can grow several-fold: about 9× on AA^T + nI with uniform random A, to about
2 \times 10^{-7}. With Float16 blocks above the leaves, their error dominates and the update
adds nothing visible.
using NextLA, NextLA.MixedPrecision, LinearAlgebra
A = rand(1024, 1024)
S = A * A' + 1024I
M = SymmMixedPrec(S, 'L'; precisions = DataType[Float32, Float64])
potrf_recursive!(M) # M now holds the factor L
L = TriMixedPrec(M) # the factor as triangular: shares M's blocks, copies their scales
b = rand(1024, 1)
x = unified_rectrxm!('L', 'L', 'N', 1.0, 'S', L, copy(b)) # solve L x = b
isapprox(reconstruct_matrix(L) * x, b; rtol = 1e-6)
# output
true
using NextLA, NextLA.MixedPrecision, LinearAlgebra
D = rand(1024, 1024) + 1024I # diagonally dominant, so no pivoting is needed
F = FullMixedPrec(D; precisions = DataType[Float32, Float64])
lu_recursive!(F)
LU = reconstruct_matrix(F) # L below the diagonal, U on and above
isapprox(UnitLowerTriangular(LU) * UpperTriangular(LU), D; rtol = 1e-5)
# output
true
lu_recursive! on a FullMixedPrec does not pivot; use matrices that
need none (e.g. diagonally dominant).uplo must match. For SymmMixedPrec and TriMixedPrec, unified_rectrxm! requires
uplo == A.uplo and throws an ArgumentError otherwise, since only that triangle is stored.Aᵀ, pass trans = 'T' rather than transpose(A).
unified_rectrxm! and lu_recursive! throw an ArgumentError for a transposed
container (a SymmMixedPrec is its own transpose, so it passes unchanged), and so does
recgemm! for a transposed C.potrf_recursive!(M), use
reconstruct_matrix(TriMixedPrec(M)) for the dense factor; reconstruct_matrix(M) would mirror
it into both triangles.TriMixedPrec(M) after the factorization. It shares M’s blocks but copies their
scales, so build it after the last in-place update of M (potrf_recursive!,
recsyrk!). One built before reads the updated blocks with the old scales, which can be
far off.TiledTriMixedPrec is a storage format with precisions of at most two types (off-diagonal
tiles, diagonal tiles) and a threshold for the tile size; the recursive routines above do not
take it.The docstrings are on the Mixed-precision core, Mixed-precision BLAS and Mixed-precision LAPACK pages.