-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTensorAlgebraTensorOperationsExt.jl
More file actions
152 lines (137 loc) · 3.88 KB
/
Copy pathTensorAlgebraTensorOperationsExt.jl
File metadata and controls
152 lines (137 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
module TensorAlgebraTensorOperationsExt
using TensorAlgebra: TensorAlgebra, BlockedPermutation, Algorithm, blocklengths
using TupleTools: TupleTools
using TensorOperations: TensorOperations, AbstractBackend, DefaultBackend, Index2Tuple
"""
TensorOperationsAlgorithm(backend::AbstractBackend)
Wrapper type for making a TensorOperations backend work as a TensorAlgebra algorithm.
"""
struct TensorOperationsAlgorithm{B<:AbstractBackend} <: Algorithm
backend::B
end
TensorAlgebra.Algorithm(backend::AbstractBackend) = TensorOperationsAlgorithm(backend)
trivtuple(n) = ntuple(identity, n)
function _index2tuple(p::BlockedPermutation{2})
N₁, N₂ = blocklengths(p)
return (
TupleTools.getindices(Tuple(p), trivtuple(N₁)),
TupleTools.getindices(Tuple(p), N₁ .+ trivtuple(N₂)),
)
end
_blockedpermutation(p::Index2Tuple) = TensorAlgebra.blockedpermvcat(p...)
# Using TensorOperations backends as TensorAlgebra implementations
# ----------------------------------------------------------------
# not in-place
function TensorAlgebra.contract(
algorithm::TensorOperationsAlgorithm,
bipermAB::BlockedPermutation,
A::AbstractArray,
bipermA::BlockedPermutation,
B::AbstractArray,
bipermB::BlockedPermutation,
α::Number,
)
pA = _index2tuple(bipermA)
pB = _index2tuple(bipermB)
pAB = _index2tuple(bipermAB)
return TensorOperations.tensorcontract(
A, pA, false, B, pB, false, pAB, α, algorithm.backend
)
end
function TensorAlgebra.contract(
algorithm::TensorOperationsAlgorithm,
labelsC,
A::AbstractArray,
labelsA,
B::AbstractArray,
labelsB,
α::Number,
)
pA, pB, pAB = TensorOperations.contract_indices(labelsA, labelsB, labelsC)
return tensorcontract(A, pA, false, B, pB, false, pAB, α, algorithm.backend)
end
# in-place
function TensorAlgebra.contractadd!(
algorithm::TensorOperationsAlgorithm,
C::AbstractArray,
bipermAB::BlockedPermutation,
A::AbstractArray,
bipermA::BlockedPermutation,
B::AbstractArray,
bipermB::BlockedPermutation,
α::Number,
β::Number,
)
pA = _index2tuple(bipermA)
pB = _index2tuple(bipermB)
pAB = _index2tuple(bipermAB)
return TensorOperations.tensorcontract!(
C, A, pA, false, B, pB, false, pAB, α, β, algorithm.backend
)
end
function TensorAlgebra.contractadd!(
algorithm::TensorOperationsAlgorithm,
C::AbstractArray,
labelsC,
A::AbstractArray,
labelsA,
B::AbstractArray,
labelsB,
α::Number,
β::Number,
)
pA, pB, pAB = TensorOperations.contract_indices(labelsA, labelsB, labelsC)
return TensorOperations.tensorcontract!(
C, A, pA, false, B, pB, false, pAB, α, β, algorithm.backend
)
end
# Using TensorAlgebra implementations as TensorOperations backends
# ----------------------------------------------------------------
function TensorOperations.tensorcontract!(
C::AbstractArray,
A::AbstractArray,
pA::Index2Tuple,
conjA::Bool,
B::AbstractArray,
pB::Index2Tuple,
conjB::Bool,
pAB::Index2Tuple,
α::Number,
β::Number,
backend::Algorithm,
allocator,
)
bipermA = _blockedpermutation(pA)
bipermB = _blockedpermutation(pB)
bipermAB = _blockedpermutation(pAB)
A′ = conjA ? conj(A) : A
B′ = conjB ? conj(B) : B
return TensorAlgebra.contractadd!(backend, C, bipermAB, A′, bipermA, B′, bipermB, α, β)
end
# For now no trace/add is supported, so simply reselect default backend from TensorOperations
function TensorOperations.tensortrace!(
C::AbstractArray,
A::AbstractArray,
p::Index2Tuple,
q::Index2Tuple,
conjA::Bool,
α::Number,
β::Number,
::Algorithm,
allocator,
)
return TensorOperations.tensortrace!(C, A, p, q, conjA, α, β, DefaultBackend(), allocator)
end
function TensorOperations.tensoradd!(
C::AbstractArray,
A::AbstractArray,
pA::Index2Tuple,
conjA::Bool,
α::Number,
β::Number,
::Algorithm,
allocator,
)
return TensorOperations.tensoradd!(C, A, pA, conjA, α, β, DefaultBackend(), allocator)
end
end