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

Support StaticArrays of TrackedReals #154

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/derivatives/linalg/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ for A in ARRAY_TYPES
@eval @inline Base.:+(x::$(A), y::TrackedArray{V,D}) where {V,D} = record_plus(x, y, D)
end

@inline Base.:+(x::TrackedArray{V,D}, y::StaticArray) where {V,D} = record_plus(x, Array(y), D)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try changing to an MArray instead of Array here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then inplace operations will work.

Copy link
Member

@mohamed82008 mohamed82008 Feb 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And please post a test of a MWE that was broken.

@inline Base.:+(x::StaticArray, y::TrackedArray{V,D}) where {V,D} = record_plus(Array(x), y, D)

function record_plus(x, y, ::Type{D}) where D
tp = tape(x, y)
out = track(value(x) + value(y), D, tp)
Expand Down Expand Up @@ -108,6 +111,9 @@ for A in ARRAY_TYPES
@eval Base.:-(x::$(A), y::TrackedArray{V,D}) where {V,D} = record_minus(x, y, D)
end

@inline Base.:-(x::TrackedArray{V,D}, y::StaticArray) where {V,D} = record_minus(x, Array(y), D)
@inline Base.:-(x::StaticArray, y::TrackedArray{V,D}) where {V,D} = record_minus(Array(x), y, D)

function Base.:-(x::TrackedArray{V,D}) where {V,D}
tp = tape(x)
out = track(-(value(x)), D, tp)
Expand Down
4 changes: 4 additions & 0 deletions src/tracked.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ function deriv!(t::NTuple{N,Any}, v::NTuple{N,Any}) where N
return nothing
end

deriv!(t::StaticArray, v::AbstractArray) = deriv!(Tuple(t), Tuple(v))

# pulling values from origin #
#----------------------------#

Expand Down Expand Up @@ -223,6 +225,8 @@ unseed!(x::AbstractArray, i) = unseed!(x[i])
capture(t::TrackedReal) = ifelse(hastape(t), t, value(t))
capture(t::TrackedArray) = t
capture(t::AbstractArray) = istracked(t) ? map!(capture, similar(t), t) : copy(t)
# `StaticArray`s don't support mutation unless the eltype is a bits type (`isbitstype`).
capture(t::SA) where SA <: StaticArray = istracked(t) ? SA(map(capture, t)) : copy(t)

########################
# Conversion/Promotion #
Expand Down