-
Notifications
You must be signed in to change notification settings - Fork 56
Start on CUDA extension #325
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
Open
kshyatt
wants to merge
19
commits into
main
Choose a base branch
from
ksh/cuda_small
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+873
−61
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3d1ba4e
Start on CUDA extension
kshyatt b174787
More small fixes
kshyatt 7da4835
move collect in
kshyatt e4f6d82
More test via CPU
kshyatt a384cdc
Update example
kshyatt 084f2ee
Comments
kshyatt bd1966c
Fixup diagonal
kshyatt 832ed15
More comments
kshyatt 68b282a
Update test/cuda/tensors.jl
kshyatt f188aed
Update test/cuda/tensors.jl
kshyatt 329afe6
Update test/cuda/tensors.jl
kshyatt e55f338
Update test/cuda/tensors.jl
kshyatt 61ba421
Fix bad end
kshyatt 1c75de5
Make format happy
kshyatt 3949ec0
Small updates
kshyatt 7172691
Update Project.toml
5a9c8cd
Try to fix spacing
658235a
one more format attempt
4d79f14
two more format attempt
lkdvos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| module TensorKitCUDAExt | ||
|
|
||
| using CUDA, CUDA.CUBLAS, CUDA.CUSOLVER, LinearAlgebra | ||
| using CUDA: @allowscalar | ||
| using cuTENSOR: cuTENSOR | ||
| import CUDA: rand as curand, rand! as curand!, randn as curandn, randn! as curandn! | ||
|
|
||
| using TensorKit | ||
| using TensorKit.Factorizations | ||
| using TensorKit.Strided | ||
| using TensorKit.Factorizations: AbstractAlgorithm | ||
| using TensorKit: SectorDict, tensormaptype, scalar, similarstoragetype, AdjointTensorMap, scalartype, project_symmetric_and_check | ||
| import TensorKit: randisometry, rand, randn | ||
|
|
||
| using TensorKit: MatrixAlgebraKit | ||
|
|
||
| using Random | ||
|
|
||
| include("cutensormap.jl") | ||
|
|
||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| const CuTensorMap{T, S, N₁, N₂} = TensorMap{T, S, N₁, N₂, CuVector{T, CUDA.DeviceMemory}} | ||
| const CuTensor{T, S, N} = CuTensorMap{T, S, N, 0} | ||
|
|
||
| const AdjointCuTensorMap{T, S, N₁, N₂} = AdjointTensorMap{T, S, N₁, N₂, CuTensorMap{T, S, N₁, N₂}} | ||
|
|
||
| function CuTensorMap(t::TensorMap{T, S, N₁, N₂, A}) where {T, S, N₁, N₂, A} | ||
| return CuTensorMap{T, S, N₁, N₂}(CuArray{T}(t.data), space(t)) | ||
| end | ||
|
|
||
| # project_symmetric! doesn't yet work for GPU types, so do this on the host, then copy | ||
| function TensorKit.project_symmetric_and_check(::Type{T}, ::Type{A}, data::AbstractArray, V::TensorMapSpace; tol = sqrt(eps(real(float(eltype(data)))))) where {T, A <: CuVector{T}} | ||
| h_t = TensorKit.TensorMapWithStorage{T, Vector{T}}(undef, V) | ||
| h_t = TensorKit.project_symmetric!(h_t, Array(data)) | ||
| # verify result | ||
| isapprox(Array(reshape(data, dims(h_t))), convert(Array, h_t); atol = tol) || | ||
| throw(ArgumentError("Data has non-zero elements at incompatible positions")) | ||
| return TensorKit.TensorMapWithStorage{T, A}(A(h_t.data), V) | ||
| end | ||
|
|
||
| for (fname, felt) in ((:zeros, :zero), (:ones, :one)) | ||
| @eval begin | ||
| function CUDA.$fname( | ||
| codomain::TensorSpace{S}, | ||
| domain::TensorSpace{S} = one(codomain) | ||
| ) where {S <: IndexSpace} | ||
| return CUDA.$fname(codomain ← domain) | ||
| end | ||
| function CUDA.$fname( | ||
| ::Type{T}, codomain::TensorSpace{S}, | ||
| domain::TensorSpace{S} = one(codomain) | ||
| ) where {T, S <: IndexSpace} | ||
| return CUDA.$fname(T, codomain ← domain) | ||
| end | ||
| CUDA.$fname(V::TensorMapSpace) = CUDA.$fname(Float64, V) | ||
| function CUDA.$fname(::Type{T}, V::TensorMapSpace) where {T} | ||
| t = CuTensorMap{T}(undef, V) | ||
| fill!(t, $felt(T)) | ||
| return t | ||
| end | ||
| end | ||
| end | ||
|
|
||
| for randfun in (:curand, :curandn) | ||
| randfun! = Symbol(randfun, :!) | ||
| @eval begin | ||
| # converting `codomain` and `domain` into `HomSpace` | ||
| function $randfun( | ||
| codomain::TensorSpace{S}, | ||
| domain::TensorSpace{S} = one(codomain), | ||
| ) where {S <: IndexSpace} | ||
| return $randfun(codomain ← domain) | ||
| end | ||
| function $randfun( | ||
| ::Type{T}, codomain::TensorSpace{S}, | ||
| domain::TensorSpace{S} = one(codomain), | ||
| ) where {T, S <: IndexSpace} | ||
| return $randfun(T, codomain ← domain) | ||
| end | ||
| function $randfun( | ||
| rng::Random.AbstractRNG, ::Type{T}, | ||
| codomain::TensorSpace{S}, | ||
| domain::TensorSpace{S} = one(codomain), | ||
| ) where {T, S <: IndexSpace} | ||
| return $randfun(rng, T, codomain ← domain) | ||
| end | ||
|
|
||
| # filling in default eltype | ||
| $randfun(V::TensorMapSpace) = $randfun(Float64, V) | ||
| function $randfun(rng::Random.AbstractRNG, V::TensorMapSpace) | ||
| return $randfun(rng, Float64, V) | ||
| end | ||
|
|
||
| # filling in default rng | ||
| function $randfun(::Type{T}, V::TensorMapSpace) where {T} | ||
| return $randfun(Random.default_rng(), T, V) | ||
| end | ||
|
|
||
| # implementation | ||
| function $randfun( | ||
| rng::Random.AbstractRNG, ::Type{T}, | ||
| V::TensorMapSpace | ||
| ) where {T} | ||
| t = CuTensorMap{T}(undef, V) | ||
| $randfun!(rng, t) | ||
Jutho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return t | ||
| end | ||
|
|
||
| function $randfun!(rng::Random.AbstractRNG, t::CuTensorMap) | ||
| for (_, b) in blocks(t) | ||
| $randfun!(rng, b) | ||
| end | ||
| return t | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Scalar implementation | ||
| #----------------------- | ||
| function TensorKit.scalar(t::CuTensorMap{T, S, 0, 0}) where {T, S} | ||
| inds = findall(!iszero, t.data) | ||
| return isempty(inds) ? zero(scalartype(t)) : @allowscalar @inbounds t.data[only(inds)] | ||
| end | ||
|
|
||
| function Base.convert( | ||
kshyatt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| TT::Type{CuTensorMap{T, S, N₁, N₂}}, | ||
| t::AbstractTensorMap{<:Any, S, N₁, N₂} | ||
| ) where {T, S, N₁, N₂} | ||
| if typeof(t) === TT | ||
| return t | ||
| else | ||
| tnew = TT(undef, space(t)) | ||
| return copy!(tnew, t) | ||
| end | ||
| end | ||
|
|
||
| function LinearAlgebra.isposdef(t::CuTensorMap) | ||
| domain(t) == codomain(t) || | ||
| throw(SpaceMismatch("`isposdef` requires domain and codomain to be the same")) | ||
| InnerProductStyle(spacetype(t)) === EuclideanInnerProduct() || return false | ||
| for (c, b) in blocks(t) | ||
| # do our own hermitian check | ||
| isherm = MatrixAlgebraKit.ishermitian(b; atol = eps(real(eltype(b))), rtol = eps(real(eltype(b)))) | ||
| isherm || return false | ||
| isposdef(Hermitian(b)) || return false | ||
| end | ||
| return true | ||
| end | ||
|
|
||
| function Base.promote_rule( | ||
| ::Type{<:TT₁}, | ||
| ::Type{<:TT₂} | ||
| ) where { | ||
| S, N₁, N₂, TTT₁, TTT₂, | ||
| TT₁ <: CuTensorMap{TTT₁, S, N₁, N₂}, | ||
| TT₂ <: CuTensorMap{TTT₂, S, N₁, N₂}, | ||
| } | ||
| T = TensorKit.VectorInterface.promote_add(TTT₁, TTT₂) | ||
kshyatt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return CuTensorMap{T, S, N₁, N₂} | ||
| end | ||
|
|
||
| # CuTensorMap exponentation: | ||
| function TensorKit.exp!(t::CuTensorMap) | ||
| domain(t) == codomain(t) || | ||
| error("Exponential of a tensor only exist when domain == codomain.") | ||
| !MatrixAlgebraKit.ishermitian(t) && throw(ArgumentError("`exp!` is currently only supported on hermitian CUDA tensors")) | ||
| for (c, b) in blocks(t) | ||
| copy!(b, parent(Base.exp(Hermitian(b)))) | ||
kshyatt marked this conversation as resolved.
Show resolved
Hide resolved
kshyatt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
| return t | ||
| end | ||
|
|
||
| # functions that don't map ℝ to (a subset of) ℝ | ||
| for f in (:sqrt, :log, :asin, :acos, :acosh, :atanh, :acoth) | ||
| sf = string(f) | ||
| @eval function Base.$f(t::CuTensorMap) | ||
| domain(t) == codomain(t) || | ||
| throw(SpaceMismatch("`$($sf)` of a tensor only exists when domain == codomain")) | ||
| !MatrixAlgebraKit.ishermitian(t) && throw(ArgumentError("`$($sf)` is currently only supported on hermitian CUDA tensors")) | ||
| T = complex(float(scalartype(t))) | ||
| tf = similar(t, T) | ||
| for (c, b) in blocks(t) | ||
| copy!(block(tf, c), parent($f(Hermitian(b)))) | ||
kshyatt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
| return tf | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.