Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/implementation/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@ function tensoradd!(
end
return C
end

# needed to avoid ambiguity error
function tensoradd!(
C::AbstractArray,
A::Diagonal, pA::Index2Tuple, conjA::Bool,
α::Number, β::Number,
::BaseView, allocator = DefaultAllocator()
)
argcheck_tensoradd(C, A, pA)
dimcheck_tensoradd(C, A, pA)

# can we assume that C is mutable?
# is there more functionality in base that we can use?
if conjA
if iszero(β)
C .= α .* conj.(A)
else
C .= β .* C .+ α .* conj.(A)
end
else
if iszero(β)
C .= α .* A
else
C .= β .* C .+ α .* A
end
end
return C
end
function tensoradd!(
C::AbstractArray,
A::AbstractArray, pA::Index2Tuple, conjA::Bool,
Expand All @@ -39,8 +67,13 @@ function tensoradd!(

# can we assume that C is mutable?
# is there more functionality in base that we can use?
Atemp = tensoralloc_add(eltype(A), A, pA, conjA, Val(true), allocator)
à = permutedims!(Atemp, A, linearize(pA))
à = if !isa(A, Diagonal)
Atemp = tensoralloc_add(eltype(A), A, pA, conjA, Val(true), allocator)
permutedims!(Atemp, A, linearize(pA))
else
Atemp = nothing
A
end
if conjA
if iszero(β)
C .= α .* conj.(Ã)
Expand All @@ -54,7 +87,7 @@ function tensoradd!(
C .= β .* C .+ α .* Ã
end
end
tensorfree!(Atemp, allocator)
!isa(A, Diagonal) && tensorfree!(Atemp, allocator)
return C
end

Expand Down
26 changes: 26 additions & 0 deletions test/gpu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,32 @@ bufferallocator(::Type{ROCArray}; kwargs...) = TensorOperations.AMDBufferAllocat
tensoradd!(c, a, (p, ()), false, α, β, backend)
end

T <: Real || @test compare(AT, C, A) do c, a
tensoradd!(c, a, (p, ()), true, α, β, backend)
end
end
# test Diagonal special case
sz = (8, 8)
p = (2, 1)
diag_backends = [BaseCopy(), BaseView()]
for backend in diag_backends, T in (Float32, ComplexF32)
A = Diagonal(randn(T, sz[1]))
C = randn(T, TupleTools.getindices(sz, p))

@test compare(AT, C, A) do c, a
tensoradd!(c, a, (p, ()), false, One(), Zero(), backend)
end

α = rand(T)
@test compare(AT, C, A) do c, a
tensoradd!(c, a, (p, ()), false, α, Zero(), backend)
end

β = rand(T)
@test compare(AT, C, A) do c, a
tensoradd!(c, a, (p, ()), false, α, β, backend)
end

T <: Real || @test compare(AT, C, A) do c, a
tensoradd!(c, a, (p, ()), true, α, β, backend)
end
Expand Down
Loading