Mixed-precision core
The NextLA.MixedPrecision module, its containers, precision selection and quantization, in src/mxp/core. Load it with using NextLA.MixedPrecision; see the Mixed precision guide for concepts and examples.
NextLA.MixedPrecision — Module
MixedPrecisionMixed-precision matrix containers and the routines that work over them. Not re-exported by NextLA; opt in with using NextLA.MixedPrecision.
Containers
NextLA.MixedPrecision.FullMixedPrec — Type
FullMixedPrec{T_Base<:AbstractFloat} <: AbstractMixedPrec{T_Base}A hierarchical, recursive mixed-precision data structure that maps to full dense square matrices. It recursively partitions a matrix into four sub-blocks (A11, A22, A21, A12), enabling varying precision levels to be stored dynamically at different depths of the recursive hierarchy. A node holds either A11, A22, A21 and A12, or a leaf block Base, never both. Every stored block carries a Float32 scale factor, 1 when it is unscaled. The struct is mutable so that a block an update takes past floatmax of its type can be stored back with a larger scale.
NextLA.MixedPrecision.FullMixedPrec — Method
FullMixedPrec(A::AbstractMatrix; precisions::Vector{DataType}) -> FullMixedPrecConstructs a FullMixedPrec representation of the dense square matrix A.
The matrix is partitioned using a base-2 recursive splitting scheme. If the dimension n is a power of 2, it splits evenly; otherwise, it splits at the largest power of 2 less than n. Each level stores A21 and A12 in precisions[1] and recurses on A11 and A22 with the rest. The recursion stops when one precision remains or the block is 1×1; that block is stored in precisions[end], which is also T_Base.
precisions may contain Float8_E5M2 (q52), BFloat16, Float16, Float32 and Float64. The last, precisions[end], must be Float32 or Float64, which have the range of the scale factors. A block whose largest magnitude exceeds floatmax of its storage type is divided by a Float32 scale factor and stored with it. A block whose largest magnitude exceeds floatmax(Float32) * floatmax of its storage type cannot be represented and throws an ArgumentError. A must be square, non-empty and finite.
Examples
julia> using NextLA.MixedPrecision, LinearAlgebra
julia> A = rand(1024, 1024);
julia> M = FullMixedPrec(A; precisions = DataType[Float32, Float64]); # off-diagonal blocks in Float32
julia> isapprox(reconstruct_matrix(M), A; rtol = 1e-5)
trueNextLA.MixedPrecision.reconstruct_matrix — Method
reconstruct_matrix(A::FullMixedPrec{T_Base}) -> AbstractMatrix{T_Base}Copies the hierarchical mixed-precision matrix back into one dense array with element type T_Base, of the same array type as the stored blocks, with each block's scale applied. For transpose(A) or adjoint(A) of a container it returns the transpose of that dense array.
Examples
julia> using NextLA.MixedPrecision, LinearAlgebra
julia> A = rand(1024, 1024);
julia> M = FullMixedPrec(A; precisions = DataType[Float16, Float32, Float64]);
julia> B = reconstruct_matrix(M);
julia> B isa Matrix{Float64} && isapprox(B, A; rtol = 1e-3)
trueNextLA.MixedPrecision.SymmMixedPrec — Type
SymmMixedPrec{T_Base<:AbstractFloat} <: AbstractMixedPrec{T_Base}A hierarchical, recursive mixed-precision data structure that maps to symmetric matrices. Only one triangle (specified by uplo as 'U' or 'L') is explicitly stored. It recursively partitions the symmetric matrix into two symmetric diagonal sub-blocks (A11, A22) and one corresponding off-diagonal block (OffDiag). A node holds either A11, A22 and OffDiag, or a leaf block Base, never both. Every stored block carries a Float32 scale factor, 1 when it is unscaled. The struct is mutable so that a block an update takes past floatmax of its type can be stored back with a larger scale.
NextLA.MixedPrecision.SymmMixedPrec — Method
SymmMixedPrec(A::AbstractMatrix, uplo::Char; precisions::Vector{DataType}) -> SymmMixedPrecConstructs a SymmMixedPrec representation of the symmetric matrix A.
The matrix is partitioned using a base-2 recursive splitting scheme, as for FullMixedPrec. Each level stores the off-diagonal block of the uplo triangle in precisions[1] and recurses on A11 and A22 with the rest. The recursion stops when one precision remains or the block is 1×1; that block's uplo triangle is stored in precisions[end], which is also T_Base, with zeros in the other half. A is assumed symmetric: only its uplo triangle is read back.
precisions may contain Float8_E5M2 (q52), BFloat16, Float16, Float32 and Float64. The last, precisions[end], must be Float32 or Float64, which have the range of the scale factors. A block whose largest magnitude exceeds floatmax of its storage type is divided by a Float32 scale factor and stored with it. A block whose largest magnitude exceeds floatmax(Float32) * floatmax of its storage type cannot be represented and throws an ArgumentError. A must be square, non-empty and finite, and uplo must be 'L' or 'U'.
Examples
julia> using NextLA.MixedPrecision, LinearAlgebra
julia> A = rand(1024, 1024); S = A + A';
julia> M = SymmMixedPrec(S, 'L'; precisions = DataType[Float32, Float64]);
julia> eltype(M.OffDiag) == Float32 && eltype(M.A11.Base) == Float64
true
julia> isapprox(reconstruct_matrix(M), S; rtol = 1e-5)
trueNextLA.MixedPrecision.reconstruct_matrix — Method
reconstruct_matrix(A::SymmMixedPrec{T_Base}) -> AbstractMatrix{T_Base}Copies the symmetric mixed-precision matrix back into one dense array with element type T_Base, of the same array type as the stored blocks, with both triangles filled from the stored one and each block's scale applied.
NextLA.MixedPrecision.TiledTriMixedPrec — Type
TiledTriMixedPrec{T_Diag, T_OffDiag, M_Diag, M_OffDiag} <: AbstractMixedPrec{T_Diag}A triangular matrix stored as a flat grid of tiles. The tile sizes come from the same recursive splitting as TriMixedPrec, stopped at threshold; diagonal tiles are stored in T_Diag and the off-diagonal tiles of the uplo triangle in T_OffDiag, one tile per grid cell, each with its own Float32 scale factor. The other triangle is not stored and reads as zero.
NextLA.MixedPrecision.TiledTriMixedPrec — Method
TiledTriMixedPrec(A::AbstractMatrix, uplo::Char; precisions::Vector{DataType},
threshold) -> TiledTriMixedPrecConstructs a TiledTriMixedPrec representation of the triangular matrix A.
A is split as TriMixedPrec splits it until a diagonal block has at most threshold rows; those blocks set the tile sizes. As in the other containers, off-diagonal tiles are stored in precisions[1] and diagonal tiles in precisions[end], which is also T_Diag; precisions holds one or two types. Only the uplo triangle of A is read. A tile whose largest magnitude exceeds floatmax of its storage type is divided by a Float32 scale factor and stored with it; a tile beyond floatmax(Float32) * floatmax of its storage type throws an ArgumentError. precisions may contain Float8_E5M2 (q52), BFloat16, Float16, Float32 and Float64; the diagonal tiles' type, precisions[end], must be Float32 or Float64, which have the range of the scale factors. A must be square, non-empty and finite, uplo 'L' or 'U' and threshold at least 1.
Examples
julia> using NextLA.MixedPrecision, LinearAlgebra
julia> A = rand(1024, 1024);
julia> M = TiledTriMixedPrec(A, 'L'; precisions = DataType[Float32, Float64], threshold = 256); # 4 × 4 tiles
julia> isapprox(reconstruct_matrix(M), tril(A); rtol = 1e-5)
trueNextLA.MixedPrecision.TriMixedPrec — Type
TriMixedPrec{T_Base<:AbstractFloat} <: AbstractMixedPrec{T_Base}A hierarchical, recursive mixed-precision data structure that maps to triangular matrices. It partitions the matrix into two recursive diagonal blocks (A11, A22) and a single dense off-diagonal block (OffDiag), placed by its uplo character ('U' for upper, 'L' for lower). A node holds either A11, A22 and OffDiag, or a leaf block Base, never both. Every stored block carries a Float32 scale factor, 1 when it is unscaled. The struct is mutable so that a block an update takes past floatmax of its type can be stored back with a larger scale.
NextLA.MixedPrecision.TriMixedPrec — Method
TriMixedPrec(A::AbstractMatrix, uplo::Char; precisions::Vector{DataType}) -> TriMixedPrecConstructs a TriMixedPrec representation of the triangular matrix A.
The matrix is partitioned using a base-2 recursive splitting scheme, as for FullMixedPrec. Each level stores the off-diagonal block of the uplo triangle in precisions[1] and recurses on A11 and A22 with the rest. The recursion stops when one precision remains or the block is 1×1; that block's uplo triangle is stored in precisions[end], which is also T_Base, with zeros in the other half. Only the uplo triangle of A is read back; the other triangle reads as zero. Every block is a copy, so later changes to A do not show through.
precisions may contain Float8_E5M2 (q52), BFloat16, Float16, Float32 and Float64. The last, precisions[end], must be Float32 or Float64, which have the range of the scale factors. A block whose largest magnitude exceeds floatmax of its storage type is divided by a Float32 scale factor and stored with it. A block whose largest magnitude exceeds floatmax(Float32) * floatmax of its storage type cannot be represented and throws an ArgumentError. A must be square, non-empty and finite, and uplo must be 'L' or 'U'.
Examples
julia> using NextLA.MixedPrecision, LinearAlgebra
julia> A = rand(1024, 1024);
julia> M = TriMixedPrec(A, 'L'; precisions = DataType[Float32, Float64]); # reads tril(A) only
julia> isapprox(reconstruct_matrix(M), tril(A); rtol = 1e-5)
trueNextLA.MixedPrecision.TriMixedPrec — Method
TriMixedPrec(A::SymmMixedPrec{T_Base}) -> TriMixedPrec{T_Base}Builds a TriMixedPrec over the same stored blocks as A, without copying them. The result reads only the uplo triangle, so a SymmMixedPrec factored in place can be used as its triangular factor.
The blocks are shared but each block's scale factor is copied when the result is built. Build it after the last in-place update of A (potrf_recursive!, recsyrk!): an update can store a block with a larger scale, and a result built before it reads the new values with the old scale.
Examples
julia> using NextLA.MixedPrecision, LinearAlgebra
julia> A = rand(1024, 1024); S = A + A';
julia> M = SymmMixedPrec(S, 'L'; precisions = DataType[Float32, Float64]);
julia> reconstruct_matrix(TriMixedPrec(M)) == tril(reconstruct_matrix(M))
trueNextLA.MixedPrecision.reconstruct_matrix — Method
reconstruct_matrix(A::TiledTriMixedPrec{T_Diag}) -> AbstractMatrix{T_Diag}Copies the tiled matrix back into one dense array with element type T_Diag, of the same array type as the stored tiles, with each tile's scale applied and zeros in the other triangle.
NextLA.MixedPrecision.reconstruct_matrix — Method
reconstruct_matrix(A::TriMixedPrec{T_Base}) -> AbstractMatrix{T_Base}Copies the triangular mixed-precision matrix back into one dense array with element type T_Base, of the same array type as the stored blocks, with each block's scale applied and zeros in the other triangle.
Precision selection and quantization
NextLA.MixedPrecision.adaptive_precisions — Function
adaptive_precisions(A, U=DataType[Float32, Float64], n_min=4, epsilon=1e-8;
uplo='L') -> Vector{DataType}Chooses a working precision for each level of the recursive splitting of A, and returns them as a Vector{DataType} that can be passed as precisions to FullMixedPrec, SymmMixedPrec or TriMixedPrec. uplo is 'L' or 'U' for a triangular or symmetric A, of which only that triangle is read, and 'F' for a full one.
A is split as those containers split it. At level k, counted from 0 at the top, E_k is the largest Frobenius norm of the level's off-diagonal blocks – the uplo one, or both for 'F' – relative to the norm of the part of A that is read, both taken in Float64 for a 16- or 8-bit A so they neither overflow nor underflow, and the target unit roundoff is epsilon / (2^((k+1)/2) * E_k). The level gets the coarsest type, no finer than the leaves', whose unit roundoff eps(T)/2 does not exceed the target, or the leaves' type if none does. Splitting stops before any block would have fewer than n_min rows on either side, and the result ends with the leaves' type, so a matrix too small to split returns that type alone.
The leaves get the coarsest type in U that is at least as fine as eltype(A) and has the range of Float32, so a Float32 matrix gets Float32 leaves with the default U; no level uses a type finer than the leaves', since A has no digits for it. When eltype(A) is not a floating-point type or no type in U qualifies, the leaves get the finest type in U.
U may list Float8_E5M2 (q52), BFloat16, Float16, Float32 and Float64, in any order. The part of A that is read must be finite and not zero, A square and non-empty, uplo 'L', 'U' or 'F', U non-empty, n_min at least 1 and epsilon positive; a non-square A throws DimensionMismatch and any other violation an ArgumentError. The result builds a container only when its last type meets the containers' leaf rule, Float32 or Float64, which it does whenever U holds one of them.
Examples
julia> using NextLA.MixedPrecision, LinearAlgebra
julia> A = rand(1024, 1024); S = A * A' + 1024I;
julia> p = adaptive_precisions(S, DataType[Float16, Float32, Float64], 256, 1e-6); # two levels and the leaves
julia> M = SymmMixedPrec(S, 'L'; precisions = p);
julia> length(p) == 3 && p[end] == Float64 && isapprox(reconstruct_matrix(M), S; rtol = 1e-5)
trueNextLA.MixedPrecision.dequantize — Method
dequantize(Q, s, T) -> AbstractMatrix{T}Inverse of quantize: returns Q * s in T, of the same array type as Q.
Examples
julia> using NextLA.MixedPrecision, LinearAlgebra
julia> A = randn(1024, 1024);
julia> Q, s = quantize(A, Float32);
julia> B = dequantize(Q, s, Float64);
julia> B isa Matrix{Float64} && isapprox(B, A; rtol = 1e-6)
trueNextLA.MixedPrecision.quantize — Method
quantize(A, T=Float16) -> (Q, s)Stores A in T with one Float32 scale factor, as the containers store each block: Q = A / s, with s = 1 unless the values of A exceed floatmax(T). A must have a floating-point element type. An empty A gives an empty Q and s = 1. A non-finite A, or one whose values need a scale past floatmax(Float32), throws an ArgumentError.
Examples
julia> using NextLA.MixedPrecision, LinearAlgebra
julia> A = 1.0e6 .* randn(1024, 1024); # past floatmax(Float16) = 65504
julia> Q, s = quantize(A, Float16);
julia> eltype(Q) == Float16 && s > 1 && isapprox(s .* Q, A; rtol = 1e-3)
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.
Building the containers from one precisions list
precisions[1] is used for the top-level off-diagonal blocks, precisions[2] for the next level, and precisions[end] for the diagonal leaves, which is also the element type.
using NextLA, NextLA.MixedPrecision, LinearAlgebra
A = rand(2048, 2048)
precisions = DataType[Float16, Float32, Float64] # level 0, level 1, leaves
F = FullMixedPrec(A; precisions) # all of A
S = SymmMixedPrec(A + A', 'L'; precisions) # lower triangle of a symmetric matrix
T = TriMixedPrec(A, 'U'; precisions) # upper triangle of A
relerr(X, Y) = norm(X - Y) / norm(Y)
eltype(F.A21) == Float16 && eltype(F.A11.A21) == Float32 && eltype(F) == Float64 &&
relerr(reconstruct_matrix(F), A) < 1e-3 &&
relerr(reconstruct_matrix(S), A + A') < 1e-3 &&
relerr(reconstruct_matrix(T), triu(A)) < 1e-3
# output
trueStoring large values in Float16 with quantize
quantize divides a block by a Float32 scale when its values exceed the range of the storage type, and dequantize multiplies it back.
using NextLA, NextLA.MixedPrecision, LinearAlgebra
A = 1e6 .* randn(2048, 2048) # far past floatmax(Float16) = 65504
Q, s = quantize(A, Float16) # A ≈ s * Q, with Q in Float16
B = dequantize(Q, s, Float64)
eltype(Q) == Float16 && s > 1 && norm(B - A) / norm(A) < 1e-3
# output
true