Skip to content

Commit

Permalink
Don't use ref when unnnecessary for gradient sugar (#2156)
Browse files Browse the repository at this point in the history
* Don't use ref when unnnecessary for gradient sugar

* fix

* fix

* fix

* fix
  • Loading branch information
wsmoses authored Dec 2, 2024
1 parent 4c6e7cc commit 3ad827f
Showing 1 changed file with 59 additions and 49 deletions.
108 changes: 59 additions & 49 deletions src/sugar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,15 @@ grad = gradient(ReverseWithPrimal, mul, [2.0], Const([3.0]))
```
"""
# TODO eventually add an invalidation edge here from inactive_type
@generated function gradient(
rm::ReverseMode{ReturnPrimal,RuntimeActivity,ABI,Holomorphic,ErrIfFuncWritten},
f::F,
x::ty_0,
args::Vararg{Any,N},
) where {F,ty_0,ReturnPrimal,RuntimeActivity,ABI,Holomorphic,ErrIfFuncWritten,N}
toemit = Expr[quote
act_0 =
!(x isa Enzyme.Const) &&
Compiler.active_reg_inner(Core.Typeof(x), (), nothing, Val(true)) ==
Compiler.ActiveState #=justActive=#
end]
rargs = Union{Symbol,Expr}[:x]
gentys = Type[x]
acts = Symbol[Symbol("act_0")]

for i = 1:N
Expand All @@ -276,55 +272,69 @@ grad = gradient(ReverseWithPrimal, mul, [2.0], Const([3.0]))
push!(rargs, argidx)
sym = Symbol("act_$i")
push!(acts, sym)
push!(
toemit,
quote
$sym =
!($argidx isa Enzyme.Const) &&
Compiler.active_reg_inner(
Core.Typeof($argidx),
(),
nothing,
Val(true),
) == Compiler.ActiveState #=justActive=#
end,
)
push!(gentys, args[i])
end

toemit = Expr[]
states = Compiler.ActivityState[]

for (argidx, act, genty) in zip(rargs, acts, gentys)
if genty <: Enzyme.Const
push!(
toemit,
quote
$act = false
end
)
push!(states, Compiler.AnyState)
else
state = Compiler.active_reg_inner(genty, (), nothing)
push!(states, state)
end
end

idx = 0
shadows = Symbol[]
enz_args = Expr[]
resargs = Expr[]
for (arg, act) in zip(rargs, acts)
shad = Symbol("shad_$idx")
push!(shadows, shad)
push!(toemit, quote
$shad = if $arg isa Enzyme.Const
nothing
elseif $act
Ref(make_zero($arg))
else
make_zero($arg)
end
end)
push!(enz_args, quote
if $arg isa Enzyme.Const
$arg
elseif $act
enz_args = Union{Expr,Symbol}[]
resargs = Union{Expr,Symbol}[]
for (i, (arg, act, state, genty)) in enumerate(zip(rargs, acts, states, gentys))
shad = Symbol("shad_$i")
if genty <: Enzyme.Const
push!(enz_args, arg)
push!(resargs, :nothing)
elseif state == Compiler.MixedState
push!(toemit, quote
$shad = Ref(make_zero($arg))
end)
push!(enz_args, quote
MixedDuplicated($arg, $shad)
else
Duplicated($arg, $shad)
end
end)
push!(resargs, quote
if $arg isa Enzyme.Const
nothing
elseif $act
end)
push!(resargs, quote
$shad[]
else
end)
elseif state == Compiler.DupState
push!(toemit, quote
$shad = make_zero($arg)
end)
push!(enz_args, quote
Duplicated($arg, $shad)
end)
push!(resargs, quote
$shad
end
end)
end)
elseif state == Compiler.ActiveState
push!(enz_args, quote
Active($arg)
end)
push!(resargs, quote
res[1][$i]
end)
else
@assert state == Compiler.AnyState
push!(enz_args, quote
Const($arg)
end)
push!(resargs, :nothing)
end
idx += 1
end
push!(toemit, quote
Expand Down

2 comments on commit 3ad827f

@wsmoses
Copy link
Member Author

@wsmoses wsmoses commented on 3ad827f Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/120510

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.13.18 -m "<description of version>" 3ad827f69299299b92a1448f52dd746a65eb5db7
git push origin v0.13.18

Please sign in to comment.