|
| 1 | +# need these due to Enzyme choking on blocks |
| 2 | + |
| 3 | +for f in (:project_hermitian, :project_antihermitian) |
| 4 | + f! = Symbol(f, :!) |
| 5 | + @eval begin |
| 6 | + function EnzymeRules.augmented_primal( |
| 7 | + config::EnzymeRules.RevConfigWidth{1}, |
| 8 | + func::Const{typeof($f!)}, |
| 9 | + ::Type{RT}, |
| 10 | + A::Annotation{<:AbstractTensorMap}, |
| 11 | + arg::Annotation{<:AbstractTensorMap}, |
| 12 | + alg::Const, |
| 13 | + ) where {RT} |
| 14 | + $f!(A.val, arg.val, alg.val) |
| 15 | + primal = EnzymeRules.needs_primal(config) ? arg.val : nothing |
| 16 | + shadow = EnzymeRules.needs_shadow(config) ? arg.dval : nothing |
| 17 | + cache = nothing |
| 18 | + return EnzymeRules.AugmentedReturn(primal, shadow, cache) |
| 19 | + end |
| 20 | + function EnzymeRules.reverse( |
| 21 | + config::EnzymeRules.RevConfigWidth{1}, |
| 22 | + func::Const{typeof($f!)}, |
| 23 | + ::Type{RT}, |
| 24 | + cache, |
| 25 | + A::Annotation{<:AbstractTensorMap}, |
| 26 | + arg::Annotation{<:AbstractTensorMap}, |
| 27 | + alg::Const, |
| 28 | + ) where {RT} |
| 29 | + if !isa(A, Const) && !isa(arg, Const) |
| 30 | + $f!(arg.dval, arg.dval, alg.val) |
| 31 | + if A.dval !== arg.dval |
| 32 | + A.dval .+= arg.dval |
| 33 | + make_zero!(arg.dval) |
| 34 | + end |
| 35 | + end |
| 36 | + return (nothing, nothing, nothing) |
| 37 | + end |
| 38 | + function EnzymeRules.augmented_primal( |
| 39 | + config::EnzymeRules.RevConfigWidth{1}, |
| 40 | + func::Const{typeof($f)}, |
| 41 | + ::Type{RT}, |
| 42 | + A::Annotation{<:AbstractTensorMap}, |
| 43 | + alg::Const, |
| 44 | + ) where {RT} |
| 45 | + ret = $f(A.val, alg.val) |
| 46 | + dret = make_zero(ret) |
| 47 | + primal = EnzymeRules.needs_primal(config) ? ret : nothing |
| 48 | + shadow = EnzymeRules.needs_shadow(config) ? dret : nothing |
| 49 | + cache = dret |
| 50 | + return EnzymeRules.AugmentedReturn(primal, shadow, cache) |
| 51 | + end |
| 52 | + function EnzymeRules.reverse( |
| 53 | + config::EnzymeRules.RevConfigWidth{1}, |
| 54 | + func::Const{typeof($f)}, |
| 55 | + ::Type{RT}, |
| 56 | + cache, |
| 57 | + A::Annotation{<:AbstractTensorMap}, |
| 58 | + alg::Const, |
| 59 | + ) where {RT} |
| 60 | + dret = cache |
| 61 | + if !isa(A, Const) |
| 62 | + $f!(dret, dret, alg.val) |
| 63 | + add!(A.dval, dret) |
| 64 | + end |
| 65 | + make_zero!(dret) |
| 66 | + return (nothing, nothing) |
| 67 | + end |
| 68 | + end |
| 69 | +end |
| 70 | + |
| 71 | +# Enzyme seems to have trouble with this one |
| 72 | +function EnzymeRules.augmented_primal( |
| 73 | + config::EnzymeRules.RevConfigWidth{1}, |
| 74 | + func::Const{typeof(svd_compact)}, |
| 75 | + ::Type{RT}, |
| 76 | + A::Annotation{<:AbstractTensorMap}, |
| 77 | + alg::Const, |
| 78 | + ) where {RT} |
| 79 | + USVᴴ = svd_compact(A.val, alg.val) |
| 80 | + primal = EnzymeRules.needs_primal(config) ? USVᴴ : nothing |
| 81 | + shadow = EnzymeRules.needs_shadow(config) ? make_zero(USVᴴ) : nothing |
| 82 | + cache = (USVᴴ, shadow) |
| 83 | + return EnzymeRules.AugmentedReturn(primal, shadow, cache) |
| 84 | +end |
| 85 | +function EnzymeRules.reverse( |
| 86 | + config::EnzymeRules.RevConfigWidth{1}, |
| 87 | + func::Const{typeof(svd_compact)}, |
| 88 | + ::Type{RT}, |
| 89 | + cache, |
| 90 | + A::Annotation{<:AbstractTensorMap}, |
| 91 | + alg::Const, |
| 92 | + ) where {RT} |
| 93 | + !isa(A, Const) && MatrixAlgebraKit.svd_pullback!(A.dval, A.val, cache...) |
| 94 | + return (nothing, nothing) |
| 95 | +end |
| 96 | + |
| 97 | +# Enzyme seems to have trouble with this one |
| 98 | +function EnzymeRules.augmented_primal( |
| 99 | + config::EnzymeRules.RevConfigWidth{1}, |
| 100 | + func::Const{typeof(svd_trunc_no_error)}, |
| 101 | + ::Type{RT}, |
| 102 | + A::Annotation{<:AbstractTensorMap}, |
| 103 | + alg::Const, |
| 104 | + ) where {RT} |
| 105 | + USVᴴ = svd_compact(A.val, alg.val.alg) |
| 106 | + USVᴴtrunc, ind = MatrixAlgebraKit.truncate(svd_trunc!, USVᴴ, alg.val.trunc) |
| 107 | + dUSVᴴtrunc = make_zero(USVᴴtrunc) |
| 108 | + cache = (USVᴴ, USVᴴtrunc, dUSVᴴtrunc, ind) |
| 109 | + return EnzymeRules.AugmentedReturn(USVᴴtrunc, dUSVᴴtrunc, cache) |
| 110 | +end |
| 111 | +function EnzymeRules.reverse( |
| 112 | + config::EnzymeRules.RevConfigWidth{1}, |
| 113 | + func::Const{typeof(svd_trunc_no_error)}, |
| 114 | + ::Type{RT}, |
| 115 | + cache, |
| 116 | + A::Annotation{<:AbstractTensorMap}, |
| 117 | + alg::Const, |
| 118 | + ) where {RT} |
| 119 | + USVᴴ, USVᴴtrunc, dUSVᴴtrunc, ind = cache |
| 120 | + MatrixAlgebraKit.svd_pullback!(A.dval, A.val, USVᴴ, dUSVᴴtrunc, ind) |
| 121 | + return (nothing, nothing) |
| 122 | +end |
0 commit comments