JuliaDiff / JuliaDiff/DifferentiationInterface.jl

Feat: A third preparation mode between `strict=Val(true)` and `strict=Val(false)`: fall back to unprepared execution on signature mismatch

Open
#1,052 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
313
Forks
35
PR merge metrics
No merged PRs in 30d

Description

Suggested by @gdalle in rsenne/ParallelMCMC.jl#62

the strict kwarg in prepare_gradient has two modes: true and false with the basic idea that true just ensures the type matches else throws PreparationMismatchError

It could be useful to have a middle ground between the lax and strict mode (In ParallelMCMC I have a
gradient prepared for Vector{Float64} that gets called on Vector{<:Dual}). E.g., like i wrote in PMCMC:

struct _ADGradient{F,B<:AbstractADType,P,TX}
    logdensity::F
    backend::B
    prep::P
end

function (g::_ADGradient{F,B,P,TX})(x) where {F,B,P,TX}
    if x isa TX
        return DI.gradient(g.logdensity, g.prep, g.backend, x)   # fast path
    else
        return DI.gradient(g.logdensity, g.backend, x)           # from scratch
    end
end

So simply, if prep matches, great, else run from scratch

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 by reading prepare_gradient and its existing strict=true and strict=false behavior, then use the proposed _ADGradient callable path as the concrete reference for the fallback semantics. Done means a third preparation mode is defined and implemented so matching signatures use the preparation while mismatches execute from scratch.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.