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

@non_differentiable _denom #687

Merged
merged 5 commits into from
Jan 11, 2023
Merged
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
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