Skip to content

Commit

Permalink
fix: disable ForwardDiff tag checking with custom backend tags (#631)
Browse files Browse the repository at this point in the history
* fix: disable ForwardDiff tag checking with custom backend tags

* Fix sparse
  • Loading branch information
gdalle authored Nov 20, 2024
1 parent fd60623 commit 2743e18
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,44 +305,56 @@ function DI.value_and_gradient!(
f::F,
grad,
prep::ForwardDiffGradientPrep,
::AutoForwardDiff,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc = with_contexts(f, contexts...)
result = DiffResult(zero(eltype(x)), (grad,))
result = gradient!(result, fc, x, prep.config)
CHK = tag_type(backend) === Nothing
result = gradient!(result, fc, x, prep.config, Val(CHK))
y = DR.value(result)
grad === DR.gradient(result) || copyto!(grad, DR.gradient(result))
return y, grad
end

function DI.value_and_gradient(
f::F, prep::ForwardDiffGradientPrep, ::AutoForwardDiff, x, contexts::Vararg{Constant,C}
f::F,
prep::ForwardDiffGradientPrep,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc = with_contexts(f, contexts...)
result = GradientResult(x)
result = gradient!(result, fc, x, prep.config)
CHK = tag_type(backend) === Nothing
result = gradient!(result, fc, x, prep.config, Val(CHK))
return DR.value(result), DR.gradient(result)
end

function DI.gradient!(
f::F,
grad,
prep::ForwardDiffGradientPrep,
::AutoForwardDiff,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc = with_contexts(f, contexts...)
return gradient!(grad, fc, x, prep.config)
CHK = tag_type(backend) === Nothing
return gradient!(grad, fc, x, prep.config, Val(CHK))
end

function DI.gradient(
f::F, prep::ForwardDiffGradientPrep, ::AutoForwardDiff, x, contexts::Vararg{Constant,C}
f::F,
prep::ForwardDiffGradientPrep,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc = with_contexts(f, contexts...)
return gradient(fc, x, prep.config)
CHK = tag_type(backend) === Nothing
return gradient(fc, x, prep.config, Val(CHK))
end

## Jacobian
Expand Down Expand Up @@ -422,14 +434,15 @@ function DI.value_and_jacobian!(
f::F,
jac,
prep::ForwardDiffOneArgJacobianPrep,
::AutoForwardDiff,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc = with_contexts(f, contexts...)
y = fc(x)
result = DiffResult(y, (jac,))
result = jacobian!(result, fc, x, prep.config)
CHK = tag_type(backend) === Nothing
result = jacobian!(result, fc, x, prep.config, Val(CHK))
y = DR.value(result)
jac === DR.jacobian(result) || copyto!(jac, DR.jacobian(result))
return y, jac
Expand All @@ -438,35 +451,38 @@ end
function DI.value_and_jacobian(
f::F,
prep::ForwardDiffOneArgJacobianPrep,
::AutoForwardDiff,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc = with_contexts(f, contexts...)
return fc(x), jacobian(fc, x, prep.config)
CHK = tag_type(backend) === Nothing
return fc(x), jacobian(fc, x, prep.config, Val(CHK))
end

function DI.jacobian!(
f::F,
jac,
prep::ForwardDiffOneArgJacobianPrep,
::AutoForwardDiff,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc = with_contexts(f, contexts...)
return jacobian!(jac, fc, x, prep.config)
CHK = tag_type(backend) === Nothing
return jacobian!(jac, fc, x, prep.config, Val(CHK))
end

function DI.jacobian(
f::F,
prep::ForwardDiffOneArgJacobianPrep,
::AutoForwardDiff,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc = with_contexts(f, contexts...)
return jacobian(fc, x, prep.config)
CHK = tag_type(backend) === Nothing
return jacobian(fc, x, prep.config, Val(CHK))
end

## Second derivative
Expand Down Expand Up @@ -681,44 +697,56 @@ function DI.hessian!(
f::F,
hess,
prep::ForwardDiffHessianPrep,
::AutoForwardDiff,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc = with_contexts(f, contexts...)
return hessian!(hess, fc, x, prep.array_config)
CHK = tag_type(backend) === Nothing
return hessian!(hess, fc, x, prep.array_config, Val(CHK))
end

function DI.hessian(
f::F, prep::ForwardDiffHessianPrep, ::AutoForwardDiff, x, contexts::Vararg{Constant,C}
f::F,
prep::ForwardDiffHessianPrep,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc = with_contexts(f, contexts...)
return hessian(fc, x, prep.array_config)
CHK = tag_type(backend) === Nothing
return hessian(fc, x, prep.array_config, Val(CHK))
end

function DI.value_gradient_and_hessian!(
f::F,
grad,
hess,
prep::ForwardDiffHessianPrep,
::AutoForwardDiff,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc = with_contexts(f, contexts...)
result = DiffResult(one(eltype(x)), (grad, hess))
result = hessian!(result, fc, x, prep.result_config)
CHK = tag_type(backend) === Nothing
result = hessian!(result, fc, x, prep.result_config, Val(CHK))
y = DR.value(result)
grad === DR.gradient(result) || copyto!(grad, DR.gradient(result))
hess === DR.hessian(result) || copyto!(hess, DR.hessian(result))
return (y, grad, hess)
end

function DI.value_gradient_and_hessian(
f::F, prep::ForwardDiffHessianPrep, ::AutoForwardDiff, x, contexts::Vararg{Constant,C}
f::F,
prep::ForwardDiffHessianPrep,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc = with_contexts(f, contexts...)
result = HessianResult(x)
result = hessian!(result, fc, x, prep.result_config)
CHK = tag_type(backend) === Nothing
result = hessian!(result, fc, x, prep.result_config, Val(CHK))
return (DR.value(result), DR.gradient(result), DR.hessian(result))
end
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,14 @@ function DI.value_and_derivative(
f!::F,
y,
prep::ForwardDiffTwoArgDerivativePrep,
::AutoForwardDiff,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc! = with_contexts(f!, contexts...)
result = MutableDiffResult(y, (similar(y),))
result = derivative!(result, fc!, y, x, prep.config)
CHK = tag_type(backend) === Nothing
result = derivative!(result, fc!, y, x, prep.config, Val(CHK))
return DiffResults.value(result), DiffResults.derivative(result)
end

Expand All @@ -196,39 +197,42 @@ function DI.value_and_derivative!(
y,
der,
prep::ForwardDiffTwoArgDerivativePrep,
::AutoForwardDiff,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc! = with_contexts(f!, contexts...)
result = MutableDiffResult(y, (der,))
result = derivative!(result, fc!, y, x, prep.config)
CHK = tag_type(backend) === Nothing
result = derivative!(result, fc!, y, x, prep.config, Val(CHK))
return DiffResults.value(result), DiffResults.derivative(result)
end

function DI.derivative(
f!::F,
y,
prep::ForwardDiffTwoArgDerivativePrep,
::AutoForwardDiff,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc! = with_contexts(f!, contexts...)
return derivative(fc!, y, x, prep.config)
CHK = tag_type(backend) === Nothing
return derivative(fc!, y, x, prep.config, Val(CHK))
end

function DI.derivative!(
f!::F,
y,
der,
prep::ForwardDiffTwoArgDerivativePrep,
::AutoForwardDiff,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc! = with_contexts(f!, contexts...)
return derivative!(der, fc!, y, x, prep.config)
CHK = tag_type(backend) === Nothing
return derivative!(der, fc!, y, x, prep.config, Val(CHK))
end

## Jacobian
Expand Down Expand Up @@ -308,14 +312,15 @@ function DI.value_and_jacobian(
f!::F,
y,
prep::ForwardDiffTwoArgJacobianPrep,
::AutoForwardDiff,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc! = with_contexts(f!, contexts...)
jac = similar(y, length(y), length(x))
result = MutableDiffResult(y, (jac,))
result = jacobian!(result, fc!, y, x, prep.config)
CHK = tag_type(backend) === Nothing
result = jacobian!(result, fc!, y, x, prep.config, Val(CHK))
return DiffResults.value(result), DiffResults.jacobian(result)
end

Expand All @@ -324,37 +329,40 @@ function DI.value_and_jacobian!(
y,
jac,
prep::ForwardDiffTwoArgJacobianPrep,
::AutoForwardDiff,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc! = with_contexts(f!, contexts...)
result = MutableDiffResult(y, (jac,))
result = jacobian!(result, fc!, y, x, prep.config)
CHK = tag_type(backend) === Nothing
result = jacobian!(result, fc!, y, x, prep.config, Val(CHK))
return DiffResults.value(result), DiffResults.jacobian(result)
end

function DI.jacobian(
f!::F,
y,
prep::ForwardDiffTwoArgJacobianPrep,
::AutoForwardDiff,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc! = with_contexts(f!, contexts...)
return jacobian(fc!, y, x, prep.config)
CHK = tag_type(backend) === Nothing
return jacobian(fc!, y, x, prep.config, Val(CHK))
end

function DI.jacobian!(
f!::F,
y,
jac,
prep::ForwardDiffTwoArgJacobianPrep,
::AutoForwardDiff,
backend::AutoForwardDiff,
x,
contexts::Vararg{Constant,C},
) where {F,C}
fc! = with_contexts(f!, contexts...)
return jacobian!(jac, fc!, y, x, prep.config)
CHK = tag_type(backend) === Nothing
return jacobian!(jac, fc!, y, x, prep.config, Val(CHK))
end
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function get_tag(f::F, ::AutoForwardDiff{chunksize,Nothing}, x) where {F,chunksi
return Tag(f, eltype(x))
end

tag_type(::AutoForwardDiff{chunksize,T}) where {chunksize,T} = T
tag_type(f::F, backend::AutoForwardDiff, x) where {F} = typeof(get_tag(f, backend, x))

function make_dual_similar(::Type{T}, x::Number, tx::NTuple{B}) where {T,B}
Expand Down
16 changes: 11 additions & 5 deletions DifferentiationInterface/test/Back/ForwardDiff/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ using Test

LOGGING = get(ENV, "CI", "false") == "false"

struct MyTag end

backends = [
AutoForwardDiff(), AutoForwardDiff(; tag=:hello), AutoForwardDiff(; chunksize=5)
AutoForwardDiff(),
AutoForwardDiff(; chunksize=5),
AutoForwardDiff(; tag=ForwardDiff.Tag(MyTag(), Float64)),
]

for backend in backends
Expand Down Expand Up @@ -54,10 +58,10 @@ test_differentiation(

## Sparse

test_differentiation(MyAutoSparse.(backends[1:2]), default_scenarios(); logging=LOGGING);
test_differentiation(MyAutoSparse(AutoForwardDiff()), default_scenarios(); logging=LOGGING);

test_differentiation(
MyAutoSparse.(backends[1:2]),
MyAutoSparse(AutoForwardDiff()),
sparse_scenarios(; include_constantified=true);
sparsity=true,
logging=LOGGING,
Expand All @@ -78,7 +82,9 @@ test_differentiation(AutoForwardDiff(), static_scenarios(); logging=LOGGING)
excluded=[:hessian, :pullback], # TODO: figure this out
logging=LOGGING,
)
@testset "$(row[:scenario])" for row in eachrow(data)
@test row[:allocs] == 0
@testset "Analyzing benchmark results" begin
@testset "$(row[:scenario])" for row in eachrow(data)
@test row[:allocs] == 0
end
end
end;

4 comments on commit 2743e18

@gdalle
Copy link
Member Author

@gdalle gdalle commented on 2743e18 Nov 20, 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.

Error while trying to register: "File (Julia)Project.toml not found"

@gdalle
Copy link
Member Author

@gdalle gdalle commented on 2743e18 Nov 20, 2024

Choose a reason for hiding this comment

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

@JuliaRegistrator register subdir=DifferentiationInterface

@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/119800

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 DifferentiationInterface-v0.6.23 -m "<description of version>" 2743e185937ecf8c1b9cd6b6d6072cbb327ed0ab
git push origin DifferentiationInterface-v0.6.23

Please sign in to comment.