-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Enzyme Forward mode custom rule.
Co-authored-by: Seth Axen <seth@sethaxen.com> Co-authored-by: "William S. Moses" <gh@wsmoses.com>
- Loading branch information
1 parent
f5100a1
commit b2835c3
Showing
5 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,49 @@ | ||
module CUDAEnzymeCoreExt | ||
|
||
using CUDA | ||
|
||
if isdefined(Base, :get_extension) | ||
using EnzymeCore | ||
using EnzymeCore.EnzymeRules | ||
else | ||
using ..EnzymeCore | ||
using ..EnzymeCore.EnzymeRules | ||
end | ||
|
||
function metaf(fn, args::Vararg{Any, N}) where N | ||
EnzymeCore.autodiff_deferred(Forward, fn, Const, args...) | ||
nothing | ||
end | ||
|
||
function EnzymeCore.EnzymeRules.forward(ofn::Const{typeof(cufunction)}, | ||
::Type{<:Duplicated}, f::Const{F}, | ||
tt::Const{TT}; kwargs...) where {F,TT} | ||
res = ofn.val(f.val, tt.val; kwargs...) | ||
return Duplicated(res, res) | ||
end | ||
|
||
function EnzymeCore.EnzymeRules.forward(ofn::Const{typeof(cufunction)}, | ||
::Type{BatchDuplicated{T,N}}, f::Const{F}, | ||
tt::Const{TT}; kwargs...) where {F,TT,T,N} | ||
res = ofn.val(f.val, tt.val; kwargs...) | ||
return BatchDuplicated(res, ntuple(Val(N)) do _ | ||
res | ||
end) | ||
end | ||
|
||
function EnzymeCore.EnzymeRules.forward(ofn::EnzymeCore.Annotation{CUDA.HostKernel{F,TT}}, | ||
::Type{Const{Nothing}}, args...; | ||
kwargs...) where {F,TT} | ||
|
||
GC.@preserve args begin | ||
args = ((cudaconvert(a) for a in args)...,) | ||
T2 = (F, (typeof(a) for a in args)...) | ||
TT2 = Tuple{T2...} | ||
cuf = cufunction(metaf, TT2) | ||
res = cuf(ofn.val.f, args...; kwargs...) | ||
end | ||
|
||
return nothing | ||
end | ||
|
||
end |
This file contains 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 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 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,31 @@ | ||
using Enzyme | ||
|
||
function square_kernel!(x) | ||
i = threadIdx().x | ||
x[i] *= x[i] | ||
sync_threads() | ||
return nothing | ||
end | ||
|
||
# basic squaring on GPU | ||
function square!(x) | ||
@cuda blocks = 1 threads = length(x) square_kernel!(x) | ||
return nothing | ||
end | ||
|
||
A = CUDA.rand(64) | ||
dA = CUDA.ones(64) | ||
A .= (1:1:64) | ||
dA .= 1 | ||
Enzyme.autodiff(Forward, square!, Duplicated(A, dA)) | ||
@test all(dA .≈ (2:2:128)) | ||
|
||
A = CUDA.rand(32) | ||
dA = CUDA.ones(32) | ||
dA2 = CUDA.ones(32) | ||
A .= (1:1:32) | ||
dA .= 1 | ||
dA2 .= 3 | ||
Enzyme.autodiff(Forward, square!, BatchDuplicated(A, (dA, dA2))) | ||
@test all(dA .≈ (2:2:64)) | ||
@test all(dA2 .≈ 3*(2:2:64)) |