-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathindexing.jl
238 lines (191 loc) · 10.2 KB
/
indexing.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
@testset "getindex" begin
@testset "getindex(::Tuple, ...)" begin
x = (1.2, 3.4, 5.6)
x2 = (rand(2), (a=1.0, b=x))
# Forward
test_frule(getindex, x, 2)
test_frule(getindex, x2, 1)
test_frule(getindex, x, 1:2)
test_frule(getindex, x2, :)
# Reverse
test_rrule(getindex, x, 2)
@test_skip test_rrule(getindex, x2, 1, check_inferred=false) # method ambiguity, maybe fixed by https://github.com/JuliaDiff/ChainRulesTestUtils.jl/pull/253
test_rrule(getindex, x, 2:3; check_inferred=false)
test_rrule(getindex, x, [1, 1, 2], check_inferred=false)
test_rrule(getindex, x2, 1:2, check_inferred=false)
test_rrule(getindex, x, :)
end
@testset "getindex(::Matrix{<:Number}, ...)" begin
x = [1.0 2.0 3.0; 10.0 20.0 30.0]
@testset "forward mode" begin
test_frule(getindex, x, 2)
test_frule(getindex, x, 2, 1)
test_frule(getindex, x, CartesianIndex(2, 3))
test_frule(getindex, x, 2:3)
test_frule(getindex, x, (:), 2:3)
end
@testset "single element" begin
test_rrule(getindex, x, 2)
test_rrule(getindex, x, 2, 1; check_inferred=false)
test_rrule(getindex, x, 2, 2; check_inferred=false)
test_rrule(getindex, x, CartesianIndex(2, 3); check_inferred=false)
end
@testset "slice/index positions" begin
test_rrule(getindex, x, 2:3)
test_rrule(getindex, x, 3:-1:2)
test_rrule(getindex, x, [3,2])
test_rrule(getindex, x, [2,3])
test_rrule(getindex, x, 1:2, 2:3)
test_rrule(getindex, x, (:), 2:3)
test_rrule(getindex, x, 1:2, 1)
test_rrule(getindex, x, 1, 1:2)
test_rrule(getindex, x, 1:2, 2:3)
test_rrule(getindex, x, (:), 2:3)
test_rrule(getindex, x, (:), (:))
test_rrule(getindex, x, (:))
end
@testset "masking" begin
test_rrule(getindex, x, trues(size(x)))
test_rrule(getindex, x, trues(length(x)))
mask = falses(size(x))
mask[2,3] = true
mask[1,2] = true
test_rrule(getindex, x, mask)
test_rrule(getindex, x, [true, false], (:))
end
@testset "By position with repeated elements" begin
test_rrule(getindex, x, [2, 2])
test_rrule(getindex, x, [2, 2, 2])
test_rrule(getindex, x, [2,2], [3,3])
end
end
@testset "getindex for structured arrays" begin
test_frule(getindex, Diagonal(rand(3)), 1)
test_frule(getindex, Symmetric(rand(3, 3)), 2, 3)
test_rrule(getindex, Diagonal(rand(3)), 1)
@test_skip test_rrule(getindex, Diagonal(rand(3)), 2, :) # https://github.com/JuliaDiff/ChainRulesTestUtils.jl/issues/260
dgrad = rrule(getindex, Diagonal(rand(3)), 2, :)[2]([1,2,3])[2]
@test unthunk(dgrad) ≈ Diagonal([0, 2, 0])
test_rrule(getindex, Symmetric(rand(3, 3)), 2, 2; check_inferred=false) # Infers to Any
sgrad = rrule(getindex, Symmetric(rand(3, 3)), 2, 3)[2](1.0)[2]
@test unthunk(sgrad) ≈ [0 0 0; 0 0 1/2; 0 1/2 0]
end
@testset "getindex(::Array{<:Array})" begin
test_frule(getindex, [rand(2) for _ in 1:3], 1)
test_frule(getindex, [rand(2) for _ in 1:3], 2:3)
test_frule(getindex, [rand(2) for _ in 1:3], [true, false, true])
test_rrule(getindex, [rand(2) for _ in 1:3], 1; check_inferred=false)
test_rrule(getindex, [rand(2) for _ in 1:3], 2:3; check_inferred=false)
test_frule(getindex, [rand(2) for _ in 1:3], [true, false, true]; check_inferred=false)
end
@testset "getindex(::Array{<:Weird})" begin
xfix = [Base.Fix1(*, pi), Base.Fix1(^, ℯ), Base.Fix1(/, -1)]
dxfix = [Tangent{Base.Fix1}(; x = i/10) for i in 1:3]
# test_frule(getindex, xfix ⊢ dxfix, 1)
# test_rrule(getindex, xfix ⊢ dxfix, 1)
dx1 = unthunk(rrule(getindex, xfix, 1)[2](dxfix[1])[2])
@test dx1[1] == dxfix[1]
@test iszero(dx1[2])
dx23 = unthunk(rrule(getindex, xfix, 2:3)[2](dxfix[2:3])[2])
@test iszero(dx23[1])
@test dx23[3] == dxfix[3]
end
@testset "second derivatives: ∇getindex" begin
@eval using ChainRules: ∇getindex
# Forward, scalar result
test_frule(∇getindex, rand(2, 3), rand(), 3)
test_frule(∇getindex, rand(2, 3), rand()+im, 2, 1)
# array result
test_frule(∇getindex, rand(2, 3), rand(2), 4:5)
test_frule(∇getindex, rand(2, 3), rand(3), 1, :)
test_frule(∇getindex, rand(2, 3), rand(1, 2), [CartesianIndex(2, 1) CartesianIndex(2, 2)] ⊢ NoTangent())
test_frule(∇getindex, rand(2, 3), rand(3), Bool[1 0 1; 0 1 0])
# arrays of arrays
test_frule(∇getindex, [rand(2) for _ in 1:3], rand(2), 3)
test_frule(∇getindex, [rand(2) for _ in 1:3], [rand(2), rand(2)], 1:2)
# Reverse, scalar result
test_rrule(∇getindex, rand(2, 3), rand(), 3; check_inferred=false)
test_rrule(∇getindex, rand(2, 3), rand()+im, 2, 1; check_inferred=false)
# array result
test_rrule(∇getindex, rand(2, 3), rand(2), 4:5; check_inferred=false)
test_rrule(∇getindex, rand(2, 3), rand(3), 1, :; check_inferred=false)
test_rrule(∇getindex, rand(2, 3), rand(1, 2), [CartesianIndex(2, 1) CartesianIndex(2, 2)] ⊢ NoTangent(); check_inferred=false)
test_rrule(∇getindex, rand(2, 3), rand(3), Bool[1 0 1; 0 1 0]; check_inferred=false)
# arrays of arrays
test_rrule(∇getindex, [rand(2) for _ in 1:3], rand(2), 3; check_inferred=false)
test_rrule(∇getindex, [rand(2) for _ in 1:3], [rand(2), rand(2)], 1:2; check_inferred=false)
end
@testset "getindex(::AbstractGPUArray)" begin
x_23_gpu = jl(rand(2, 3)) # using JLArrays, loaded for @gpu in test_helpers.jl
# Scalar indexing, copied from: @macroexpand @allowscalar A[i]
@test_skip begin # This gives
y1, bk1 = rrule(CFG, Base.task_local_storage, () -> x_23_gpu[1], :ScalarIndexing, ScalarAllowed)
@test y1 == @allowscalar x_23_gpu[1]
end
@test_skip begin
bk1(1.0) # This gives a StackOverflowError! Or gives zero in global scope.
true
end
# But this works, and calls the rule:
# Zygote.gradient(x -> @allowscalar(x[1]), jl(rand(3)))[1]
y2, bk2 = rrule(getindex, x_23_gpu, :, 2:3) # fast path, just broadcast .+=
@test unthunk(bk2(jl(ones(2,2)))[2]) == jl([0 1 1; 0 1 1])
y3, bk3 = rrule(getindex, x_23_gpu, 1, [1,1,2]) # slow path, copy to CPU
@test Array(y3) == Array(x_23_gpu)[1, [1,1,2]]
@test unthunk(bk3(jl(ones(3)))[2]) == jl([2 1 0; 0 0 0])
end
end
@testset "first & tail" begin
x = (1.2, 3.4, 5.6)
x2 = (rand(2), (a=1.0, b=x))
test_frule(first, x)
test_frule(first, x2)
test_rrule(first, x)
# test_rrule(first, x2) # MethodError: (::ChainRulesTestUtils.var"#test_approx##kw")(::NamedTuple{(:rtol, :atol), Tuple{Float64, Float64}}, ::typeof(test_approx), ::NoTangent, ::Tangent{NamedTuple{(:a, :b), Tuple{Float64, Tuple{Float64, Float64, Float64}}}, NamedTuple{(:a, :b), Tuple{Float64, Tangent{Tuple{Float64, Float64, Float64}, Tuple{Float64, Float64, Float64}}}}}, ::String) is ambiguous
test_frule(Base.tail, x, check_inferred=false) # return type Tuple{Tuple{Float64, Float64}, Tangent{Tuple{Float64, Float64}, Tuple{Float64, Float64}}} does not match inferred return type Tuple{Tuple{Float64, Float64}, Tangent{Tuple{Float64, Float64}}}
test_frule(Base.tail, x2, check_inferred=false)
test_rrule(Base.tail, x)
test_rrule(Base.tail, x2)
end
@testset "view" begin
test_frule(view, rand(3, 4), :, 1)
test_frule(view, rand(3, 4), 2, [1, 1, 2])
test_frule(view, rand(3, 4), 3, 4)
test_rrule(view, rand(3, 4), :, 1)
test_rrule(view, rand(3, 4), 2, [1, 1, 2])
test_rrule(view, rand(3, 4), 3, 4)
end
@testset "setindex!" begin
test_frule(setindex!, rand(3, 4), rand(), 1, 2)
test_frule(setindex!, rand(3, 4), [1,10,100.0], :, 3)
end
@testset "unsafe_getindex" begin
# In real life this is called only on some AbstractRanges, but easier to test on Array:
test_frule(Base.unsafe_getindex, collect(1:0.1:2), 3)
test_rrule(Base.unsafe_getindex, collect(1:0.1:2), 3)
end
@testset "eachslice" begin
# Testing eachrow not collect∘eachrow leads to errors, e.g.
# test_rrule: eachrow on Vector{Float64}: Error During Test at /Users/me/.julia/packages/ChainRulesTestUtils/8dFTY/src/testers.jl:195
# Got exception outside of a @test
# DimensionMismatch("second dimension of A, 6, does not match length of x, 5")
# Probably similar to https://github.com/JuliaDiff/ChainRulesTestUtils.jl/issues/234 (about Broadcasted not Generator)
test_rrule(collect∘eachrow, rand(5))
test_rrule(collect∘eachrow, rand(3, 4))
test_rrule(collect∘eachcol, rand(3, 4))
@test_skip test_rrule(collect∘eachcol, Diagonal(rand(5))) # works locally!
if VERSION >= v"1.7"
# On 1.6, ComposedFunction doesn't take keywords. Only affects this testing strategy, not real use.
test_rrule(collect∘eachslice, rand(3, 4, 5); fkwargs = (; dims = 3))
test_rrule(collect∘eachslice, rand(3, 4, 5); fkwargs = (; dims = (2,)))
end
# Make sure pulling back an array that mixes some AbstractZeros in works right
_, back = rrule(eachcol, rand(3, 4))
@test back([1:3, ZeroTangent(), 7:9, NoTangent()]) == (NoTangent(), [1 0 7 0; 2 0 8 0; 3 0 9 0])
@test back([1:3, ZeroTangent(), 7:9, NoTangent()])[2] isa Matrix{Float64}
@test back([ZeroTangent(), ZeroTangent(), NoTangent(), NoTangent()]) == (NoTangent(), [0 0 0 0; 0 0 0 0; 0 0 0 0])
# Second derivative rule
test_rrule(ChainRules.∇eachslice, [rand(4) for _ in 1:3], rand(3, 4), Val(1))
test_rrule(ChainRules.∇eachslice, [rand(3) for _ in 1:4], rand(3, 4), Val(2))
test_rrule(ChainRules.∇eachslice, [rand(2, 3) for _ in 1:4], rand(2, 3, 4), Val(3), check_inferred=false)
end