From d8b9487a7a32f06db084dbaae716b92ff32d8cfc Mon Sep 17 00:00:00 2001 From: mtfishman Date: Fri, 1 Nov 2024 15:45:33 -0400 Subject: [PATCH] Fix namespace issues --- NDTensors/src/abstractarray/set_types.jl | 3 ++- NDTensors/src/abstractarray/similar.jl | 2 +- NDTensors/src/blocksparse/blockdims.jl | 6 +++++- NDTensors/src/blocksparse/blocksparsetensor.jl | 2 ++ NDTensors/src/blocksparse/diagblocksparse.jl | 2 ++ NDTensors/src/blocksparse/similar.jl | 2 ++ NDTensors/src/diag/similar.jl | 4 +++- NDTensors/src/dims.jl | 3 ++- NDTensors/src/empty/empty.jl | 9 ++++++--- NDTensors/src/tensor/similar.jl | 6 +++--- NDTensors/src/tensorstorage/similar.jl | 8 ++++++-- 11 files changed, 34 insertions(+), 13 deletions(-) diff --git a/NDTensors/src/abstractarray/set_types.jl b/NDTensors/src/abstractarray/set_types.jl index 77c3c2562c..823c2fa11e 100644 --- a/NDTensors/src/abstractarray/set_types.jl +++ b/NDTensors/src/abstractarray/set_types.jl @@ -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: diff --git a/NDTensors/src/abstractarray/similar.jl b/NDTensors/src/abstractarray/similar.jl index c06a1e1c74..440b1b86b8 100644 --- a/NDTensors/src/abstractarray/similar.jl +++ b/NDTensors/src/abstractarray/similar.jl @@ -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`. diff --git a/NDTensors/src/blocksparse/blockdims.jl b/NDTensors/src/blocksparse/blockdims.jl index bb68c6144a..8de0439417 100644 --- a/NDTensors/src/blocksparse/blockdims.jl +++ b/NDTensors/src/blocksparse/blockdims.jl @@ -1,3 +1,5 @@ +using .TypeParameterAccessors: TypeParameterAccessors + """ BlockDim @@ -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 diff --git a/NDTensors/src/blocksparse/blocksparsetensor.jl b/NDTensors/src/blocksparse/blocksparsetensor.jl index 896fafdb91..ce515e4c00 100644 --- a/NDTensors/src/blocksparse/blocksparsetensor.jl +++ b/NDTensors/src/blocksparse/blocksparsetensor.jl @@ -1,3 +1,5 @@ +using .TypeParameterAccessors: similartype + # # BlockSparseTensor (Tensor using BlockSparse storage) # diff --git a/NDTensors/src/blocksparse/diagblocksparse.jl b/NDTensors/src/blocksparse/diagblocksparse.jl index a8b46b17cb..42882102d7 100644 --- a/NDTensors/src/blocksparse/diagblocksparse.jl +++ b/NDTensors/src/blocksparse/diagblocksparse.jl @@ -1,3 +1,5 @@ +using .TypeParameterAccessors: similartype + export DiagBlockSparse, DiagBlockSparseTensor # DiagBlockSparse can have either Vector storage, in which case diff --git a/NDTensors/src/blocksparse/similar.jl b/NDTensors/src/blocksparse/similar.jl index d9e87bd67c..5a8395b1a7 100644 --- a/NDTensors/src/blocksparse/similar.jl +++ b/NDTensors/src/blocksparse/similar.jl @@ -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)) diff --git a/NDTensors/src/diag/similar.jl b/NDTensors/src/diag/similar.jl index 368d396480..bf7113aab7 100644 --- a/NDTensors/src/diag/similar.jl +++ b/NDTensors/src/diag/similar.jl @@ -1,3 +1,5 @@ +using NDTensors.TypeParameterAccessors: TypeParameterAccessors + # NDTensors.similar function similar(storagetype::Type{<:Diag}, dims::Dims) return setdata(storagetype, similar(datatype(storagetype), mindim(dims))) @@ -5,7 +7,7 @@ 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 diff --git a/NDTensors/src/dims.jl b/NDTensors/src/dims.jl index 663309c8b6..c49895f18b 100644 --- a/NDTensors/src/dims.jl +++ b/NDTensors/src/dims.jl @@ -1,4 +1,5 @@ using .DiagonalArrays: DiagonalArrays +using .TypeParameterAccessors: TypeParameterAccessors export dense, dims, dim, mindim, diaglength @@ -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 diff --git a/NDTensors/src/empty/empty.jl b/NDTensors/src/empty/empty.jl index 645019dacd..3aeda04710 100644 --- a/NDTensors/src/empty/empty.jl +++ b/NDTensors/src/empty/empty.jl @@ -1,3 +1,4 @@ +using .TypeParameterAccessors: TypeParameterAccessors, set_eltype, similartype # # Represents a tensor order that could be set to any order. @@ -5,18 +6,20 @@ 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)} diff --git a/NDTensors/src/tensor/similar.jl b/NDTensors/src/tensor/similar.jl index 8ca6f493d7..266500cf17 100644 --- a/NDTensors/src/tensor/similar.jl +++ b/NDTensors/src/tensor/similar.jl @@ -1,4 +1,4 @@ -using .TypeParameterAccessors: set_indstype +using .TypeParameterAccessors: TypeParameterAccessors, set_indstype, similartype # NDTensors.similar similar(tensor::Tensor) = setstorage(tensor, similar(storage(tensor))) @@ -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. diff --git a/NDTensors/src/tensorstorage/similar.jl b/NDTensors/src/tensorstorage/similar.jl index d309b32727..374a3235f3 100644 --- a/NDTensors/src/tensorstorage/similar.jl +++ b/NDTensors/src/tensorstorage/similar.jl @@ -1,3 +1,5 @@ +using .TypeParameterAccessors: TypeParameterAccessors, set_ndims, similartype + # NDTensors.similar similar(storage::TensorStorage) = setdata(storage, NDTensors.similar(data(storage))) @@ -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.