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

Use ADTypes.jl #33

Closed
wants to merge 2 commits into from
Closed
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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
strategy:
fail-fast: false
matrix:
julia_version: ["1.8", "1"]
julia_version: ["1.10", "1"]
name: julia ${{ matrix.julia_version }}
runs-on: ubuntu-latest
steps:
Expand Down
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["lassepe <[email protected]> and contributors"]
version = "0.1.14"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
FastDifferentiation = "eb9bf01b-bf85-4b60-bf87-ee5de06c00be"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Expand All @@ -13,9 +14,10 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"

[compat]
ADTypes = "1"
ChainRulesCore = "1"
FastDifferentiation = "0.3"
ForwardDiff = "0.10"
PATHSolver = "1.4"
Symbolics = "4,5"
FastDifferentiation = "0.3"
julia = "1.8"
julia = "1.10"
1 change: 1 addition & 0 deletions src/ParametricMCPs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using PATHSolver: PATHSolver
using SparseArrays: SparseArrays
using FastDifferentiation: FastDifferentiation as FD
using Symbolics: Symbolics
using ADTypes: ADTypes

include("Internals.jl")
include("sparse_utils.jl")
Expand Down
25 changes: 21 additions & 4 deletions src/SymbolicUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@
using Symbolics: Symbolics
using FastDifferentiation: FastDifferentiation as FD

using ADTypes: ADTypes

export SymbolicsBackend, FastDifferentiationBackend, make_variables, build_function

struct SymbolicsBackend end
struct FastDifferentiationBackend end
function SymbolicsBackend()
Base.depwarn(

Check warning on line 14 in src/SymbolicUtils.jl

View check run for this annotation

Codecov / codecov/patch

src/SymbolicUtils.jl#L13-L14

Added lines #L13 - L14 were not covered by tests
"The `SymbolicsBackend` type is deprecated and will be removed in a future release. Use `ADTypes.AutoSymbolics` instead.",
:SymbolicUtils,
force = true,
)
ADTypes.AutoSymbolics()

Check warning on line 19 in src/SymbolicUtils.jl

View check run for this annotation

Codecov / codecov/patch

src/SymbolicUtils.jl#L19

Added line #L19 was not covered by tests
end

function FastDifferentiationBackend()
Base.depwarn(

Check warning on line 23 in src/SymbolicUtils.jl

View check run for this annotation

Codecov / codecov/patch

src/SymbolicUtils.jl#L22-L23

Added lines #L22 - L23 were not covered by tests
"The `FastDifferentiationBackend` type is deprecated and will be removed in a future release. Use `ADTypes.AutoFastDifferentiation` instead.",
:SymbolicUtils,
force = true,
)
ADTypes.AutoFastDifferentiation()

Check warning on line 28 in src/SymbolicUtils.jl

View check run for this annotation

Codecov / codecov/patch

src/SymbolicUtils.jl#L28

Added line #L28 was not covered by tests
end

"""
make_variables(backend, name, dimension)
Expand All @@ -18,7 +35,7 @@
"""
function make_variables end

function make_variables(::SymbolicsBackend, name::Symbol, dimension::Int)
function make_variables(::ADTypes.AutoSymbolics, name::Symbol, dimension::Int)
vars = Symbolics.@variables($name[1:dimension]) |> only |> Symbolics.scalarize

if isempty(vars)
Expand All @@ -28,7 +45,7 @@
vars
end

function make_variables(::FastDifferentiationBackend, name::Symbol, dimension::Int)
function make_variables(::ADTypes.AutoFastDifferentiation, name::Symbol, dimension::Int)
FD.make_variables(name, dimension)
end

Expand Down
4 changes: 2 additions & 2 deletions src/parametric_problem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ parameter vector `θ` of size `parameter_dimension` to an length `n` vector outp
- `parameter_dimension`: the size of the parameter vector `θ` in `f`.

Keyword arguments:
- `[backend]`: the backend (from `ParametricMCPs.SymbolicUtils`) to be used for compiling callbacks for `f` and its Jacobians needed by PATH. `SymbolicsBackend` (default) is slightly more flexible. `FastDifferentiationBackend` has reduced compilation times and reduced runtime in some cases.
- `[backend]`: the backend (from ADTypes.jl) to be used for compiling callbacks for `f` and its Jacobians needed by PATH. `AutoSymbolics()` (default) is slightly more flexible. `AutoFastDifferentiation()` has reduced compilation times and reduced runtime in some cases.
- `compute_sensitivities`: whether to compile the callbacks needed for sensitivity computation.
- `[problem_size]`: the number of decision variables. If not provided and `lower_bounds` or `upper_bounds` are vectors, the problem size is inferred from the length of these vectors.

Expand All @@ -55,7 +55,7 @@ function ParametricMCP(
lower_bounds,
upper_bounds,
parameter_dimension;
backend = SymbolicUtils.SymbolicsBackend(),
backend = ADTypes.AutoSymbolics(),
problem_size = Internals.infer_problem_size(lower_bounds, upper_bounds),
kwargs...,
)
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
FiniteDiff = "6a86dc24-6348-571c-b903-95158fe2bd41"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand Down
28 changes: 23 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ using LinearAlgebra: norm
using Zygote: Zygote
using FiniteDiff: FiniteDiff
using Symbolics: Symbolics
using ADTypes: ADTypes

@testset "ParametricMCPs.jl" begin
for backend in [ParametricMCPs.SymbolicUtils.SymbolicsBackend(), ParametricMCPs.SymbolicUtils.FastDifferentiationBackend()]
for backend in [ADTypes.AutoSymbolics(), ADTypes.AutoFastDifferentiation()]
rng = Random.MersenneTwister(1)
parameter_dimension = 2
# setup a dummy MCP which represents a QP with:
Expand All @@ -16,8 +17,21 @@ using Symbolics: Symbolics
f(z, θ) = [2z[1:2] - z[3:4] - 2θ; z[1:2]]
lower_bounds = [-Inf, -Inf, 0, 0]
upper_bounds = [Inf, Inf, Inf, Inf]
problem = ParametricMCPs.ParametricMCP(f, lower_bounds, upper_bounds, parameter_dimension; backend)
problem_no_jacobian = ParametricMCPs.ParametricMCP(f, lower_bounds, upper_bounds, parameter_dimension; compute_sensitivities=false, backend)
problem = ParametricMCPs.ParametricMCP(
f,
lower_bounds,
upper_bounds,
parameter_dimension;
backend,
)
problem_no_jacobian = ParametricMCPs.ParametricMCP(
f,
lower_bounds,
upper_bounds,
parameter_dimension;
compute_sensitivities=false,
backend,
)

feasible_parameters = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [rand(rng, 2) for _ in 1:10]...]
infeasible_parameters = -feasible_parameters
Expand All @@ -42,7 +56,8 @@ using Symbolics: Symbolics

for θ in [feasible_parameters; infeasible_parameters]
∇_autodiff_reverse = only(Zygote.gradient(dummy_pipeline, θ))
∇_autodiff_forward = only(Zygote.gradient(θ -> Zygote.forwarddiff(dummy_pipeline, θ), θ))
∇_autodiff_forward =
only(Zygote.gradient(θ -> Zygote.forwarddiff(dummy_pipeline, θ), θ))
∇_finitediff = FiniteDiff.finite_difference_gradient(dummy_pipeline, θ)
@test isapprox(∇_autodiff_reverse, ∇_finitediff; atol=1e-4)
@test isapprox(∇_autodiff_reverse, ∇_autodiff_forward; atol=1e-4)
Expand All @@ -55,7 +70,10 @@ using Symbolics: Symbolics
sum(solution.z .^ 2)
end

@test_throws ArgumentError Zygote.gradient(θ -> dummy_pipeline(θ, problem_no_jacobian), feasible_parameters[1])
@test_throws ArgumentError Zygote.gradient(
θ -> dummy_pipeline(θ, problem_no_jacobian),
feasible_parameters[1],
)
end
end
end
Loading