GPU - Unified Memory error with NDTensors.cu()

Hi, ITensor Teams!

I am trying to use GPU to improve a code using multiples GPU at the same time trying to follow Multiple GPUs · CUDA.jl (juliagpu.org). In general is important to use the unified memory, and usually to do that it is enough to put unified = true as an extra parameter. However, in my code I am using NDTensors.cu() instead of cu() because when I use cu() sometimes I got errors as the one showed here: TEBD with GPU - error with eigen - ITensor Julia Questions - ITensor Discourse

My problem is that when I put unified = true inside NDTensors.cu() I got the following error:

using ITensors

sites = siteinds("S=1/2",50)

#cu()
A = cu(randomMPS(sites); unified = true) 
#This works without problem

#NDTensors.cu()
A = NDTensors.cu(randomMPS(sites); unified=true) 
#Here I got: MethodError: no method matching cu(::MPS; unified::Bool)

So, I am not sure how to use unified memory and NDTensors.cu() at the same time.

Thanks for the report, we’ll have to look into that and think about how we want to support that.

In the meantime, you could try using adapt(CuArray{Float64,<:Any,UnifiedMemory}, x) where x is your MPS. adapt is defined in the package Adapt.jl so you’ll have to install and load that package.

Also note that the latest syntax is random_mps, not randomMPS, we’ll be deprecating randomMPS.

1 Like

Note that you may have to use adapt(CuArray{Float64,1,UnifiedMemory}, x) instead because of a bug/limitation in CUDA.jl.

I raised an issue for the Metal.jl backend here: Generalize `adapt` to allow specifying the storage mode but not the element type and/or number of dimensions · Issue #363 · JuliaGPU/Metal.jl · GitHub but the same issue should be raised in CUDA.jl and other GPU backends.

1 Like

@Joacop16 I think you could also use NDTensors.cu(x; storagemode=UnifiedMemory), I forgot we had added that.

2 Likes

Hi @joacop16, we did change the implementation of our NDTensors.cu function and using the keyword unified should throw an error because that is not an acceptable keyword.

julia> NDTensors.cu(randomMPS(sites); unified=true)
ERROR: MethodError: no method matching cu(::MPS; unified::Bool)

Closest candidates are:
  cu(::Any; storagemode) got unsupported keyword argument "unified"
   @ NDTensorsCUDAExt ~/.julia/dev/ITensors/NDTensors/ext/NDTensorsCUDAExt/adapt.jl:10

Stacktrace:
 [1] kwerr(::@NamedTuple{unified::Bool}, ::Function, ::MPS)
   @ Base ./error.jl:165
 [2] top-level scope

@ REPL[24]:1 What version of ITensors/NDTensors are you using?
I did run your example with the code Matt suggests and it does properly construct an MPS on GPU with UnifiedMemory

julia> sites = siteinds("S=1/2",2)
2-element Vector{Index{Int64}}:
 (dim=2|id=214|"S=1/2,Site,n=1")
 (dim=2|id=199|"S=1/2,Site,n=2")

julia> NDTensors.cu(randomMPS(sites); storagemode=CUDA.UnifiedMemory)[1]
ITensor ord=2 (dim=2|id=214|"S=1/2,Site,n=1") (dim=1|id=406|"Link,l=1")
NDTensors.Dense{Float64, CuArray{Float64, 1, CUDA.UnifiedMemory}}

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.