JuliaDiff / JuliaDiff/ReverseDiff.jl
Error when using scalar vs. vector to operate on tracked inupt
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 393
- Forks
- 60
- Avg merge
- 18h 24m
- Merged PRs (30d)
- 8
Description
Hi ReverseDiff team,
I've found an error that comes up when an object used in a function being differentiated is the result of a tracked array and another argument (not the one we are finding the derivative with respect to) when the other argument is a scalar. I understand my explanation is a bit imprecise, a minimum reproducible example is below. In the line labeled "doesn't work" the arg c is treated as a scalar. In the line labeled "works" it is treated as a length one vector. The object d is used in further computation; when c is treated as a scalar this leads to a method error when it (an element of the resulting object d) is used in the line aa*sin(dd).
[EDIT]: I'm on Julia 1.8.3 and the version of ReverseDiff.jl on the Julia general repo (v1.14.4).
using ReverseDiff
using Distributions
# a,b are vectors; want Hessian w.r.t a
# c is "scalar" arg
function mwe(a,b,c)
d = @. a/(b+c)
e = map(a,b,d) do aa, bb, dd
if dd < bb
exp(bb)
else
aa*sin(dd)
end
end
return sum(e)
end
a = rand(Normal(),5)
b = rand(Normal(),5)
c = 50.0
ReverseDiff.hessian(x -> mwe(x, b, c), a) # doesn't work
ReverseDiff.hessian(x -> mwe(x, b, [c]), a) # works
Contributor guide
No contributing guide indexed for this repository
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 by running the provided mwe function with ReverseDiff.hessian on Julia 1.8.3 and ReverseDiff.jl v1.14.4, comparing scalar c with one-element vector [c]. Trace the failing path through the scalar case and verify that the Hessian completes without the method error seen at aa*sin(dd).
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100