@@ -217,7 +217,7 @@ const CFG = ChainRulesTestUtils.ADviaRuleConfig()
217217 # `foldl(op, itr; init)` goes to `mapfoldr_impl(identity, op, init, itr)`. The rule is
218218 # now attached there, as this is the simplest way to handle `init` keyword.
219219 @eval using Base: mapfoldl_impl
220- @eval _INIT = VERSION >= v " 1.5" ? Base. _InitialValue () : NamedTuple ()
220+ _INIT = VERSION >= v " 1.5" ? Base. _InitialValue () : NamedTuple ()
221221
222222 # Simple
223223 y1, b1 = rrule (CFG, mapfoldl_impl, identity, * , 1 , [1 , 2 , 3 ])
@@ -337,36 +337,45 @@ end
337337 end # cumprod
338338
339339 @testset " accumulate(f, ::Array)" begin
340+ # `accumulate(f, A; init)` goes to `_accumulate!(op, B, A, dims::Nothing, init::Nothing)`.
341+ # The rule is now attached there, as this is the simplest way to handle `init` keyword.
342+ @eval using Base: _accumulate!
343+
340344 # Simple
341- y1, b1 = rrule (CFG, accumulate , * , [1 , 2 , 3 , 4 ]; init = 1 )
345+ y1, b1 = rrule (CFG, _accumulate! , * , [0 , 0 , 0 , 0 ], [ 1 , 2 , 3 , 4 ], nothing , Some ( 1 ) )
342346 @test y1 == [1 , 2 , 6 , 24 ]
343- @test b1 ([1 , 1 , 1 , 1 ]) == (NoTangent (), NoTangent (), [33 , 16 , 10 , 6 ])
347+ @test b1 ([1 , 1 , 1 , 1 ])[3 ] isa ChainRulesCore. NotImplemented
348+ @test b1 ([1 , 1 , 1 , 1 ])[4 ] == [33 , 16 , 10 , 6 ]
349+ @test b1 ([1 , 1 , 1 , 1 ])[6 ] isa Tangent{Some{Int64}}
350+ @test b1 ([1 , 1 , 1 , 1 ])[6 ]. value isa ChainRulesCore. NotImplemented
344351
345352 y2, b2 = rrule (CFG, accumulate, / , [1 2 ; 3 4 ])
346353 @test y2 ≈ accumulate (/ , [1 2 ; 3 4 ])
347354 @test b2 (ones (2 , 2 ))[3 ] ≈ [1.5416666 - 0.104166664 ; - 0.18055555 - 0.010416667 ] atol= 1e-6
348355
349356 # Test execution order
350357 c3 = Counter ()
351- y3, b3 = rrule (CFG, accumulate , c3, [5 , 7 , 11 ]; init = 3 )
358+ y3, b3 = rrule (CFG, _accumulate! , c3, [0 , 0 , 0 ], [ 5 , 7 , 11 ], nothing , Some ( 3 ) )
352359 @test c3 == Counter (3 )
353360 @test y3 == [8 , 30 , 123 ] == accumulate (Counter (), [5 , 7 , 11 ]; init= 3 )
354- @test b3 ([1 , 1 , 1 ]) == ( NoTangent (), NoTangent (), [29169 , 602 , 23 ]) # the 23 is clear!
361+ @test b3 ([1 , 1 , 1 ])[ 4 ] == [29169 , 602 , 23 ] # the 23 is clear!
355362
356363 c4 = Counter ()
357- y4, b4 = rrule (CFG, accumulate , c4, [5 , 7 , 11 ])
364+ y4, b4 = rrule (CFG, _accumulate! , c4, [0 , 0 , 0 ], [ 5 , 7 , 11 ], nothing , nothing )
358365 @test c4 == Counter (2 )
359366 @test y4 == [5 , (5 + 7 )* 1 , ((5 + 7 )* 1 + 11 )* 2 ] == accumulate (Counter (), [5 , 7 , 11 ])
360- @test b4 ([1 , 1 , 1 ]) == ( NoTangent (), NoTangent (), [417 , 42 * (1 + 12 ), 22 ])
367+ @test b4 ([1 , 1 , 1 ])[ 4 ] == [417 , 42 * (1 + 12 ), 22 ]
361368
362369 # Test gradient of function
363- y7, b7 = rrule (CFG, accumulate , Multiplier (3 ), [5 , 7 , 11 ])
370+ y7, b7 = rrule (CFG, _accumulate! , Multiplier (3 ), [0 , 0 , 0 ], [ 5 , 7 , 11 ], nothing , nothing )
364371 @test y7 == accumulate ((x,y)-> x* y* 3 , [5 , 7 , 11 ])
365- @test b7 ([1 , 1 , 1 ]) == (NoTangent (), Tangent {Multiplier{Int}} (x = 2345 ,), [715 , 510 , 315 ])
372+ @test b7 ([1 , 1 , 1 ])[2 ] == Tangent {Multiplier{Int}} (; x = 2345 ,)
373+ @test b7 ([1 , 1 , 1 ])[4 ] == [715 , 510 , 315 ]
366374
367- y8, b8 = rrule (CFG, accumulate , Multiplier (13 ), [5 , 7 , 11 ], init = 3 )
375+ y8, b8 = rrule (CFG, _accumulate! , Multiplier (13 ), [0 , 0 , 0 ], [ 5 , 7 , 11 ], nothing , Some ( 3 ) )
368376 @test y8 == [195 , 17745 , 2537535 ] == accumulate ((x,y)-> x* y* 13 , [5 , 7 , 11 ], init= 3 )
369- @test b8 ([1 , 1 , 1 ]) == (NoTangent (), Tangent {Multiplier{Int}} (x = 588330 ,), [511095 , 365040 , 230685 ])
377+ @test b8 ([1 , 1 , 1 ])[2 ] == Tangent {Multiplier{Int}} (; x = 588330 ,)
378+ @test b8 ([1 , 1 , 1 ])[4 ] == [511095 , 365040 , 230685 ]
370379 # To find these numbers:
371380 # ForwardDiff.derivative(z -> sum(accumulate((x,y)->x*y*z, [5,7,11], init=3)), 13)
372381 # ForwardDiff.gradient(z -> sum(accumulate((x,y)->x*y*13, z, init=3)), [5,7,11]) |> string
385394 # Finite differencing
386395 test_rrule (accumulate, * , Tuple (randn (5 )); fkwargs= (; init= rand ()))
387396 test_rrule (accumulate, / , Tuple (1 .+ rand (5 )); check_inferred= false )
397+
398+ test_rrule (_accumulate!, * , randn (5 ) ⊢ NoTangent (), randn (5 ), nothing , nothing )
399+ test_rrule (_accumulate!, / , randn (5 ) ⊢ NoTangent (), randn (5 ), nothing , Some (1 + rand ()))
400+ # if VERSION >= v"1.5"
401+ # test_rrule(accumulate, /, 1 .+ rand(3, 4))
402+ # test_rrule(accumulate, ^, 1 .+ rand(2, 3); fkwargs=(; init=rand()))
403+ # end
388404 end
405+ # VERSION >= v"1.5" && @testset "accumulate(f, ::Tuple)" begin
406+ # # Simple
407+ # y1, b1 = rrule(CFG, accumulate, *, (1, 2, 3, 4); init=1)
408+ # @test y1 == (1, 2, 6, 24)
409+ # @test b1((1, 1, 1, 1)) == (NoTangent(), NoTangent(), Tangent{NTuple{4,Int}}(33, 16, 10, 6))
410+
411+ # # Finite differencing
412+ # test_rrule(accumulate, *, Tuple(randn(5)); fkwargs=(; init=rand()))
413+ # test_rrule(accumulate, /, Tuple(1 .+ rand(5)); check_inferred=false)
414+ # end
389415end
0 commit comments