-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ChainRulesCoreExt into separate files
- Loading branch information
Showing
6 changed files
with
343 additions
and
351 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
ext/TensorKitChainRulesCoreExt/TensorKitChainRulesCoreExt.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module TensorKitChainRulesCoreExt | ||
|
||
using TensorOperations | ||
using VectorInterface | ||
using TensorKit | ||
using ChainRulesCore | ||
using LinearAlgebra | ||
using TupleTools | ||
|
||
import TensorOperations as TO | ||
using TensorOperations: Backend, promote_contract | ||
using VectorInterface: promote_scale, promote_add | ||
|
||
ext = @static if isdefined(Base, :get_extension) | ||
Base.get_extension(TensorOperations, :TensorOperationsChainRulesCoreExt) | ||
else | ||
TensorOperations.TensorOperationsChainRulesCoreExt | ||
end | ||
const _conj = ext._conj | ||
const trivtuple = ext.trivtuple | ||
|
||
include("utility.jl") | ||
include("constructors.jl") | ||
include("linalg.jl") | ||
include("tensoroperations.jl") | ||
include("factorizations.jl") | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
@non_differentiable TensorKit.TensorMap(f::Function, storagetype, cod, dom) | ||
@non_differentiable TensorKit.id(args...) | ||
@non_differentiable TensorKit.isomorphism(args...) | ||
@non_differentiable TensorKit.isometry(args...) | ||
@non_differentiable TensorKit.unitary(args...) | ||
|
||
function ChainRulesCore.rrule(::Type{<:TensorMap}, d::DenseArray, args...) | ||
function TensorMap_pullback(Δt) | ||
∂d = convert(Array, Δt) | ||
return NoTangent(), ∂d, fill(NoTangent(), length(args))... | ||
end | ||
return TensorMap(d, args...), TensorMap_pullback | ||
end | ||
|
||
function ChainRulesCore.rrule(::typeof(Base.copy), t::AbstractTensorMap) | ||
copy_pullback(Δt) = NoTangent(), Δt | ||
return copy(t), copy_pullback | ||
end | ||
|
||
function ChainRulesCore.rrule(::typeof(Base.convert), T::Type{<:Array}, | ||
t::AbstractTensorMap) | ||
A = convert(T, t) | ||
function convert_pullback(ΔA) | ||
∂t = TensorMap(ΔA, codomain(t), domain(t)) | ||
return NoTangent(), NoTangent(), ∂t | ||
end | ||
return A, convert_pullback | ||
end | ||
|
||
function ChainRulesCore.rrule(::typeof(Base.convert), ::Type{Dict}, t::AbstractTensorMap) | ||
out = convert(Dict, t) | ||
function convert_pullback(c) | ||
if haskey(c, :data) # :data is the only thing for which this dual makes sense | ||
dual = copy(out) | ||
dual[:data] = c[:data] | ||
return (NoTangent(), NoTangent(), convert(TensorMap, dual)) | ||
else | ||
# instead of zero(t) you can also return ZeroTangent(), which is type unstable | ||
return (NoTangent(), NoTangent(), zero(t)) | ||
end | ||
end | ||
return out, convert_pullback | ||
end | ||
function ChainRulesCore.rrule(::typeof(Base.convert), ::Type{TensorMap}, | ||
t::Dict{Symbol,Any}) | ||
return convert(TensorMap, t), v -> (NoTangent(), NoTangent(), convert(Dict, v)) | ||
end |
Oops, something went wrong.