Skip to content

Commit

Permalink
Fix Base.zero type output
Browse files Browse the repository at this point in the history
zero(x::T)::T is a standard that applies to pretty much any other array type, but TrackedArray fails to match the standard interfaces. This fixes that issue. The only major violation to where this behavior is expected is if you're trying to write a grad rule that's mutating, which really only shows up in rules libraries, and those are thus updated here.

Note that there is an alternative implementation via `zero.(x)`, but this implementation drops the compute graph that isn't needed if you have a zero.
  • Loading branch information
ChrisRackauckas committed Aug 29, 2024
1 parent e384881 commit 2febf9f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Tracker"
uuid = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
version = "0.2.34"
version = "0.2.35"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
13 changes: 7 additions & 6 deletions src/lib/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ Base.getindex(xs::TrackedArray, i...; kwargs...) = track(getindex, xs, i...; kwa

@grad function getindex(xs::AbstractArray, i...; kwargs...)
getindex(data(xs), i...; kwargs...), function (Δ)
Δ′ = zero(xs)
Δ′ = zero(data(xs))
setindex!(Δ′, data(Δ), i...; kwargs...)
(nobacksies(:getindex, Δ′), map(_->nothing, i)...)
end
end

@grad function getindex(xs::AbstractArray, i::Array...)
data(xs)[i...], function (Δ)
Δ′ = zero(xs)
Δ′ = zero(data(xs))
@views Δ′[i...] .+= data(Δ)
(nobacksies(:getindex, Δ′), map(_->nothing, i)...)
end
Expand All @@ -117,7 +117,7 @@ Base.view(x::TrackedArray, inds...; kwargs...) = track(Base.view, x, inds...; kw

@grad function view(x::AbstractArray, inds...; kwargs...)
view(data(x), inds...; kwargs...), function (Δ)
grad_output = zero(x)
grad_output = zero(data(x))
subgrad = view(grad_output, inds...; kwargs...)
subgrad[:] = data(Δ)
(nobacksies(:view, grad_output), map(_->nothing, inds)...)
Expand All @@ -144,10 +144,11 @@ logabsdet(xs::TrackedArray) = track(logabsdet, xs)
@grad logabsdet(xs) = logabsdet(data(xs)), Δ -> (Δ[1] * transpose(inv(xs)),)

Base.repeat(xs::TrackedArray; kw...) = track(repeat, xs; kw...)
Base.zero(x::Tracker.TrackedArray) = zero.(x)

@grad function repeat(xs; inner=ntuple(x->1, ndims(xs)), outer=ntuple(x->1, ndims(xs)))
repeat(data(xs), inner = inner, outer = outer), function (Δ)
Δ′ = zero(xs)
Δ′ = zero(data(xs))
S = size(xs)

# Loop through each element of Δ, calculate source dimensions, accumulate into Δ′
Expand Down Expand Up @@ -433,7 +434,7 @@ Base.minimum(xs::TrackedArray; dims = :) = track(minimum, xs, dims = dims)

@grad function maximum(xs; dims = dims)
maximum(data(xs), dims = dims), function (Δ)
Δ′ = zero(xs)
Δ′ = zero(data(xs))
_, i = findmax(data(xs), dims = dims)
Δ′[i] = data(Δ)
return (nobacksies(:maximum, Δ′),)
Expand All @@ -442,7 +443,7 @@ end

@grad function minimum(xs; dims = dims)
minimum(data(xs), dims = dims), function (Δ)
Δ′ = zero(xs)
Δ′ = zero(data(xs))
_, i = findmin(data(xs), dims = dims)
Δ′[i] = data(Δ)
return (nobacksies(:minimum, Δ′),)
Expand Down
5 changes: 5 additions & 0 deletions test/tracker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ RNG = NNlib.Random.MersenneTwister(1)

end # @testset gradtests

@testset "zero" begin
@test zero(TrackedArray(rand(2))) isa TrackedArray
@test gradtest(x-> zero(x) .* x, (2,))
end

@testset "indexing & slicing" begin
@test gradtest(x->view(x, 1:2, 1:2), rand(4, 4))
end
Expand Down

2 comments on commit 2febf9f

@ChrisRackauckas
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/114071

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.35 -m "<description of version>" 2febf9f1737eeb48f8d841eba0c3b332adc78f7b
git push origin v0.2.35

Please sign in to comment.