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

Minor broadcasting fixes #673

Merged
merged 5 commits into from
Sep 4, 2022
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.44.5"
version = "1.44.6"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
8 changes: 6 additions & 2 deletions src/rulesets/Base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,14 @@ function split_bc_pullbacks(cfg::RCR, f::F, args::Vararg{Any,N}) where {F,N}
rrule_via_ad(cfg, f, a...)
end
function back_generic(dys)
deltas = unzip_broadcast(backs, unthunk(dys)) do back, dy # (could be map, sizes match)
deltas = unzip_broadcast(backs, dys) do back, dy # (could be map, sizes match)
map(unthunk, back(dy))
end
dargs = map(unbroadcast, args, Base.tail(deltas))
df = ProjectTo(f)(sum(first(deltas)))
return (NoTangent(), NoTangent(), df, dargs...)
end
back_generic(dys::AbstractThunk) = back_generic(unthunk(dys))
back_generic(z::AbstractZero) = (TRI_NO..., map(Returns(z), args)...)
return ys3, back_generic
Comment on lines 130 to 140
Copy link
Member Author

Choose a reason for hiding this comment

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

Somehow Diffractor fed this pullback a thunk containing a ZeroTangent. And... well maybe I tried this with #671 which changes this to unzip_map, and that was not happy about the zero. So I changed them all to have 3 methods, not two.

Copy link
Member Author

Choose a reason for hiding this comment

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

Now some removed, as this caused inference problems.

end
Expand Down Expand Up @@ -318,7 +319,7 @@ rrule(::typeof(broadcasted), ::typeof(complex), x::Number) = rrule(complex, x) |

function unbroadcast(x::Base.AbstractArrayOrBroadcasted, dx_raw)
dx = unthunk(dx_raw)
N = ndims(dx)
N = _ndims(dx)
Comment on lines 320 to +322
Copy link
Member Author

Choose a reason for hiding this comment

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

This one has a test, lazy broadcasting of - means this can be called with dx::Tuple

if length(x) == length(dx)
ProjectTo(x)(dx) # handles trivial reshapes, offsets, structured matrices, row vectors
else
Expand All @@ -328,6 +329,9 @@ function unbroadcast(x::Base.AbstractArrayOrBroadcasted, dx_raw)
end
unbroadcast(x::Base.AbstractArrayOrBroadcasted, dx::AbstractZero) = dx

_ndims(x) = ndims(x)
_ndims(::Tuple) = 1

function unbroadcast(x::T, dx_raw) where {T<:Tuple{Vararg{Any,N}}} where {N}
dx = unthunk(dx_raw)
val = if N == length(dx)
Expand Down
5 changes: 5 additions & 0 deletions src/rulesets/Base/nondiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,11 @@ end
@non_differentiable Broadcast.result_style(::Any)
@non_differentiable Broadcast.result_style(::Any, ::Any)

@non_differentiable Base.CoreLogging.current_logger_for_env(::Any...)
@non_differentiable Base.CoreLogging._invoked_shouldlog(::Any...)
@non_differentiable Base.CoreLogging.Base.fixup_stdlib_path(::Any)
@non_differentiable Base.CoreLogging.handle_message(::Any...)
Copy link
Member Author

Choose a reason for hiding this comment

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

These fix this -- a second derivative notices @debug inside rules, and fails.

julia> gradient([1,2,3]) do x
         gradient(x) do y
           sum(y .* y)
         end[1] |> sum
       end
ERROR: (1, current_logger_for_env(std_level::Base.CoreLogging.LogLevel, group, _module) @ Base.CoreLogging logging.jl:500, :($(Expr(:meta, :noinline))))
Stacktrace:
  [1] error(s::Tuple{Int64, Method, Expr})
    @ Base ./error.jl:44
  [2] transform!(ci::Core.CodeInfo, meth::Method, nargs::Int64, sparams::Core.SimpleVector, N::Int64)
    @ Diffractor ~/.julia/dev/Diffractor/src/stage1/recurse.jl:621
  [3] perform_optic_transform(ff::Type{Diffractor.∂⃖recurse{1}}, args::Any)
    @ Diffractor ~/.julia/dev/Diffractor/src/stage1/generated.jl:25


@non_differentiable Libc.free(::Any)
@non_differentiable Libc.getpid()
@non_differentiable Libc.strptime(::AbstractString)
Expand Down
1 change: 1 addition & 0 deletions test/rulesets/Base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,6 @@ BT1 = Broadcast.BroadcastStyle(Tuple)

@testset "bugs" begin
@test ChainRules.unbroadcast((1, 2, [3]), [4, 5, [6]]) isa Tangent # earlier, NTuple demanded same type
@test ChainRules.unbroadcast(broadcasted(-, (1, 2), 3), (4, 5)) == (4, 5) # earlier, called ndims(::Tuple)
end
end