-
Notifications
You must be signed in to change notification settings - Fork 89
/
mapreduce.jl
193 lines (175 loc) · 6.08 KB
/
mapreduce.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
#####
##### `sum`
#####
function frule((_, ẋ), ::typeof(sum), x; dims=:)
return sum(x; dims=dims), sum(ẋ; dims=dims)
end
function rrule(::typeof(sum), x::AbstractArray{T}; dims=:) where {T<:Number}
y = sum(x; dims=dims)
function sum_pullback(ȳ)
# broadcasting the two works out the size no-matter `dims`
x̄ = InplaceableThunk(
@thunk(broadcast(last∘tuple, x, ȳ)),
x -> x .+= ȳ
)
return (NoTangent(), x̄)
end
return y, sum_pullback
end
# Can't map over Adjoint/Transpose Vector
function rrule(
config::RuleConfig{>:HasReverseMode},
::typeof(sum),
f,
xs::Union{Adjoint{<:Number,<:AbstractVector},Transpose{<:Number,<:AbstractVector}};
kwargs...
)
op = xs isa Adjoint ? adjoint : transpose
# since summing a vector we don't need to worry about dims which simplifies adjointing
vector = parent(xs)
y, vector_sum_pb = rrule(config, sum, f, vector; kwargs...)
function covector_sum_pb(ȳ)
s̄um, f̄, v̄ = vector_sum_pb(ȳ)
return s̄um, f̄, op(v̄)
end
return y, covector_sum_pb
end
function rrule(
config::RuleConfig{>:HasReverseMode}, ::typeof(sum), f, xs::AbstractArray; dims=:
)
fx_and_pullbacks = map(x->rrule_via_ad(config, f, x), xs)
y = sum(first, fx_and_pullbacks; dims=dims)
pullbacks = last.(fx_and_pullbacks)
function sum_pullback(ȳ)
call(f, x) = f(x) # we need to broadcast this to handle dims kwarg
f̄_and_x̄s = call.(pullbacks, ȳ)
# no point thunking as most of work is in f̄_and_x̄s which we need to compute for both
f̄ = if fieldcount(typeof(f)) === 0 # Then don't need to worry about derivative wrt f
NoTangent()
else
sum(first, f̄_and_x̄s)
end
x̄s = map(last, f̄_and_x̄s)
return NoTangent(), f̄, x̄s
end
return y, sum_pullback
end
function frule(
(_, _, ẋ),
::typeof(sum),
::typeof(abs2),
x::AbstractArray{T};
dims=:,
) where {T<:Union{Real,Complex}}
y = sum(abs2, x; dims=dims)
∂y = if dims isa Colon
2 * real(dot(x, ẋ))
elseif VERSION ≥ v"1.2" # multi-iterator mapreduce introduced in v1.2
mapreduce(+, x, ẋ; dims=dims) do xi, dxi
2 * _realconjtimes(xi, dxi)
end
else
2 * sum(_realconjtimes.(x, ẋ); dims=dims)
end
return y, ∂y
end
function rrule(
::typeof(sum),
::typeof(abs2),
x::AbstractArray{T};
dims=:,
) where {T<:Union{Real,Complex}}
y = sum(abs2, x; dims=dims)
function sum_abs2_pullback(ȳ)
x_thunk = InplaceableThunk(
@thunk(2 .* real.(ȳ) .* x),
dx -> dx .+= 2 .* real.(ȳ) .* x
)
return (NoTangent(), NoTangent(), x_thunk)
end
return y, sum_abs2_pullback
end
# Fix dispatch for this pidgeon-hole optimization,
# Rules with RuleConfig dispatch with priority over without (regardless of other args).
# and if we don't specify what do do for one that HasReverseMode then it is ambigious
for Config in (RuleConfig, RuleConfig{>:HasReverseMode})
@eval function rrule(
::$Config, ::typeof(sum), ::typeof(abs2), x::AbstractArray{T}; dims=:,
) where {T<:Union{Real,Complex}}
return rrule(sum, abs2, x; dims=dims)
end
end
#####
##### `prod`
#####
function rrule(::typeof(prod), x::AbstractArray{T}; dims=:) where {T<:CommutativeMulNumber}
y = prod(x; dims=dims)
# vald = dims isa Colon ? nothing : dims isa Integer ? Val(Int(dims)) : Val(Tuple(dims))
function prod_pullback(dy)
x_thunk = InplaceableThunk(
# Out-of-place versions
@thunk if dims === (:)
∇prod(x, dy, y)
elseif any(iszero, x) # Then, and only then, will ./x lead to NaN
vald = dims isa Colon ? nothing : dims isa Integer ? Val(Int(dims)) : Val(Tuple(dims))
∇prod_dims(vald, x, dy, y) # val(Int(dims)) is about 2x faster than Val(Tuple(dims))
else
conj.(y ./ x) .* dy
end
,
# In-place versions -- same branching
dx -> if dims === (:)
∇prod!(dx, x, dy, y)
elseif any(iszero, x)
vald = dims isa Colon ? nothing : dims isa Integer ? Val(Int(dims)) : Val(Tuple(dims))
∇prod_dims!(dx, vald, x, dy, y)
else
dx .+= conj.(y ./ x) .* dy
end
)
return (NoTangent(), x_thunk)
end
return y, prod_pullback
end
function ∇prod_dims(vald::Val{dims}, x, dy, y=prod(x; dims=dims)) where {dims}
T = promote_type(eltype(x), eltype(dy))
dx = fill!(similar(x, T, axes(x)), zero(T))
∇prod_dims!(dx, vald, x, dy, y)
return dx
end
function ∇prod_dims!(dx, ::Val{dims}, x, dy, y) where {dims}
iters = ntuple(d -> d in dims ? tuple(:) : axes(x,d), ndims(x)) # Without Val(dims) this is a serious type instability
@inbounds for ind in Iterators.product(iters...)
jay = map(i -> i isa Colon ? 1 : i, ind)
@views ∇prod!(dx[ind...], x[ind...], dy[jay...], y[jay...])
end
return dx
end
function ∇prod(x, dy::Number=1, y::Number=prod(x))
T = promote_type(eltype(x), eltype(dy))
dx = fill!(similar(x, T, axes(x)), zero(T)) # axes(x) makes MArray on StaticArrays, Array for structured matrices
∇prod!(dx, x, dy, y)
return dx
end
function ∇prod!(dx, x, dy::Number=1, y::Number=prod(x))
numzero = iszero(y) ? count(iszero, x) : 0
if numzero == 0 # This can happen while y==0, if there are several small xs
dx .+= conj.(y ./ x) .* dy
elseif numzero == 1
∇prod_one_zero!(dx, x, dy)
else
# numzero > 1, then all first derivatives are zero
end
return dx
end
function ∇prod_one_zero!(dx, x, dy::Number=1) # Assumes exactly one x is zero
i_zero = 0
p_rest = one(promote_type(eltype(x), typeof(dy)))
for i in eachindex(x)
xi = @inbounds x[i]
p_rest *= ifelse(iszero(xi), one(xi), conj(xi))
i_zero = ifelse(iszero(xi), i, i_zero)
end
dx[i_zero] += p_rest * dy
return
end