@@ -660,161 +660,4 @@ end
660660 test_scalar (g, 2.0 )
661661 test_scalar (g, 3.0 )
662662 test_scalar (Base. inv, 3.0 + 4.0im )
663- end
664-
665- f1 (x) = prod (ntuple (i -> i * x, 3 ))
666- @test autodiff (Reverse, f1, Active, Active (2.0 ))[1 ][1 ] == 72
667- @test autodiff (Forward, f1, Duplicated (2.0 , 1.0 ))[1 ] == 72
668-
669- f2 (x) = x * something (nothing , 2 )
670- @test autodiff (Reverse, f2, Active, Active (1.0 ))[1 ][1 ] == 2
671- @test autodiff (Forward, f2, Duplicated (1.0 , 1.0 ))[1 ] == 2
672-
673- f3 (x) = x * sum (unique ([x, 2.0 , 2.0 , 3.0 ]))
674- @test autodiff (Reverse, f3, Active, Active (1.0 ))[1 ][1 ] == 7
675- @test autodiff (Forward, f3, Duplicated (1.0 , 1.0 ))[1 ] == 7
676-
677- for rf in (reduce, foldl, foldr)
678- f4 (x) = rf (* , [1.0 , x, x, 3.0 ])
679- @test autodiff (Reverse, f4, Active, Active (2.0 ))[1 ][1 ] == 12
680- @test autodiff (Forward, f4, Duplicated (2.0 , 1.0 ))[1 ] == 12
681- end
682-
683- f5 (x) = sum (accumulate (+ , [1.0 , x, x, 3.0 ]))
684- @test autodiff (Reverse, f5, Active, Active (2.0 ))[1 ][1 ] == 5
685- @test autodiff (Forward, f5, Duplicated (2.0 , 1.0 ))[1 ] == 5
686-
687- f6 (x) = x |> inv |> abs
688- @test autodiff (Reverse, f6, Active, Active (- 2.0 ))[1 ][1 ] == 1 / 4
689- @test autodiff (Forward, f6, Duplicated (- 2.0 , 1.0 ))[1 ] == 1 / 4
690-
691- f7 (x) = (inv ∘ abs)(x)
692- @test autodiff (Reverse, f7, Active, Active (- 2.0 ))[1 ][1 ] == 1 / 4
693- @test autodiff (Forward, f7, Duplicated (- 2.0 , 1.0 ))[1 ] == 1 / 4
694-
695- f8 (x) = x * count (i -> i > 1 , [0.5 , x, 1.5 ])
696- @test autodiff (Reverse, f8, Active, Active (2.0 ))[1 ][1 ] == 2
697- @test autodiff (Forward, f8, Duplicated (2.0 , 1.0 ))[1 ] == 2
698-
699- Enzyme. API. strictAliasing! (false )
700- function f9 (x)
701- y = []
702- foreach (i -> push! (y, i^ 2 ), [1.0 , x, x])
703- return sum (y)
704- end
705- @test autodiff (Reverse, f9, Active, Active (2.0 ))[1 ][1 ] == 8
706- @test autodiff (Forward, f9, Duplicated (2.0 , 1.0 ))[1 ] == 8
707-
708- Enzyme. API. strictAliasing! (true )
709- f10 (x) = hypot (x, 2 x)
710- @test autodiff (Reverse, f10, Active, Active (2.0 ))[1 ][1 ] == sqrt (5 )
711- @test autodiff (Forward, f10, Duplicated (2.0 , 1.0 ))[1 ] == sqrt (5 )
712-
713- f11 (x) = x * sum (LinRange (x, 10.0 , 6 ))
714- @test autodiff (Reverse, f11, Active, Active (2.0 ))[1 ][1 ] == 42
715- @test autodiff (Forward, f11, Duplicated (2.0 , 1.0 ))[1 ] == 42
716-
717- f12 (x, k) = get (Dict (1 => 1.0 , 2 => x, 3 => 3.0 ), k, 1.0 )
718- @test autodiff (Reverse, f12, Active, Active (2.0 ), Const (2 ))[1 ] == (1.0 , nothing )
719- @test autodiff (Forward, f12, Duplicated (2.0 , 1.0 ), Const (2 )) == (1.0 ,)
720- @test autodiff (Reverse, f12, Active, Active (2.0 ), Const (3 ))[1 ] == (0.0 , nothing )
721- @test autodiff (Forward, f12, Duplicated (2.0 , 1.0 ), Const (3 )) == (0.0 ,)
722- @test autodiff (Reverse, f12, Active, Active (2.0 ), Const (4 ))[1 ] == (0.0 , nothing )
723- @test autodiff (Forward, f12, Duplicated (2.0 , 1.0 ), Const (4 )) == (0.0 ,)
724-
725- f13 (x) = muladd (x, 3 , x)
726- @test autodiff (Reverse, f13, Active, Active (2.0 ))[1 ][1 ] == 4
727- @test autodiff (Forward, f13, Duplicated (2.0 , 1.0 ))[1 ] == 4
728-
729- f14 (x) = x * cmp (x, 3 )
730- @test autodiff (Reverse, f14, Active, Active (2.0 ))[1 ][1 ] == - 1
731- @test autodiff (Forward, f14, Duplicated (2.0 , 1.0 ))[1 ] == - 1
732-
733- f15 (x) = x * argmax ([1.0 , 3.0 , 2.0 ])
734- @test autodiff (Reverse, f15, Active, Active (3.0 ))[1 ][1 ] == 2
735- @test autodiff (Forward, f15, Duplicated (3.0 , 1.0 ))[1 ] == 2
736-
737- f16 (x) = evalpoly (2 , (1 , 2 , x))
738- @test autodiff (Reverse, f16, Active, Active (3.0 ))[1 ][1 ] == 4
739- @test autodiff (Forward, f16, Duplicated (3.0 , 1.0 ))[1 ] == 4
740-
741- f17 (x) = @evalpoly (2 , 1 , 2 , x)
742- @test autodiff (Reverse, f17, Active, Active (3.0 ))[1 ][1 ] == 4
743- @test autodiff (Forward, f17, Duplicated (3.0 , 1.0 ))[1 ] == 4
744-
745- f18 (x) = widemul (x, 5.0f0 )
746- @test autodiff (Reverse, f18, Active, Active (2.0f0 ))[1 ][1 ] == 5
747- @test autodiff (Forward, f18, Duplicated (2.0f0 , 1.0f0 ))[1 ] == 5
748-
749- f19 (x) = copysign (x, - x)
750- @test autodiff (Reverse, f19, Active, Active (2.0 ))[1 ][1 ] == - 1
751- @test autodiff (Forward, f19, Duplicated (2.0 , 1.0 ))[1 ] == - 1
752-
753- f20 (x) = sum ([ifelse (i > 5 , i, zero (i)) for i in [x, 2 x, 3 x, 4 x]])
754- @test autodiff (Reverse, f20, Active, Active (2.0 ))[1 ][1 ] == 7
755- @test autodiff (Forward, f20, Duplicated (2.0 , 1.0 ))[1 ] == 7
756-
757- function f21 (x)
758- nt = (a= x, b= 2 x, c= 3 x)
759- return nt. c
760- end
761- @test autodiff (Reverse, f21, Active, Active (2.0 ))[1 ][1 ] == 3
762- @test autodiff (Forward, f21, Duplicated (2.0 , 1.0 ))[1 ] == 3
763-
764- f22 (x) = sum (fill (x, (3 , 3 )))
765- @test autodiff (Reverse, f22, Active, Active (2.0 ))[1 ][1 ] == 9
766- @test autodiff (Forward, f22, Duplicated (2.0 , 1.0 ))[1 ] == 9
767-
768- function f23 (x)
769- a = similar (rand (3 , 3 ))
770- fill! (a, x)
771- return sum (a)
772- end
773- @test autodiff (Reverse, f23, Active, Active (2.0 ))[1 ][1 ] == 9
774- @test autodiff (Forward, f23, Duplicated (2.0 , 1.0 ))[1 ] == 9
775-
776- function f24 (x)
777- try
778- return 3 x
779- catch
780- return 2 x
781- end
782- end
783- @test autodiff (Reverse, f24, Active, Active (2.0 ))[1 ][1 ] == 3
784- @test autodiff (Forward, f24, Duplicated (2.0 , 1.0 ))[1 ] == 3
785-
786- function f25 (x)
787- try
788- sqrt (- 1.0 )
789- return 3 x
790- catch
791- return 2 x
792- end
793- end
794- @test autodiff (Reverse, f25, Active, Active (2.0 ))[1 ][1 ] == 2
795- @test autodiff (Forward, f25, Duplicated (2.0 , 1.0 ))[1 ] == 2
796-
797- f26 (x) = circshift ([1.0 , 2 x, 3.0 ], 1 )[end ]
798- @test autodiff (Reverse, f26, Active, Active (2.0 ))[1 ][1 ] == 2
799- @test autodiff (Forward, f26, Duplicated (2.0 , 1.0 ))[1 ] == 2
800-
801- f27 (x) = repeat ([x 3 x], 3 )[2 , 2 ]
802- @test autodiff (Reverse, f27, Active, Active (2.0 ))[1 ][1 ] == 3
803- @test autodiff (Forward, f27, Duplicated (2.0 , 1.0 ))[1 ] == 3
804-
805- f28 (x) = x * sum (trues (4 , 3 ))
806- @test autodiff (Reverse, f28, Active, Active (2.0 ))[1 ][1 ] == 12
807- @test autodiff (Forward, f28, Duplicated (2.0 , 1.0 ))[1 ] == 12
808-
809- f29 (x) = sum (Set ([1.0 , x, 2 x, x]))
810- @static if VERSION ≥ v " 1.11-"
811- @test autodiff (set_runtime_activity (Reverse), f29, Active, Active (2.0 ))[1 ][1 ] == 3
812- @test autodiff (set_runtime_activity (Forward), f29, Duplicated (2.0 , 1.0 ))[1 ] == 3
813- else
814- @test autodiff (Reverse, f29, Active, Active (2.0 ))[1 ][1 ] == 3
815- @test autodiff (Forward, f29, Duplicated (2.0 , 1.0 ))[1 ] == 3
816- end
817-
818- f30 (x) = reverse ([x 2.0 3 x])[1 ]
819- @test autodiff (Reverse, f30, Active, Active (2.0 ))[1 ][1 ] == 3
820- @test autodiff (Forward, f30, Duplicated (2.0 , 1.0 ))[1 ] == 3
663+ end
0 commit comments