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

Preserve ndims of wrappers that hard-code them. #25

Merged
merged 1 commit into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/wrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Only use this type for dispatch purposes. To convert instances of an array wrapp
const WrappedArray{T,N,Src,Dst} = @eval Union{$([W for (W,ctor) in Adapt._wrappers]...)} where {T,N,Src,Dst}

# XXX: this Union is a hack:
# - only works with one level of wrappi ng
# - only works with one level of wrapping
# - duplication of Src and Dst typevars (without it, we get `WrappedGPUArray{T,N,AT{T,N}}`
# not matching `SubArray{T,1,AT{T,2}}`, and leaving out `{T,N}` makes it impossible to
# match e.g. `Diagonal{T,AT}` and get `N` out of that). alternatively, computed types
Expand All @@ -59,7 +59,16 @@ const WrappedArray{T,N,Src,Dst} = @eval Union{$([W for (W,ctor) in Adapt._wrappe
# https://github.com/JuliaLang/julia/pull/31563

# accessors for extracting information about the wrapper type
Base.ndims(::Type{<:WrappedArray{T,N,Src,Dst}}) where {T,N,Src,Dst} = @isdefined(N) ? N : ndims(Dst)
Base.eltype(::Type{<:WrappedArray{T,N,Src,Dst}}) where {T,N,Src,Dst} = @isdefined(T) ? T : ndims(Dst)
Base.ndims(W::Type{<:WrappedArray{T,N,Src,Dst}}) where {T,N,Src,Dst} = @isdefined(N) ? N : specialized_ndims(W)
Base.eltype(::Type{<:WrappedArray{T,N,Src,Dst}}) where {T,N,Src,Dst} = T # every wrapper has a T typevar
Base.parent(W::Type{<:WrappedArray{T,N,Src,Dst}}) where {T,N,Src,Dst} = @isdefined(Dst) ? Dst.name.wrapper : Src.name.wrapper

# some wrappers don't have a N typevar because it is constant, but we can't extract that from <:WrappedArray
specialized_ndims(::Type{<:LinearAlgebra.Adjoint}) = 2
specialized_ndims(::Type{<:LinearAlgebra.Transpose}) = 2
specialized_ndims(::Type{<:LinearAlgebra.LowerTriangular}) = 2
specialized_ndims(::Type{<:LinearAlgebra.UnitLowerTriangular}) = 2
specialized_ndims(::Type{<:LinearAlgebra.UpperTriangular}) = 2
specialized_ndims(::Type{<:LinearAlgebra.UnitUpperTriangular}) = 2
specialized_ndims(::Type{<:LinearAlgebra.Diagonal}) = 2
specialized_ndims(::Type{<:LinearAlgebra.Tridiagonal}) = 2
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,5 @@ const dl = CustomArray{Float64,1}(rand(2))
const du = CustomArray{Float64,1}(rand(2))
const d = CustomArray{Float64,1}(rand(3))
@test_adapt CustomArray Tridiagonal(dl.arr, d.arr, du.arr) Tridiagonal(dl, d, du)

@test ndims(LinearAlgebra.Transpose{Float64,Array{Float64,1}}) == 2