Skip to content

Commit

Permalink
Fix namespace issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Nov 1, 2024
1 parent 11a5c98 commit d8b9487
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 13 deletions.
3 changes: 2 additions & 1 deletion NDTensors/src/abstractarray/set_types.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using .TypeParameterAccessors: TypeParameterAccessors, set_ndims
using .TypeParameterAccessors: TypeParameterAccessors

"""
# Do we still want to define things like this?
TODO: Use `Accessors.jl` notation:
Expand Down
2 changes: 1 addition & 1 deletion NDTensors/src/abstractarray/similar.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Base: DimOrInd, Dims, OneTo
using .TypeParameterAccessors: IsWrappedArray, unwrap_array_type, set_eltype
using .TypeParameterAccessors: IsWrappedArray, unwrap_array_type, set_eltype, similartype

## Custom `NDTensors.similar` implementation.
## More extensive than `Base.similar`.
Expand Down
6 changes: 5 additions & 1 deletion NDTensors/src/blocksparse/blockdims.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using .TypeParameterAccessors: TypeParameterAccessors

"""
BlockDim
Expand All @@ -18,7 +20,9 @@ const BlockDims{N} = NTuple{N,BlockDim}

Base.ndims(ds::Type{<:BlockDims{N}}) where {N} = N

similartype(::Type{<:BlockDims}, ::Type{Val{N}}) where {N} = BlockDims{N}
function TypeParameterAccessors.similartype(::Type{<:BlockDims}, ::Type{Val{N}}) where {N}
return BlockDims{N}
end

Base.copy(ds::BlockDims) = ds

Expand Down
2 changes: 2 additions & 0 deletions NDTensors/src/blocksparse/blocksparsetensor.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using .TypeParameterAccessors: similartype

#
# BlockSparseTensor (Tensor using BlockSparse storage)
#
Expand Down
2 changes: 2 additions & 0 deletions NDTensors/src/blocksparse/diagblocksparse.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using .TypeParameterAccessors: similartype

export DiagBlockSparse, DiagBlockSparseTensor

# DiagBlockSparse can have either Vector storage, in which case
Expand Down
2 changes: 2 additions & 0 deletions NDTensors/src/blocksparse/similar.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using .TypeParameterAccessors: similartype

# NDTensors.similar
function similar(storagetype::Type{<:BlockSparse}, blockoffsets::BlockOffsets, dims::Tuple)
data = similar(datatype(storagetype), nnz(blockoffsets, dims))
Expand Down
4 changes: 3 additions & 1 deletion NDTensors/src/diag/similar.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using NDTensors.TypeParameterAccessors: TypeParameterAccessors

# NDTensors.similar
function similar(storagetype::Type{<:Diag}, dims::Dims)
return setdata(storagetype, similar(datatype(storagetype), mindim(dims)))
end

# TODO: Redesign UniformDiag to make it handled better
# by generic code.
function similartype(storagetype::Type{<:UniformDiag}, eltype::Type)
function TypeParameterAccessors.similartype(storagetype::Type{<:UniformDiag}, eltype::Type)
# This will also set the `datatype`.
return set_eltype(storagetype, eltype)
end
Expand Down
3 changes: 2 additions & 1 deletion NDTensors/src/dims.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using .DiagonalArrays: DiagonalArrays
using .TypeParameterAccessors: TypeParameterAccessors

export dense, dims, dim, mindim, diaglength

Expand Down Expand Up @@ -52,7 +53,7 @@ dim_to_stride(ds, k::Int) = dim_to_strides(ds)[k]
# code (it helps to construct a Tuple(::NTuple{N,Int}) where the
# only known thing for dispatch is a concrete type such
# as Dims{4})
similartype(::Type{<:Dims}, ::Type{Val{N}}) where {N} = Dims{N}
TypeParameterAccessors.similartype(::Type{<:Dims}, ::Type{Val{N}}) where {N} = Dims{N}

# This is to help with ITensor compatibility
dim(i::Int) = i
Expand Down
9 changes: 6 additions & 3 deletions NDTensors/src/empty/empty.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
using .TypeParameterAccessors: TypeParameterAccessors, set_eltype, similartype

#
# Represents a tensor order that could be set to any order.
#

struct EmptyOrder end

function similartype(StoreT::Type{<:TensorStorage{EmptyNumber}}, ElT::Type)
function TypeParameterAccessors.similartype(
StoreT::Type{<:TensorStorage{EmptyNumber}}, ElT::Type
)
return set_eltype(StoreT, ElT)
end

function similartype(
function TypeParameterAccessors.similartype(
StoreT::Type{<:TensorStorage{EmptyNumber}}, DataT::Type{<:AbstractArray}
)
return set_datatype(StoreT, DataT)
end

## TODO fix this similartype to use set eltype for BlockSparse
function similartype(
function TypeParameterAccessors.similartype(
::Type{StoreT}, ::Type{ElT}
) where {StoreT<:BlockSparse{EmptyNumber},ElT}
return BlockSparse{ElT,similartype(datatype(StoreT), ElT),ndims(StoreT)}
Expand Down
6 changes: 3 additions & 3 deletions NDTensors/src/tensor/similar.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using .TypeParameterAccessors: set_indstype
using .TypeParameterAccessors: TypeParameterAccessors, set_indstype, similartype

# NDTensors.similar
similar(tensor::Tensor) = setstorage(tensor, similar(storage(tensor)))
Expand Down Expand Up @@ -58,11 +58,11 @@ function Base.similar(tensor::Tensor, eltype::Type, dims::Dims)
return NDTensors.similar(tensor, eltype, dims)
end

function similartype(tensortype::Type{<:Tensor}, eltype::Type)
function TypeParameterAccessors.similartype(tensortype::Type{<:Tensor}, eltype::Type)
return set_storagetype(tensortype, similartype(storagetype(tensortype), eltype))
end

function similartype(tensortype::Type{<:Tensor}, dims::Tuple)
function TypeParameterAccessors.similartype(tensortype::Type{<:Tensor}, dims::Tuple)
tensortype_new_inds = set_indstype(tensortype, dims)
# Need to pass `dims` in case that information is needed to make a storage type,
# for example `BlockSparse` needs the number of dimensions.
Expand Down
8 changes: 6 additions & 2 deletions NDTensors/src/tensorstorage/similar.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using .TypeParameterAccessors: TypeParameterAccessors, set_ndims, similartype

# NDTensors.similar
similar(storage::TensorStorage) = setdata(storage, NDTensors.similar(data(storage)))

Expand Down Expand Up @@ -61,14 +63,16 @@ Base.similar(storage::TensorStorage, eltype::Type) = NDTensors.similar(storage,
## Base.similar(storage::TensorStorage, dims::Dims...) = NDTensors.similar(storage, dims...)
## Base.similar(storage::TensorStorage, dims::DimOrInd...) = NDTensors.similar(storage, dims...)

function similartype(storagetype::Type{<:TensorStorage}, eltype::Type)
function TypeParameterAccessors.similartype(
storagetype::Type{<:TensorStorage}, eltype::Type
)
# TODO: Don't convert to an `AbstractVector` with `set_ndims(datatype, 1)`, once we support
# more general data types.
# return set_datatype(storagetype, NDTensors.similartype(datatype(storagetype), eltype))
return set_datatype(storagetype, set_ndims(similartype(datatype(storagetype), eltype), 1))
end

function similartype(storagetype::Type{<:TensorStorage}, dims::Tuple)
function TypeParameterAccessors.similartype(storagetype::Type{<:TensorStorage}, dims::Tuple)
# TODO: In the future, set the dimensions of the data type based on `dims`, once
# more general data types beyond `AbstractVector` are supported.
# `similartype` unwraps any wrapped data.
Expand Down

0 comments on commit d8b9487

Please sign in to comment.