-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
gradcheck.jl
2174 lines (1856 loc) · 72.4 KB
/
gradcheck.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Zygote, Test, Random, LinearAlgebra, Statistics, SparseArrays, FillArrays,
AbstractFFTs, FFTW, Distances
using Zygote: gradient
using Base.Broadcast: broadcast_shape
using Distributed: pmap, CachingPool, workers
import FiniteDifferences
function ngradient(f, xs::AbstractArray...)
grads = zero.(xs)
for (x, Δ) in zip(xs, grads), i in 1:length(x)
δ = sqrt(eps())
tmp = x[i]
x[i] = tmp - δ/2
y1 = f(xs...)
x[i] = tmp + δ/2
y2 = f(xs...)
x[i] = tmp
Δ[i] = (y2-y1)/δ
end
return grads
end
function gradcheck(f, xs...)
grad_zygote = gradient(f, xs...)
grad_finite_difference = ngradient(f, xs...)
return all(isapprox.(grad_zygote, grad_finite_difference; rtol = 1e-5, atol = 1e-5))
end
gradtest(f, xs::AbstractArray...) = gradcheck((xs...) -> sum(sin.(f(xs...))), xs...)
gradtest(f, dims...) = gradtest(f, rand.(Float64, dims)...)
# utilities for using gradcheck with complex matrices
_splitreim(A) = (real(A),)
_splitreim(A::AbstractArray{<:Complex}) = reim(A)
_joinreim(A, B) = complex.(A, B)
_joinreim(A) = A
function _dropimaggrad(A)
back(Δ) = real(Δ)
back(Δ::Nothing) = nothing
return Zygote.hook(back, A)
end
Random.seed!(0)
@testset "println, show, string, etc" begin
function foo(x)
Base.show(x)
Base.print(x)
Base.print(stdout, x)
Base.println(x)
Base.println(stdout, x)
Core.show(x)
Core.print(x)
Core.println(x)
return x
end
println("The following printout is from testing that `print` doesn't upset gradients:")
@test gradtest(foo, [5.0])
function bar(x)
string(x)
repr(x)
return x
end
@test gradtest(bar, [5.0])
end
@test gradient(//, 2, 3) === (1//3, -2//9)
@testset "power" begin
@test gradient(x -> x^2, -2) == (-4,)
@test gradient(x -> x^10, -1.0) == (-10,) # literal_pow
_pow = 10
@test gradient(x -> x^_pow, -1.0) == (-_pow,)
@test gradient(p -> real(2^p), 2)[1] ≈ 4*log(2)
@test gradient(xs ->sum(xs .^ 2), [2, -1]) == ([4, -2],)
@test gradient(xs ->sum(xs .^ 10), [3, -1]) == ([10*3^9, -10],)
@test gradient(xs ->sum(xs .^ _pow), [4, -1]) == ([_pow*4^9, -10],)
@test gradient(x -> real((1+3im) * x^2), 5+7im) == (-32 - 44im,)
@test gradient(p -> real((1+3im) * (5+7im)^p), 2)[1] ≈ real((-234 + 2im)*log(5 - 7im))
# D[(1+3I)x^p, p] /. {x->5+7I, p->2} // Conjugate
end
@test gradtest((a,b)->sum(reim(acosh(complex(a[1], b[1])))), [-2.0], [1.0])
@test gradtest((x, W, b) -> identity.(W*x .+ b), 5, (2,5), 2)
@test gradtest((x, W, b) -> identity.(W*x .+ b), (5,3), (2,5), 2)
@test gradtest((x, W, b) -> tanh.(W*x .+ b), 5, (2,5), 2)
@test gradtest((x, W, b) -> tanh.(W*x .+ b), (5,3), (2,5), 2)
@test gradtest((w, x) -> w'*x, randn(10, 2), randn(10))
@test gradtest((w, x) -> Adjoint(w)*x, randn(10, 2), randn(10))
@test gradtest((w, x) -> transpose(w)*x, randn(5,5), randn(5,5))
@test gradtest((w, x) -> Transpose(w)*x, randn(5,5), randn(5,5))
@test gradtest((w, x) -> parent(w)*x, randn(5,5)', randn(5,5))
@test gradtest((w, x) -> parent(w)*x, transpose(randn(5,5)), randn(5,5))
@testset "sum, prod, cumsum" begin
@test gradtest(x -> sum(x, dims = (2, 3)), (3,4,5))
@test gradtest(x -> sum(abs2, x), randn(4, 3, 2))
@test gradtest(x -> sum(abs2, x; dims=1), randn(4, 3, 2))
@test gradtest(x -> sum(x[i] for i in 1:length(x)), randn(10))
@test gradtest(x -> sum(i->x[i], 1:length(x)), randn(10)) # issue #231
@test gradtest(x -> sum((i->x[i]).(1:length(x))), randn(10))
@test gradtest(X -> sum(x -> x^2, X), randn(10))
@test gradtest(X -> sum(sum(x -> x^2, X; dims=1)), randn(10)) # issue #681
# Non-differentiable sum of booleans
@test gradient(sum, [true, false, true]) == (nothing,)
@test gradient(x->sum(x .== 0.0), [1.2, 0.2, 0.0, -1.1, 100.0]) == (nothing,)
# https://github.com/FluxML/Zygote.jl/issues/314
@test gradient((x,y) -> sum(yi -> yi*x, y), 1, [1,1]) == (2, [1, 1])
@test gradient((x,y) -> prod(yi -> yi*x, y), 1, [1,1]) == (2, [1, 1])
@test gradient((x,y) -> sum(map(yi -> yi*x, y)), 1, [1,1]) == (2, [1, 1])
@test gradient((x,y) -> prod(map(yi -> yi*x, y)), 1, [1,1]) == (2, [1, 1])
@test gradtest(x -> prod(x, dims = (2, 3)), (3,4,5))
@test gradtest(x -> prod(x), (3,4))
@test gradient(x -> prod(x), (1,2,3))[1] == (6,3,2)
@test gradtest(x -> cumsum(x, dims=2), (3,4,5))
@test gradtest(x -> cumsum(x, dims=1), (3,))
@test gradtest(x -> cumsum(x), (4,))
@test gradtest(x -> cumsum(x, dims=3), (5,)) # trivial
@test gradtest(x -> cumsum(x, dims=3), (3,4)) # trivial
end
@test gradtest(x -> x', rand(5))
@test gradtest(det, (4, 4))
@test gradtest(logdet, map(x -> x*x', (rand(4, 4),))[1])
@test gradtest(x -> logabsdet(x)[1], (4, 4))
@test gradient(det, 2.0)[1] == 1
@test gradient(logdet, 2.0)[1] == 0.5
@testset "getindex" begin
@test gradtest(x -> x[:, 2, :], (3, 4, 5))
@test gradtest(x -> x[1:2, 3:4], (3, 4))
imat = [1 2; 3 4]
@test gradtest(x -> x[:, imat], (3, 4))
@test gradtest(x -> x[:, [1, 2, 2]], (3, 4))
irep = [1 2; 2 2]
@test gradtest(x -> x[1, irep], (3, 4))
# https://github.com/invenia/Nabla.jl/issues/139
x = rand(3)
z = [1, 2, 3, 3]
y(x, z) = dot(ones(4), x[z])
@test gradient(y, x, z) == ([1, 1, 2], nothing)
# https://github.com/FluxML/Zygote.jl/issues/376
_, back = Zygote._pullback(x->x[1]*im, randn(2))
@test back(1.0)[2] == real([-im, 0]) == [0, 0]
# _droplike
@test gradient(x -> sum(inv, x[1, :]'), ones(2, 2)) == ([-1 -1; 0 0],)
@test gradient(x -> sum(inv, x[1:1, :]'), ones(2, 2)) == ([-1 -1; 0 0],)
@test gradient(x -> sum(inv, transpose(view(x, 1, :))), ones(2, 2)) == ([-1 -1; 0 0],)
# https://github.com/FluxML/Zygote.jl/issues/513
@test gradient(p -> sum(Float32[1, 0] - p), [2, 3]) == ([-1, -1],)
@test gradient(x -> sum(Float32[1, x] .+ x), 4) == (3.0f0,)
# Ensure that nothings work with numeric types.
_, back = Zygote.pullback(getindex, randn(4), [1])
@test back([nothing]) === nothing
# Ensure that nothings work with non-numeric types.
_, back = Zygote.pullback(getindex, [randn(2) for _ in 1:3], [1])
@test back([nothing]) === nothing
end
@testset "view" begin
@test gradtest(x -> view(x,:,2,:), (3,4,5))
@test gradtest(x -> view(x,1:2,3:4), (3,4))
@test gradtest(x -> view(x,:,[1,2,2]), (3,4))
# https://github.com/FluxML/Zygote.jl/issues/272
g(x) = view(x,1:2)[1]
@test gradient(g, ones(3)) == ([1,0,0],)
end
@testset "eachcol" begin
@test gradtest(x -> map(sum, eachcol(x)), (3,4))
@test gradtest(x -> map(sum, eachcol(transpose(x))), (3,4))
@test gradtest(x -> map(norm, eachcol(x)), (3,4))
@test gradtest(x -> map(norm, eachrow(x)), (3,4))
@test gradtest(x -> map(norm, eachslice(x, dims=3)), (3,4,5))
# some slices may have gradient nothing
@test gradient(x -> sum(y -> rand()>0.5 ? 0 : first(y), eachcol(x)), rand(3,10))[1] isa Matrix
# strange errors
@test_skip gradient(x -> sum(norm, eachcol(x)), [1 2 3; 4 5 6])[1] isa Matrix
@test_skip gradient(x -> sum(norm, eachcol(x)), rand(3,400))[1] isa Matrix
end
@testset "collect" begin
@test gradient(x -> sum(inv, collect(x)), (1,2)) === ((-1.0, -1/4),)
@test gradient(x -> sum(collect(view(x, 1:1))), rand(2)) == ([1,0],)
@test gradient(x -> sum(inv, collect(view(x', 1,:))), ones(2,2)) == ([-1 0; -1 0],)
@test gradient(xs -> sum(inv, [x^2 for x in xs]), ones(2)) == ([-2, -2],)
# adjoint of generators is available and should support generic arrays and iterators
# generator of array
@test gradient(p -> sum(collect(p*i for i in [1.0, 2.0, 3.0])), 2.0) == (6.0,)
# generator of iterator with HasShape
@test gradient(p -> sum(collect(p*i for (i,) in zip([1.0, 2.0, 3.0]))), 2.0) == (6.0,)
# generator of iterator with HasLength
@test gradient(p -> sum(collect(p*i for i in Iterators.take([1.0, 2.0, 3.0], 3))), 2.0) == (6.0,)
@test gradient(p -> sum(collect(p*i for i in Iterators.take(p*[1.0, 2.0, 3.0], 2))), 2.0) == (12.0,)
# generator 0-d behavior handled incorrectly
@test_broken gradient(p -> sum(collect(p*i for i in 1.0)), 2.0)
@test_broken gradient(p -> sum(collect(p*i for i in fill(1.0))), 2.0)
# adjoints for iterators
@test gradient(x -> sum(collect(Iterators.take([x*i for i in 1:5], 4))), 1.0) == (10.0,)
@test gradient(x -> sum(collect(Iterators.take([x*i for i in 1:5], 5))), 1.0) == (15.0,)
@test_broken gradient(sum∘collect, 1.0) == (1.0,) # broken since no generic adjoint
end
@test gradtest(x -> reverse(x), rand(17))
@test gradtest(x -> reverse(x, 8), rand(17))
@test gradtest(x -> reverse(x, 8, 13), rand(17))
@test gradtest(x -> reverse(x, dims=2), rand(17, 42))
@test gradtest(x -> permutedims(x), rand(2))
@test gradtest(x -> permutedims(x), rand(2,3))
@test gradtest(x -> permutedims(x, [3,1,2]), rand(4,5,6))
@test gradtest(x -> PermutedDimsArray(x, (3,1,2)), rand(4,5,6))
let
y, back = Zygote.pullback(permutedims, randn(3))
@test first(back(randn(1, 3))) isa Vector
end
@test gradtest(x -> repeat(x; inner=2), rand(5))
@test gradtest(x -> repeat(x; inner=2, outer=3), rand(5))
@test gradtest(x -> repeat(x; inner=(2,2,1), outer=(1,1,3)), rand(5,4,3))
@test gradtest(x -> repeat(x, 3), rand(5))
@test gradtest(x -> repeat(x, 2, 3), rand(5))
@test gradtest(x -> repeat(x, 5), rand(5,7))
@test gradtest(x -> repeat(x, 3, 2), rand(5,3))
@test gradtest(tr, rand(4, 4))
@testset "fill" begin
rng, N, M, P = MersenneTwister(123456), 11, 6, 5
@test gradtest(x->fill(first(x), N), randn(rng, 1))
@test gradtest(x->fill(first(x), N, M), randn(rng, 1))
@test gradtest(x->fill(first(x), N, M, P), randn(rng, 1))
# fill(struct, ...) handled by ChainRules after
# https://github.com/FluxML/Zygote.jl/pull/1051
@test gradient(x -> fill(x, 3)[1][1], (1,2)) === ((1.0, nothing),)
@test gradient(x -> fill(x, 3)[1].a, (a=1, b=2)) == ((a=1.0, b=nothing),) # 1 not 1.0
end
@testset "circshift" begin
L = 5
for D ∈ 1:5, reps ∈ 1:5
x0 = zeros(ntuple(d->L, D))
g = gradient(x -> x[1], x0)[1] #Zero shift gradient
shift = ntuple(_ -> rand(-L:L), D) #Random shift
@test gradient(x -> circshift(x, shift)[1], x0)[1] == circshift(g, map(-, shift))
end
end
@testset "dot" begin
rng = MersenneTwister(123456)
@test gradtest((x, y)->dot(x[1], y[1]), [randn(rng)], [randn(rng)])
@test gradtest(dot, randn(rng, 10), randn(rng, 10))
@test gradtest(dot, randn(rng, 10, 3), randn(rng, 10, 3))
end
@test gradtest(kron, rand(5), rand(3))
@test gradtest(kron, rand(5), rand(3), rand(8))
@test gradtest(kron, rand(5,1), rand(3,1))
@test gradtest(kron, rand(5,1), rand(3,1), rand(8,1))
@test gradtest(kron, rand(5,2), rand(3,2), rand(8,2))
@test gradtest(kron, rand(5), rand(3, 2))
@test gradtest(kron, rand(3, 2), rand(5))
for mapfunc in [map,pmap]
@testset "$mapfunc" begin
@test gradtest(xs -> sum(mapfunc(x -> x^2, xs)), rand(2,3))
@test gradtest((xss...) -> sum(mapfunc((xs...) -> sqrt(sum(xs.^2)), xss...)), [rand(5) for _ in 1:6]...)
function foo(y)
bar = (x) -> x*y
sum(mapfunc(bar, 1:5))
end
@test gradtest(foo, 3)
@test gradient(v -> sum([x for x in v]), [1.1,2.2,3.3]) == ([1, 1, 1],)
end
@testset "Tuple adjoint" begin
x = randn(3)
_, pb = Zygote.pullback(x -> map(abs2, x), x)
Δy = randn(3)
@test first(pb((Δy..., ))) ≈ first(pb(Δy))
end
@testset "empty tuples" begin
out, pb = Zygote.pullback(map, -, ())
@test pb(out) === (nothing, ())
out, pb = Zygote.pullback(map, +, (), ())
@test pb(()) === (nothing, (), ())
function build_foo(z)
foo(x) = x * z
return foo
end
out, pb = Zygote.pullback(map, build_foo(5.0), ())
@test pb(()) === (nothing, ())
end
@testset "Vector{Nothing} cotangent" begin
Δ = Vector{Nothing}(nothing, 5)
# Unary stateless
out, pb = Zygote.pullback(map, -, randn(5))
@test pb(Δ)[2] isa Vector{Nothing}
# Binary stateless
out, pb = Zygote.pullback(map, +, randn(5), randn(5))
@test pb(Δ)[2] isa Vector{Nothing}
@test pb(Δ)[3] isa Vector{Nothing}
# Stateful
function build_foo(z)
foo(x) = x * z
return foo
end
out, pb = Zygote.pullback(map, build_foo(5.0), randn(5))
@test pb(Δ)[2] isa Vector{Nothing}
end
end
# Check that map infers correctly. pmap still doesn't infer.
@testset "map inference" begin
@testset "$name" for (name, f, ȳ, xs) in [
("unary empty vector", sin, Float64[], (Float64[], )),
("unary vector", sin, randn(3), (randn(3), )),
("unary empty tuple", sin, (), ((), )),
("unary tuple", sin, (randn(), randn()), ((randn(), randn()), )),
("binary empty vector", +, Float64[], (Float64[], Float64[])),
("binary vector", +, randn(2), (randn(2), randn(2))),
("binary empty tuple", +, (), ((), ())),
("binary tuple", +, (randn(), randn()), ((randn(), randn()), (randn(), randn()))),
]
@inferred Zygote._pullback(Zygote.Context(), map, f, xs...)
y, pb = Zygote._pullback(Zygote.Context(), map, f, xs...)
@inferred pb(ȳ)
end
end
@testset "map and tuples" begin
# arrays of tuples, ChainRules's Tangent should not escape
@test gradient(x -> sum(map(first, x)), [(1,2), (3,4)]) == ([(1.0, nothing), (1.0, nothing)],)
@test gradient(x -> sum(first, x), [(1,2), (3,4)]) == ([(1.0, nothing), (1.0, nothing)],)
@test gradient(x -> map(+, x, (1,2,3))[1], (4,5,6)) == ((1.0, nothing, nothing),)
@test gradient(x -> map(+, x, [1,2,3])[1], (4,5,6)) == ((1.0, 0.0, 0.0),)
@test gradient(x -> map(+, x, (1,2,3))[1], [4,5,6]) == ([1,0,0],)
# mismatched lengths, should zip
@test gradient(x -> map(+, x, [1,2,3,99])[1], (4,5,6)) == ((1.0, 0.0, 0.0),)
@test gradient(x -> map(+, x, [1,2,3])[1], (4,5,6,99)) == ((1.0, 0.0, 0.0, nothing),)
end
@testset "Alternative Pmap Dispatch" begin
cache_and_map(f,xs...) = pmap(f, CachingPool(workers()), xs...; batch_size = 1)
@test gradtest(xs -> sum(cache_and_map(x -> x^2, xs)), rand(2,3))
@test gradtest((xss...) -> sum(cache_and_map((xs...) -> sqrt(sum(xs.^2)), xss...)), [rand(5) for _ in 1:6]...)
function foo(y)
bar = (x) -> x*y
sum(cache_and_map(bar, 1:5))
end
@test gradtest(foo, 3)
@test gradient(v -> sum([x for x in v]), [1.1,2.2,3.3]) == ([1, 1, 1],)
end
@testset "Stateful Map" begin
s = 0
f(x) = (s += x)
@test_broken gradient(x -> sum(f.(x)), 1:10) == (10:-1:1,)
s = 0
@test gradient(x -> sum(map(f, x)), 1:10) == (10:-1:1,)
end
@testset "vararg map" begin
# early stop
if VERSION >= v"1.5"
# In Julia 1.4 and earlier, map(*,rand(5),[1,2,3]) is a DimensionMismatch
@test gradient(x -> sum(map(*,x,[1,2,3])), rand(5)) == ([1,2,3,0,0],)
end
@test gradient(x -> sum(map(*,x,(1,2,3))), rand(5)) == ([1,2,3,0,0],)
@test gradient(x -> sum(map(*,x,[1,2,3])), Tuple(rand(5))) == ((1.0, 2.0, 3.0, nothing, nothing),)
# mixed shapes
@test gradient((x,y) -> sum(map(*,x,y)), [1,2,3,4], [1 2; 3 4]) == ([1,3,2,4], [1 3; 2 4])
@test gradient((x,y) -> sum(map(*,x,y)), [1,2,3], [1 2; 3 4]) == ([1,3,2], [1 3; 2 0])
@test gradient((x,y) -> sum(map(*,x,y)), (1,2,3), [1 2; 3 4]) == ((1,3,2), [1 3; 2 0])
@test gradient((x,y) -> sum(map(*,x,y)), [1,2,3,4,5], [1 2; 3 4]) == ([1,3,2,4,0], [1 3; 2 4])
@test gradient((x,y) -> sum(map(*,x,y)), (1,2,3,4,5), [1 2; 3 4]) == ((1,3,2,4,nothing), [1 3; 2 4])
end
@testset "map: issye 1374" begin
# The code to reverse iteration in map was very sloppy, could reverse fwd & not reverse, wtf.
# https://github.com/FluxML/Zygote.jl/issues/1374
struct Affine1374
W
b
end
(m::Affine1374)(x) = [sum(x.*r) for r in eachrow(m.W)] + m.b
m = Affine1374(zeros(3,3), zeros(3,1))
x = [ 1.0, 2.0, 3.0]
y = [-1.0, -2.0, -3.0]
l1374(y,ŷ) = sum(abs2.(y - ŷ))/2
grads = gradient(m -> l1374(y,m(x)), m)
@test grads[1].W ≈ [1 2 3; 2 4 6; 3 6 9]
end
@testset "sort" begin
@test gradtest(sort, 5)
correct = [
[2,3,1],
[1, 2, 3],
[1,2,3],
[2,1,3],
[1,3,2],
[3,2,1]
]
for i = 1:3
@test gradient(v->sort(v)[i], [3.,1,2])[1][correct[1][i]] == 1
@test gradient(v->sort(v)[i], [1.,2,3])[1][correct[2][i]] == 1
@test gradient(v->sort(v,by=x->x%10)[i], [11,2,99])[1][correct[3][i]] == 1
@test gradient(v->sort(v,by=x->x%10)[i], [2,11,99])[1][correct[4][i]] == 1
@test gradient(v->sort(v,rev=true)[i], [3.,1,2])[1][correct[5][i]] == 1
@test gradient(v->sort(v,rev=true)[i], [1.,2,3])[1][correct[6][i]] == 1
end
end
@testset "filter" begin
@test gradtest(xs -> filter(x -> x > 0.5, xs), 20)
@test gradient(x -> sum(log, filter(iseven, x)), 1:10) ==
(map(x -> iseven(x) ? 1/x : 0, 1:10),)
@test gradient(x -> sum(abs2, im .+ filter(iseven, x)), 1:10) ==
(map(x -> iseven(x) ? 2x : 0, 1:10),)
# (map(x -> iseven(x) ? 2x+2im : 0, 1:10),)
end
@testset "mean" begin
@test gradtest(mean, rand(2, 3))
@test gradtest(x -> mean(x, dims=1), rand(2, 3))
@test gradtest(x -> mean(x, dims=2), rand(2, 3))
@test gradtest(x -> mean(x, dims=3), rand(2, 3, 4))
@test gradtest(x -> mean(x, dims=[1, 2]), rand(2, 3, 4))
end
@testset "var" begin
@test gradtest(var, rand(2, 3))
@test gradtest(x -> var(x, dims=1), rand(2, 3))
@test gradtest(x -> var(x, dims=2), rand(2, 3))
@test gradtest(x -> var(x, dims=3), rand(2, 3, 4))
@test gradtest(x -> var(x, dims=[1, 2]), rand(2, 3, 4))
@test gradtest(x -> var(x, corrected=false), rand(2, 3))
@test gradtest(x -> var(x, dims=1, corrected=false), rand(2, 3))
@test gradtest(x -> var(x, dims=2, corrected=false), rand(2, 3))
@test gradtest(x -> var(x, dims=3, corrected=false), rand(2, 3, 4))
@test gradtest(x -> var(x, dims=[1, 2], corrected=false), rand(2, 3, 4))
@test gradtest(x -> var(x, mean=mean(x)), rand(2, 3))
@test gradtest(x -> var(x, dims=1, mean=mean(x, dims=1)), rand(2, 3))
@test gradtest(x -> var(x, dims=2, mean=mean(x, dims=2)), rand(2, 3))
@test gradtest(x -> var(x, dims=3, mean=mean(x, dims=3)), rand(2, 3, 4))
@test gradtest(x -> var(x, dims=[1, 2], mean=mean(x, dims=[1, 2])), rand(2, 3, 4))
@test gradtest(x -> var(x, corrected=false, mean=mean(x)), rand(2, 3))
@test gradtest(x -> var(x, dims=1, corrected=false, mean=mean(x, dims=1)), rand(2, 3))
@test gradtest(x -> var(x, dims=2, corrected=false, mean=mean(x, dims=2)), rand(2, 3))
@test gradtest(x -> var(x, dims=3, corrected=false, mean=mean(x, dims=3)), rand(2, 3, 4))
@test gradtest(x -> var(x, dims=[1, 2], corrected=false, mean=mean(x, dims=[1, 2])), rand(2, 3, 4))
end
@testset "std" begin
@test gradtest(std, rand(2, 3))
@test gradtest(x -> std(x, dims=1), rand(2, 3))
@test gradtest(x -> std(x, dims=2), rand(2, 3))
@test gradtest(x -> std(x, dims=3), rand(2, 3, 4))
@test gradtest(x -> std(x, dims=[1, 2]), rand(2, 3, 4))
@test gradtest(x -> std(x, corrected=false), rand(2, 3))
@test gradtest(x -> std(x, dims=1, corrected=false), rand(2, 3))
@test gradtest(x -> std(x, dims=2, corrected=false), rand(2, 3))
@test gradtest(x -> std(x, dims=3, corrected=false), rand(2, 3, 4))
@test gradtest(x -> std(x, dims=[1, 2], corrected=false), rand(2, 3, 4))
@test gradtest(x -> std(x, mean=mean(x)), rand(2, 3))
@test gradtest(x -> std(x, dims=1, mean=mean(x, dims=1)), rand(2, 3))
@test gradtest(x -> std(x, dims=2, mean=mean(x, dims=2)), rand(2, 3))
@test gradtest(x -> std(x, dims=3, mean=mean(x, dims=3)), rand(2, 3, 4))
@test gradtest(x -> std(x, dims=[1, 2], mean=mean(x, dims=[1, 2])), rand(2, 3, 4))
@test gradtest(x -> std(x, corrected=false, mean=mean(x)), rand(2, 3))
@test gradtest(x -> std(x, dims=1, corrected=false, mean=mean(x, dims=1)), rand(2, 3))
@test gradtest(x -> std(x, dims=2, corrected=false, mean=mean(x, dims=2)), rand(2, 3))
@test gradtest(x -> std(x, dims=3, corrected=false, mean=mean(x, dims=3)), rand(2, 3, 4))
@test gradtest(x -> std(x, dims=[1, 2], corrected=false, mean=mean(x, dims=[1, 2])), rand(2, 3, 4))
end
@testset "maximum" begin
@test gradtest(maximum, rand(2, 3))
@test gradtest(x -> maximum(x, dims=1), rand(2, 3))
@test gradtest(x -> maximum(x, dims=2), rand(2, 3))
@test gradtest(x -> maximum(x, dims=3), rand(2, 3, 4))
@test gradtest(x -> maximum(x, dims=[1, 2]), rand(2, 3, 4))
@test gradient(x -> 1 / maximum(x), [1., 2, 3])[1] == [0, 0, -1/9]
# issue 1224, second order
f1244(w, x) = sum(maximum((w * x).^2, dims=1))
g1244(w, x) = sum(gradient(f1244, w, x)[2].^2)
h1244(w, x) = gradient(g1244, w, x)[2]
@test h1244([1 2 3; 4 5 6.0], [7,8,9.0]) ≈ [300608, 375760, 450912]
end
@testset "minimum" begin
@test gradtest(minimum, rand(2, 3))
@test gradtest(x -> minimum(x, dims=1), rand(2, 3))
@test gradtest(x -> minimum(x, dims=2), rand(2, 3))
@test gradtest(x -> minimum(x, dims=3), rand(2, 3, 4))
@test gradtest(x -> minimum(x, dims=[1, 2]), rand(2, 3, 4))
end
@testset "dropdims" begin
@test gradtest(x -> dropdims(x, dims = 3), rand(2, 2, 1, 2))
@test gradtest(x -> dropdims(x, dims = (2, 3)), rand(2, 1, 1, 3))
@test gradtest(x -> dropdims(x, dims = (1, 2, 3)), rand(1, 1, 1, 3))
end
@testset "$f(::AbstractArray)" for f in (real, conj, imag)
rng, N = MersenneTwister(123456), 3
Ts = (Float64, ComplexF64)
@testset "$f(::Array{$IT})" for IT in Ts
A = randn(IT, N, N)
y, back = Zygote.pullback(f, A)
y2, back2 = Zygote.pullback(x->f.(x), A)
@test y == y2
@testset "back(::Array{$BT})" for BT in Ts
ȳ = randn(BT, N, N)
@test back(ȳ)[1] == back2(ȳ)[1]
end
end
end
@testset "(p)inv" begin
rng, P, Q = MersenneTwister(123456), 13, 11
A, B, C = randn(rng, P, Q), randn(rng, P, P), randn(Q, P)
@test gradtest(pinv, A)
@test gradtest(inv, B)
@test gradtest(pinv, C)
@test gradient(inv, 2.0)[1] == -0.25
end
@testset "multiplication" begin
rng, M, P, Q = MersenneTwister(123456), 13, 7, 11
@testset "matrix-matrix" begin
@test gradtest(*, randn(rng, M, P), randn(rng, P, Q))
@test gradtest(*, randn(rng, M, P), randn(rng, P))
@test gradtest(*, randn(rng, M, 1), randn(rng, 1, Q))
@test gradtest(*, randn(rng, M), randn(rng, 1, Q))
@test gradtest(*, randn(rng, 10)', randn(rng, 10))
@test gradtest(*, randn(rng, 10)', randn(rng, 10))
let
y, back = Zygote.pullback(*, randn(rng, M, P), randn(rng, P))
@test last(back(randn(rng, M))) isa Vector
end
let
y, back = Zygote.pullback(*, randn(rng, M), randn(rng, 1, P))
@test first(back(randn(rng, M, P))) isa Vector
end
end
end
@testset "backsolve" begin
rng, M, P, Q = MersenneTwister(123456), 13, 10, 9
X, Y, y = randn(rng, P, P), randn(rng, P, Q), randn(rng, P)
A, B = randn(rng, P, M), randn(P, Q)
D = collect(Diagonal(randn(rng, P)))
L = collect(LowerTriangular(randn(rng, P, P)))
L[diagind(L)] .= 1 .+ 0.01 .* randn(rng, P)
U = collect(UpperTriangular(randn(rng, P, P)))
U[diagind(U)] .= 1 .+ 0.01 .* randn(rng, P)
# \ (Dense square)
@test gradtest(\, X, Y)
@test gradtest(\, X, y)
# \ (Dense rectangular)
@test gradtest(\, A, Y)
@test gradtest(\, A, y)
@test gradtest(\, B, Y)
@test gradtest(\, B, y)
# \ (Diagonal)
@test gradtest(\, D, Y)
@test gradtest(\, D, y)
@test gradtest((D, Y)-> Diagonal(D) \ Y, D, Y)
@test gradtest((D, Y)-> Diagonal(D) \ Y, D, y)
# \ (LowerTriangular)
@test gradtest(\, L, Y)
@test gradtest(\, L, y)
@test gradtest((L, Y) -> LowerTriangular(L) \ Y, L, Y)
@test gradtest((L, Y) -> LowerTriangular(L) \ Y, L, y)
# \ (UpperTriangular)
@test gradtest(\, U, Y)
@test gradtest(\, U, y)
@test gradtest((U, Y) -> UpperTriangular(U) \ Y, U, Y)
@test gradtest((U, Y) -> UpperTriangular(U) \ Y, U, y)
# /
@test gradtest(/, Y', X)
@test gradtest((y, X)->y' / X, y, X)
# / (rectangular)
@test gradtest(/, Y', A')
@test gradtest((y, A)->y' / A', y, A)
@test gradtest(/, Y', B')
@test gradtest((y, A)->y' / A', y, B)
# / (Diagonal)
@test gradtest((D, Y) -> Y' / D, D, Y)
@test gradtest((D, Y) -> Y' / D, D, y)
@test gradtest((D, Y)-> Y' / Diagonal(D), D, Y)
@test gradtest((D, Y)-> Y' / Diagonal(D), D, y)
# / (LowerTriangular)
@test gradtest((L, Y) -> Y' / L, L, Y)
@test gradtest((L, Y) -> Y' / L, L, y)
@test gradtest((L, Y) -> Y' / LowerTriangular(L), L, Y)
@test gradtest((L, Y) -> Y' / LowerTriangular(L), L, y)
# / (UpperTriangular)
@test gradtest((U, Y) -> Y' / U, U, Y)
@test gradtest((U, Y) -> Y' / U, U, y)
@test gradtest((U, Y) -> Y' / UpperTriangular(U), U, Y)
@test gradtest((U, Y) -> Y' / UpperTriangular(U), U, y)
# / (UnitLowerTriangular)
@test gradtest((L, Y) -> Y' / L, L, Y)
@test gradtest((L, Y) -> Y' / L, L, y)
@test gradtest((L, Y) -> Y' / UnitLowerTriangular(L), L, Y)
@test gradtest((L, Y) -> Y' / UnitLowerTriangular(L), L, y)
# / (UnitUpperTriangular)
@test gradtest((U, Y) -> Y' / U, U, Y)
@test gradtest((U, Y) -> Y' / U, U, y)
@test gradtest((U, Y) -> Y' / UnitUpperTriangular(U), U, Y)
@test gradtest((U, Y) -> Y' / UnitUpperTriangular(U), U, y)
@testset "Cholesky" begin
# Check that the forwards pass computes the correct thing.
f(X, Y) = cholesky(X * X' + I) \ Y
@test Zygote.pullback(X -> f(X, Y), X)[1] ≈ cholesky(X * X' + I) \ Y
@test gradtest(X -> f(X, Y), X)
@test gradtest(Y -> f(X, Y), Y)
@test gradtest(X -> f(X, y), X)
@test gradtest(y -> f(X, y), y)
g(X) = cholesky(X * X' + I)
@test Zygote.pullback(g, X)[2]((factors=LowerTriangular(X),))[1] ≈
Zygote.pullback(g, X)[2]((factors=Matrix(LowerTriangular(X)),))[1]
# https://github.com/FluxML/Zygote.jl/issues/932
@test gradcheck(rand(5, 5), rand(5)) do A, x
C = cholesky(Symmetric(A' * A + I))
return sum(C \ x) + logdet(C)
end
end
end
@testset "Symmetric" begin
@testset "real" begin
rng, P = MersenneTwister(123456), 7
A = randn(rng, P, P)
@testset "uplo=$uplo" for uplo in (:U, :L)
@test gradtest(x->Symmetric(x, uplo), A)
y, back = Zygote.pullback(Symmetric, A, uplo)
@test y isa Symmetric
@testset "back(::Diagonal)" begin
D̄ = Diagonal(randn(rng, P))
@test back(Diagonal(D̄))[1] isa Diagonal
@test back(Diagonal(D̄))[1] ≈ back(Matrix(D̄))[1]
end
@testset "back(::$TTri)" for TTri in (LowerTriangular,UpperTriangular)
D̄ = TTri(randn(rng, P, P))
@test back(D̄)[1] isa Matrix
@test back(D̄)[2] === nothing
@test back(D̄)[1] ≈ back(Matrix(D̄))[1]
end
end
end
@testset "complex" begin
rng, P = MersenneTwister(123456), 7
Re = randn(rng, P, P)
Im = randn(rng, P, P)
A = complex.(Re, Im)
@testset "gradcheck dense" begin
for uplo in (:U, :L)
@test gradcheck(Re,Im) do a, b
c = Symmetric(complex.(a, b), uplo)
d = exp.(c)
sum(real.(d) + imag.(d))
end
end
end
@testset "uplo=$uplo" for uplo in (:U, :L)
y, back = Zygote.pullback(Symmetric, A, uplo)
@test y isa Symmetric
@testset "back(::Diagonal)" begin
D̄ = Diagonal(complex.(randn(rng, P), randn(rng, P)))
@test back(Diagonal(D̄))[1] isa Diagonal
@test back(Diagonal(D̄))[1] ≈ back(Matrix(D̄))[1]
end
@testset "back(::$TTri)" for TTri in (LowerTriangular,UpperTriangular)
D̄ = TTri(complex.(randn(rng, P, P), randn(rng, P, P)))
@test back(D̄)[1] isa Matrix
@test back(D̄)[2] === nothing
@test back(D̄)[1] ≈ back(Matrix(D̄))[1]
end
end
end
end
@testset "Hermitian" begin
rng, P = MersenneTwister(123456), 7
Re = randn(rng, P, P)
Im = randn(rng, P, P)
A = complex.(Re, Im)
@testset "gradcheck dense" begin
for uplo in (:U, :L)
@test gradcheck(Re,Im) do a, b
c = Hermitian(complex.(a, b), uplo)
d = exp.(c)
sum(real.(d) + imag.(d))
end
end
end
@testset "uplo=$uplo" for uplo in (:U, :L)
y, back = Zygote.pullback(Hermitian, A, uplo)
_, back_sym = Zygote.pullback(Symmetric, A, uplo)
@test y isa Hermitian
@testset "back" begin
D̄ = randn(rng, P, P)
@test back(D̄)[1] ≈ back_sym(D̄)[1]
end
@testset "back(::Diagonal)" begin
D̄ = Diagonal(complex.(randn(rng, P), randn(rng, P)))
@test back(D̄)[1] isa Diagonal
@test back(D̄)[2] === nothing
@test back(D̄)[1] ≈ back(Matrix(D̄))[1]
@test back(real(D̄))[1] ≈ back_sym(real(D̄))[1]
end
@testset "back(::$TTri)" for TTri in (LowerTriangular,UpperTriangular)
D̄ = TTri(complex.(randn(rng, P, P), randn(rng, P, P)))
@test back(D̄)[1] isa Matrix
@test back(D̄)[2] === nothing
@test back(D̄)[1] ≈ back(Matrix(D̄))[1]
@test back(real(D̄))[1] ≈ back_sym(real(D̄))[1]
end
end
end
@testset "diag" begin
rng, P = MersenneTwister(123456), 10
A = randn(rng, P, P)
@test gradtest(diag, A)
end
@testset "Diagonal" begin
rng, P = MersenneTwister(123456), 10
d = randn(rng, P)
@test gradtest(Diagonal, d)
y, back = Zygote.pullback(Diagonal, d)
D̄ = randn(rng, P, P)
@test back(D̄)[1] ≈ back(Diagonal(D̄))[1]
@test back(D̄)[1] ≈ back((diag=diag(D̄),))[1]
end
@testset "dense + UniformScaling" begin
rng = MersenneTwister(123456)
A, λ = randn(rng, 10, 10), randn(rng)
@test gradtest(A->A + 5I, A)
@test gradtest(A->5I - A, A)
@test gradtest(λ->A + λ[1] * I, [λ])
end
@testset "cholesky" begin
@testset "cholesky - dense" begin
rng, N = MersenneTwister(123456), 5
A = randn(rng, N, N)
@test cholesky(A' * A + I).U ≈ first(Zygote.pullback(A->cholesky(A' * A + I), A)).U
@test gradtest(A->cholesky(A' * A + I).U, A)
@test gradtest(A->logdet(cholesky(A' * A + I)), A)
@test gradtest(B->cholesky(Symmetric(B)).U, A * A' + I)
@test gradtest(B->logdet(cholesky(Symmetric(B))), A * A' + I)
end
@testset "cholesky - scalar" begin
rng = MersenneTwister(123456)
y, back = Zygote.pullback(cholesky, 5.0 * ones(1, 1))
y′, back′ = Zygote.pullback(cholesky, 5.0)
C̄ = randn(rng, 1, 1)
@test back′((factors=C̄,))[1] isa Real
@test back′((factors=C̄,))[1] ≈ back((factors=C̄,))[1][1, 1]
end
@testset "cholesky - Diagonal" begin
rng, N = MersenneTwister(123456), 3
D = Diagonal(exp.(randn(rng, N)))
Dmat = Matrix(D)
y, back = Zygote.pullback(cholesky, Dmat)
y′, back′ = Zygote.pullback(cholesky, D)
C̄ = (factors=randn(rng, N, N),)
@test back′(C̄)[1] isa Diagonal
@test diag(back′(C̄)[1]) ≈ diag(back(C̄)[1])
end
@testset "cholesky - Hermitian{Complex}" begin
rng, N = MersenneTwister(123456), 3
A = randn(rng, Complex{Float64}, N, N)
H = Hermitian(A * A' + I)
Hmat = Matrix(H)
y, back = Zygote.pullback(cholesky, Hmat)
y′, back′ = Zygote.pullback(cholesky, H)
C̄ = (factors=randn(rng, N, N),)
@test only(back′(C̄)) isa Hermitian
# gradtest does not support complex gradients, even though the pullback exists
d = only(back(C̄))
d′ = only(back′(C̄))
@test (d + d')/2 ≈ d′
end
@testset "cholesky - Hermitian{Real}" begin
rng, N = MersenneTwister(123456), 3
A = randn(rng, N, N)
H = Hermitian(A * A' + I)
Hmat = Matrix(H)
y, back = Zygote.pullback(cholesky, Hmat)
y′, back′ = Zygote.pullback(cholesky, H)
C̄ = (factors=randn(rng, N, N),)
@test back′(C̄)[1] isa Hermitian
@test gradtest(B->cholesky(Hermitian(B)).U, Hmat)
@test gradtest(B->logdet(cholesky(Hermitian(B))), Hmat)
end
end
@testset "lyap" begin
rng, N = MersenneTwister(6865943), 5
for i = 1:5
A = randn(rng, N, N)
C = randn(rng, N, N)
@test gradtest(lyap, A, C)
end
@test gradcheck(x->lyap(x[1],x[2]),[3.1,4.6])
end
@testset "matrix exponential" begin
@testset "real dense" begin
rng, N = MersenneTwister(6865931), 8
for i = 1:5
A = randn(rng, N, N)
@test gradtest(exp, A)
@testset "similar eigenvalues" begin
λ, V = eigen(A)
λ[1] = λ[3] + sqrt(eps(real(eltype(λ)))) / 10
A2 = real.(V * Diagonal(λ) / V)
@test gradtest(exp, A2)
end
end
end
@testset "complex dense" begin
rng, N = MersenneTwister(6865931), 8
for i = 1:5
A = randn(rng, ComplexF64, N, N)
@test gradcheck(reim(A)...) do a,b
c = complex.(a, b)
d = exp(c)
return sum(real.(d) + 2 .* imag.(d))
end
@testset "similar eigenvalues" begin
λ, V = eigen(A)
λ[1] = λ[3] + sqrt(eps(real(eltype(λ)))) / 10
A2 = V * Diagonal(λ) / V
@test gradcheck(reim(A2)...) do a,b
c = complex.(a, b)
d = exp(c)
return sum(real.(d) + 2 .* imag.(d))
end
end
end
A = [ 0.0 1.0 0.0
0.0 0.0 1.0
-4.34 -18.31 -0.43]
_,back = Zygote.pullback(exp,A)
Ȳ = rand(3,3)
@test isreal(back(Ȳ)[1])
end
end
_hermsymtype(::Type{<:Symmetric}) = Symmetric
_hermsymtype(::Type{<:Hermitian}) = Hermitian
function _gradtest_hermsym(f, ST, A)
gradtest(_splitreim(collect(A))...) do (args...)
B = f(ST(_joinreim(_dropimaggrad.(args)...)))
return sum(_splitreim(B))
end
end
@testset "eigen(::RealHermSymComplexHerm)" begin
MTs = (Symmetric{Float64}, Hermitian{Float64}, Hermitian{ComplexF64})
rng, N = MersenneTwister(123), 7
@testset "eigen(::$MT)" for MT in MTs
T = eltype(MT)
ST = _hermsymtype(MT)
A = ST(randn(rng, T, N, N))
U = eigvecs(A)
@test _gradtest_hermsym(ST, A) do (A)
d, U = eigen(A)
return U * Diagonal(exp.(d)) * U'
end
y = Zygote.pullback(eigen, A)[1]
y2 = eigen(A)
@test y.values ≈ y2.values
@test y.vectors ≈ y2.vectors
@testset "low rank" begin
A2 = Symmetric(U * Diagonal([randn(rng), zeros(N-1)...]) * U')
@test_broken _gradtest_hermsym(ST, A2) do (A)
d, U = eigen(A)
return U * Diagonal(exp.(d)) * U'
end
end
end
end
@testset "eigvals(::RealHermSymComplexHerm)" begin
MTs = (Symmetric{Float64}, Hermitian{Float64}, Hermitian{ComplexF64})
rng, N = MersenneTwister(123), 7
@testset "eigvals(::$MT)" for MT in MTs
T = eltype(MT)
ST = _hermsymtype(MT)
A = ST(randn(rng, T, N, N))
@test _gradtest_hermsym(A ->eigvals(A), ST, A)
@test Zygote.pullback(eigvals, A)[1] ≈ eigvals(A)