Skip to content

convert active argument in case of a preparation mismatch #859

@tpapp

Description

@tpapp

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    coreRelated to the core utilities of the package

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions