Skip to content

Commit

Permalink
Fixup more than simple jacobian (#1712)
Browse files Browse the repository at this point in the history
* Fixup more than simple jacobian

* remove prints

* work around stack

* ease jacobian

* fix

* fix

* fix

* fix

* fix

* fix
  • Loading branch information
wsmoses authored Aug 8, 2024
1 parent 356ef34 commit 2b681d5
Show file tree
Hide file tree
Showing 3 changed files with 362 additions and 45 deletions.
277 changes: 232 additions & 45 deletions src/Enzyme.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,10 @@ end
end
end

@inline function onehot(x::AbstractFloat)
return (one(x),)
end

"""
gradient(::ReverseMode, f, x)
Expand Down Expand Up @@ -1126,10 +1130,15 @@ grad = gradient(Forward, f, [2.0, 3.0])
```
"""
@inline function gradient(::ForwardMode, f, x; shadow=onehot(x))
if length(x) == 0
if length(shadow) == 0
return ()
end
values(only(autodiff(Forward, f, BatchDuplicatedNoNeed, BatchDuplicated(x, shadow))))
res = values(only(autodiff(Forward, f, BatchDuplicatedNoNeed, BatchDuplicated(x, shadow))))
if x isa AbstractFloat
res[1]
else
res
end
end

@inline function chunkedonehot(x, ::Val{chunk}) where chunk
Expand All @@ -1141,6 +1150,10 @@ end
end
end

@inline function chunkedonehot(x::AbstractFloat, ::Val{chunk}) where chunk
return ((one(x),),)
end

@inline tupleconcat(x) = x
@inline tupleconcat(x, y) = (x..., y...)
@inline tupleconcat(x, y, z...) = (x..., tupleconcat(y, z...)...)
Expand Down Expand Up @@ -1171,95 +1184,194 @@ grad = gradient(Forward, f, [2.0, 3.0], Val(2))
tmp = ntuple(length(shadow)) do i
values(autodiff(Forward, f, BatchDuplicatedNoNeed, BatchDuplicated(x, shadow[i]))[1])
end
tupleconcat(tmp...)
res = tupleconcat(tmp...)
if x isa AbstractFloat
res[1]
else
res
end
end

@inline function gradient(::ForwardMode, f::F, x::X, ::Val{1}; shadow=onehot(x)) where {F, X}
ntuple(length(shadow)) do i
res = ntuple(length(shadow)) do i
autodiff(Forward, f, DuplicatedNoNeed, Duplicated(x, shadow[i]))[1]
end
if x isa AbstractFloat
res[1]
else
res
end
end

"""
jacobian(::ForwardMode, f, x; shadow=onehot(x))
jacobian(::ForwardMode, f, x, ::Val{chunk}; shadow=onehot(x))
Compute the jacobian of an array-input function `f` using (potentially vector)
forward mode. This is a simple rename of the [`gradient`](@ref) function,
and all relevant arguments apply here.
Compute the jacobian of an array or scalar-input function `f` using (potentially vector)
forward mode. All relevant arguments of the forward-mode [`gradient`](@ref) function
apply here.
Example:
```jldoctest
f(x) = [x[1]*x[2], x[2]]
f(x) = [ x[1] * x[2], x[2] + x[3] ]
grad = jacobian(Forward, f, [2.0, 3.0])
grad = jacobian(Forward, f, [2.0, 3.0, 4.0])
# output
2 Matrix{Float64}:
3.0 2.0
0.0 1.0
3 Matrix{Float64}:
3.0 2.0 0.0
0.0 1.0 1.0
```
For functions which return an AbstractArray, this function will return an array
whose shape is `(size(output)..., size(input)...)`
For functions who return other types, this function will retun an array or tuple
of shape `size(input)` of values of the output type.
"""
@inline function jacobian(::ForwardMode, f, x; shadow=onehot(x))
cols = if length(x) == 0
return ()
cols = if length(shadow) == 0
()
else
values(only(autodiff(Forward, f, BatchDuplicatedNoNeed, BatchDuplicated(x, shadow))))
end
reduce(hcat, cols)
if x isa AbstractFloat
cols[1]
elseif length(cols) > 0 && cols[1] isa AbstractArray
inshape = size(x)
outshape = size(cols[1])
# st : outshape x total inputs
st = @static if VERSION >= v"1.9"
Base.stack(cols)
else
reshape(cat(cols..., dims=length(outshape)), (outshape..., inshape...))
end

st3 = if length(inshape) <= 1 || VERSION < v"1.9"
st
else
reshape(st, (outshape..., inshape...))
end

st3
elseif x isa AbstractArray
inshape = size(x)
reshape(collect(cols), inshape)
else
cols
end
end

@inline function jacobian(::ForwardMode, f::F, x::X, ::Val{chunk}; shadow=chunkedonehot(x, Val(chunk))) where {F, X, chunk}
if chunk == 0
throw(ErrorException("Cannot differentiate with a batch size of 0"))
end
tmp = ntuple(length(shadow)) do i
Base.@_inline_meta
values(autodiff(Forward, f, BatchDuplicatedNoNeed, BatchDuplicated(x, shadow[i]))[1])
end
cols = tupleconcat(tmp...)
reduce(hcat, cols)
if x isa AbstractFloat
cols[1]
elseif length(cols) > 0 && cols[1] isa AbstractArray
inshape = size(x)
outshape = size(cols[1])
# st : outshape x total inputs
st = @static if VERSION >= v"1.9"
Base.stack(cols)
else
reshape(cat(cols..., dims=length(outshape)), (outshape..., inshape...))
end

st3 = if length(inshape) <= 1 || VERSION < v"1.9"
st
else
reshape(st, (outshape..., inshape...))
end

st3
elseif x isa AbstractArray
inshape = size(x)
reshape(collect(cols), inshape)
else
cols
end
end

@inline function jacobian(::ForwardMode, f::F, x::X, ::Val{1}; shadow=onehot(x)) where {F,X}
cols = ntuple(length(shadow)) do i
Base.@_inline_meta
autodiff(Forward, f, DuplicatedNoNeed, Duplicated(x, shadow[i]))[1]
end
reduce(hcat, cols)
if x isa AbstractFloat
cols[1]
elseif length(cols) > 0 && cols[1] isa AbstractArray
inshape = size(x)
outshape = size(cols[1])
# st : outshape x total inputs
st = @static if VERSION >= v"1.9"
Base.stack(cols)
else
reshape(cat(cols..., dims=length(outshape)), (outshape..., inshape...))
end

st3 = if length(inshape) <= 1 || VERSION < v"1.9"
st
else
reshape(st, (outshape..., inshape...))
end

st3
elseif x isa AbstractArray
inshape = size(x)
reshape(collect(cols), inshape)
else
cols
end
end

"""
jacobian(::ReverseMode, f, x, ::Val{num_outs}, ::Val{chunk})
jacobian(::ReverseMode, f, x, ::Val{num_outs}, ::Val{chunk}=Val(1))
jacobian(::ReverseMode, f, x)
Compute the jacobian of an array-input function `f` using (potentially vector)
Compute the jacobian of an array-output function `f` using (potentially vector)
reverse mode. The `chunk` argument denotes the chunk size to use and `num_outs`
denotes the number of outputs `f` will return in an array.
Example:
```jldoctest
f(x) = [x[1]*x[2], x[2]]
f(x) = [ x[1] * x[2], x[2] + x[3] ]
grad = jacobian(Reverse, f, [2.0, 3.0], Val(2))
grad = jacobian(Reverse, f, [2.0, 3.0, 4.0], Val(2))
# output
2×2 Matrix{Float64}:
3.0 2.0
0.0 1.0
2×3 transpose(::Matrix{Float64}) with eltype Float64:
3.0 2.0 0.0
0.0 1.0 1.0
```
For functions which return an AbstractArray, this function will return an array
whose shape is `(size(output)..., size(input)...)`
For functions who return other types, this function will retun an array or tuple
of shape `size(output)` of values of the input type.
```
"""
@inline function jacobian(::ReverseMode{ReturnPrimal,RABI, ErrIfFuncWritten}, f::F, x::X, n_outs::Val{n_out_val}, ::Val{chunk}) where {F, X, chunk, n_out_val, ReturnPrimal, RABI<:ABI, ErrIfFuncWritten}
@assert !ReturnPrimal
@inline function jacobian(::ReverseMode{#=ReturnPrimal=#false,RABI, ErrIfFuncWritten}, f::F, x::X, n_outs::Val{n_out_val}, ::Val{chunk}) where {F, X, chunk, n_out_val, RABI<:ABI, ErrIfFuncWritten}
num = ((n_out_val + chunk - 1) ÷ chunk)

if chunk == 0
throw(ErrorException("Cannot differentiate with a batch size of 0"))
end

tt′ = Tuple{BatchDuplicated{Core.Typeof(x), chunk}}
tt = Tuple{Core.Typeof(x)}
XT = Core.Typeof(x)
MD = Compiler.active_reg_inner(XT, #=seen=#(), #=world=#nothing, #=justActive=#Val(true)) == Compiler.ActiveState
tt′ = MD ? Tuple{BatchMixedDuplicated{XT, chunk}} : Tuple{BatchDuplicated{XT, chunk}}
tt = Tuple{XT}
rt = Core.Compiler.return_type(f, tt)
ModifiedBetween = Val((false, false))
FA = Const{Core.Typeof(f)}
Expand All @@ -1281,28 +1393,59 @@ grad = jacobian(Reverse, f, [2.0, 3.0], Val(2))

tmp = ntuple(num) do i
Base.@_inline_meta
dx = ntuple(i == num ? last_size : chunk) do idx
dx = ntuple(Val(i == num ? last_size : chunk)) do idx
Base.@_inline_meta
zero(x)
z = make_zero(x)
MD ? Ref(z) : z
end
res = (i == num ? primal2 : primal)(Const(f), BatchDuplicated(x, dx))
res = (i == num ? primal2 : primal)(Const(f), MD ? BatchMixedDuplicated(x, dx) : BatchDuplicated(x, dx))
tape = res[1]
j = 0
for shadow in res[3]
j += 1
@inbounds shadow[(i-1)*chunk+j] += Compiler.default_adjoint(eltype(typeof(shadow)))
end
(i == num ? adjoint2 : adjoint)(Const(f), BatchDuplicated(x, dx), tape)
return dx
(i == num ? adjoint2 : adjoint)(Const(f), MD ? BatchMixedDuplicated(x, dx) : BatchDuplicated(x, dx), tape)
return MD ? (ntuple(Val(i == num ? last_size : chunk)) do idx
Base.@_inline_meta
dx[idx][]
end) : dx, (i == 1 ? size(res[3][1]) : nothing)
end
rows = tupleconcat(map(first, tmp)...)
outshape = tmp[1][2]
if x isa AbstractArray
inshape = size(x)

st = @static if VERSION >= v"1.9"
Base.stack(rows)
else
reshape(cat(rows..., dims=length(inshape)), (inshape..., outshape...))
end

st2 = if length(outshape) == 1 || VERSION < v"1.9"
st
else
reshape(st, (inshape..., outshape...))
end

st3 = if length(outshape) == 1 && length(inshape) == 1
transpose(st2)
else
transp = ( ((length(inshape)+1):(length(inshape)+length(outshape)))... , (1:length(inshape))... )
PermutedDimsArray(st2, transp)
end

st3
else
reshape(collect(rows), outshape)
end
rows = tupleconcat(tmp...)
mapreduce(LinearAlgebra.adjoint, vcat, rows)
end

@inline function jacobian(::ReverseMode{ReturnPrimal,RABI, ErrIfFuncWritten}, f::F, x::X, n_outs::Val{n_out_val}, ::Val{1} = Val(1)) where {F, X, n_out_val,ReturnPrimal,RABI<:ABI, ErrIfFuncWritten}
@assert !ReturnPrimal
tt′ = Tuple{Duplicated{Core.Typeof(x)}}
tt = Tuple{Core.Typeof(x)}
@inline function jacobian(::ReverseMode{#=ReturnPrimal=#false,RABI, ErrIfFuncWritten}, f::F, x::X, n_outs::Val{n_out_val}, ::Val{1} = Val(1)) where {F, X, n_out_val,RABI<:ABI, ErrIfFuncWritten}
XT = Core.Typeof(x)
MD = Compiler.active_reg_inner(XT, #=seen=#(), #=world=#nothing, #=justActive=#Val(true)) == Compiler.ActiveState
tt′ = MD ? Tuple{MixedDuplicated{XT}} : Tuple{Duplicated{XT}}
tt = Tuple{XT}
rt = Core.Compiler.return_type(f, tt)
ModifiedBetween = Val((false, false))
FA = Const{Core.Typeof(f)}
Expand All @@ -1312,16 +1455,60 @@ end
Val(codegen_world_age(Core.Typeof(f), tt))
end
primal, adjoint = Enzyme.Compiler.thunk(opt_mi, FA, DuplicatedNoNeed{rt}, tt′, #=Split=# Val(API.DEM_ReverseModeGradient), #=width=#Val(1), ModifiedBetween, #=ReturnPrimal=#Val(false), #=ShadowInit=#Val(false), RABI, Val(ErrIfFuncWritten))
rows = ntuple(n_outs) do i
tmp = ntuple(n_outs) do i
Base.@_inline_meta
dx = zero(x)
res = primal(Const(f), Duplicated(x, dx))
z = make_zero(x)
dx = MD ? Ref(z) : z
res = primal(Const(f), MD ? MixedDuplicated(x, dx) : Duplicated(x, dx))
tape = res[1]
@inbounds res[3][i] += Compiler.default_adjoint(eltype(typeof(res[3])))
adjoint(Const(f), Duplicated(x, dx), tape)
return dx
adjoint(Const(f), MD ? MixedDuplicated(x, dx) : Duplicated(x, dx), tape)
return MD ? dx[] : dx, (i == 1 ? size(res[3]) : nothing)
end
rows = map(first, tmp)
outshape = tmp[1][2]
if x isa AbstractArray
inshape = size(x)
st = @static if VERSION >= v"1.9"
Base.stack(rows)
else
reshape(cat(rows..., dims=length(inshape)), (inshape..., outshape...))
end

st2 = if length(outshape) == 1 || VERSION < v"1.9"
st
else
reshape(st, (inshape..., outshape...))
end

st3 = if length(outshape) == 1 && length(inshape) == 1
transpose(st2)
else
transp = ( ((length(inshape)+1):(length(inshape)+length(outshape)))... , (1:length(inshape))... )
PermutedDimsArray(st2, transp)
end

st3
else
reshape(collect(rows), outshape)
end
end

@inline function jacobian(::ReverseMode{ReturnPrimal,RABI, ErrIfFuncWritten}, f::F, x::X) where {ReturnPrimal, F, X, n_out_val,RABI<:ABI, ErrIfFuncWritten}
res = f(x)
jac = if res isa AbstractArray
jacobian(ReverseMode{false,RABI, ErrIfFuncWritten}(), f, x, Val(length(jac)))
elseif res isa AbstractFloat
gradient(ReverseMode{false,RABI, ErrIfFuncWritten}(), f, x)
else
throw(AssertionError("Unsupported return type of function for reverse-mode jacobian, $(Core.Typeof(res))"))
end

if ReturnPrimal
(res, jac)
else
jac
end
mapreduce(LinearAlgebra.adjoint, vcat, rows)
end

"""
Expand Down
Loading

2 comments on commit 2b681d5

@wsmoses
Copy link
Member Author

@wsmoses wsmoses commented on 2b681d5 Aug 8, 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/112689

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.12.28 -m "<description of version>" 2b681d54234190fdcf70b2844af8cd33700ee0be
git push origin v0.12.28

Please sign in to comment.