Skip to content

Commit

Permalink
@non_differentiable _denom (#687)
Browse files Browse the repository at this point in the history
* non_differentiable _denom

* Update Project.toml

* ∇getindex(::AbstractZero) paths

* non_differentiable foreach(f, ())

* also fix summary
  • Loading branch information
mcabbott authored Jan 11, 2023
1 parent 9a405f7 commit 4ee4ef5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRules"
uuid = "082447d4-558c-5d27-93f4-14fc19e9eca2"
version = "1.46.0"
version = "1.46.1"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
15 changes: 7 additions & 8 deletions src/rulesets/Base/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ function frule((_, ẋ), ::typeof(getindex), x::AbstractArray, inds...)
end

function rrule(::typeof(getindex), x::AbstractArray, inds...)
function getindex_pullback(dy)
nots = map(Returns(NoTangent()), inds)
return (NoTangent(), thunked_∇getindex(x, dy, inds...), nots...)
end
nots = map(Returns(NoTangent()), inds)
getindex_pullback(dy) = (NoTangent(), thunked_∇getindex(x, dy, inds...), nots...)
getindex_pullback(z::AbstractZero) = (NoTangent(), z, nots...)
return x[inds...], getindex_pullback
end

Expand All @@ -90,6 +89,7 @@ function ∇getindex(x::AbstractArray, dy, inds...)
∇getindex!(dx, dy, plain_inds...)
return ProjectTo(x)(dx) # since we have x, may as well do this inside, not in rules
end
∇getindex(x::AbstractArray, z::AbstractZero, inds...) = z

"""
_setindex_zero(x, dy, inds...)
Expand Down Expand Up @@ -191,10 +191,9 @@ function frule((_, ẋ), ::typeof(view), x::AbstractArray, inds...)
end

function rrule(::typeof(view), x::AbstractArray, inds...)
function view_pullback(dy)
nots = map(Returns(NoTangent()), inds)
return (NoTangent(), thunked_∇getindex(x, dy, inds...), nots...)
end
nots = map(Returns(NoTangent()), inds)
view_pullback(dy) = (NoTangent(), thunked_∇getindex(x, dy, inds...), nots...)
view_pullback(z::AbstractZero) = (NoTangent(), z, nots...)
return view(x, inds...), view_pullback
end

Expand Down
3 changes: 3 additions & 0 deletions src/rulesets/Base/nondiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
@non_differentiable floatmax(::Any)
@non_differentiable floatmin(::Any)
@non_differentiable flush(::Any)
@non_differentiable foreach(::Any, ::Tuple{})

@non_differentiable gensym(::Symbol)
@non_differentiable gensym(::String...)
Expand Down Expand Up @@ -422,6 +423,7 @@ end
@non_differentiable supertype(::Any)
@non_differentiable Symbol(::Any...)
@non_differentiable symlink(::AbstractString, ::AbstractString)
@non_differentiable summary(::Any)

@non_differentiable take!(::Base.GenericIOBuffer)
@non_differentiable take!(::IOStream)
Expand Down Expand Up @@ -472,6 +474,7 @@ elseif isdefined(Base, :cumulative_compile_time_ns)
end
@non_differentiable Base.time_print(::Any...)
@non_differentiable Base.OneTo(::Any...)
@non_differentiable Base.array_summary(::Any)

@non_differentiable Broadcast.combine_styles(::Any...)
@non_differentiable Broadcast.result_style(::Any)
Expand Down
2 changes: 2 additions & 0 deletions src/rulesets/Statistics/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ _denom(x, dims) = size(x, dims)
_denom(x, dims::Colon) = length(x)
_denom(x, dims::Union{Tuple, AbstractArray}) = mapreduce(i->size(x, i), Base.mul_prod, unique(dims), init=1)

@non_differentiable _denom(::Any, ::Any) # else Zygote tries to AD unique(::Tuple)

function rrule(::typeof(mean), x::AbstractArray{<:Union{Real,Complex,AbstractArray}}; dims=:)
y_sum, sum_pullback = rrule(sum, x; dims)
n = _denom(x, dims)
Expand Down

2 comments on commit 4ee4ef5

@mcabbott
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
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/75489

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 v1.46.1 -m "<description of version>" 4ee4ef5ffa86809885a944d8167503b7e2e550fa
git push origin v1.46.1

Please sign in to comment.