|
| 1 | +function EnzymeRules.reverse( |
| 2 | + config::EnzymeRules.RevConfigWidth{1}, |
| 3 | + func::Const{typeof(MatrixAlgebraKit.copy_input)}, |
| 4 | + ::Type{RT}, |
| 5 | + cache, |
| 6 | + f::Annotation, |
| 7 | + A::Annotation{<:AbstractTensorMap} |
| 8 | + ) where {RT} |
| 9 | + copy_shadow = cache |
| 10 | + if !isa(A, Const) && !isnothing(copy_shadow) |
| 11 | + add!(A.dval, copy_shadow) |
| 12 | + end |
| 13 | + return (nothing, nothing) |
| 14 | +end |
| 15 | + |
| 16 | +for (f, pb) in ( |
| 17 | + (:eig_full, :(MatrixAlgebraKit.eig_pullback!)), |
| 18 | + (:eigh_full, :(MatrixAlgebraKit.eigh_pullback!)), |
| 19 | + (:lq_compact, :(MatrixAlgebraKit.lq_pullback!)), |
| 20 | + (:qr_compact, :(MatrixAlgebraKit.qr_pullback!)), |
| 21 | + ) |
| 22 | + @eval begin |
| 23 | + function EnzymeRules.augmented_primal( |
| 24 | + config::EnzymeRules.RevConfigWidth{1}, |
| 25 | + func::Const{typeof($f)}, |
| 26 | + ::Type{RT}, |
| 27 | + A::Annotation{<:AbstractTensorMap}, |
| 28 | + alg::Const, |
| 29 | + ) where {RT} |
| 30 | + ret = $f(A.val, alg.val) |
| 31 | + dret = make_zero(ret) |
| 32 | + cache = (ret, dret) |
| 33 | + return EnzymeRules.AugmentedReturn(ret, dret, cache) |
| 34 | + end |
| 35 | + function EnzymeRules.reverse( |
| 36 | + config::EnzymeRules.RevConfigWidth{1}, |
| 37 | + func::Const{typeof($f)}, |
| 38 | + ::Type{RT}, |
| 39 | + cache, |
| 40 | + A::Annotation{<:AbstractTensorMap}, |
| 41 | + alg::Const, |
| 42 | + ) where {RT} |
| 43 | + ret, dret = cache |
| 44 | + $pb(A.dval, A.val, ret, dret) |
| 45 | + return (nothing, nothing) |
| 46 | + end |
| 47 | + end |
| 48 | +end |
| 49 | + |
| 50 | +for f in (:svd_compact, :svd_full) |
| 51 | + @eval begin |
| 52 | + function EnzymeRules.augmented_primal( |
| 53 | + config::EnzymeRules.RevConfigWidth{1}, |
| 54 | + func::Const{typeof($f)}, |
| 55 | + ::Type{RT}, |
| 56 | + A::Annotation{<:AbstractTensorMap}, |
| 57 | + alg::Const, |
| 58 | + ) where {RT} |
| 59 | + USVᴴ = $f(A.val, alg.val) |
| 60 | + dUSVᴴ = make_zero(USVᴴ) |
| 61 | + cache = (USVᴴ, dUSVᴴ) |
| 62 | + return EnzymeRules.AugmentedReturn(USVᴴ, dUSVᴴ, cache) |
| 63 | + end |
| 64 | + function EnzymeRules.reverse( |
| 65 | + config::EnzymeRules.RevConfigWidth{1}, |
| 66 | + func::Const{typeof($f)}, |
| 67 | + ::Type{RT}, |
| 68 | + cache, |
| 69 | + A::Annotation{<:AbstractTensorMap}, |
| 70 | + alg::Const, |
| 71 | + ) where {RT} |
| 72 | + USVᴴ, dUSVᴴ = cache |
| 73 | + MatrixAlgebraKit.svd_pullback!(A.dval, A.val, USVᴴ, dUSVᴴ) |
| 74 | + return (nothing, nothing) |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + # mutating version is not guaranteed to actually mutate |
| 79 | + # so we can simply use the non-mutating version instead |
| 80 | + f! = Symbol(f, :!) |
| 81 | + #=@eval begin |
| 82 | + function EnzymeRules.augmented_primal( |
| 83 | + config::EnzymeRules.RevConfigWidth{1}, |
| 84 | + func::Const{typeof($f!)}, |
| 85 | + ::Type{RT}, |
| 86 | + A::Annotation{<:AbstractTensorMap}, |
| 87 | + USVᴴ::Annotation, |
| 88 | + alg::Const, |
| 89 | + ) where {RT} |
| 90 | + EnzymeRules.augmented_primal(func, RT, A, alg) |
| 91 | + end |
| 92 | + function EnzymeRules.reverse( |
| 93 | + config::EnzymeRules.RevConfigWidth{1}, |
| 94 | + func::Const{typeof($f!)}, |
| 95 | + ::Type{RT}, |
| 96 | + cache, |
| 97 | + A::Annotation{<:AbstractTensorMap}, |
| 98 | + USVᴴ::Annotation, |
| 99 | + alg::Const, |
| 100 | + ) where {RT} |
| 101 | + EnzymeRules.reverse(func, RT, A, alg) |
| 102 | + end |
| 103 | + end=# #hmmmm |
| 104 | +end |
| 105 | + |
| 106 | +# TODO |
| 107 | +#= |
| 108 | +function EnzymeRules.augmented_primal( |
| 109 | + config::EnzymeRules.RevConfigWidth{1}, |
| 110 | + func::Const{typeof(svd_trunc)}, |
| 111 | + ::Type{RT}, |
| 112 | + A::Annotation{<:AbstractTensorMap}, |
| 113 | + alg::Const, |
| 114 | + ) where {RT} |
| 115 | +
|
| 116 | + USVᴴ = svd_compact(A.val, alg.val.alg) |
| 117 | + USVᴴtrunc, ind = MatrixAlgebraKit.truncate(svd_trunc!, USVᴴ, alg.val.trunc) |
| 118 | + ϵ = MatrixAlgebraKit.truncation_error(diagview(USVᴴ[2]), ind) |
| 119 | + dUSVᴴtrunc = make_zero(USVᴴtrunc) |
| 120 | + cache = (USVᴴtrunc, dUSVᴴtrunc) |
| 121 | + return EnzymeRules.AugmentedReturn(USVᴴtrunc, dUSVᴴtrunc, cache) |
| 122 | +end |
| 123 | +function EnzymeRules.reverse( |
| 124 | + config::EnzymeRules.RevConfigWidth{1}, |
| 125 | + func::Const{typeof(svd_trunc)}, |
| 126 | + ::Type{RT}, |
| 127 | + cache, |
| 128 | + A::Annotation{<:AbstractTensorMap}, |
| 129 | + alg::Const, |
| 130 | +) where {RT} |
| 131 | + USVᴴ, dUSVᴴ = cache |
| 132 | + MatrixAlgebraKit.svd_pullback!(A.dval, A.val, USVᴴ, dUSVᴴ) |
| 133 | + return (nothing, nothing) |
| 134 | +end=# |
0 commit comments