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

LogDensityFunctions interface - duplicated code #2473

Open
penelopeysm opened this issue Jan 20, 2025 · 1 comment
Open

LogDensityFunctions interface - duplicated code #2473

penelopeysm opened this issue Jan 20, 2025 · 1 comment

Comments

@penelopeysm
Copy link
Member

function LogDensityProblemsAD.ADgradient(ℓ::DynamicPPL.LogDensityFunction)
return LogDensityProblemsAD.ADgradient(getADType(DynamicPPL.getcontext(ℓ)), ℓ)
end
function LogDensityProblems.logdensity(
f::Turing.LogDensityFunction{<:AbstractVarInfo,<:Model,<:DynamicPPL.DefaultContext},
x::NamedTuple,
)
return DynamicPPL.logjoint(f.model, DynamicPPL.unflatten(f.varinfo, x))
end
# TODO: make a nicer `set_namedtuple!` and move these functions to DynamicPPL.
function DynamicPPL.unflatten(vi::TypedVarInfo, θ::NamedTuple)
set_namedtuple!(deepcopy(vi), θ)
return vi
end
function DynamicPPL.unflatten(vi::SimpleVarInfo, θ::NamedTuple)
return SimpleVarInfo(θ, vi.logp, vi.transformation)
end

This code looks suspiciously similar to existing code in DynamicPPL, and in any case is type piracy and should be moved there or deleted as appropriate.

Unsure why Aqua doesn't flag this.

@penelopeysm penelopeysm changed the title LogDensityFunctions interface LogDensityFunctions interface - duplicated code Jan 21, 2025
@penelopeysm
Copy link
Member Author

penelopeysm commented Jan 21, 2025

Oh, I see, the ADgradient method is a single-argument version that automatically 'detects' the AD backend, as opposed to the usual two-argument method which takes the AD backend as the first argument.

Still type piracy though, should be given its own name imo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant