Skip to content
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

Realias DiffResults whenever possible #261

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ReverseDiff"
uuid = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
version = "1.15.3"
version = "1.15.4"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down Expand Up @@ -36,7 +36,8 @@ julia = "1"
DiffTests = "de460e47-3fe3-5279-bb4a-814414816d5d"
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
IrrationalConstants = "92d709cd-6900-40b7-9082-c6be49f344b6"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["DiffTests", "FillArrays", "IrrationalConstants", "Test"]
test = ["DiffTests", "FillArrays", "IrrationalConstants", "StaticArrays", "Test"]
8 changes: 4 additions & 4 deletions src/api/gradients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ call.
function gradient(f, input, cfg::GradientConfig = GradientConfig(input))
tape = GradientTape(f, input, cfg)
result = construct_result(input_hook(tape))
seeded_reverse_pass!(result, tape)
result = seeded_reverse_pass!(result, tape)
empty!(cfg.tape)
return result
end
Expand All @@ -39,7 +39,7 @@ in it as well.
"""
function gradient!(result, f, input, cfg::GradientConfig = GradientConfig(input))
tape = GradientTape(f, input, cfg)
seeded_reverse_pass!(result, tape)
result = seeded_reverse_pass!(result, tape)
empty!(cfg.tape)
return result
end
Expand All @@ -60,7 +60,7 @@ of `f` w.r.t. `input[i].`
"""
function gradient!(tape::Union{GradientTape,CompiledGradient}, input)
result = construct_result(input_hook(tape))
gradient!(result, tape, input)
result = gradient!(result, tape, input)
return result
end

Expand All @@ -77,6 +77,6 @@ in it as well.
"""
function gradient!(result, tape::Union{GradientTape,CompiledGradient}, input)
seeded_forward_pass!(tape, input)
seeded_reverse_pass!(result, tape)
result = seeded_reverse_pass!(result, tape)
return result
end
8 changes: 5 additions & 3 deletions src/api/hessians.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ If `result` is a `DiffResults.DiffResult`, the primal value `f(input)` and the g
"""
function hessian!(result, f, input::AbstractArray, cfg::HessianConfig = HessianConfig(input))
∇f = x -> gradient(f, x, cfg.gradient_config)
jacobian!(result, ∇f, input, cfg.jacobian_config)
result = jacobian!(result, ∇f, input, cfg.jacobian_config)
return result
end

function hessian!(result::DiffResult, f, input::AbstractArray,
cfg::HessianConfig = HessianConfig(result, input))
# TODO: how to realias `result` here?
∇f! = (y, x) -> begin
gradient_result = DiffResult(zero(eltype(y)), y)
gradient!(gradient_result, f, x, cfg.gradient_config)
Expand All @@ -68,7 +69,7 @@ return the Hessian `H(f)(input)`.
"""
function hessian!(tape::Union{HessianTape,CompiledHessian}, input::AbstractArray)
result = construct_result(output_hook(tape), input_hook(tape))
hessian!(result, tape, input)
result = hessian!(result, tape, input)
return result
end

Expand All @@ -85,11 +86,12 @@ If `result` is a `DiffResults.DiffResult`, the primal value `f(input)` and the g
"""
function hessian!(result::AbstractArray, tape::Union{HessianTape,CompiledHessian}, input::AbstractArray)
seeded_forward_pass!(tape, input)
seeded_reverse_pass!(result, tape)
result = seeded_reverse_pass!(result, tape)
return result
end

function hessian!(result::DiffResult, tape::Union{HessianTape,CompiledHessian}, input::AbstractArray)
# TODO: how to realias `result` here?
seeded_forward_pass!(tape, input)
seeded_reverse_pass!(DiffResult(DiffResults.gradient(result), DiffResults.hessian(result)), tape)
result = DiffResults.value!(result, func_hook(tape)(input))
Expand Down
8 changes: 4 additions & 4 deletions src/api/jacobians.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ in it as well.
function jacobian!(result, f, input, cfg::JacobianConfig = JacobianConfig(input))
tape = JacobianTape(f, input, cfg)
isa(input, TrackedArray) && empty!(input.tape)
jacobian!(result, tape, input)
result = jacobian!(result, tape, input)
empty!(tape.tape)
return result
end
Expand Down Expand Up @@ -74,7 +74,7 @@ form `f!(output::AbstractArray{<:Real}, input::AbstractArray{<:Real}...)`.
function jacobian!(result, f!, output, input, cfg::JacobianConfig = JacobianConfig(output, input))
tape = JacobianTape(f!, output, input, cfg)
isa(input, TrackedArray) && empty!(input.tape)
jacobian!(result, tape, input)
result = jacobian!(result, tape, input)
extract_result_value!(output, output_hook(tape))
empty!(tape.tape)
return result
Expand Down Expand Up @@ -103,7 +103,7 @@ new `output` values into the tape.
"""
function jacobian!(tape::Union{JacobianTape,CompiledJacobian}, input)
result = construct_result(output_hook(tape), input_hook(tape))
jacobian!(result, tape, input)
result = jacobian!(result, tape, input)
return result
end

Expand All @@ -119,7 +119,7 @@ case the primal value of the target function will be stored in it as well.
"""
function jacobian!(result, tape::Union{JacobianTape,CompiledJacobian}, input)
seeded_forward_pass!(tape, input)
seeded_reverse_pass!(result, tape)
result = seeded_reverse_pass!(result, tape)
return result
end

Expand Down
16 changes: 8 additions & 8 deletions src/api/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ function seeded_reverse_pass!(result, output::TrackedReal, input, tape)
unseed!(input)
seed!(output)
reverse_pass!(tape)
extract_result!(result, output, input)
result = extract_result!(result, output, input)
return result
end

function seeded_reverse_pass!(result, output::Number, input, tape)
extract_result!(result, output)
result = extract_result!(result, output)
return result
end

Expand All @@ -58,13 +58,13 @@ function seeded_reverse_pass!(result::AbstractArray, output::AbstractArray, inpu
end

function seeded_reverse_pass!(result::DiffResult, output::AbstractArray, input::TrackedArray, tape)
seeded_reverse_pass!(DiffResults.jacobian(result), output, input, tape)
extract_result_value!(result, output)
result = seeded_reverse_pass!(DiffResults.jacobian(result), output, input, tape)
result = extract_result_value!(result, output)
return result
end

function seeded_reverse_pass!(result::Tuple, output::AbstractArray, input::Tuple, tape)
for i in eachindex(result)
result = map(eachindex(result, input)) do i
seeded_reverse_pass!(result[i], output, input[i], tape)
end
return result
Expand All @@ -75,14 +75,14 @@ end
#####################

function extract_result!(result::Tuple, output, input::Tuple)
for i in eachindex(result)
result = map(eachindex(result, input)) do i
extract_result!(result[i], output, input[i])
end
return result
end

function extract_result!(result::Tuple, output)
for i in eachindex(result)
result = map(eachindex(result)) do i
extract_result!(result[i], output)
end
return result
Expand Down Expand Up @@ -111,7 +111,7 @@ function extract_result!(result::DiffResult, output::Number)
end

function extract_result_value!(result::Tuple, output)
for i in eachindex(result)
result = map(eachindex(result)) do i
extract_result_value!(result[i], output)
end
return result
Expand Down
Loading
Loading