@@ -216,7 +216,7 @@ struct SumRuleConfig <: RuleConfig{Union{HasReverseMode}} end
216
216
# `foldl(op, itr; init)` goes to `mapfoldr_impl(identity, op, init, itr)`. The rule is
217
217
# now attached there, as this is the simplest way to handle `init` keyword.
218
218
@eval using Base: mapfoldl_impl
219
- @eval _INIT = VERSION >= v " 1.5" ? Base. _InitialValue () : NamedTuple ()
219
+ _INIT = VERSION >= v " 1.5" ? Base. _InitialValue () : NamedTuple ()
220
220
221
221
# Simple
222
222
y1, b1 = rrule (CFG, mapfoldl_impl, identity, * , 1 , [1 , 2 , 3 ])
@@ -336,36 +336,45 @@ end
336
336
end # cumprod
337
337
338
338
@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
+
339
343
# 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 ) )
341
345
@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
343
350
344
351
y2, b2 = rrule (CFG, accumulate, / , [1 2 ; 3 4 ])
345
352
@test y2 ≈ accumulate (/ , [1 2 ; 3 4 ])
346
353
@test b2 (ones (2 , 2 ))[3 ] ≈ [1.5416666 - 0.104166664 ; - 0.18055555 - 0.010416667 ] atol= 1e-6
347
354
348
355
# Test execution order
349
356
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 ) )
351
358
@test c3 == Counter (3 )
352
359
@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!
354
361
355
362
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 )
357
364
@test c4 == Counter (2 )
358
365
@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 ]
360
367
361
368
# 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 )
363
370
@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 ]
365
373
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 ) )
367
375
@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 ]
369
378
# To find these numbers:
370
379
# ForwardDiff.derivative(z -> sum(accumulate((x,y)->x*y*z, [5,7,11], init=3)), 13)
371
380
# ForwardDiff.gradient(z -> sum(accumulate((x,y)->x*y*13, z, init=3)), [5,7,11]) |> string
384
393
# Finite differencing
385
394
test_rrule (accumulate, * , Tuple (randn (5 )); fkwargs= (; init= rand ()))
386
395
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
387
403
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
388
414
end
0 commit comments