DIT errors with custom types in contexts

Open Beginner friendly
#1,069 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
julia
Domain
testing

Research direction

Start at DifferentiationInterfaceTest/src/scenarios/scenario.jl:78, where zero_contexts handles DI.Constant, and reproduce the shown DIT.Scenario call with MyStruct. Trace how context zero values are constructed and add regression coverage for this custom context; done when the example scenario runs without the shown zero(::MyStruct) error.

Written by the indexing model from the issue text.

Description

import DifferentiationInterface as DI
import DifferentiationInterfaceTest as DIT

struct MyStruct{V}
    v::V
end
ms = MyStruct(randn(2))
f(x, ms) = sum(x .* ms.v)

using ForwardDiff
DI.gradient(f, DI.AutoForwardDiff(), randn(2), DI.Constant(ms)) # This is fine

DIT.Scenario{:gradient,:out}(f, randn(2), DI.Constant(ms)) # This errors

Error:

julia> DIT.Scenario{:gradient,:out}(f, randn(2), DI.Constant(ms))
ERROR: MethodError: no method matching zero(::MyStruct{Vector{Float64}})
The function `zero` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  zero(::Type{Union{}}, Any...)
   @ Base number.jl:359
  zero(::Type{LibGit2.GitHash})
   @ LibGit2 ~/.julia/juliaup/julia-1.13.0+0.aarch64.apple.darwin14/Julia-1.13.app/Contents/Resources/julia/share/julia/stdlib/v1.13/LibGit2/src/oid.jl:221
  zero(::Type{Pkg.Resolve.VersionWeight})
   @ Pkg ~/.julia/juliaup/julia-1.13.0+0.aarch64.apple.darwin14/Julia-1.13.app/Contents/Resources/julia/share/julia/stdlib/v1.13/Pkg/src/Resolve/versionweights.jl:15
  ...

Stacktrace:
 [1] call_composed
   @ ./operators.jl:1110 [inlined]
 [2] (::ComposedFunction{…})(x::DifferentiationInterface.Constant{…}; kw::@Kwargs{})
   @ Base ./operators.jl:1107
 [3] map
   @ ./tuple.jl:354 [inlined]
 [4] zero_contexts(contexts::DifferentiationInterface.Constant{MyStruct{Vector{Float64}}})
   @ DifferentiationInterfaceTest ~/jl/DifferentiationInterface.jl/DifferentiationInterfaceTest/src/scenarios/scenario.jl:78
 [5] (DifferentiationInterfaceTest.Scenario{…})(f::Function, x::Vector{…}, contexts::DifferentiationInterface.Constant{…})
   @ DifferentiationInterfaceTest ~/jl/DifferentiationInterface.jl/DifferentiationInterfaceTest/src/scenarios/scenario.jl:81
 [6] top-level scope
   @ REPL[19]:1
 [7] top-level scope
   @ REPL:1
Some type information was truncated. Use `show(err)` to see complete types.
Dominant language
Julia
Stars
313
Forks
35
PR merge metrics
No merged PRs in 30d

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.

More from JuliaDiff/DifferentiationInterface.jl

All issues in JuliaDiff/DifferentiationInterface.jl

Similar issues

More Julia issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.