Skip to content

Commit 806406f

Browse files
leburgelJutho
andauthored
Fix dβ in the Enzyme rule for tensoradd! when an Active β is zero (#307)
* Fix Enzyme `tensoradd!` rule caching `C` based on the value of `β` instead of its activity * Preserve type of second option * Update ext/TensorOperationsEnzymeExt/TensorOperationsEnzymeExt.jl Co-authored-by: Jutho <Jutho@users.noreply.github.com> --------- Co-authored-by: Jutho <Jutho@users.noreply.github.com>
1 parent 1250807 commit 806406f

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

‎ext/TensorOperationsEnzymeExt/TensorOperationsEnzymeExt.jl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ function EnzymeRules.augmented_primal(
189189
) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TC <: Number}
190190
# form caches if needed
191191
cache_A = EnzymeRules.overwritten(config)[3] ? copy(A_dA.val) : nothing
192-
cache_C = !iszero(β_dβ.val) ? copy(C_dC.val) : C_dC.val
192+
cache_C = !isa(β_dβ, Const) ? copy(C_dC.val) : C_dC.val
193193
ba = map(ba_ -> getfield(ba_, :val), ba_dba)
194194
α = α_dα.val
195195
β = β_dβ.val
@@ -309,7 +309,7 @@ function EnzymeRules.augmented_primal(
309309
) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TC <: Number}
310310
# form caches if needed
311311
cache_A = EnzymeRules.overwritten(config)[3] ? copy(A_dA.val) : nothing
312-
cache_C = !isa(β_dβ, Const) ? copy(C_dC.val) : nothing
312+
cache_C = !isa(β_dβ, Const) ? copy(C_dC.val) : C_dC.val
313313
ba = map(ba_ -> getfield(ba_, :val), ba_dba)
314314
α = α_dα.val
315315
β = β_dβ.val

‎test/enzyme.jl‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,12 @@ end
251251
test_reverse(tensorscalar, Active, (C, Duplicated); atol, rtol)
252252
test_forward(tensorscalar, Duplicated, (C, Duplicated); atol, rtol)
253253
end
254+
255+
# dβ needs the original C whenever β is Active, also when its value is zero
256+
@testset "tensoradd! with Active β = 0" begin
257+
pA = ((2, 1, 4, 3, 5), ())
258+
A = rand(Float64, (2, 3, 4, 2, 1))
259+
C = rand(Float64, size.(Ref(A), pA[1]))
260+
test_reverse(tensoradd!, Duplicated, (C, Duplicated), (A, Duplicated), (pA, Const), (false, Const), (randn(), Active), (0.0, Active))
261+
test_reverse(tensoradd!, Duplicated, (C, Duplicated), (A, Duplicated), (pA, Const), (false, Const), (randn(), Active), (randn(), Const))
262+
end

0 commit comments

Comments
 (0)