Skip to content

Commit bac9993

Browse files
authored
Add some rules for a tensorfree which also takes the tensor (#321)
* Add some rules for a tensorfree which also takes the tensor * dumb typo
1 parent 0df6d3e commit bac9993

3 files changed

Lines changed: 7 additions & 3 deletions

File tree

‎ext/TensorOperationsChainRulesCoreExt.jl‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ using LinearAlgebra
2727

2828
# Cannot free intermediate tensors when using AD
2929
# Thus we change the forward passes: `istemp=false` and `tensorfree!` is a no-op
30-
function ChainRulesCore.rrule(
31-
::typeof(TensorOperations.tensorfree!), allocator = DefaultAllocator()
32-
)
30+
function ChainRulesCore.rrule(::typeof(TensorOperations.tensorfree!), C)
3331
tensorfree!_pullback(Δargs...) = (NoTangent(), NoTangent())
3432
return nothing, tensorfree!_pullback
3533
end
34+
function ChainRulesCore.rrule(::typeof(TensorOperations.tensorfree!), C, allocator)
35+
tensorfree!_pullback(Δargs...) = (NoTangent(), NoTangent(), NoTangent())
36+
return nothing, tensorfree!_pullback
37+
end
3638
function ChainRulesCore.rrule(
3739
::typeof(TensorOperations.tensoralloc), ttype, structure,
3840
istemp, allocator = DefaultAllocator()

‎ext/TensorOperationsEnzymeExt/TensorOperationsEnzymeExt.jl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ using Enzyme.EnzymeCore
99
using Enzyme.EnzymeCore: EnzymeRules
1010

1111
@inline EnzymeRules.inactive(::typeof(TensorOperations.tensorfree!), ::Any) = true
12+
@inline EnzymeRules.inactive(::typeof(TensorOperations.tensorfree!), ::Any, ::Any) = true
1213
@inline EnzymeRules.inactive_type(v::Type{<:AbstractBackend}) = true
1314
@inline EnzymeRules.inactive_type(v::Type{DefaultAllocator}) = true
1415
@inline EnzymeRules.inactive_type(v::Type{<:CUDAAllocator}) = true

‎ext/TensorOperationsMooncakeExt/TensorOperationsMooncakeExt.jl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Mooncake.tangent_type(::Type{ManualAllocator}) = Mooncake.NoTangent
2727
@zero_derivative DefaultCtx Tuple{typeof(TensorOperations.promote_add), Any}
2828

2929
Mooncake.@from_rrule Mooncake.DefaultCtx Tuple{typeof(TensorOperations.tensorfree!), Any}
30+
Mooncake.@from_rrule Mooncake.DefaultCtx Tuple{typeof(TensorOperations.tensorfree!), Any, Any}
3031
Mooncake.@from_rrule Mooncake.DefaultCtx Tuple{typeof(TensorOperations.tensoralloc), Any, Any, Any, Any}
3132

3233
Mooncake.@is_primitive DefaultCtx Tuple{typeof(tensorcontract!), AbstractArray, AbstractArray, Index2Tuple, Bool, AbstractArray, Index2Tuple, Bool, Index2Tuple, Number, Number, Vararg{Any}}

0 commit comments

Comments
 (0)