From f18da045bb46e9894f240666191bb89eebe96785 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Wed, 29 May 2024 09:51:25 +0200 Subject: [PATCH 1/3] Start using DifferentiationInterface --- Project.toml | 4 +++- src/CTBase.jl | 3 ++- src/utils.jl | 24 ++++++++++++++++++------ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/Project.toml b/Project.toml index 421e937e..0a76d1a7 100644 --- a/Project.toml +++ b/Project.toml @@ -1,10 +1,11 @@ name = "CTBase" uuid = "54762871-cc72-4466-b8e8-f6c8b58076cd" authors = ["Olivier Cots "] -version = "0.9.0" +version = "0.9.1" [deps] DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" +DifferentiationInterface = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63" DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59" @@ -21,6 +22,7 @@ Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" [compat] DataStructures = "0.18" +DifferentiationInterface = "0.5" DocStringExtensions = "0.9" ForwardDiff = "0.10" Interpolations = "0.15" diff --git a/src/CTBase.jl b/src/CTBase.jl index 44d30887..c4fd3602 100644 --- a/src/CTBase.jl +++ b/src/CTBase.jl @@ -14,7 +14,8 @@ module CTBase # using import Base using DocStringExtensions -using ForwardDiff: jacobian, gradient, ForwardDiff # automatic differentiation +using DifferentiationInterface: AutoForwardDiff, derivative, gradient, jacobian, prepare_derivative, prepare_gradient, prepare_jacobian +using ForwardDiff: ForwardDiff # automatic differentiation using Interpolations: linear_interpolation, Line, Interpolations # for default interpolation using MLStyle # pattern matching using Parameters # @with_kw: to have default values in struct diff --git a/src/utils.jl b/src/utils.jl index 2a597f1e..476b0dee 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -74,7 +74,9 @@ $(TYPEDSIGNATURES) Return the gradient of `f` at `x`. """ function ctgradient(f::Function, x::ctNumber) - return ForwardDiff.derivative(x -> f(x), x) + backend = AutoForwardDiff() + extras = prepare_derivative(f, backend, x) + return derivative(f, backend, x, extras) end """ @@ -83,7 +85,9 @@ $(TYPEDSIGNATURES) Return the gradient of `f` at `x`. """ function ctgradient(f::Function, x) - return ForwardDiff.gradient(f, x) + backend = AutoForwardDiff() + extras = prepare_gradient(f, backend, x) + return gradient(f, backend, x, extras) end """ @@ -98,8 +102,12 @@ $(TYPEDSIGNATURES) Return the Jacobian of `f` at `x`. """ -function ctjacobian(f::Function, x::ctNumber) - return ForwardDiff.jacobian(x -> f(x[1]), [x]) +function ctjacobian(f::Function, x::ctNumber) + f_number_to_number = only ∘ f ∘ only + backend = AutoForwardDiff() + extras = prepare_derivative(f_number_to_number, backend, x) + der = derivative(f_number_to_number, backend, x, extras) + return [der;;] end """ @@ -107,7 +115,11 @@ $(TYPEDSIGNATURES) Return the Jacobian of `f` at `x`. """ -ctjacobian(f::Function, x) = ForwardDiff.jacobian(f, x) +function ctjacobian(f::Function, x) + backend = AutoForwardDiff() + extras = prepare_jacobian(f, backend, x) + return jacobian(f, backend, x, extras) +end """ $(TYPEDSIGNATURES) @@ -195,4 +207,4 @@ function matrix2vec(x::Matrix{<:ctNumber}, dim::Integer=__matrix_dimension_stock end end return y -end \ No newline at end of file +end From 1314e97f6c661368d9335ee144c02493421d0bd4 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Thu, 30 May 2024 06:23:07 +0200 Subject: [PATCH 2/3] Add __auto --- src/CTBase.jl | 4 +++- src/utils.jl | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/CTBase.jl b/src/CTBase.jl index c4fd3602..d51aeed1 100644 --- a/src/CTBase.jl +++ b/src/CTBase.jl @@ -15,7 +15,7 @@ module CTBase import Base using DocStringExtensions using DifferentiationInterface: AutoForwardDiff, derivative, gradient, jacobian, prepare_derivative, prepare_gradient, prepare_jacobian -using ForwardDiff: ForwardDiff # automatic differentiation +import ForwardDiff using Interpolations: linear_interpolation, Line, Interpolations # for default interpolation using MLStyle # pattern matching using Parameters # @with_kw: to have default values in struct @@ -95,6 +95,8 @@ Type alias for a tangent vector to the costate space. """ const DCostate = ctVector +__auto() = AutoForwardDiff() + # include("exception.jl") include("description.jl") diff --git a/src/utils.jl b/src/utils.jl index 476b0dee..a9e9aae7 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -74,7 +74,7 @@ $(TYPEDSIGNATURES) Return the gradient of `f` at `x`. """ function ctgradient(f::Function, x::ctNumber) - backend = AutoForwardDiff() + backend = __auto() extras = prepare_derivative(f, backend, x) return derivative(f, backend, x, extras) end @@ -85,7 +85,7 @@ $(TYPEDSIGNATURES) Return the gradient of `f` at `x`. """ function ctgradient(f::Function, x) - backend = AutoForwardDiff() + backend = __auto() extras = prepare_gradient(f, backend, x) return gradient(f, backend, x, extras) end @@ -104,7 +104,7 @@ Return the Jacobian of `f` at `x`. """ function ctjacobian(f::Function, x::ctNumber) f_number_to_number = only ∘ f ∘ only - backend = AutoForwardDiff() + backend = __auto() extras = prepare_derivative(f_number_to_number, backend, x) der = derivative(f_number_to_number, backend, x, extras) return [der;;] @@ -116,7 +116,7 @@ $(TYPEDSIGNATURES) Return the Jacobian of `f` at `x`. """ function ctjacobian(f::Function, x) - backend = AutoForwardDiff() + backend = __auto() extras = prepare_jacobian(f, backend, x) return jacobian(f, backend, x, extras) end @@ -207,4 +207,4 @@ function matrix2vec(x::Matrix{<:ctNumber}, dim::Integer=__matrix_dimension_stock end end return y -end +end \ No newline at end of file From df88418558c79a153c9e59f4634cc40b85caaa06 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Caillau Date: Thu, 30 May 2024 11:16:37 +0200 Subject: [PATCH 3/3] added helpers to chose AD backend in ctgradient, etc. --- src/CTBase.jl | 2 +- src/print.jl | 2 +- src/utils.jl | 12 ++++-------- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/CTBase.jl b/src/CTBase.jl index d51aeed1..13d052c5 100644 --- a/src/CTBase.jl +++ b/src/CTBase.jl @@ -95,7 +95,7 @@ Type alias for a tangent vector to the costate space. """ const DCostate = ctVector -__auto() = AutoForwardDiff() +__auto() = AutoForwardDiff() # default AD backend # include("exception.jl") diff --git a/src/print.jl b/src/print.jl index da0697b9..20f616d7 100644 --- a/src/print.jl +++ b/src/print.jl @@ -190,7 +190,7 @@ function Base.show(io::IO, ::MIME"text/plain", ocp::OptimalControlModel{<: TimeD # println(io) printstyled(io, "Declarations ", bold=true) - printstyled(io, "(* required):\n", bold=false) + printstyled(io, "(* for required):\n", bold=false) #println(io) # print table of settings diff --git a/src/utils.jl b/src/utils.jl index a9e9aae7..c608a0eb 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -73,8 +73,7 @@ $(TYPEDSIGNATURES) Return the gradient of `f` at `x`. """ -function ctgradient(f::Function, x::ctNumber) - backend = __auto() +function ctgradient(f::Function, x::ctNumber; backend=__auto()) extras = prepare_derivative(f, backend, x) return derivative(f, backend, x, extras) end @@ -84,8 +83,7 @@ $(TYPEDSIGNATURES) Return the gradient of `f` at `x`. """ -function ctgradient(f::Function, x) - backend = __auto() +function ctgradient(f::Function, x; backend=__auto()) extras = prepare_gradient(f, backend, x) return gradient(f, backend, x, extras) end @@ -102,9 +100,8 @@ $(TYPEDSIGNATURES) Return the Jacobian of `f` at `x`. """ -function ctjacobian(f::Function, x::ctNumber) +function ctjacobian(f::Function, x::ctNumber; backend=__auto()) f_number_to_number = only ∘ f ∘ only - backend = __auto() extras = prepare_derivative(f_number_to_number, backend, x) der = derivative(f_number_to_number, backend, x, extras) return [der;;] @@ -115,8 +112,7 @@ $(TYPEDSIGNATURES) Return the Jacobian of `f` at `x`. """ -function ctjacobian(f::Function, x) - backend = __auto() +function ctjacobian(f::Function, x; backend=__auto()) extras = prepare_jacobian(f, backend, x) return jacobian(f, backend, x, extras) end