JuliaDiff / JuliaDiff/DifferentiationInterface.jl

Inconsistency in handling empty arguments

Open
#802 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When differentiating with respect to an empty array, the results tend to vary:

using DifferentiationInterface, ForwardDiff, ReverseDiff, Mooncake, Enzyme

ADTYPES = [
    AutoForwardDiff(),
    AutoReverseDiff(),
    AutoMooncake(; config=nothing),
    AutoEnzyme(; mode=Forward),
    AutoEnzyme(; mode=Reverse),
    # and more...
]

for adtype in ADTYPES
    DifferentiationInterface.value_and_gradient(sum, adtype, Float64[])
end

ReverseDiff, Mooncake, and reverse Enzyme all happily return (0.0, []) 😄

Forward Enzyme tries to use a batch size of 0 and errors:

https://github.com/JuliaDiff/DifferentiationInterface.jl/blob/6a58124902a9cc3e623014db28eacd4b56ba4434/DifferentiationInterface/ext/DifferentiationInterfaceEnzymeExt/utils.jl#L11-L14

And ForwardDiff tries to construct a GradientResult which errors:

https://github.com/JuliaDiff/DifferentiationInterface.jl/blob/6a58124902a9cc3e623014db28eacd4b56ba4434/DifferentiationInterface/ext/DifferentiationInterfaceForwardDiffExt/onearg.jl#L315-L318

https://github.com/JuliaDiff/DiffResults.jl/blob/fcf7858d393f0597fc74e195ed46f7bcbe5ff66c/src/DiffResults.jl#L64-L65

Funnily enough gradient with ForwardDiff (rather than value_and_gradient) is fine because it doesn't try to construct the GradientResult. I imagine the other operators would also have varying behaviour.

I suppose it is a bit of a trivial edge case, but would it be possible to unify the behaviour of the AD backends?

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 with the linked empty-argument handling in DifferentiationInterface/ext/DifferentiationInterfaceEnzymeExt/utils.jl and DifferentiationInterface/ext/DifferentiationInterfaceForwardDiffExt/onearg.jl, then run the provided value_and_gradient example across the listed backends. Done means the affected backends and operators have deliberately consistent behavior for empty arguments.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.