Skip to content

Commit

Permalink
Merge #445
Browse files Browse the repository at this point in the history
445: Fix Fill ctor r=MikeInnes a=willtebbutt

The Fill constructor adjoint fell over if you used any indexing operations on a given `Fill`. This is annoying as there are legitimate reasons for wanting to do this.

Co-authored-by: willtebbutt <wt0881@my.bristol.ac.uk>
  • Loading branch information
bors[bot] and willtebbutt committed Jan 11, 2020
2 parents 2ac3528 + 7f4c63a commit 829b2c2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/lib/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ using Base.Broadcast: broadcasted, broadcast_shape

# Array Constructors
@adjoint (::Type{T})(x::T) where T<:Array = T(x), ȳ -> (ȳ,)
@adjoint (::Type{T})(x::Number, sz) where {T <: Fill} = Fill(x, sz), Δ -> (sum(Δ), nothing)
@adjoint function (::Type{T})(x::Number, sz) where {T <: Fill}
back::AbstractArray) = (sum(Δ), nothing)
back::NamedTuple) =.value, nothing)
return Fill(x, sz), back
end

@adjoint (::Type{T})(sz) where {T<:Zeros} = Zeros(sz), Δ->(nothing,)
@adjoint (::Type{T})(sz) where {T<:Ones} = Ones(sz), Δ->(nothing,)

Expand Down
2 changes: 2 additions & 0 deletions test/gradcheck.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,8 @@ end
@test gradcheck(x->sum(Fill(x[], (2, 2))), [0.1])
@test first(Zygote.gradient(sz->sum(Ones(sz)), 6)) === nothing
@test first(Zygote.gradient(sz->sum(Zeros(sz)), 6)) === nothing
@test gradcheck(x->Fill(x[], 5).value, [0.1])
@test gradcheck(x->FillArrays.getindex_value(Fill(x[], 5)), [0.1])
end

@testset "AbstractArray Addition / Subtraction / Negation" begin
Expand Down

0 comments on commit 829b2c2

Please sign in to comment.