Jutho / TensorOperations.jl

Julia package for tensor contractions and related operations
https://jutho.github.io/TensorOperations.jl/stable/
Other
438 stars 55 forks source link

Wrong result with subnetworks with equal labels #149

Closed frankwswang closed 9 months ago

frankwswang commented 10 months ago

MWE:

using TensorOperations # v4.0.4

using Random

Random.seed!(1234)

v1 = rand(8);
v2 = rand(8);
v3 = rand(8);
v4 = rand(8);
v5 = rand(8);
v6 = rand(8);
v7 = rand(8);
v8 = rand(8);
m1 = rand(8,8,8,8);
m2 = rand(8,8,8,8);

eval1 = function ()
    res = 0
    for a in eachindex(v1), b in eachindex(v2), c in eachindex(v3), d in eachindex(v4)
        res += 
        (v1[a] * v2[b] * v3[c] * v4[d] + v2[a] * v1[b] * v3[c] * v4[d] + 
         v1[c] * v2[d] * v3[a] * v4[b] + v1[c] * v2[d] * v4[a] * v3[b]) * 
         m1[a,b,c,d] + 
        (v5[a] * v2[b] * v3[c] * v4[d] + 
         v1[a] * v6[b] * v3[c] * v4[d] + 
         v1[a] * v2[b] * v7[c] * v4[d] + 
         v1[a] * v2[b] * v3[c] * v8[d] ) * 
         m2[a,b,c,d]
    end
    res
end

eval2 = function ()
    @tensor res = 
        (v1[a] * v2[b] * v3[c] * v4[d] + v2[a] * v1[b] * v3[c] * v4[d] + 
         v1[c] * v2[d] * v3[a] * v4[b] + v1[c] * v2[d] * v4[a] * v3[b]) * 
         m1[a,b,c,d] + 
        (v5[a] * v2[b] * v3[c] * v4[d] + 
         v1[a] * v6[b] * v3[c] * v4[d] + 
         v1[a] * v2[b] * v7[c] * v4[d] + 
         v1[a] * v2[b] * v3[c] * v8[d] ) * 
         m2[a,b,c,d]
    res
end

eval3 = function ()
    @tensor res1 = 
        (v1[a] * v2[b] * v3[c] * v4[d] + v2[a] * v1[b] * v3[c] * v4[d] + 
         v1[c] * v2[d] * v3[a] * v4[b] + v1[c] * v2[d] * v4[a] * v3[b]) * 
         m1[a,b,c,d]
    @tensor res2 = 
        (v5[a] * v2[b] * v3[c] * v4[d] + 
         v1[a] * v6[b] * v3[c] * v4[d] + 
         v1[a] * v2[b] * v7[c] * v4[d] + 
         v1[a] * v2[b] * v3[c] * v8[d] ) * 
         m2[a,b,c,d]
    res1 + res2
end

eval1() ≈ eval2() # false
eval1() ≈ eval3() # false
eval2() ≈ eval3() # false

eval11 = function ()
    res = 0
    for a in eachindex(v1), b in eachindex(v2), c in eachindex(v3), d in eachindex(v4)
        res += 
        (v1[a] * v2[b] * v3[c] * v4[d]) * m1[a,b,c,d] + 
        (v5[a] * v2[b] * v3[c] * v4[d]) * m2[a,b,c,d]
    end
    res
end

eval22 = function ()
    @tensor res = 
        (v1[a] * v2[b] * v3[c] * v4[d]) * m1[a,b,c,d] + 
        (v5[a] * v2[b] * v3[c] * v4[d]) * m2[a,b,c,d]
    res
end

eval33 = function ()
    @tensor res1 = (v1[a] * v2[b] * v3[c] * v4[d]) * m1[a,b,c,d]
    @tensor res2 = (v5[a] * v2[b] * v3[c] * v4[d]) * m2[a,b,c,d]
    res1 + res2
end

eval11() ≈ eval22() # false
eval11() ≈ eval33() # true

System info:

Julia Version 1.9.3
Commit bed2cd540a (2023-08-24 14:43 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 20 × 12th Gen Intel(R) Core(TM) i9-12900HK
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, alderlake)
  Threads: 8 on 20 virtual cores
Environment:
  JULIA_IMAGE_THREADS = 1
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 8

I noticed this issue after I upgraded TensorOperations.jl from v3 to v4.

lkdvos commented 10 months ago

Thanks for the report, we're looking into fixing this ASAP.

Jutho commented 9 months ago

I am now tagging 4.0.5, which is resolving this issue.