Skip to content

Commit 2de1bb1

Browse files
Katharine Hyattkshyatt
authored andcommitted
Fix
1 parent bb1cee6 commit 2de1bb1

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

‎src/implementation/base.jl‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ function tensoradd!(
2828
end
2929
return C
3030
end
31+
32+
# needed to avoid ambiguity error
3133
function tensoradd!(
3234
C::AbstractArray,
3335
A::Diagonal, pA::Index2Tuple, conjA::Bool,
3436
α::Number, β::Number,
35-
backend, allocator = DefaultAllocator()
37+
::BaseView, allocator = DefaultAllocator()
3638
)
3739
argcheck_tensoradd(C, A, pA)
3840
dimcheck_tensoradd(C, A, pA)
@@ -65,8 +67,13 @@ function tensoradd!(
6567

6668
# can we assume that C is mutable?
6769
# is there more functionality in base that we can use?
68-
Atemp = tensoralloc_add(eltype(A), A, pA, conjA, Val(true), allocator)
69-
à = permutedims!(Atemp, A, linearize(pA))
70+
à = if !isa(A, Diagonal)
71+
Atemp = tensoralloc_add(eltype(A), A, pA, conjA, Val(true), allocator)
72+
permutedims!(Atemp, A, linearize(pA))
73+
else
74+
Atemp = nothing
75+
A
76+
end
7077
if conjA
7178
if iszero(β)
7279
C .= α .* conj.(Ã)
@@ -80,7 +87,7 @@ function tensoradd!(
8087
C .= β .* C .+ α .* Ã
8188
end
8289
end
83-
tensorfree!(Atemp, allocator)
90+
!isa(A, Diagonal) && tensorfree!(Atemp, allocator)
8491
return C
8592
end
8693

‎test/gpu.jl‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,32 @@ bufferallocator(::Type{ROCArray}; kwargs...) = TensorOperations.AMDBufferAllocat
5454
tensoradd!(c, a, (p, ()), false, α, β, backend)
5555
end
5656

57+
T <: Real || @test compare(AT, C, A) do c, a
58+
tensoradd!(c, a, (p, ()), true, α, β, backend)
59+
end
60+
end
61+
# test Diagonal special case
62+
sz = (8, 8)
63+
p = (2, 1)
64+
diag_backends = [BaseCopy(), BaseView()]
65+
for backend in diag_backends, T in (Float32, ComplexF32)
66+
A = Diagonal(randn(T, sz[1]))
67+
C = randn(T, TupleTools.getindices(sz, p))
68+
69+
@test compare(AT, C, A) do c, a
70+
tensoradd!(c, a, (p, ()), false, One(), Zero(), backend)
71+
end
72+
73+
α = rand(T)
74+
@test compare(AT, C, A) do c, a
75+
tensoradd!(c, a, (p, ()), false, α, Zero(), backend)
76+
end
77+
78+
β = rand(T)
79+
@test compare(AT, C, A) do c, a
80+
tensoradd!(c, a, (p, ()), false, α, β, backend)
81+
end
82+
5783
T <: Real || @test compare(AT, C, A) do c, a
5884
tensoradd!(c, a, (p, ()), true, α, β, backend)
5985
end

0 commit comments

Comments
 (0)