-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
broadcast.jl
216 lines (168 loc) · 7.89 KB
/
broadcast.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# .-'''-. _..._
# ' _ \ _______ .-'_..._''.
# /| / /` '. \ \ ___ `'. .' .' '.\
# || . | \ ' ' |--.\ \ / .'
# || .-,.--. | ' | ' | | \ ' . ' .|
# || __ | .-. |\ \ / / __ | | | '| | __ .' |_
# ||/'__ '. | | | | `. ` ..' /.:--.'. | | | || | .:--.'. _ .' |
# |:/` '. '| | | | '-...-'`/ | \ | | | ' .'. ' / | \ | .' |'--. .-'
# || | || | '- `" __ | | | |___.' /' \ '. .`" __ | | . | / | |
# ||\ / '| | .'.''| | /_______.'/ '. `._____.-'/ .'.''| | .'.'| |// | |
# |/\'..' / | | / / | |_\_______|/ `-.______ / / / | |_.'.'.-' / | '.'
# ' `'-'` |_| \ \._,\ '/ ` \ \._,\ '/.' \_.' | /
# `--' `" `--' `" `'-'
using Base.Broadcast
using Base.Broadcast: Broadcasted, AbstractArrayStyle, broadcasted, materialize
using NNlib
# There's a saying that debugging code is about twice as hard as writing it in
# the first place. So if you're as clever as you can be when writing code, how
# will you ever debug it?
# AD faces a similar dilemma: if you write code that's as clever as the compiler
# can handle, how will you ever differentiate it? Differentiating makes clever
# code that bit more complex and the compiler gives up, usually resulting in
# 100x worse performance.
# Base's broadcasting is very cleverly written, and this makes differentiating
# it... somewhat tricky.
# Utilities
# =========
accum_sum(xs; dims = :) = reduce(accum, xs, dims = dims)
# Work around reducedim_init issue
# https://github.com/JuliaLang/julia/issues/31427
accum_sum(xs::Nothing; dims = :) = nothing
accum_sum(xs::AbstractArray{Nothing}; dims = :) = nothing
accum_sum(xs::AbstractArray{<:Number}; dims = :) = sum(xs, dims = dims)
accum_sum(xs::AbstractArray{<:AbstractArray{<:Number}}; dims = :) = sum(xs, dims = dims)
accum_sum(xs::Number; dims = :) = xs
# https://github.com/FluxML/Zygote.jl/issues/594
function Base.reducedim_init(::typeof(identity), ::typeof(accum), A::AbstractArray, region)
Base.reducedim_initarray(A, region, nothing, Union{Nothing,eltype(A)})
end
trim(x, Δ) = reshape(Δ, ntuple(i -> size(Δ, i), Val(ndims(x))))
unbroadcast(x::AbstractArray, x̄) =
size(x) == size(x̄) ? x̄ :
length(x) == length(x̄) ? trim(x, x̄) :
trim(x, accum_sum(x̄, dims = ntuple(i -> size(x, i) == 1 ? i : ndims(x̄)+1, Val(ndims(x̄)))))
unbroadcast(x::Number, x̄) = accum_sum(x̄)
unbroadcast(x::Tuple{<:Any}, x̄) = (accum_sum(x̄),)
unbroadcast(x::Base.RefValue, x̄) = (x=accum_sum(x̄),)
unbroadcast(x::AbstractArray, x̄::Nothing) = nothing
# Split Reverse Mode
# ==================
# TODO: use DiffRules here. It's complicated a little by the fact that we need
# to do CSE, then broadcast-ify the expression so that the closure captures the
# right arrays.
Numeric{T<:Number} = Union{T,AbstractArray{<:T}}
@adjoint broadcasted(::typeof(+), xs::Numeric...) =
broadcast(+, xs...), ȳ -> (nothing, map(x -> unbroadcast(x, ȳ), xs)...)
@adjoint broadcasted(::typeof(-), x::Numeric, y::Numeric) = x .- y,
Δ -> (nothing, unbroadcast(x, Δ), -unbroadcast(y, Δ))
@adjoint broadcasted(::typeof(*), x::Numeric, y::Numeric) = x.*y,
z̄ -> (nothing, unbroadcast(x, z̄ .* conj.(y)), unbroadcast(y, z̄ .* conj.(x)))
@adjoint function broadcasted(::typeof(/), x::Numeric, y::Numeric)
res = x ./ y
res, Δ -> (nothing, unbroadcast(x, Δ ./ conj.(y)), unbroadcast(y, -Δ .* conj.(res ./ y)))
end
@adjoint function broadcasted(::typeof(Base.literal_pow), ::typeof(^), x::Numeric, exp::Val{p}) where p
y = Base.literal_pow.(^, x, exp)
y, ȳ -> (nothing, nothing, ȳ .* p .* conj.(x .^ (p - 1)), nothing)
end
@adjoint broadcasted(::typeof(identity), x::Numeric) = x, Δ -> (nothing, Δ)
@adjoint function broadcasted(::typeof(σ), x::Numeric)
y = σ.(x)
y, ȳ -> (nothing, ȳ .* conj.(y .* (1 .- y)))
end
@adjoint function broadcasted(::typeof(tanh), x::Numeric)
y = tanh.(x)
y, ȳ -> (nothing, ȳ .* conj.(1 .- y.^2))
end
@adjoint broadcasted(::typeof(conj), x::Numeric) =
conj.(x), z̄ -> (nothing, conj.(z̄))
@adjoint broadcasted(::typeof(real), x::Numeric) =
real.(x), z̄ -> (nothing, real.(z̄))
@adjoint broadcasted(::typeof(imag), x::Numeric) =
imag.(x), z̄ -> (nothing, im .* real.(z̄))
# General Fallback
# ================
# The fused reverse mode implementation is the most general but currently has
# poor performance. It works by flattening the broadcast and mapping the call to
# `_pullback` over the input.
# However, the core call
# broadcast(_pullback, (cx,), f, args...)
# is already 10x slower than a simple broadcast (presumably due to inlining
# issues, or something similar) and the other operations needed take it to about
# 100x overhead.
@generated inclen(::NTuple{N,Any}) where N = Val(N+1)
# Avoid hitting special cases for `Adjoint` etc.
_broadcast(f::F, x...) where F = materialize(broadcasted(f, x...))
_get(x::Tuple, i) = x[i]
_get(::Nothing, i) = nothing
collapse_nothings(xs::Vector{Nothing}) = nothing
collapse_nothings(xs) = xs
@adjoint function broadcasted(::AbstractArrayStyle, f, args...)
len = inclen(args)
y∂b = _broadcast((x...) -> _pullback(__context__, f, x...), args...)
y = map(x -> x[1], y∂b)
∂b = map(x -> x[2], y∂b)
y, function (ȳ)
dxs_zip = map((∂b, ȳ) -> ∂b(ȳ), ∂b, ȳ)
dxs = collapse_nothings.(ntuple(i -> map(x -> _get(x, i), dxs_zip), len))
(nothing, accum_sum(dxs[1]), map(unbroadcast, args, Base.tail(dxs))...)
end
end
@adjoint function broadcasted(::AbstractArrayStyle{0}, f, args...)
len = inclen(args)
y, ∂b = _broadcast((x...) -> _pullback(__context__, f, x...), args...)
y, function (ȳ)
dxs = ∂b(ȳ)
(nothing, dxs...)
end
end
# Use the `map` adjoint in this special case, which is the same but applies
# pullbacks in reverse order.
# This leaves regular `broadcast` technically incorrect when the broadcasted
# function is stateful.
# Look, I'm not proud of it, but this is extremely rare in practice.
# @adjoint function broadcasted(f, x)
# ∇map(__context__, f, x)
# end
@adjoint! (b::typeof(broadcast))(f, args...) = _pullback(__context__, broadcasted, f, args...)
# Forward Mode (mainly necessary for CUDA)
import ForwardDiff
using ForwardDiff: Dual
dual(x, p) = x
dual(x::Real, p) = Dual(x, p)
dualtype(::Type{Dual{G,T,P}}) where {G,T,P} = T
dualtype(T) = T
function dual_function(f::F) where F
function (args::Vararg{Any,N}) where N
ds = map(args, ntuple(identity,Val(N))) do x, i
dual(x, ntuple(j -> i==j, Val(N)))
end
return f(ds...)
end
end
@inline function broadcast_forward(f, args::Vararg{Any,N}) where N
T = Broadcast.combine_eltypes(f, args)
out = dual_function(f).(args...)
eltype(out) <: Dual || return (out, _ -> nothing)
y = map(x -> x.value, out)
_back(ȳ, i) = unbroadcast(args[i], ((a, b) -> a*b.partials[i]).(ȳ, out))
back(ȳ) = ntuple(i -> _back(ȳ, i), N)
return y, back
end
@init @require CUDA="052768ef-5323-5732-b1bb-66c8b64840ba" begin
const CuArrayStyle = CUDA.CuArrayStyle
@adjoint function broadcasted(::CuArrayStyle, f, args...)
y, back = broadcast_forward(CUDA.cufunc(f), args...)
y, ȳ -> (nothing, nothing, back(ȳ)...)
end
@adjoint CUDA.CuArray{N,T}(xs::Array) where {N,T} =
CUDA.CuArray{N,T}(xs), Δ -> (convert(Array, Δ), )
@adjoint function sum(xs::CUDA.CuArray; dims = :)
placeholder = similar(xs)
sum(xs, dims = dims), Δ -> (placeholder .= Δ,)
end
@adjoint function Base.convert(::Type{T}, xs::Array) where {T<:CUDA.CuArray}
Base.convert(T, xs), Δ -> (nothing, Base.convert(Array, Δ),)
end
end