@@ -216,7 +216,7 @@ struct SumRuleConfig <: RuleConfig{Union{HasReverseMode}} end
216216 # `foldl(op, itr; init)` goes to `mapfoldr_impl(identity, op, init, itr)`. The rule is
217217 # now attached there, as this is the simplest way to handle `init` keyword.
218218 @eval using Base: mapfoldl_impl
219- @eval _INIT = VERSION >= v " 1.5" ? Base. _InitialValue () : NamedTuple ()
219+ _INIT = VERSION >= v " 1.5" ? Base. _InitialValue () : NamedTuple ()
220220
221221 # Simple
222222 y1, b1 = rrule (CFG, mapfoldl_impl, identity, * , 1 , [1 , 2 , 3 ])
@@ -336,36 +336,45 @@ end
336336 end # cumprod
337337
338338 @testset " accumulate(f, ::Array)" begin
339+ # `accumulate(f, A; init)` goes to `_accumulate!(op, B, A, dims::Nothing, init::Nothing)`.
340+ # The rule is now attached there, as this is the simplest way to handle `init` keyword.
341+ @eval using Base: _accumulate!
342+
339343 # Simple
340- y1, b1 = rrule (CFG, accumulate , * , [1 , 2 , 3 , 4 ]; init = 1 )
344+ y1, b1 = rrule (CFG, _accumulate! , * , [0 , 0 , 0 , 0 ], [ 1 , 2 , 3 , 4 ], nothing , Some ( 1 ) )
341345 @test y1 == [1 , 2 , 6 , 24 ]
342- @test b1 ([1 , 1 , 1 , 1 ]) == (NoTangent (), NoTangent (), [33 , 16 , 10 , 6 ])
346+ @test b1 ([1 , 1 , 1 , 1 ])[3 ] isa ChainRulesCore. NotImplemented
347+ @test b1 ([1 , 1 , 1 , 1 ])[4 ] == [33 , 16 , 10 , 6 ]
348+ @test b1 ([1 , 1 , 1 , 1 ])[6 ] isa Tangent{Some{Int64}}
349+ @test b1 ([1 , 1 , 1 , 1 ])[6 ]. value isa ChainRulesCore. NotImplemented
343350
344351 y2, b2 = rrule (CFG, accumulate, / , [1 2 ; 3 4 ])
345352 @test y2 ≈ accumulate (/ , [1 2 ; 3 4 ])
346353 @test b2 (ones (2 , 2 ))[3 ] ≈ [1.5416666 - 0.104166664 ; - 0.18055555 - 0.010416667 ] atol= 1e-6
347354
348355 # Test execution order
349356 c3 = Counter ()
350- y3, b3 = rrule (CFG, accumulate , c3, [5 , 7 , 11 ]; init = 3 )
357+ y3, b3 = rrule (CFG, _accumulate! , c3, [0 , 0 , 0 ], [ 5 , 7 , 11 ], nothing , Some ( 3 ) )
351358 @test c3 == Counter (3 )
352359 @test y3 == [8 , 30 , 123 ] == accumulate (Counter (), [5 , 7 , 11 ]; init= 3 )
353- @test b3 ([1 , 1 , 1 ]) == ( NoTangent (), NoTangent (), [29169 , 602 , 23 ]) # the 23 is clear!
360+ @test b3 ([1 , 1 , 1 ])[ 4 ] == [29169 , 602 , 23 ] # the 23 is clear!
354361
355362 c4 = Counter ()
356- y4, b4 = rrule (CFG, accumulate , c4, [5 , 7 , 11 ])
363+ y4, b4 = rrule (CFG, _accumulate! , c4, [0 , 0 , 0 ], [ 5 , 7 , 11 ], nothing , nothing )
357364 @test c4 == Counter (2 )
358365 @test y4 == [5 , (5 + 7 )* 1 , ((5 + 7 )* 1 + 11 )* 2 ] == accumulate (Counter (), [5 , 7 , 11 ])
359- @test b4 ([1 , 1 , 1 ]) == ( NoTangent (), NoTangent (), [417 , 42 * (1 + 12 ), 22 ])
366+ @test b4 ([1 , 1 , 1 ])[ 4 ] == [417 , 42 * (1 + 12 ), 22 ]
360367
361368 # Test gradient of function
362- y7, b7 = rrule (CFG, accumulate , Multiplier (3 ), [5 , 7 , 11 ])
369+ y7, b7 = rrule (CFG, _accumulate! , Multiplier (3 ), [0 , 0 , 0 ], [ 5 , 7 , 11 ], nothing , nothing )
363370 @test y7 == accumulate ((x,y)-> x* y* 3 , [5 , 7 , 11 ])
364- @test b7 ([1 , 1 , 1 ]) == (NoTangent (), Tangent {Multiplier{Int}} (x = 2345 ,), [715 , 510 , 315 ])
371+ @test b7 ([1 , 1 , 1 ])[2 ] == Tangent {Multiplier{Int}} (; x = 2345 ,)
372+ @test b7 ([1 , 1 , 1 ])[4 ] == [715 , 510 , 315 ]
365373
366- y8, b8 = rrule (CFG, accumulate , Multiplier (13 ), [5 , 7 , 11 ], init = 3 )
374+ y8, b8 = rrule (CFG, _accumulate! , Multiplier (13 ), [0 , 0 , 0 ], [ 5 , 7 , 11 ], nothing , Some ( 3 ) )
367375 @test y8 == [195 , 17745 , 2537535 ] == accumulate ((x,y)-> x* y* 13 , [5 , 7 , 11 ], init= 3 )
368- @test b8 ([1 , 1 , 1 ]) == (NoTangent (), Tangent {Multiplier{Int}} (x = 588330 ,), [511095 , 365040 , 230685 ])
376+ @test b8 ([1 , 1 , 1 ])[2 ] == Tangent {Multiplier{Int}} (; x = 588330 ,)
377+ @test b8 ([1 , 1 , 1 ])[4 ] == [511095 , 365040 , 230685 ]
369378 # To find these numbers:
370379 # ForwardDiff.derivative(z -> sum(accumulate((x,y)->x*y*z, [5,7,11], init=3)), 13)
371380 # ForwardDiff.gradient(z -> sum(accumulate((x,y)->x*y*13, z, init=3)), [5,7,11]) |> string
384393 # Finite differencing
385394 test_rrule (accumulate, * , Tuple (randn (5 )); fkwargs= (; init= rand ()))
386395 test_rrule (accumulate, / , Tuple (1 .+ rand (5 )); check_inferred= false )
396+
397+ test_rrule (_accumulate!, * , randn (5 ) ⊢ NoTangent (), randn (5 ), nothing , nothing )
398+ test_rrule (_accumulate!, / , randn (5 ) ⊢ NoTangent (), randn (5 ), nothing , Some (1 + rand ()))
399+ # if VERSION >= v"1.5"
400+ # test_rrule(accumulate, /, 1 .+ rand(3, 4))
401+ # test_rrule(accumulate, ^, 1 .+ rand(2, 3); fkwargs=(; init=rand()))
402+ # end
387403 end
404+ # VERSION >= v"1.5" && @testset "accumulate(f, ::Tuple)" begin
405+ # # Simple
406+ # y1, b1 = rrule(CFG, accumulate, *, (1, 2, 3, 4); init=1)
407+ # @test y1 == (1, 2, 6, 24)
408+ # @test b1((1, 1, 1, 1)) == (NoTangent(), NoTangent(), Tangent{NTuple{4,Int}}(33, 16, 10, 6))
409+
410+ # # Finite differencing
411+ # test_rrule(accumulate, *, Tuple(randn(5)); fkwargs=(; init=rand()))
412+ # test_rrule(accumulate, /, Tuple(1 .+ rand(5)); check_inferred=false)
413+ # end
388414end
0 commit comments