-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NDTensors] JLArrays Extension (#1508)
- Loading branch information
Showing
15 changed files
with
201 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
# TypeParameterAccessors definitions | ||
using NDTensors.TypeParameterAccessors: TypeParameterAccessors, Position | ||
using NDTensors.TypeParameterAccessors: | ||
TypeParameterAccessors, Position, default_type_parameters | ||
using NDTensors.GPUArraysCoreExtensions: storagemode | ||
using AMDGPU: AMDGPU, ROCArray | ||
|
||
function TypeParameterAccessors.default_type_parameters(::Type{<:ROCArray}) | ||
return (Float64, 1, AMDGPU.Mem.HIPBuffer) | ||
return (default_type_parameters(AbstractArray)..., AMDGPU.Mem.HIPBuffer) | ||
end | ||
TypeParameterAccessors.position(::Type{<:ROCArray}, ::typeof(eltype)) = Position(1) | ||
TypeParameterAccessors.position(::Type{<:ROCArray}, ::typeof(ndims)) = Position(2) | ||
|
||
TypeParameterAccessors.position(::Type{<:ROCArray}, ::typeof(storagemode)) = Position(3) |
This file contains 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 |
---|---|---|
@@ -1,18 +1,13 @@ | ||
# TypeParameterAccessors definitions | ||
using CUDA: CUDA, CuArray | ||
using NDTensors.TypeParameterAccessors: TypeParameterAccessors, Position | ||
using NDTensors.TypeParameterAccessors: | ||
TypeParameterAccessors, Position, default_type_parameters | ||
using NDTensors.GPUArraysCoreExtensions: storagemode | ||
|
||
function TypeParameterAccessors.position(::Type{<:CuArray}, ::typeof(eltype)) | ||
return Position(1) | ||
end | ||
function TypeParameterAccessors.position(::Type{<:CuArray}, ::typeof(ndims)) | ||
return Position(2) | ||
end | ||
function TypeParameterAccessors.position(::Type{<:CuArray}, ::typeof(storagemode)) | ||
return Position(3) | ||
end | ||
|
||
function TypeParameterAccessors.default_type_parameters(::Type{<:CuArray}) | ||
return (Float64, 1, CUDA.Mem.DeviceBuffer) | ||
return (default_type_parameters(AbstractArray)..., CUDA.Mem.DeviceBuffer) | ||
end |
This file contains 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,7 @@ | ||
module NDTensorsJLArraysExt | ||
include("copyto.jl") | ||
include("indexing.jl") | ||
include("linearalgebra.jl") | ||
include("mul.jl") | ||
include("permutedims.jl") | ||
end |
This file contains 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,30 @@ | ||
using JLArrays: JLArray | ||
using NDTensors.Expose: Exposed, expose, unexpose | ||
using LinearAlgebra: Adjoint | ||
|
||
# Same definition as `CuArray`. | ||
function Base.copy(src::Exposed{<:JLArray,<:Base.ReshapedArray}) | ||
return reshape(copy(parent(src)), size(unexpose(src))) | ||
end | ||
|
||
function Base.copy( | ||
src::Exposed{ | ||
<:JLArray,<:SubArray{<:Any,<:Any,<:Base.ReshapedArray{<:Any,<:Any,<:Adjoint}} | ||
}, | ||
) | ||
return copy(@view copy(expose(parent(src)))[parentindices(unexpose(src))...]) | ||
end | ||
|
||
# Catches a bug in `copyto!` in CUDA backend. | ||
function Base.copyto!(dest::Exposed{<:JLArray}, src::Exposed{<:JLArray,<:SubArray}) | ||
copyto!(dest, expose(copy(src))) | ||
return unexpose(dest) | ||
end | ||
|
||
# Catches a bug in `copyto!` in JLArray backend. | ||
function Base.copyto!( | ||
dest::Exposed{<:JLArray}, src::Exposed{<:JLArray,<:Base.ReshapedArray} | ||
) | ||
copyto!(dest, expose(parent(src))) | ||
return unexpose(dest) | ||
end |
This file contains 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,19 @@ | ||
using JLArrays: JLArray | ||
using GPUArraysCore: @allowscalar | ||
using NDTensors: NDTensors | ||
using NDTensors.Expose: Exposed, expose, unexpose | ||
|
||
function Base.getindex(E::Exposed{<:JLArray}) | ||
return @allowscalar unexpose(E)[] | ||
end | ||
|
||
function Base.setindex!(E::Exposed{<:JLArray}, x::Number) | ||
@allowscalar unexpose(E)[] = x | ||
return unexpose(E) | ||
end | ||
|
||
function Base.getindex(E::Exposed{<:JLArray,<:Adjoint}, i, j) | ||
return (expose(parent(E))[j, i])' | ||
end | ||
|
||
Base.any(f, E::Exposed{<:JLArray,<:NDTensors.Tensor}) = any(f, data(unexpose(E))) |
This file contains 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,40 @@ | ||
using Adapt: adapt | ||
using JLArrays: JLArray, JLMatrix | ||
using LinearAlgebra: LinearAlgebra, Hermitian, Symmetric, qr, eigen | ||
using NDTensors: NDTensors | ||
using NDTensors.Expose: Expose, expose, qr, qr_positive, ql, ql_positive | ||
using NDTensors.GPUArraysCoreExtensions: cpu | ||
using NDTensors.TypeParameterAccessors: unwrap_array_type | ||
|
||
## TODO this function exists because of the same issue below. when | ||
## that issue is resolved we can rely on the abstractarray version of | ||
## this operation. | ||
function Expose.qr(A::Exposed{<:JLArray}) | ||
Q, L = qr(unexpose(A)) | ||
return adapt(unwrap_array_type(A), Matrix(Q)), adapt(unwrap_array_type(A), L) | ||
end | ||
## TODO this should work using a JLArray but there is an error converting the Q from its packed QR from | ||
## back into a JLArray see https://github.com/JuliaGPU/GPUArrays.jl/issues/545. To fix call cpu for now | ||
function Expose.qr_positive(A::Exposed{<:JLArray}) | ||
Q, L = qr_positive(expose(cpu(A))) | ||
return adapt(unwrap_array_type(A), copy(Q)), adapt(unwrap_array_type(A), L) | ||
end | ||
|
||
function Expose.ql(A::Exposed{<:JLMatrix}) | ||
Q, L = ql(expose(cpu(A))) | ||
return adapt(unwrap_array_type(A), copy(Q)), adapt(unwrap_array_type(A), L) | ||
end | ||
function Expose.ql_positive(A::Exposed{<:JLMatrix}) | ||
Q, L = ql_positive(expose(cpu(A))) | ||
return adapt(unwrap_array_type(A), copy(Q)), adapt(unwrap_array_type(A), L) | ||
end | ||
|
||
function LinearAlgebra.eigen(A::Exposed{<:JLMatrix,<:Symmetric}) | ||
q, l = (eigen(expose(cpu(A)))) | ||
return adapt.(unwrap_array_type(A), (q, l)) | ||
end | ||
|
||
function LinearAlgebra.eigen(A::Exposed{<:JLMatrix,<:Hermitian}) | ||
q, l = (eigen(expose(Hermitian(cpu(unexpose(A).data))))) | ||
return adapt.(JLArray, (q, l)) | ||
end |
This file contains 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,43 @@ | ||
using JLArrays: JLArray | ||
using LinearAlgebra: LinearAlgebra, mul!, transpose | ||
using NDTensors.Expose: Exposed, expose, unexpose | ||
|
||
function LinearAlgebra.mul!( | ||
CM::Exposed{<:JLArray,<:LinearAlgebra.Transpose}, | ||
AM::Exposed{<:JLArray}, | ||
BM::Exposed{<:JLArray}, | ||
α, | ||
β, | ||
) | ||
mul!(transpose(CM), transpose(BM), transpose(AM), α, β) | ||
return unexpose(CM) | ||
end | ||
|
||
function LinearAlgebra.mul!( | ||
CM::Exposed{<:JLArray,<:LinearAlgebra.Adjoint}, | ||
AM::Exposed{<:JLArray}, | ||
BM::Exposed{<:JLArray}, | ||
α, | ||
β, | ||
) | ||
mul!(CM', BM', AM', α, β) | ||
return unexpose(CM) | ||
end | ||
|
||
## Fix issue in JLArrays.jl where it cannot distinguish Transpose{Reshape{Adjoint{JLArray}}} | ||
## as a JLArray and calls generic matmul | ||
function LinearAlgebra.mul!( | ||
CM::Exposed{<:JLArray}, | ||
AM::Exposed{<:JLArray}, | ||
BM::Exposed{ | ||
<:JLArray, | ||
<:LinearAlgebra.Transpose{ | ||
<:Any,<:Base.ReshapedArray{<:Any,<:Any,<:LinearAlgebra.Adjoint} | ||
}, | ||
}, | ||
α, | ||
β, | ||
) | ||
mul!(CM, AM, expose(transpose(copy(expose(parent(BM))))), α, β) | ||
return unexpose(CM) | ||
end |
This file contains 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,24 @@ | ||
using JLArrays: JLArray | ||
using LinearAlgebra: Adjoint | ||
using NDTensors.Expose: Exposed, expose, unexpose | ||
|
||
function Base.permutedims!( | ||
Edest::Exposed{<:JLArray,<:Base.ReshapedArray}, Esrc::Exposed{<:JLArray}, perm | ||
) | ||
Aperm = permutedims(Esrc, perm) | ||
copyto!(expose(parent(Edest)), expose(Aperm)) | ||
return unexpose(Edest) | ||
end | ||
|
||
## Found an issue in CUDA where if Edest is a reshaped{<:Adjoint} | ||
## .= can fail. So instead force Esrc into the shape of parent(Edest) | ||
function Base.permutedims!( | ||
Edest::Exposed{<:JLArray,<:Base.ReshapedArray{<:Any,<:Any,<:Adjoint}}, | ||
Esrc::Exposed{<:JLArray}, | ||
perm, | ||
f, | ||
) | ||
Aperm = reshape(permutedims(Esrc, perm), size(parent(Edest))) | ||
parent(Edest) .= f.(parent(Edest), Aperm) | ||
return unexpose(Edest) | ||
end |
This file contains 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 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 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 |
---|---|---|
@@ -1,4 +1,2 @@ | ||
position(::Type{<:Array}, ::typeof(eltype)) = Position(1) | ||
position(::Type{<:Array}, ::typeof(ndims)) = Position(2) | ||
|
||
default_type_parameters(::Type{<:Array}) = (Float64, 1) |
This file contains 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 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.