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

fix: disable ForwardDiff tag checking with custom backend tags #631

Merged
merged 2 commits into from
Nov 20, 2024
Merged
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
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;
Loading