@@ -4,21 +4,40 @@ using LinearAlgebra, Test
4
4
using Random
5
5
Random. seed! (123 )
6
6
N = 300
7
- const A = rand (N, N)
8
- f (y, x) = mul! (y, A, x)
9
- f (x) = A * x
7
+
10
8
x = rand (N)
11
9
v = rand (N)
10
+
11
+ # Save original values of x and v to make sure they are not ever mutated
12
+ x0 = copy (x)
13
+ v0 = copy (v)
14
+
12
15
a, b = rand (2 )
13
16
dy = similar (x)
14
- g (x) = sum (abs2, x)
15
- function h (x)
16
- FiniteDiff. finite_difference_gradient (g, x)
17
+
18
+ # Define functions for testing
19
+
20
+ A = rand (N, N)
21
+ _f (y, x) = mul! (y, A, x.^ 2 )
22
+ _f (x) = A * (x.^ 2 )
23
+
24
+ _g (x) = sum (abs2, x.^ 2 )
25
+ function _h (x)
26
+ FiniteDiff. finite_difference_gradient (_g, x)
17
27
end
18
- function h (dy, x)
19
- FiniteDiff. finite_difference_gradient! (dy, g , x)
28
+ function _h (dy, x)
29
+ FiniteDiff. finite_difference_gradient! (dy, _g , x)
20
30
end
21
31
32
+ # Make functions state-dependent for operator tests
33
+
34
+ include (" update_coeffs_testutils.jl" )
35
+ f = WrapFunc (_f, 1.0 , 1.0 )
36
+ g = WrapFunc (_g, 1.0 , 1.0 )
37
+ h = WrapFunc (_h, 1.0 , 1.0 )
38
+
39
+ # ##
40
+
22
41
cache1 = ForwardDiff. Dual{typeof (ForwardDiff. Tag (SparseDiffTools. DeivVecTag (), eltype (x))),
23
42
eltype (x), 1 }. (x, ForwardDiff. Partials .(tuple .(v)))
24
43
cache2 = ForwardDiff. Dual {typeof(ForwardDiff.Tag(SparseDiffTools.DeivVecTag(), eltype(x))), eltype(x), 1} .(x, ForwardDiff. Partials .(tuple .(v)))
@@ -36,122 +55,147 @@ cache2 = ForwardDiff.Dual{typeof(ForwardDiff.Tag(SparseDiffTools.DeivVecTag(), e
36
55
similar (v))≈ ForwardDiff. hessian (g, x) * v rtol= 1e-2
37
56
@test num_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol= 1e-2
38
57
39
- @test numauto_hesvec! (dy, g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-8
58
+ @test numauto_hesvec! (dy, g, x, v)≈ ForwardDiff. hessian (g, x) * v
40
59
@test numauto_hesvec! (dy, g, x, v, ForwardDiff. GradientConfig (g, x), similar (v),
41
- similar (v))≈ ForwardDiff. hessian (g, x) * v rtol = 1e-8
42
- @test numauto_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-8
60
+ similar (v))≈ ForwardDiff. hessian (g, x) * v
61
+ @test numauto_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v
43
62
44
- @test autonum_hesvec! (dy, g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-2
63
+ @test autonum_hesvec! (dy, g, x, v)≈ ForwardDiff. hessian (g, x) * v
45
64
@test autonum_hesvec! (dy, g, x, v, cache1, cache2)≈ ForwardDiff. hessian (g, x) * v rtol= 1e-2
46
- @test autonum_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-8
65
+ @test autonum_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v
47
66
48
- @test numback_hesvec! (dy, g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-8
49
- @test numback_hesvec! (dy, g, x, v, similar (v), similar (v))≈ ForwardDiff. hessian (g, x) * v rtol = 1e-8
50
- @test numback_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-8
67
+ @test numback_hesvec! (dy, g, x, v)≈ ForwardDiff. hessian (g, x) * v
68
+ @test numback_hesvec! (dy, g, x, v, similar (v), similar (v))≈ ForwardDiff. hessian (g, x) * v
69
+ @test numback_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v
51
70
52
71
cache3 = ForwardDiff. Dual{typeof (ForwardDiff. Tag (Nothing, eltype (x))), eltype (x), 1
53
72
}. (x, ForwardDiff. Partials .(tuple .(v)))
54
73
cache4 = ForwardDiff. Dual{typeof (ForwardDiff. Tag (Nothing, eltype (x))), eltype (x), 1
55
74
}. (x, ForwardDiff. Partials .(tuple .(v)))
56
- @test autoback_hesvec! (dy, g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-8
57
- @test autoback_hesvec! (dy, g, x, v, cache3, cache4)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-8
58
- @test autoback_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-8
75
+ @test autoback_hesvec! (dy, g, x, v)≈ ForwardDiff. hessian (g, x) * v
76
+ @test autoback_hesvec! (dy, g, x, v, cache3, cache4)≈ ForwardDiff. hessian (g, x) * v
77
+ @test autoback_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v
59
78
60
79
@test num_hesvecgrad! (dy, h, x, v)≈ ForwardDiff. hessian (g, x) * v rtol= 1e-2
61
80
@test num_hesvecgrad! (dy, h, x, v, similar (v), similar (v))≈ ForwardDiff. hessian (g, x) * v rtol= 1e-2
62
81
@test num_hesvecgrad (h, x, v)≈ ForwardDiff. hessian (g, x) * v rtol= 1e-2
63
82
64
- @test auto_hesvecgrad! (dy, h, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-2
65
- @test auto_hesvecgrad! (dy, h, x, v, cache1, cache2)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-2
66
- @test auto_hesvecgrad (h, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-2
83
+ @test auto_hesvecgrad! (dy, h, x, v)≈ ForwardDiff. hessian (g, x) * v
84
+ @test auto_hesvecgrad! (dy, h, x, v, cache1, cache2)≈ ForwardDiff. hessian (g, x) * v
85
+ @test auto_hesvecgrad (h, x, v)≈ ForwardDiff. hessian (g, x) * v
67
86
68
87
@info " JacVec"
69
88
70
- L = JacVec (f, x)
89
+ L = JacVec (f, copy (x), 1.0 , 1.0 )
90
+ update_coefficients! (f, x, 1.0 , 1.0 )
71
91
@test L * x ≈ auto_jacvec (f, x, x)
72
92
@test L * v ≈ auto_jacvec (f, x, v)
73
93
@test mul! (dy, L, v) ≈ auto_jacvec (f, x, v)
74
94
dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b) ≈ a* auto_jacvec (f,x,v) + b* _dy
75
- update_coefficients! (L, v, nothing , 0.0 )
76
- @test mul! (dy, L, v) ≈ auto_jacvec (f, v, v)
77
- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b) ≈ a* auto_jacvec (f,x,v) + b* _dy
78
-
79
- L = JacVec (f, x, autodiff = AutoFiniteDiff ())
95
+ update_coefficients! (L, v, 3.0 , 4.0 )
96
+ update_coefficients! (f, v, 3.0 , 4.0 )
97
+ @test mul! (dy, L, x) ≈ auto_jacvec (f, v, x)
98
+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,x,a,b) ≈ a* auto_jacvec (f,v,x) + b* _dy
99
+ update_coefficients! (f, v, 5.0 , 6.0 )
100
+ @test L (dy, v, 5.0 , 6.0 ) ≈ auto_jacvec (f,v,v)
101
+
102
+ L = JacVec (f, copy (x), 1.0 , 1.0 ; autodiff = AutoFiniteDiff ())
103
+ update_coefficients! (f, x, 1.0 , 1.0 )
80
104
@test L * x ≈ num_jacvec (f, x, x)
81
105
@test L * v ≈ num_jacvec (f, x, v)
82
106
@test mul! (dy, L, v)≈ num_jacvec (f, x, v) rtol= 1e-6
83
107
dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b) ≈ a* num_jacvec (f,x,v) + b* _dy rtol= 1e-6
84
- update_coefficients! (L, v, nothing , 0.0 )
85
- @test mul! (dy, L, v)≈ num_jacvec (f, v, v) rtol= 1e-6
86
- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b) ≈ a* num_jacvec (f,x,v) + b* _dy rtol= 1e-6
108
+ update_coefficients! (L, v, 3.0 , 4.0 )
109
+ update_coefficients! (f, v, 3.0 , 4.0 )
110
+ @test mul! (dy, L, x)≈ num_jacvec (f, v, x) rtol= 1e-6
111
+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,x,a,b) ≈ a* num_jacvec (f,v,x) + b* _dy rtol= 1e-6
112
+ update_coefficients! (f, v, 5.0 , 6.0 )
113
+ @test L (dy, v, 5.0 , 6.0 ) ≈ num_jacvec (f,v,v) rtol= 1e-6
87
114
88
115
out = similar (v)
89
- gmres! (out, L, v)
116
+ @test_nowarn gmres! (out, L, v)
90
117
91
118
@info " HesVec"
92
119
93
- x = rand (N)
94
- v = rand (N)
95
- L = HesVec (g, x, autodiff = AutoFiniteDiff ())
96
- @test L * x ≈ num_hesvec (g, x, x)
97
- @test L * v ≈ num_hesvec (g, x, v)
120
+ L = HesVec (g, copy (x), 1.0 , 1.0 , autodiff = AutoFiniteDiff ())
121
+ update_coefficients! (g, x, 1.0 , 1.0 )
122
+ @test L * x ≈ num_hesvec (g, x, x) rtol= 1e-2
123
+ @test L * v ≈ num_hesvec (g, x, v) rtol= 1e-2
98
124
@test mul! (dy, L, v)≈ num_hesvec (g, x, v) rtol= 1e-2
99
125
dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b) ≈ a* num_hesvec (g,x,v) + b* _dy rtol= 1e-2
100
- update_coefficients! (L, v, nothing , 0.0 )
101
- @test mul! (dy, L, v)≈ num_hesvec (g, v, v) rtol= 1e-2
102
- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b) ≈ a* num_hesvec (g,x,v) + b* _dy rtol= 1e-2
103
-
104
- L = HesVec (g, x)
126
+ update_coefficients! (L, v, 3.0 , 4.0 )
127
+ update_coefficients! (g, v, 3.0 , 4.0 )
128
+ @test mul! (dy, L, x)≈ num_hesvec (g, v, x) rtol= 1e-2
129
+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,x,a,b) ≈ a* num_hesvec (g,v,x) + b* _dy rtol= 1e-2
130
+ update_coefficients! (g, v, 5.0 , 6.0 )
131
+ @test L (dy, v, 5.0 , 6.0 ) ≈ num_hesvec (g,v,v) rtol= 1e-2
132
+
133
+ L = HesVec (g, copy (x), 1.0 , 1.0 )
105
134
@test L * x ≈ numauto_hesvec (g, x, x)
106
135
@test L * v ≈ numauto_hesvec (g, x, v)
107
- @test mul! (dy, L, v)≈ numauto_hesvec (g, x, v) rtol= 1e-8
108
- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* numauto_hesvec (g,x,v)+ b* _dy rtol= 1e-8
109
- update_coefficients! (L, v, nothing , 0.0 )
110
- @test mul! (dy, L, v)≈ numauto_hesvec (g, v, v) rtol= 1e-8
111
- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* numauto_hesvec (g,x,v)+ b* _dy rtol= 1e-8
136
+ @test mul! (dy, L, v)≈ numauto_hesvec (g, x, v)
137
+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,0 )≈ a* numauto_hesvec (g,x,v)+ 0 * _dy
138
+ update_coefficients! (L, v, 3.0 , 4.0 )
139
+ update_coefficients! (g, v, 3.0 , 4.0 )
140
+ @test mul! (dy, L, x)≈ numauto_hesvec (g, v, x)
141
+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,x,a,b)≈ a* numauto_hesvec (g,v,x)+ b* _dy
142
+ update_coefficients! (g, v, 5.0 , 6.0 )
143
+ @test L (dy, v, 5.0 , 6.0 ) ≈ numauto_hesvec (g,v,v)
112
144
113
145
out = similar (v)
114
146
gmres! (out, L, v)
115
147
116
- using Zygote
117
-
118
- x = rand (N)
119
- v = rand (N)
120
-
121
- L = HesVec (g, x, autodiff = AutoZygote ())
148
+ L = HesVec (g, copy (x), 1.0 , 1.0 ; autodiff = AutoZygote ())
149
+ update_coefficients! (g, x, 1.0 , 1.0 )
122
150
@test L * x ≈ autoback_hesvec (g, x, x)
123
151
@test L * v ≈ autoback_hesvec (g, x, v)
124
- @test mul! (dy, L, v)≈ autoback_hesvec (g, x, v) rtol= 1e-8
125
- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* autoback_hesvec (g,x,v)+ b* _dy rtol= 1e-8
126
- update_coefficients! (L, v, nothing , 0.0 )
127
- @test mul! (dy, L, v)≈ autoback_hesvec (g, v, v) rtol= 1e-8
128
- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* autoback_hesvec (g,x,v)+ b* _dy rtol= 1e-8
152
+ @test mul! (dy, L, v)≈ autoback_hesvec (g, x, v)
153
+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* autoback_hesvec (g,x,v)+ b* _dy
154
+ update_coefficients! (L, v, 3.0 , 4.0 )
155
+ update_coefficients! (g, v, 3.0 , 4.0 )
156
+ @test mul! (dy, L, x)≈ autoback_hesvec (g, v, x)
157
+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,x,a,b)≈ a* autoback_hesvec (g,v,x)+ b* _dy
158
+ update_coefficients! (g, v, 5.0 , 6.0 )
159
+ @test L (dy, v, 5.0 , 6.0 ) ≈ autoback_hesvec (g,v,v)
129
160
130
161
out = similar (v)
131
162
gmres! (out, L, v)
132
163
133
164
@info " HesVecGrad"
134
165
135
- x = rand (N )
136
- v = rand (N )
137
- L = HesVecGrad (h , x, autodiff = AutoFiniteDiff () )
166
+ L = HesVecGrad (h, copy (x), 1.0 , 1.0 ; autodiff = AutoFiniteDiff () )
167
+ update_coefficients! (h, x, 1.0 , 1.0 )
168
+ update_coefficients! (g , x, 1.0 , 1.0 )
138
169
@test L * x ≈ num_hesvec (g, x, x) rtol= 1e-2
139
170
@test L * v ≈ num_hesvec (g, x, v) rtol= 1e-2
140
171
@test mul! (dy, L, v)≈ num_hesvec (g, x, v) rtol= 1e-2
141
172
dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* num_hesvec (g,x,v)+ b* _dy rtol= 1e-2
142
- update_coefficients! (L, v, nothing , 0.0 )
143
- @test mul! (dy, L, v)≈ num_hesvec (g, v, v) rtol= 1e-2
144
- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* num_hesvec (g,x,v)+ b* _dy rtol= 1e-2
145
-
146
- L = HesVecGrad (h, x)
173
+ for op in (L, g, h) update_coefficients! (op, v, 3.0 , 4.0 ) end
174
+ @test mul! (dy, L, x)≈ num_hesvec (g, v, x) rtol= 1e-2
175
+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,x,a,b)≈ a* num_hesvec (g,v,x)+ b* _dy rtol= 1e-2
176
+ update_coefficients! (g, v, 5.0 , 6.0 )
177
+ @test L (dy, v, 5.0 , 6.0 ) ≈ num_hesvec (g,v,v) rtol= 1e-2
178
+
179
+ L = HesVecGrad (h, copy (x), 1.0 , 1.0 )
180
+ update_coefficients! (g, x, 1.0 , 1.0 )
181
+ update_coefficients! (h, x, 1.0 , 1.0 )
147
182
@test L * x ≈ autonum_hesvec (g, x, x)
148
183
@test L * v ≈ numauto_hesvec (g, x, v)
149
- @test mul! (dy, L, v)≈ numauto_hesvec (g, x, v) rtol= 1e-8
150
- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* numauto_hesvec (g,x,v)+ b* _dy rtol= 1e-8
151
- update_coefficients! (L, v, nothing , 0.0 )
152
- @test mul! (dy, L, v)≈ numauto_hesvec (g, v, v) rtol= 1e-8
153
- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* numauto_hesvec (g,x,v)+ b* _dy rtol= 1e-8
184
+ @test mul! (dy, L, v)≈ numauto_hesvec (g, x, v)
185
+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* numauto_hesvec (g,x,v)+ b* _dy
186
+ for op in (L, g, h) update_coefficients! (op, v, 3.0 , 4.0 ) end
187
+ @test mul! (dy, L, x)≈ numauto_hesvec (g, v, x)
188
+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,x,a,b)≈ a* numauto_hesvec (g,v,x)+ b* _dy
189
+ update_coefficients! (g, v, 5.0 , 6.0 )
190
+ update_coefficients! (h, v, 5.0 , 6.0 )
191
+ @test L (dy, v, 5.0 , 6.0 ) ≈ numauto_hesvec (g,v,v)
154
192
155
193
out = similar (v)
156
194
gmres! (out, L, v)
195
+
196
+ # Test that x and v were not mutated
197
+ # x's rtol can't be too large since it is mutated and then restored in some algorithms
198
+ @test x ≈ x0
199
+ @test v ≈ v0
200
+
157
201
#
0 commit comments