|
| 1 | +using TensorOperations |
| 2 | +using TensorOperations: StridedBLAS, StridedNative, linearize, numout |
| 3 | +using Test |
| 4 | +using Adapt |
| 5 | +using TupleTools |
| 6 | +using JLArrays |
| 7 | +using VectorInterface |
| 8 | +using CUDACore |
| 9 | + |
| 10 | +test_result(a::AbstractArray, b::AbstractArray; kwargs...) = |
| 11 | + isapprox(collect(a), collect(b); kwargs...) |
| 12 | + |
| 13 | +function compare(f, AT::Type, xs...; kwargs...) |
| 14 | + cpu_in = map(deepcopy, xs) # copy on CPU |
| 15 | + gpu_in = map(adapt(AT), xs) # adapt on GPU |
| 16 | + |
| 17 | + cpu_out = f(cpu_in...) |
| 18 | + gpu_out = f(gpu_in...) |
| 19 | + |
| 20 | + return test_result(cpu_out, gpu_out; kwargs...) |
| 21 | +end |
| 22 | + |
| 23 | +# types to test for |
| 24 | +ATs = [] |
| 25 | +!is_buildkite && push!(ATs, JLArray) |
| 26 | +CUDACore.functional() && push!(ATs, CuArray) |
| 27 | + |
| 28 | +backends = [StridedBLAS(), StridedNative()] |
| 29 | + |
| 30 | +@testset "tensoradd! ($AT)" for AT in ATs |
| 31 | + sz = (3, 5, 4, 6) |
| 32 | + p = (3, 1, 4, 2) |
| 33 | + for backend in backends, T in (Float32, ComplexF32) |
| 34 | + A = randn(T, sz) |
| 35 | + C = randn(T, TupleTools.getindices(sz, p)) |
| 36 | + |
| 37 | + @test compare(AT, C, A) do c, a |
| 38 | + tensoradd!(c, a, (p, ()), false, One(), Zero(), backend) |
| 39 | + end |
| 40 | + |
| 41 | + α = rand(T) |
| 42 | + @test compare(AT, C, A) do c, a |
| 43 | + tensoradd!(c, a, (p, ()), false, α, Zero(), backend) |
| 44 | + end |
| 45 | + |
| 46 | + β = rand(T) |
| 47 | + @test compare(AT, C, A) do c, a |
| 48 | + tensoradd!(c, a, (p, ()), false, α, β, backend) |
| 49 | + end |
| 50 | + |
| 51 | + T <: Real || @test compare(AT, C, A) do c, a |
| 52 | + tensoradd!(c, a, (p, ()), true, α, β, backend) |
| 53 | + end |
| 54 | + end |
| 55 | +end |
| 56 | + |
| 57 | +@testset "tensortrace! ($AT)" for AT in ATs |
| 58 | + sz = (2, 4, 3, 2) |
| 59 | + p = (2, 3) |
| 60 | + q = ((1,), (4,)) |
| 61 | + |
| 62 | + for backend in backends, T in (Float32, ComplexF32) |
| 63 | + A = randn(T, sz) |
| 64 | + C = randn(T, TupleTools.getindices(sz, p)) |
| 65 | + |
| 66 | + @test compare(AT, C, A) do c, a |
| 67 | + tensortrace!(c, a, (p, ()), q, false, One(), Zero(), backend) |
| 68 | + end |
| 69 | + |
| 70 | + α = rand(T) |
| 71 | + @test compare(AT, C, A) do c, a |
| 72 | + tensortrace!(c, a, (p, ()), q, false, α, Zero(), backend) |
| 73 | + end |
| 74 | + |
| 75 | + β = rand(T) |
| 76 | + @test compare(AT, C, A) do c, a |
| 77 | + tensortrace!(c, a, (p, ()), q, false, α, β, backend) |
| 78 | + end |
| 79 | + |
| 80 | + T <: Real || @test compare(AT, C, A) do c, a |
| 81 | + tensortrace!(c, a, (p, ()), q, true, α, β, backend) |
| 82 | + end |
| 83 | + end |
| 84 | +end |
| 85 | + |
| 86 | +@testset "tensorcontract! ($AT)" for AT in ATs |
| 87 | + sz = (2, 4, 3, 4, 2, 5) |
| 88 | + pA = ((4, 1), (2, 3)) |
| 89 | + pB = ((3, 1), (2,)) |
| 90 | + pAB = ((1, 2, 3), ()) |
| 91 | + |
| 92 | + for backend in backends, T in (Float32, ComplexF32) |
| 93 | + A = randn(T, (2, 4, 3, 2)) |
| 94 | + B = randn(T, (3, 3, 4)) |
| 95 | + C = randn(T, (2, 2, 3)) |
| 96 | + |
| 97 | + @test compare(AT, C, A, B) do c, a, b |
| 98 | + tensorcontract!(c, a, pA, false, b, pB, false, pAB, One(), Zero(), backend) |
| 99 | + end |
| 100 | + |
| 101 | + α = rand(T) |
| 102 | + @test compare(AT, C, A, B) do c, a, b |
| 103 | + tensorcontract!(c, a, pA, false, b, pB, false, pAB, α, Zero(), backend) |
| 104 | + end |
| 105 | + |
| 106 | + β = rand(T) |
| 107 | + @test compare(AT, C, A, B) do c, a, b |
| 108 | + tensorcontract!(c, a, pA, false, b, pB, false, pAB, α, β, backend) |
| 109 | + end |
| 110 | + |
| 111 | + if !(T <: Real) |
| 112 | + @test compare(AT, C, A, B) do c, a, b |
| 113 | + tensorcontract!(c, a, pA, true, b, pB, false, pAB, α, β, backend) |
| 114 | + end |
| 115 | + @test compare(AT, C, A, B) do c, a, b |
| 116 | + tensorcontract!(c, a, pA, false, b, pB, true, pAB, α, β, backend) |
| 117 | + end |
| 118 | + @test compare(AT, C, A, B) do c, a, b |
| 119 | + tensorcontract!(c, a, pA, true, b, pB, true, pAB, α, β, backend) |
| 120 | + end |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | +end |
0 commit comments