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

Provide function to extract the value from a dual tensor #208

Open
wants to merge 5 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
25 changes: 25 additions & 0 deletions docs/src/man/automatic_differentiation.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,31 @@ julia> norm(majorsymmetric(E) - E_sym)
0.0
```

## Differentiating mutating functions
Some applications require the derivative of the output of a function `f(x,s)`,
wrt. `x`, where `f` also mutates `s`.
In these cases, we don't want the derivative of `s` wrt. `x`, and
the value to be set in `s` should only be the value and not be the dual part.
For scalars, `ForwardDiff.jl` provides `ForwardDiff.value`,
and for tensors, `Tensors.jl` provides `Tensors.extract_value`.

```@docs
Tensors.extract_value
```

A simple example of the use-case is
```@example
function mutating_fun(x::Vec, state::Vector)
state[1] = Tensors.extract_value(x)
return x
end

x = rand(Vec{2}); state = zeros(Vec{2}, 1)
gradient(a -> mutating_fun(a, state, true), x)
# Check that it got correctly modified by the extracted value
state[1] == x
```

## Inserting a known derivative
When conditionals are used in a function evaluation, automatic differentiation
may yield the wrong result. Consider, the simplified example of the function
Expand Down
10 changes: 10 additions & 0 deletions src/automatic_differentiation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ end
# Value extraction #
####################

"""
extract_value(v::AbstractTensor)

If `v` is used in a differentiation, such that
`eltype(v)::ForwardDiff.Dual`, extract the value-part of the derivative.
Otherwise, just return `v`.
"""
extract_value(v::AbstractTensor{<:Any,<:Any,<:Dual}) = _extract_value(v)
extract_value(v::AbstractTensor) = v

# Scalar output -> Scalar value
"""
function _extract_value(v::ForwardDiff.Dual)
Expand Down
21 changes: 21 additions & 0 deletions test/test_ad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,5 +337,26 @@ S(C) = S(C, μ, Kb)
end

end

@testset "value_extraction" begin
function mutating_fun(x::Vec, state::Vector; use_extract::Bool, contract::Bool)
v = contract ? x⋅x : x
state[1] = use_extract ? Tensors.extract_value(v) : v
return x
end
TT = Vec{2,Float64}
x = rand(TT); state = zeros(TT, 1)
gradient(a -> mutating_fun(a, state; use_extract=true, contract=false), x)
# Check that it got correctly modified by the extracted value
@test state[1] == x
# Check that test works: Should fail if no extract_value is not used
@test_throws MethodError gradient(a -> mutating_fun(a, state; use_extract=false, contract=false), x)
# Do not allow extract_value on a <:Real
@test_throws MethodError gradient(a -> mutating_fun(a, state; use_extract=false, contract=true), x)
# Check that it get correctly modified when not differentiating
x = rand(TT);
mutating_fun(x, state; use_extract=true, contract=false)
@test state[1] == x
end

end # testsection
Loading