JuliaDiff / JuliaDiff/DifferentiationInterface.jl
DIT errors with custom types in contexts
Open
Beginner friendly
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 313
- Forks
- 35
- PR merge metrics
- No merged PRs in 30d
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
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.
Assessment
- Tech stack
- julia
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100