JuliaDiff / JuliaDiff/DifferentiationInterface.jl

convert active argument in case of a preparation mismatch

Open
#859 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I understand that the argument type has to match the prepared backend. However, instead of erroring, in some situations it would make sense to just convert to the right type. Eg in the example below, the last line could work, in accordance with the robustness principle (with the understanding that it allocates, is suboptimal, etc).

julia> import ForwardDiff

julia> import DifferentiationInterface as DI

julia> backend = DI.AutoForwardDiff()
AutoForwardDiff()

julia> test(x) = x .+ 1
test (generic function with 2 methods)

julia> x = ones(3);

julia> prep = prepare_jacobian(test, backend, x);

julia> DI.value_and_jacobian(test, prep, backend, x)
([2.0, 2.0, 2.0], [1.0 0.0 0.0; 0.0 1.0 0.0; 0.0 0.0 1.0])

julia> DI.value_and_jacobian(test, prep, backend, Float32.(x))
ERROR: PreparationMismatchError (inconsistent types between preparation and execution):
  - f: ✅
  - backend: ✅
  - x: ❌
    - prep: Vector{Float64}
    - exec: Vector{Float32}
  - contexts: ✅
If you are confident that this check is superfluous, you can disable it by running preparation with the keyword argument `strict=Val(false)` inside DifferentiationInterface.

I would allow this always, but I understand that some users would just want to catch this performance issue as an error. So maybe an option, or a wrapper-like API

DI.value_and_jacobian(test, prep, backend, AutoConvert(Float32.(x)))

would make sense.

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 the prepare_jacobian and value_and_jacobian entry points shown in the issue, along with the PreparationMismatchError behavior. Compare the proposed automatic conversion with the strict preparation check and the suggested AutoConvert wrapper. Done means the project has a decided API and behavior for handling compatible type mismatches.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.