Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NDTensors] [BUG] Constructing a Tensor from a view of another tensor can fail #1438

Closed
kmp5VT opened this issue May 13, 2024 · 0 comments · Fixed by #1441
Closed

[NDTensors] [BUG] Constructing a Tensor from a view of another tensor can fail #1438

kmp5VT opened this issue May 13, 2024 · 0 comments · Fixed by #1441
Labels
bug Something isn't working NDTensors Requires changes to the NDTensors.jl library.

Comments

@kmp5VT
Copy link
Collaborator

kmp5VT commented May 13, 2024

Description of bug

There is an issue when one tries to construct a new tensor from the view of another tensor. It comes from the parameters of the new tensor being defined before the storage is copied, i.e. a type mismatch.

Minimal runnable code

using ITensors: Index
using NDTensors: randomTensor, Tensor
i,j = Index.((4,4))
t = randomTensor((i,j))
Tensor(storage(t[1:2, 1:2]), (2, 2))

Output of minimal runnable code

ERROR: MethodError: Cannot `convert` an object of type Vector{Float64} to an object of type Base.ReshapedArray{Float64, 1, SubArray{Float64, 2, Matrix{Float64}, Tuple{UnitRange{Int64}, UnitRange{Int64}}, false}, Tuple{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64}}}

Closest candidates are:
  convert(::Type{T}, ::T) where T
   @ Base Base.jl:84
  convert(::Type{T}, ::LinearAlgebra.Factorization) where T<:AbstractArray
   @ LinearAlgebra ~/.julia/juliaup/julia-1.10.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/factorization.jl:108
  convert(::Type{T}, ::T) where T<:AbstractArray
   @ Base abstractarray.jl:16
  ...

Stacktrace:
 [1] convert(::Type{NDTensors.Dense{Float64, Base.ReshapedArray{…}}}, D::NDTensors.Dense{Float64, Vector{Float64}})
   @ NDTensors ~/.julia/dev/ITensors/NDTensors/src/dense/dense.jl:142
 [2] NDTensors.DenseTensor{…}(::NDTensors.AllowAlias, storage::NDTensors.Dense{…}, inds::Tuple{…})
   @ NDTensors ~/.julia/dev/ITensors/NDTensors/src/tensor/tensor.jl:27
 [3] NDTensors.DenseTensor{…}(::NDTensors.NeverAlias, storage::NDTensors.Dense{…}, inds::Tuple{…})
   @ NDTensors ~/.julia/dev/ITensors/NDTensors/src/tensor/tensor.jl:36
 [4] Tensor(as::NDTensors.NeverAlias, storage::NDTensors.Dense{Float64, Base.ReshapedArray{…}}, inds::Tuple{Int64, Int64})
   @ NDTensors ~/.julia/dev/ITensors/NDTensors/src/tensor/tensor.jl:67
 [5] Tensor(storage::NDTensors.Dense{Float64, Base.ReshapedArray{…}}, inds::Tuple{Int64, Int64})
   @ NDTensors ~/.julia/dev/ITensors/NDTensors/src/tensor/tensor.jl:81
 [6] top-level scope
   @ Untitled-1:16
Some type information was truncated. Use `show(err)` to see complete types.

Version information

  • Output from versioninfo():
julia> versioninfo()
Julia Version 1.10.2
Commit bd47eca2c8a (2024-03-01 10:14 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 10 × Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)
Environment:
  LD_LIBRARY_PATH = /Users/kpierce/Software/triqs/triqs_install/lib::/opt/intel/oneapi/mkl/latest/lib
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 

Proposed solution

The code stack calls

function Tensor(as::AliasStyle, storage::TensorStorage, inds::Tuple)
  return Tensor{eltype(storage),length(inds),typeof(storage),typeof(inds)}(
    as, storage, inds
  )
end

which calls

function Tensor{ElT,N,StoreT,IndsT}(
  ::NeverAlias, storage::TensorStorage, inds
) where {ElT,N,StoreT<:TensorStorage,IndsT}
  return Tensor{ElT,N,StoreT,IndsT}(AllowAlias(), copy(storage), inds)
end

In the first call

typeof(storage(t[1:2, 1:2])) = NDTensors.Dense{Float64, Base.ReshapedArray{Float64, 1, SubArray{Float64, 2, Matrix{Float64}, Tuple{UnitRange{Int64}, UnitRange{Int64}}, false}, Tuple{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64}}}}

and when the type of the copied storage is

typeof(copy(storage(t[1:2, 1:2])))
NDTensors.Dense{Float64, Vector{Float64}}

So my proposal is to modify the second tensor constructor to be

function Tensor{ElT,N,StoreT,IndsT}(
  ::NeverAlias, storage::TensorStorage, inds
) where {ElT,N,StoreT<:TensorStorage,IndsT}
  store = copy(storage)
  return Tensor{ElT,N,typeof(store),IndsT}(AllowAlias(), store, inds)
end

which returns the following output

julia> Tensor(storage(t[1:2, 1:2]), (2,2)) 
Dim 1: 2
Dim 2: 2
NDTensors.Dense{Float64, Vector{Float64}}
 2×2
 -0.8736954509592826   0.30952483312000373
  1.6882572502541735  -0.7830143316102207
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working NDTensors Requires changes to the NDTensors.jl library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant