Jutho / TensorOperations.jl

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

Certain contractions with matrices resulting in zeros #120

Closed jack-dunham closed 1 year ago

jack-dunham commented 2 years ago

Hi there,

It seems that contracting a Matrix, say B, along the last dimension of an Array named A, such that A gets overwritten results in an array of all zeros.

That is:

julia> A = rands(2,2,2,2);

julia> B = randn(2,2);

julia> @tensor A[1,2,3,4]:=A[1,2,3,s]*B[s,4]
2×2×2×2 Array{Float64, 4}:
[:, :, 1, 1] =
 0.278628  -0.139971
 0.292018   0.0158706

[:, :, 2, 1] =
 -0.556978  1.47143
  0.275593  0.536201

[:, :, 1, 2] =
 -0.419582  0.6236
 -0.704791  0.656905

[:, :, 2, 2] =
 -0.934475  -1.09174
 -2.32854   -1.35743

but,

julia> A = randn(2,2,2,2);

julia> B = randn(2,2);

julia> @tensor A[1,2,3,4]=A[1,2,3,s]*B[s,4]
2×2×2×2 Array{Float64, 4}:
[:, :, 1, 1] =
 0.0  0.0
 0.0  0.0

[:, :, 2, 1] =
 0.0  0.0
 0.0  0.0

[:, :, 1, 2] =
 0.0  0.0
 0.0  0.0

[:, :, 2, 2] =
 0.0  0.0
 0.0  0.0

This only seems to occur when contracting along the last index of A.

Tested on a 2021 M1+ MacBook Pro using:

TensorOperations v3.2.4 Julia 1.7.1 (Native ARM) Julia 1.8.0-rc3 (Native ARM) Julia 1.7.3 (Rosetta)

Happy to provide any more information.

Cheers, Jack

Jutho commented 2 years ago

This kind of operation is not allowed. Even with two normal general dense matrices, you cannot multiply them in such a way that the result is stored in one of the two. You always need a third matrix to store the result (it's different if one of the two is upper or lower triangular).

However, despite this operation not being allowed, there is no error. It's very hard to detect aliasing in the most general case, e.g. like if instead of just using A two times, one is a view over part of the other or so. So to be clear, it is the user's responsibility to make sure that no aliasing arises; otherwise you can expect unexpected results.

jack-dunham commented 1 year ago

Good to know, thanks!