-
-
Notifications
You must be signed in to change notification settings - Fork 75
adaptive mixed precision #506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Rabab53
wants to merge
7
commits into
JuliaParallel:master
Choose a base branch
from
Rabab53:matmul
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4a82e4a
adaptive mixed precision
Rabab53 ed49e36
add mixed_precision.jl under example folder
Rabab53 3cc38a1
incorporating Julian's suggested changes
Rabab53 9fbb981
Merge branch 'JuliaParallel:master' into matmul
Rabab53 751736e
mixed precision cholesky with copy overhead
Rabab53 3201258
more on mixed precision
Rabab53 4520c51
more for gp
Rabab53 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
This example shows how to compute kernel matrix and infer the precision per tile. | ||
It import KernelFunctions and Distances Julia packages | ||
to compute distance matrix based by using Euclidean distance | ||
and then it calls GammaExponentialKernel for each resulted distance | ||
""" | ||
using Revise | ||
using Dagger | ||
using LinearAlgebra | ||
using KernelFunctions | ||
using Distances | ||
T = Float64 | ||
#Define Gamma value and distance matric to be used when computing kernel matrix | ||
k = GammaExponentialKernel(; γ=0.5, metric=Euclidean()); | ||
m, n = 1000, 1000 | ||
#It generates matrix of normally-distributed random numbers | ||
x = randn(m, n); | ||
|
||
#This function will compute the distance between all points of x then it will apply Exponential Kernel | ||
A = kernelmatrix(k, x); | ||
A[diagind(A)] .+= 0.1 | ||
CopyA = copy(A) | ||
#A = copy(CopyA) | ||
#LAPACK.potrf!('L', A) | ||
#Create DA of the kernel matrix | ||
DA = view(A, Blocks(200, 200)); | ||
|
||
MP = Dagger.adapt_precision(DA, 10^-4) | ||
|
||
Dagger.MixedPrecisionChol!(DA, LowerTriangular, MP) | ||
#LinearAlgebra._chol!(DA, LowerTriangular) | ||
#Cholesky!(DA) | ||
A = collect(DA) | ||
|
||
B = rand(m, 1) | ||
Bcopy = copy(B) | ||
|
||
BLAS.trsm!('L', 'L', 'N', 'N', T(1.0), A, B) | ||
BLAS.trsm!('L', 'L', 'T', 'N', T(1.0), A, B) | ||
|
||
norm(Bcopy - CopyA * B) | ||
norm(Bcopy - CopyA * B)/ n/ (norm(Bcopy)-norm(CopyA)* norm(B)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
""" | ||
tile_precision(uplo, global_norm, scalar_factor, tolerance, A) | ||
|
||
it receives tile and it compute required precision per tile | ||
|
||
### Input | ||
- `A` -- tile of size m x n | ||
- `global_norm` -- global norm of the whole matrix | ||
- `scalar_factor` -- scale tile by this value which is the number of tiles | ||
- `tolerance` -- user defined tolerance as required aby the application | ||
|
||
### Output | ||
The required precision of the tile | ||
|
||
""" | ||
function tile_precision(A, global_norm, scalar_factor, tolerance) | ||
|
||
tile_sqr = mapreduce(LinearAlgebra.norm_sqr, +, A) | ||
|
||
tile_norm = sqrt(tile_sqr) | ||
|
||
cal = tile_norm * scalar_factor / global_norm | ||
decision_hp = tile_norm * scalar_factor / global_norm < tolerance / eps(Float16) | ||
decision_sp = tile_norm * scalar_factor / global_norm < tolerance / eps(Float32) | ||
|
||
#We are planning in near future to support fp8 E4M3 and E5M2 | ||
#decision_fp8 = tile_norm * scalar_factor / global_norm < tolerance / 0.0625 | ||
#if decision_fp8 | ||
# return Float8 | ||
if decision_hp | ||
return Float16 | ||
elseif decision_sp | ||
return Float32 | ||
else | ||
return Float64 | ||
end | ||
end | ||
|
||
""" | ||
function adapt_precision( A::UpperTriangular{T,<:DArray{T,2}}, | ||
MP::UpperTriangular{String,<:DArray{String,2}}, tolerance::Float64) where {T} | ||
|
||
it iterates over all tiles and calculates the required precision per tile based on formulation from Nicholas J. Higham | ||
|
||
### Input | ||
- `A` -- Dagger UpperTriangular array of tiles with real values | ||
- `MP` -- Dagger UpperTriangular array to associate precision with each tile | ||
- `tolerance` -- User defined tolerance as required aby the application | ||
|
||
### Output | ||
The Dagger array shows the required precision of each tile | ||
|
||
""" | ||
|
||
function adapt_precision(A::UpperTriangular{T,<:DArray{T,2}}, tolerance::Float64) where {T} | ||
|
||
Ac = parent(A).chunks | ||
mt, nt = size(Ac) | ||
|
||
global_norm = LinearAlgebra.norm2(A) | ||
|
||
MP = fill(T, mt, nt) | ||
DMP = view(MP, Blocks(1, 1)) | ||
MPc = parent(DMP).chunks | ||
|
||
for n in range(1, nt) | ||
for m in range(1, n) | ||
if m == n | ||
MPc[m, n] = Dagger.@spawn tile_precision( | ||
UpperTriangular(Ac[m, n]), | ||
global_norm, | ||
max(mt, nt), | ||
tolerance) | ||
else | ||
MPc[m, n] = Dagger.@spawn tile_precision( | ||
Ac[m, n], | ||
global_norm, | ||
max(mt, nt), | ||
tolerance) | ||
end | ||
|
||
end | ||
end | ||
|
||
return UpperTriangular(collect(DMP)) | ||
end | ||
|
||
""" | ||
adapt_precision( A::LowerTriangular{T,<:DArray{T,2}}, | ||
MP::LowerTriangular{String,<:DArray{String,2}}, tolerance::Float64) where {T} | ||
|
||
it iterates over all tiles and calculates the required precision per tile based on formulation from Nicholas J. Higham | ||
|
||
### Input | ||
- `A` -- Dagger LowerTriangular array of tiles with real values | ||
- `MP` -- Dagger LowerTriangular array to associate precision with each tile | ||
- `tolerance` -- User defined tolerance as required aby the application | ||
|
||
### Output | ||
The Dagger array shows the required precision of each tile | ||
|
||
""" | ||
|
||
function adapt_precision(A::LowerTriangular{T,<:DArray{T,2}}, tolerance::T) where {T} | ||
|
||
Ac = parent(A).chunks | ||
mt, nt = size(Ac) | ||
|
||
global_norm = LinearAlgebra.norm2(A) | ||
|
||
MP = fill(T, mt, nt) | ||
DMP = view(MP, Blocks(1, 1)) | ||
MPc = parent(DMP).chunks | ||
|
||
|
||
for m in range(1, mt) | ||
for n in range(1, m) | ||
if m == n | ||
MPc[m, n] = Dagger.@spawn tile_precision( | ||
LowerTriangular(Ac[m, n]), | ||
global_norm, | ||
max(mt, nt), | ||
tolerance) | ||
else | ||
MPc[m, n] = Dagger.@spawn tile_precision( | ||
Ac[m, n], | ||
global_norm, | ||
max(mt, nt), | ||
tolerance) | ||
end | ||
|
||
end | ||
end | ||
|
||
return LowerTriangular(collect(DMP)) | ||
end | ||
|
||
""" | ||
adapt_precision(A::DArray{T,2}, MP::DArray{String,2}, tolerance::T) where {T} | ||
|
||
it iterates over all tiles and calculates the required precision per tile based on formulation from Nicholas J. Higham | ||
|
||
### Input | ||
- `A` -- Dagger array of tiles with real values | ||
- `MP` -- Dagger array to associate precision with each tile | ||
- `tolerance` -- User defined tolerance as required aby the application | ||
|
||
### Output | ||
The Dagger array shows the required precision of each tile | ||
|
||
""" | ||
|
||
function adapt_precision(A::DArray{T,2}, tolerance::T) where {T} | ||
|
||
Ac = parent(A).chunks | ||
mt, nt = size(Ac) | ||
|
||
global_norm = LinearAlgebra.norm2(A) | ||
|
||
MP = fill(T, mt, nt) | ||
DMP = view(MP, Blocks(1, 1)) | ||
MPc = DMP.chunks | ||
|
||
|
||
for m in range(1, mt) | ||
for n in range(1, nt) | ||
if m!=n | ||
MPc[m, n] = | ||
Dagger.@spawn tile_precision( | ||
Ac[m, n], | ||
global_norm, | ||
max(mt, nt), | ||
tolerance) | ||
end | ||
end | ||
end | ||
|
||
return collect(DMP) | ||
end | ||
|
||
|
||
function tile_precision_and_convert(A, MP, global_norm, scalar_factor, tolerance) | ||
|
||
tile_sqr = mapreduce(LinearAlgebra.norm_sqr, +, A) | ||
|
||
tile_norm = sqrt(tile_sqr) | ||
|
||
cal = tile_norm * scalar_factor / global_norm | ||
decision_hp = tile_norm * scalar_factor / global_norm < tolerance / eps(Float16) | ||
decision_sp = tile_norm * scalar_factor / global_norm < tolerance / eps(Float32) | ||
|
||
#We are planning in near future to support fp8 E4M3 and E5M2 | ||
#decision_fp8 = tile_norm * scalar_factor / global_norm < tolerance / 0.0625 | ||
#if decision_fp8 | ||
# return Float8 | ||
if decision_hp | ||
return Float16 | ||
elseif decision_sp | ||
return Float32 | ||
else | ||
return Float64 | ||
end | ||
end | ||
|
||
|
||
function adapt_precision_and_convert(A::DArray{T,2}, tolerance::T) where {T} | ||
|
||
Ac = parent(A).chunks | ||
mt, nt = size(Ac) | ||
|
||
global_norm = LinearAlgebra.norm2(A) | ||
|
||
MP = fill(T, mt, nt) | ||
DMP = view(MP, Blocks(1, 1)) | ||
MPc = DMP.chunks | ||
|
||
|
||
for m in range(1, mt) | ||
for n in range(1, nt) | ||
Dagger.@spawn tile_precision( | ||
InOut(Ac[m, n]), | ||
Out(MPc[m, n]), | ||
global_norm, | ||
max(mt, nt), | ||
tolerance) | ||
end | ||
end | ||
|
||
return collect(DMP) | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we have some inline comments about what is happening here, and what this example does, in common terms?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved