Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StaticArray of TrackedReals doesn't currently work (and I think it could) #153

Open
bzinberg opened this issue Oct 14, 2020 · 0 comments
Open

Comments

@bzinberg
Copy link

bzinberg commented Oct 14, 2020

Sorry for the lack of minimal example here, I'll add it when I get the chance.

I recently tried to differentiate through operations on StaticArrays.SVectors and hit two issues, both of which were easily fixable once diagnosed (unless these solutions cause other problems I'm not aware of):

  1. StaticArrays defines a catch-all +(::StaticArray, ::AbstractArray) and ReverseDiff defines a catch-all +(::AbstractArray, ::TrackedArray). So if +(::StaticArray, ::TrackedArray) is called, Julia complains that the dispatch rule is ambiguous. (Same for the other order of operands too.) The same is the case for -, and I'm guessing also for *.

  2. This line

    capture(t::AbstractArray) = istracked(t) ? map!(capture, similar(t), t) : copy(t)

    assumes that similar(t) supports setindex!. In the case of StaticArrays, this assumption breaks if isbitstype(eltype(t)) is false, and in particular if t is an StaticArray of TrackedReals. Similar issue on this line:
    @inline deriv!(t::TrackedArray, v::AbstractArray) = (copyto!(deriv(t), v); nothing)

I was able to fix these locally by:

  1. Explicitly defining +(::StaticArray, ::TrackedArray) and +(::TrackedArray, ::StaticArray), to disambiguate the dispatch rulea similarly for - (and probably also for *, though I didn't have the need for that). In order to not encounter the setindex! issue, I had to cast the StaticArray to an Array first.

    1. Redefining ReverseDiff.capture on StaticArrays to do the same thing without mutation (using map + array conversion instead of similar + map!). As an aside, if this is equally efficient, seems like we might as well make this the default definition.
    2. Redefining deriv!(::StaticArray ::AbstractArray) to apply deriv! elementwise, like it would if the argument were a Tuple.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant