Skip to content

Commit b58fa66

Browse files
authored
Use is_inactive check for TO blas_contract (#590)
* Use is_inactive check for TO blas_contract * Add config
1 parent d279dee commit b58fa66

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

‎ext/TensorKitEnzymeExt/TensorKitEnzymeExt.jl‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ using MatrixAlgebraKit
1111
using TupleTools
1212
using Random: AbstractRNG
1313

14+
"""
15+
is_inactive(config, x::Annotation) -> Bool
16+
17+
Check whether the annotated argument `x` should be treated as inactive, i.e. whether no tangent should be read from or accumulated into `x.dval`.
18+
Under runtime activity, Enzyme may pass an argument that is inactive at run time as a `Duplicated` whose shadow aliases the primal (`x.dval === x.val`);
19+
writing into that shadow would then corrupt the primal.
20+
See https://enzymead.github.io/Enzyme.jl/dev/faq/#faq-runtime-activity.
21+
22+
!!! note
23+
This is a stopgap until an equivalent helper is available in `EnzymeRules`
24+
(see https://github.com/EnzymeAD/Enzyme.jl/pull/3597#discussion_r4014313289).
25+
"""
26+
@inline is_inactive(config, ::Const) = true
27+
@inline is_inactive(config, x::Annotation) = EnzymeRules.runtime_activity(config) && x.dval === x.val
28+
1429
include("utility.jl")
1530
include("linalg.jl")
1631
include("indexmanipulations.jl")

‎ext/TensorKitEnzymeExt/tensoroperations.jl‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ function EnzymeRules.augmented_primal(
2222
backend::Const,
2323
allocator::Const
2424
) where {RT}
25-
Ccache = isa(β, Const) ? nothing : copy(C.val)
26-
A_needs_cache = EnzymeRules.overwritten(config)[3] && !(typeof(B) <: Const) && !(typeof(C) <: Const)
25+
Ccache = isa(β, Const) || is_inactive(config, C) ? nothing : copy(C.val)
26+
A_needs_cache = EnzymeRules.overwritten(config)[3] && !(typeof(B) <: Const) && !(typeof(C) <: Const) && !is_inactive(config, A)
2727
Acache = A_needs_cache ? copy(A.val) : nothing
28-
B_needs_cache = EnzymeRules.overwritten(config)[5] && !(typeof(A) <: Const) && !(typeof(C) <: Const)
28+
B_needs_cache = EnzymeRules.overwritten(config)[5] && !(typeof(A) <: Const) && !(typeof(C) <: Const) && !is_inactive(config, B)
2929
Bcache = B_needs_cache ? copy(B.val) : nothing
3030
AB = if !isa(α, Const)
3131
AB = TO.tensorcontract(A.val, pA.val, false, B.val, pB.val, false, pAB.val, One(), backend.val, allocator.val)
@@ -65,17 +65,17 @@ function EnzymeRules.reverse(
6565
Δα = pullback_dα(α, C, AB)
6666
Δβ = pullback_dβ(β, C, Cval)
6767

68-
if !isa(A, Const)
68+
if !is_inactive(config, A) && !is_inactive(config, C)
6969
TensorKit.blas_contract_pullback_ΔA!(
7070
A.dval, C.dval, Aval, pA.val, Bval, pB.val, pAB.val, α.val, backend.val, allocator.val
7171
) # this typically returns nothing
7272
end
73-
if !isa(B, Const)
73+
if !is_inactive(config, B) && !is_inactive(config, C)
7474
TensorKit.blas_contract_pullback_ΔB!(
7575
B.dval, C.dval, Aval, pA.val, Bval, pB.val, pAB.val, α.val, backend.val, allocator.val
7676
) # this typically returns nothing
7777
end
78-
!isa(C, Const) && pullback_dC!(C.dval, β.val) # this typically returns nothing
78+
!is_inactive(config, C) && pullback_dC!(C.dval, β.val) # this typically returns nothing
7979
return nothing, nothing, nothing, nothing, nothing, nothing, Δα, Δβ, nothing, nothing
8080
end
8181

0 commit comments

Comments
 (0)