-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from JuliaGPU/jps/forwarddiff
Add ForwardDiff integrations
- Loading branch information
Showing
9 changed files
with
307 additions
and
54 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
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,112 @@ | ||
# broadcasting | ||
|
||
using Base.Broadcast: BroadcastStyle, Broadcasted | ||
|
||
struct ROCArrayStyle{N} <: AbstractGPUArrayStyle{N} end | ||
ROCArrayStyle(::Val{N}) where N = ROCArrayStyle{N}() | ||
ROCArrayStyle{M}(::Val{N}) where {N,M} = ROCArrayStyle{N}() | ||
|
||
BroadcastStyle(::Type{ROCArray{T,N}}) where {T,N} = ROCArrayStyle{N}() | ||
|
||
Base.similar(bc::Broadcasted{ROCArrayStyle{N}}, ::Type{T}) where {N,T} = | ||
similar(ROCArray{T}, axes(bc)) | ||
|
||
Base.similar(bc::Broadcasted{ROCArrayStyle{N}}, ::Type{T}, dims) where {N,T} = | ||
ROCArray{T}(undef, dims) | ||
|
||
|
||
## replace base functions with libdevice alternatives | ||
|
||
rocfunc(f) = f | ||
rocfunc(::Type{T}) where T = (x...) -> T(x...) # broadcasting type ctors isn't GPU compatible | ||
|
||
Broadcast.broadcasted(::ROCArrayStyle{N}, f, args...) where {N} = | ||
Broadcasted{ROCArrayStyle{N}}(rocfunc(f), args, nothing) | ||
|
||
const device_intrinsics = :[ | ||
cos, cospi, sin, sinpi, tan, acos, asin, atan, | ||
cosh, sinh, tanh, acosh, asinh, atanh, angle, | ||
log, log10, log1p, log2, logb, ilogb, | ||
exp, exp2, exp10, expm1, ldexp, | ||
erf, erfinv, erfc, erfcinv, erfcx, | ||
brev, clz, ffs, byte_perm, popc, | ||
isfinite, isinf, isnan, nearbyint, | ||
nextafter, signbit, copysign, abs, | ||
sqrt, rsqrt, cbrt, rcbrt, pow, | ||
ceil, floor, saturate, | ||
lgamma, tgamma, | ||
j0, j1, jn, y0, y1, yn, | ||
normcdf, normcdfinv, hypot, | ||
fma, sad, dim, mul24, mul64hi, hadd, rhadd, scalbn].args | ||
|
||
for f in device_intrinsics | ||
isdefined(Base, f) || continue | ||
@eval rocfunc(::typeof(Base.$f)) = $f | ||
end | ||
|
||
# broadcast ^ | ||
|
||
rocliteral_pow(::typeof(^), x::T, ::Val{0}) where {T<:Real} = one(x) | ||
rocliteral_pow(::typeof(^), x::T, ::Val{1}) where {T<:Real} = x | ||
rocliteral_pow(::typeof(^), x::T, ::Val{2}) where {T<:Real} = x * x | ||
rocliteral_pow(::typeof(^), x::T, ::Val{3}) where {T<:Real} = x * x * x | ||
rocliteral_pow(::typeof(^), x::T, ::Val{p}) where {T<:Real,p} = pow(x, Int32(p)) | ||
|
||
rocfunc(::typeof(Base.literal_pow)) = rocliteral_pow | ||
rocfunc(::typeof(Base.:(^))) = pow | ||
|
||
using MacroTools | ||
|
||
const _rocfuncs = [copy(device_intrinsics); :^] | ||
rocfuncs() = (global _rocfuncs; _rocfuncs) | ||
|
||
_rocint(x::Int) = Int32(x) | ||
_rocint(x::Expr) = x.head == :call && x.args[1] == :Int32 && x.args[2] isa Int ? Int32(x.args[2]) : x | ||
_rocint(x) = x | ||
|
||
function _rocpowliteral(x::Expr) | ||
if x.head == :call && x.args[1] == :(AMDGPU.rocfunc(^)) && x.args[3] isa Int32 | ||
num = x.args[3] | ||
if 0 <= num <= 3 | ||
sym = gensym(:x) | ||
new_x = Expr(:block, :($sym = $(x.args[2]))) | ||
|
||
if iszero(num) | ||
push!(new_x.args, :(one($sym))) | ||
else | ||
unroll = Expr(:call, :*) | ||
for x = one(num):num | ||
push!(unroll.args, sym) | ||
end | ||
push!(new_x.args, unroll) | ||
end | ||
|
||
x = new_x | ||
end | ||
end | ||
x | ||
end | ||
_rocpowliteral(x) = x | ||
|
||
function replace_device(ex) | ||
global _rocfuncs | ||
MacroTools.postwalk(ex) do x | ||
x = x in _rocfuncs ? :(AMDGPU.rocfunc($x)) : x | ||
x = _rocint(x) | ||
x = _rocpowliteral(x) | ||
x | ||
end | ||
end | ||
|
||
macro rocfunc(ex) | ||
global _rocfuncs | ||
def = MacroTools.splitdef(ex) | ||
f = def[:name] | ||
def[:name] = Symbol(:cu, f) | ||
def[:body] = replace_device(def[:body]) | ||
push!(_rocfuncs, f) | ||
quote | ||
$(esc(MacroTools.combinedef(def))) | ||
AMDGPU.rocfunc(::typeof($(esc(f)))) = $(esc(def[:name])) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# ForwardDiff integration | ||
|
||
byhand = [:exp2, :log2, :exp10, :log10, :abs] | ||
|
||
for f in device_intrinsics | ||
if haskey(ForwardDiff.DiffRules.DEFINED_DIFFRULES, (:Base,f,1)) | ||
f ∈ byhand && continue | ||
diffrule = ForwardDiff.DiffRules.DEFINED_DIFFRULES[(:Base,f,1)] | ||
ForwardDiff.DiffRules.DEFINED_DIFFRULES[(:AMDGPU,f,1)] = | ||
(args...) -> replace_device(diffrule(args...)) | ||
eval(ForwardDiff.unary_dual_definition(:AMDGPU, f)) | ||
end | ||
end | ||
|
||
# byhand: exp2 | ||
ForwardDiff.DiffRules.DEFINED_DIFFRULES[(:AMDGPU, :exp2, 1)] = x -> | ||
:((AMDGPU.rocfunc(exp2))(x) * (AMDGPU.rocfunc(log))(oftype(x, 2))) | ||
eval(ForwardDiff.unary_dual_definition(:AMDGPU, :exp2)) | ||
|
||
# byhand: log2 | ||
ForwardDiff.DiffRules.DEFINED_DIFFRULES[(:AMDGPU, :log2, 1)] = x -> | ||
:(inv(x) / (AMDGPU.rocfunc(log))(oftype(x, 2))) | ||
eval(ForwardDiff.unary_dual_definition(:AMDGPU, :log2)) | ||
|
||
# byhand: exp10 | ||
ForwardDiff.DiffRules.DEFINED_DIFFRULES[(:AMDGPU, :exp10, 1)] = x -> | ||
:((AMDGPU.rocfunc(exp10))(x) * (AMDGPU.rocfunc(log))(oftype(x, 10))) | ||
eval(ForwardDiff.unary_dual_definition(:AMDGPU, :exp10)) | ||
|
||
# byhand: log10 | ||
ForwardDiff.DiffRules.DEFINED_DIFFRULES[(:AMDGPU, :log10, 1)] = x -> | ||
:(inv(x) / (AMDGPU.rocfunc(log))(oftype(x, 10))) | ||
eval(ForwardDiff.unary_dual_definition(:AMDGPU, :log10)) | ||
|
||
# byhand: abs | ||
ForwardDiff.DiffRules.DEFINED_DIFFRULES[(:AMDGPU, :abs, 1)] = x -> | ||
:(signbit(x) ? -one(x) : one(x)) | ||
eval(ForwardDiff.unary_dual_definition(:AMDGPU, :abs)) | ||
|
||
|
||
ForwardDiff.DiffRules.DEFINED_DIFFRULES[(:AMDGPU, :pow, 2)] = (x, y) -> | ||
replace_device.(ForwardDiff.DiffRules.DEFINED_DIFFRULES[(:Base, :^, 2)](x, y)) | ||
|
||
@eval begin | ||
ForwardDiff.@define_binary_dual_op( | ||
AMDGPU.pow, | ||
begin | ||
vx = ForwardDiff.value(x) | ||
vy = ForwardDiff.value(y) | ||
expv = (AMDGPU.pow)(vx, vy) | ||
|
||
powval = vy * AMDGPU.pow(vx , vy - Int32(1)) | ||
|
||
py = ForwardDiff.partials(y) | ||
px = ForwardDiff.partials(x) | ||
|
||
cond = all(py.values) do x | ||
x == zero(x) | ||
end | ||
|
||
if cond | ||
logval = one(expv) | ||
else | ||
logval = expv * AMDGPU.log(vx) | ||
end | ||
|
||
new_partials = powval * px + logval * py | ||
return ForwardDiff.Dual{Txy}(expv, new_partials) | ||
end, | ||
begin | ||
v = ForwardDiff.value(x) | ||
expv = (AMDGPU.pow)(v, y) | ||
if y == zero(y) | ||
new_partials = zero(ForwardDiff.partials(x)) | ||
else | ||
new_partials = ForwardDiff.partials(x) * y * (AMDGPU.pow)(v, y - Int32(1)) | ||
end | ||
return ForwardDiff.Dual{Tx}(expv, new_partials) | ||
end, | ||
begin | ||
v = ForwardDiff.value(y) | ||
expv = (AMDGPU.pow)(x, v) | ||
deriv = expv*AMDGPU.log(x) | ||
return ForwardDiff.Dual{Ty}(expv, deriv * ForwardDiff.partials(y)) | ||
end | ||
) | ||
end |
Oops, something went wrong.