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

disable tagging for ForwardDiff in MUSE ext #90

Closed
wants to merge 5 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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[weakdeps]
AbstractDifferentiation = "c29ec348-61ec-40c8-8164-b8c60e9d9f3d"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
CUDAKernels = "72cfdca4-0801-4ab0-bf6a-d52aa10adc57"
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
Expand All @@ -78,7 +79,7 @@ PythonPlot = "274fc56d-3b97-40fa-a1cd-1b4a50311bf9"

[extensions]
CMBLensingCUDAExt = "CUDA"
CMBLensingMuseInferenceExt = "MuseInference"
CMBLensingMuseInferenceExt = ["MuseInference", "AbstractDifferentiation"]
CMBLensingPythonCallExt = "PythonCall"
CMBLensingPythonPlotExt = "PythonPlot"

Expand Down
61 changes: 57 additions & 4 deletions ext/CMBLensingMuseInferenceExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,71 @@ using CMBLensing

if isdefined(Base, :get_extension)
using MuseInference
using MuseInference: AD, AbstractMuseProblem, MuseResult, Transformedθ, UnTransformedθ
using MuseInference: AbstractMuseProblem, MuseResult, Transformedθ, UnTransformedθ
import AbstractDifferentiation
import AbstractDifferentiation: pushforward_function, gradient, jacobian, hessian, value_and_gradient, value_and_gradient
else
using ..MuseInference
using ..MuseInference: AD, AbstractMuseProblem, MuseResult, Transformedθ, UnTransformedθ
using ..MuseInference: AbstractMuseProblem, MuseResult, Transformedθ, UnTransformedθ
import ..AbstractDifferentiation
import ..AbstractDifferentiation: pushforward_function, gradient, jacobian, hessian, value_and_gradient, value_and_gradient
end

const AD = AbstractDifferentiation

using Base: @kwdef
using ComponentArrays
using NamedTupleTools
using Random
using Requires
using Setfield
using ForwardDiff

# we're going to make our own backend
struct ForwardDiffNoTagBackend{CS} <: AD.AbstractForwardMode end
const CMBLensing.ForwardDiffNoTagBackend = ForwardDiffNoTagBackend

chunk(::ForwardDiffNoTagBackend{Nothing}, x) = ForwardDiff.Chunk(x)
chunk(::ForwardDiffNoTagBackend{N}, _) where {N} = ForwardDiff.Chunk{N}()

function pushforward_function(ba::ForwardDiffNoTagBackend{CS}, f, xs...) where CS
pushforward_function(AD.ForwardDiffBackend{CS}(), f, xs...)
end

function AD.gradient(ba::ForwardDiffNoTagBackend, f, x::AbstractArray)
cfg = ForwardDiff.GradientConfig(nothing, x, chunk(ba, x))
return (ForwardDiff.gradient(f, x, cfg),)
end

function AD.jacobian(ba::ForwardDiffNoTagBackend, f, x::AbstractArray)
cfg = ForwardDiff.JacobianConfig(nothing, x, chunk(ba, x))
return (ForwardDiff.jacobian(AD.asarray ∘ f, x, cfg),)
end

function AD.jacobian(ba::ForwardDiffNoTagBackend, f, x::R) where {R <: Number}
T = typeof(ForwardDiff.Tag(nothing, R))
return (ForwardDiff.extract_derivative(T, f(ForwardDiff.Dual{T}(x, one(x)))),)
end

function AD.hessian(ba::ForwardDiffNoTagBackend, f, x::AbstractArray)
cfg = ForwardDiff.HessianConfig(nothing, x, chunk(ba, x))
return (ForwardDiff.hessian(f, x, cfg),)
end

function AD.value_and_gradient(ba::ForwardDiffNoTagBackend, f, x::AbstractArray)
result = DiffResults.GradientResult(x)
cfg = ForwardDiff.GradientConfig(nothing, x, chunk(ba, x))
ForwardDiff.gradient!(result, f, x, cfg)
return DiffResults.value(result), (DiffResults.derivative(result),)
end

function AD.value_and_hessian(ba::ForwardDiffNoTagBackend, f, x)
result = DiffResults.HessianResult(x)
cfg = ForwardDiff.HessianConfig(nothing, result, x, chunk(ba, x))
ForwardDiff.hessian!(result, f, x, cfg)
return DiffResults.value(result), (DiffResults.hessian(result),)
end


@kwdef struct CMBLensingMuseProblem{DS<:DataSet,DS_SIM<:DataSet} <: AbstractMuseProblem
ds :: DS
Expand All @@ -26,7 +79,7 @@ using Setfield
θ_fixed = (;)
x = ds.d
latent_vars = nothing
autodiff = AD.HigherOrderBackend((AD.ForwardDiffBackend(tag=false), AD.ZygoteBackend()))
autodiff = AD.HigherOrderBackend((ForwardDiffNoTagBackend(), AD.ZygoteBackend()))
transform_θ = identity
inv_transform_θ = identity
end
Expand Down Expand Up @@ -91,4 +144,4 @@ function MuseInference.muse!(result::MuseResult, ds::DataSet, θ₀=nothing; par
muse!(result, CMBLensingMuseProblem(ds; parameterization, MAP_joint_kwargs), θ₀; kwargs...)
end

end
end