TuringLang / TuringLang/SSMProblems.jl

`TypelessZero` convert ambiguity with ForwardDiff dual numbers

Open
#153 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
11
Forks
7
PR merge metrics
No merged PRs in 30d

Description

TypelessZero has an ambiguous convert method when ForwardDiff dual numbers are involved. This occurs when trying to AD through a full particle filter pass (e.g. what happens when you initialise MH with an adtype since Turing v0.43.3.

MethodError: convert(::Type{ForwardDiff.Dual{T, V, N}}, ::GeneralisedFilters.TypelessZero) is ambiguous.

Candidates:
  convert(::Type{ForwardDiff.Dual{T, V, N}}, x::Number) where {T, V, N}
    @ ForwardDiff
  convert(::Type{T}, ::GeneralisedFilters.TypelessZero) where T<:Number
    @ GeneralisedFilters .../containers.jl:17

Context

This surfaces when using GeneralisedFilters inside a Turing model and running PMMH via Turing.externalsampler. As of Turing v0.43.3, parameter initialisation was unified between HMC and external samplers — external samplers now call logdensity_and_gradient during initialisation by default. This causes ForwardDiff dual numbers to flow through the filter even for gradient-free samplers, hitting the ambiguity.

Workaround

Ideally we would just do,

Turing.externalsampler(RobustAdaptiveMetropolis(; S=S); adtype=nothing)

but ExternalSampler forces a ADTypes.AbstractADType rather than an optional Nothing. This might be something that needs fixing upstream.

For now I'm just going to use Mooncake.

Fix

I think we can add a more specific convert method in containers.jl to resolve the ambiguity.

Base.convert(
    ::Type{ForwardDiff.Dual{T,V,N}}, ::TypelessZero
) where {T,V,N} = zero(ForwardDiff.Dual{T,V,N})

Though I question if this suggests there is a more fundamental structural issue so didn't want to jump in too quickly.

@charlesknipp would appreciate your thoughts.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in containers.jl at the TypelessZero convert method, then reproduce the ambiguity by running ForwardDiff through a full particle filter pass in the Turing.externalsampler context. Compare the proposed Dual conversion with the surrounding conversion behavior; done means the full pass no longer raises the ambiguous convert MethodError.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.