Skip to content

Commit a9094b7

Browse files
authored
fix: Add support for all kwargs in permute and transpose chain rules (#513)
* Pass through kwargs in `permute` and `transpose` chain rules * Remove unnecessary guard
1 parent 3e72027 commit a9094b7

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

‎ext/TensorKitChainRulesCoreExt/linalg.jl‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,24 @@ function ChainRulesCore.rrule(::typeof(⊗), A::AbstractTensorMap, B::AbstractTe
6060
end
6161

6262
function ChainRulesCore.rrule(
63-
::typeof(permute), tsrc::AbstractTensorMap, p::Index2Tuple; copy::Bool = false
63+
::typeof(permute), tsrc::AbstractTensorMap, p::Index2Tuple; copy::Bool = false, kwargs...
6464
)
6565
function permute_pullback(Δtdst)
6666
invp = TensorKit._canonicalize(TupleTools.invperm(linearize(p)), tsrc)
67-
return NoTangent(), permute(unthunk(Δtdst), invp; copy = true), NoTangent()
67+
return NoTangent(), permute(unthunk(Δtdst), invp; copy = true, kwargs...), NoTangent()
6868
end
69-
return permute(tsrc, p; copy = true), permute_pullback
69+
return permute(tsrc, p; copy = true, kwargs...), permute_pullback
7070
end
7171

7272
function ChainRulesCore.rrule(
73-
::typeof(transpose), tsrc::AbstractTensorMap, p::Index2Tuple; copy::Bool = false
73+
::typeof(transpose), tsrc::AbstractTensorMap, p::Index2Tuple; copy::Bool = false, kwargs...
7474
)
7575
function transpose_pullback(Δtdst)
7676
invp = TensorKit._canonicalize(TupleTools.invperm(linearize(p)), tsrc)
77-
Δtsrc = transpose(unthunk(Δtdst), invp; copy = true)
77+
Δtsrc = transpose(unthunk(Δtdst), invp; copy = true, kwargs...)
7878
return NoTangent(), ProjectTo(tsrc)(Δtsrc), NoTangent()
7979
end
80-
return transpose(tsrc, p; copy = true), transpose_pullback
80+
return transpose(tsrc, p; copy = true, kwargs...), transpose_pullback
8181
end
8282

8383
function ChainRulesCore.rrule(::typeof(tr), A::AbstractTensorMap)

‎test/chainrules/linalg.jl‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,28 @@ for V in spacelist
112112

113113
test_rrule(transpose, A, ((2, 5, 4), (1, 3)))
114114
symmetricbraiding && test_rrule(permute, A, ((1, 3, 2), (5, 4)))
115+
116+
# the rrules must accept every keyword the primal accepts: `repartition`
117+
# forwards its own `backend`/`allocator` defaults on to `transpose`, so a
118+
# `copy`-only rrule signature breaks AD for callers that pass no keywords at all
119+
bakwargs = (;
120+
backend = TensorOperations.DefaultBackend(),
121+
allocator = TensorOperations.DefaultAllocator(),
122+
)
123+
test_rrule(transpose, A, ((2, 5, 4), (1, 3)); fkwargs = bakwargs)
124+
symmetricbraiding &&
125+
test_rrule(permute, A, ((1, 3, 2), (5, 4)); fkwargs = bakwargs)
126+
127+
# `repartition` has no rrule of its own: it is differentiated by AD-ing through
128+
# its body down to the `transpose` rrule, so it has to be tested end-to-end
129+
# rather than with `test_rrule`
130+
for k in 0:numind(A)
131+
test_ad_rrule(repartition, A, k)
132+
end
133+
# also cover the explicit-`N₂` arity and the `copy` keyword
134+
test_ad_rrule(repartition, A, 2, numind(A) - 2)
135+
test_ad_rrule(repartition, A, 2; fkwargs = (; copy = true))
136+
115137
hasbraiding && test_rrule(twist, A, 1)
116138
hasbraiding && test_rrule(twist, A, [1, 3])
117139

0 commit comments

Comments
 (0)