Skip to content

fix: erase cache contexts before building function in Symbolics #760

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

Merged
merged 4 commits into from
Mar 28, 2025
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
2 changes: 1 addition & 1 deletion DifferentiationInterface/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DifferentiationInterface"
uuid = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
authors = ["Guillaume Dalle", "Adrian Hill"]
version = "0.6.49"
version = "0.6.50"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Moreover, each context type is supported by a specific subset of backends:
| `AutoMooncake` | ✅ | ✅ |
| `AutoPolyesterForwardDiff` | ✅ | ✅ |
| `AutoReverseDiff` | ✅ | ❌ |
| `AutoSymbolics` | ✅ | |
| `AutoSymbolics` | ✅ | |
| `AutoTracker` | ✅ | ❌ |
| `AutoZygote` | ✅ | 🔀 |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,23 @@
variablize(x::AbstractArray, name::Symbol) = variables(name, axes(x)...)

function variablize(contexts::NTuple{C,DI.Context}) where {C}
map(enumerate(contexts)) do (k, c)
return ntuple(Val(C)) do k
c = contexts[k]

Check warning on line 32 in DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/DifferentiationInterfaceSymbolicsExt.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/DifferentiationInterfaceSymbolicsExt.jl#L31-L32

Added lines #L31 - L32 were not covered by tests
variablize(DI.unwrap(c), Symbol("context$k"))
end
end

function erase_cache_vars!(

Check warning on line 37 in DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/DifferentiationInterfaceSymbolicsExt.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/DifferentiationInterfaceSymbolicsExt.jl#L37

Added line #L37 was not covered by tests
context_vars::NTuple{C}, contexts::NTuple{C,DI.Context}
) where {C}
# erase the active data from caches before building function
for (v, c) in zip(context_vars, contexts)
if c isa DI.Cache
fill!(v, zero(eltype(v)))

Check warning on line 43 in DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/DifferentiationInterfaceSymbolicsExt.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/DifferentiationInterfaceSymbolicsExt.jl#L41-L43

Added lines #L41 - L43 were not covered by tests
end
end

Check warning on line 45 in DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/DifferentiationInterfaceSymbolicsExt.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/DifferentiationInterfaceSymbolicsExt.jl#L45

Added line #L45 was not covered by tests
end

include("onearg.jl")
include("twoarg.jl")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
step_der_var = derivative(f(x_var + t_var * dx_var, context_vars...), t_var)
pf_var = substitute(step_der_var, Dict(t_var => zero(eltype(x))))

erase_cache_vars!(context_vars, contexts)

Check warning on line 21 in DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/onearg.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/onearg.jl#L21

Added line #L21 was not covered by tests
res = build_function(
pf_var, x_var, dx_var, context_vars...; expression=Val(false), cse=true
)
Expand Down Expand Up @@ -104,6 +105,7 @@
context_vars = variablize(contexts)
der_var = derivative(f(x_var, context_vars...), x_var)

erase_cache_vars!(context_vars, contexts)

Check warning on line 108 in DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/onearg.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/onearg.jl#L108

Added line #L108 was not covered by tests
res = build_function(der_var, x_var, context_vars...; expression=Val(false), cse=true)
(der_exe, der_exe!) = if res isa Tuple
res
Expand Down Expand Up @@ -179,6 +181,7 @@
# Symbolic.gradient only accepts vectors
grad_var = gradient(f(x_var, context_vars...), vec(x_var))

erase_cache_vars!(context_vars, contexts)

Check warning on line 184 in DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/onearg.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/onearg.jl#L184

Added line #L184 was not covered by tests
res = build_function(
grad_var, vec(x_var), context_vars...; expression=Val(false), cse=true
)
Expand Down Expand Up @@ -258,6 +261,7 @@
jacobian(f(x_var, context_vars...), x_var)
end

erase_cache_vars!(context_vars, contexts)

Check warning on line 264 in DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/onearg.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/onearg.jl#L264

Added line #L264 was not covered by tests
res = build_function(jac_var, x_var, context_vars...; expression=Val(false), cse=true)
(jac_exe, jac_exe!) = res
return SymbolicsOneArgJacobianPrep(_sig, jac_exe, jac_exe!)
Expand Down Expand Up @@ -337,6 +341,7 @@
hessian(f(x_var, context_vars...), vec(x_var))
end

erase_cache_vars!(context_vars, contexts)

Check warning on line 344 in DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/onearg.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/onearg.jl#L344

Added line #L344 was not covered by tests
res = build_function(
hess_var, vec(x_var), context_vars...; expression=Val(false), cse=true
)
Expand Down Expand Up @@ -425,6 +430,7 @@
hess_var = hessian(f(x_var, context_vars...), vec(x_var))
hvp_vec_var = hess_var * vec(dx_var)

erase_cache_vars!(context_vars, contexts)

Check warning on line 433 in DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/onearg.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/onearg.jl#L433

Added line #L433 was not covered by tests
res = build_function(
hvp_vec_var,
vec(x_var),
Expand Down Expand Up @@ -519,6 +525,7 @@
der_var = derivative(f(x_var, context_vars...), x_var)
der2_var = derivative(der_var, x_var)

erase_cache_vars!(context_vars, contexts)

Check warning on line 528 in DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/onearg.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/onearg.jl#L528

Added line #L528 was not covered by tests
res = build_function(der2_var, x_var, context_vars...; expression=Val(false), cse=true)
(der2_exe, der2_exe!) = if res isa Tuple
res
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
step_der_var = derivative(y_var, t_var)
pf_var = substitute(step_der_var, Dict(t_var => zero(eltype(x))))

erase_cache_vars!(context_vars, contexts)

Check warning on line 29 in DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/twoarg.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/twoarg.jl#L29

Added line #L29 was not covered by tests
res = build_function(
pf_var, x_var, dx_var, context_vars...; expression=Val(false), cse=true
)
Expand Down Expand Up @@ -116,6 +117,7 @@
f!(y_var, x_var, context_vars...)
der_var = derivative(y_var, x_var)

erase_cache_vars!(context_vars, contexts)

Check warning on line 120 in DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/twoarg.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/twoarg.jl#L120

Added line #L120 was not covered by tests
res = build_function(der_var, x_var, context_vars...; expression=Val(false), cse=true)
(der_exe, der_exe!) = res
return SymbolicsTwoArgDerivativePrep(_sig, der_exe, der_exe!)
Expand Down Expand Up @@ -203,6 +205,7 @@
jacobian(y_var, x_var)
end

erase_cache_vars!(context_vars, contexts)

Check warning on line 208 in DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/twoarg.jl

View check run for this annotation

Codecov / codecov/patch

DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/twoarg.jl#L208

Added line #L208 was not covered by tests
res = build_function(jac_var, x_var, context_vars...; expression=Val(false), cse=true)
(jac_exe, jac_exe!) = res
return SymbolicsTwoArgJacobianPrep(_sig, jac_exe, jac_exe!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ test_differentiation(
test_differentiation(
AutoSymbolics(),
default_scenarios(; include_normal=false, include_cachified=true, use_tuples=false);
excluded=[:jacobian], # TODO: figure out why this fails
logging=LOGGING,
);

Expand Down
Loading