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