Skip to content

Commit f50f00e

Browse files
committed
Vendor TypeParameterAccessors
1 parent 2a1aa19 commit f50f00e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1796
-1124
lines changed

NDTensors/Project.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "NDTensors"
22
uuid = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf"
33
authors = ["Matthew Fishman <[email protected]>"]
4-
version = "0.4.12"
4+
version = "0.4.13"
55

66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
@@ -28,7 +28,6 @@ Strided = "5e0ebb24-38b0-5f93-81fe-25c709ecae67"
2828
StridedViews = "4db3bf67-4bd7-4b4e-b153-31dc3fb37143"
2929
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
3030
TupleTools = "9d95972d-f1c8-5527-a6e0-b4b365fa01f6"
31-
TypeParameterAccessors = "7e5a90cf-f82e-492e-a09b-e3e26432c138"
3231
VectorInterface = "409d34a3-91d5-4945-b6ec-7529ddf182d8"
3332

3433
[weakdeps]
@@ -88,7 +87,6 @@ StridedViews = "0.2.2, 0.3, 0.4"
8887
TBLIS = "0.2"
8988
TimerOutputs = "0.5.5"
9089
TupleTools = "1.2.0"
91-
TypeParameterAccessors = "0.3"
9290
VectorInterface = "0.4.2, 0.5"
9391
cuTENSOR = "2"
9492
julia = "1.10"

NDTensors/src/NDTensors.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
module NDTensors
2+
3+
module Vendored
4+
include(joinpath("vendored", "TypeParameterAccessors", "src", "TypeParameterAccessors.jl"))
5+
end
6+
27
#####################################
38
# Imports and exports
49
#
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using TypeParameterAccessors:
2-
unwrap_array_type,
3-
specify_default_type_parameters,
4-
specify_type_parameters,
5-
type_parameters
1+
using .Vendored.TypeParameterAccessors:
2+
unwrap_array_type,
3+
specify_default_type_parameters,
4+
specify_type_parameters,
5+
type_parameters
66

77
# Convert to Array, avoiding copying if possible
88
array(a::AbstractArray) = a
@@ -12,33 +12,33 @@ vector(a::AbstractVector) = a
1212
## Warning to use these functions it is necessary to define `TypeParameterAccessors.position(::Type{<:YourArrayType}, ::typeof(ndims)))`
1313
# Implementation, catches if `ndims(arraytype) != length(dims)`.
1414
## TODO convert ndims to `type_parameters(::, typeof(ndims))`
15-
function generic_randn(arraytype::Type{<:AbstractArray}, dims...; rng=Random.default_rng())
16-
arraytype_specified = specify_type_parameters(
17-
unwrap_array_type(arraytype), ndims, length(dims)
18-
)
19-
arraytype_specified = specify_default_type_parameters(arraytype_specified)
20-
@assert length(dims) == ndims(arraytype_specified)
21-
data = similar(arraytype_specified, dims...)
22-
return randn!(rng, data)
15+
function generic_randn(arraytype::Type{<:AbstractArray}, dims...; rng = Random.default_rng())
16+
arraytype_specified = specify_type_parameters(
17+
unwrap_array_type(arraytype), ndims, length(dims)
18+
)
19+
arraytype_specified = specify_default_type_parameters(arraytype_specified)
20+
@assert length(dims) == ndims(arraytype_specified)
21+
data = similar(arraytype_specified, dims...)
22+
return randn!(rng, data)
2323
end
2424

2525
function generic_randn(
26-
arraytype::Type{<:AbstractArray}, dims::Tuple; rng=Random.default_rng()
27-
)
28-
return generic_randn(arraytype, dims...; rng)
26+
arraytype::Type{<:AbstractArray}, dims::Tuple; rng = Random.default_rng()
27+
)
28+
return generic_randn(arraytype, dims...; rng)
2929
end
3030

3131
# Implementation, catches if `ndims(arraytype) != length(dims)`.
3232
function generic_zeros(arraytype::Type{<:AbstractArray}, dims...)
33-
arraytype_specified = specify_type_parameters(
34-
unwrap_array_type(arraytype), ndims, length(dims)
35-
)
36-
arraytype_specified = specify_default_type_parameters(arraytype_specified)
37-
@assert length(dims) == ndims(arraytype_specified)
38-
ElT = eltype(arraytype_specified)
39-
return fill!(similar(arraytype_specified, dims...), zero(ElT))
33+
arraytype_specified = specify_type_parameters(
34+
unwrap_array_type(arraytype), ndims, length(dims)
35+
)
36+
arraytype_specified = specify_default_type_parameters(arraytype_specified)
37+
@assert length(dims) == ndims(arraytype_specified)
38+
ElT = eltype(arraytype_specified)
39+
return fill!(similar(arraytype_specified, dims...), zero(ElT))
4040
end
4141

4242
function generic_zeros(arraytype::Type{<:AbstractArray}, dims::Tuple)
43-
return generic_zeros(arraytype, dims...)
43+
return generic_zeros(arraytype, dims...)
4444
end
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using TypeParameterAccessors: unwrap_array_type
1+
using .Vendored.TypeParameterAccessors: unwrap_array_type
22
# TODO: Make `isgpu`, `ismtl`, etc.
33
# For `isgpu`, will require a `NDTensorsGPUArrayCoreExt`.
44
iscu(A::AbstractArray) = iscu(typeof(A))
55
function iscu(A::Type{<:AbstractArray})
6-
return (unwrap_array_type(A) == A ? false : iscu(unwrap_array_type(A)))
6+
return (unwrap_array_type(A) == A ? false : iscu(unwrap_array_type(A)))
77
end

NDTensors/src/abstractarray/set_types.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using TypeParameterAccessors: TypeParameterAccessors
1+
using .Vendored.TypeParameterAccessors: TypeParameterAccessors
22

33
"""
44
# Do we still want to define things like this?
@@ -13,5 +13,5 @@ TODO: Use `Accessors.jl` notation:
1313
# `FillArray` instead. This is a stand-in
1414
# to make things work with the current design.
1515
function TypeParameterAccessors.set_ndims(numbertype::Type{<:Number}, ndims)
16-
return numbertype
16+
return numbertype
1717
end

NDTensors/src/abstractarray/similar.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
using Base: DimOrInd, Dims, OneTo
2-
using TypeParameterAccessors: IsWrappedArray, unwrap_array_type, set_eltype, similartype
2+
using .Vendored.TypeParameterAccessors: IsWrappedArray, unwrap_array_type, set_eltype, similartype
33

44
## Custom `NDTensors.similar` implementation.
55
## More extensive than `Base.similar`.
66

77
# This function actually allocates the data.
88
# NDTensors.similar
99
function similar(arraytype::Type{<:AbstractArray}, dims::Tuple)
10-
shape = NDTensors.to_shape(arraytype, dims)
11-
return similartype(arraytype, shape)(undef, NDTensors.to_shape(arraytype, shape))
10+
shape = NDTensors.to_shape(arraytype, dims)
11+
return similartype(arraytype, shape)(undef, NDTensors.to_shape(arraytype, shape))
1212
end
1313

1414
# This function actually allocates the data.
1515
# Catches conversions of dimensions specified by ranges
1616
# dimensions specified by integers with `Base.to_shape`.
1717
# NDTensors.similar
1818
function similar(arraytype::Type{<:AbstractArray}, dims::Dims)
19-
return similartype(arraytype, dims)(undef, dims)
19+
return similartype(arraytype, dims)(undef, dims)
2020
end
2121

2222
# NDTensors.similar
2323
function similar(arraytype::Type{<:AbstractArray}, dims::DimOrInd...)
24-
return similar(arraytype, NDTensors.to_shape(dims))
24+
return similar(arraytype, NDTensors.to_shape(dims))
2525
end
2626

2727
# Handles range inputs, `Base.to_shape` converts them to integer dimensions.
2828
# See Julia's `base/abstractarray.jl`.
2929
# NDTensors.similar
3030
function similar(
31-
arraytype::Type{<:AbstractArray},
32-
shape::Tuple{Union{Integer,OneTo},Vararg{Union{Integer,OneTo}}},
33-
)
34-
return NDTensors.similar(arraytype, NDTensors.to_shape(shape))
31+
arraytype::Type{<:AbstractArray},
32+
shape::Tuple{Union{Integer, OneTo}, Vararg{Union{Integer, OneTo}}},
33+
)
34+
return NDTensors.similar(arraytype, NDTensors.to_shape(shape))
3535
end
3636

3737
# NDTensors.similar
3838
function similar(arraytype::Type{<:AbstractArray}, eltype::Type, dims::Tuple)
39-
return NDTensors.similar(similartype(arraytype, eltype, dims), dims)
39+
return NDTensors.similar(similartype(arraytype, eltype, dims), dims)
4040
end
4141

4242
# TODO: Add an input `structure` which can store things like the nonzero
@@ -70,19 +70,19 @@ end
7070
# TODO: Maybe makes an empty array, i.e. `similartype(arraytype, eltype)()`?
7171
# NDTensors.similar
7272
function similar(arraytype::Type{<:AbstractArray}, eltype::Type)
73-
return error("Must specify dimensions.")
73+
return error("Must specify dimensions.")
7474
end
7575

7676
## NDTensors.similar for instances
7777

7878
# NDTensors.similar
7979
function similar(array::AbstractArray, eltype::Type, dims::Tuple)
80-
return NDTensors.similar(similartype(typeof(array), eltype), dims)
80+
return NDTensors.similar(similartype(typeof(array), eltype), dims)
8181
end
8282

8383
# NDTensors.similar
8484
function similar(array::AbstractArray, eltype::Type, dims::Int)
85-
return NDTensors.similar(similartype(typeof(array), eltype), dims)
85+
return NDTensors.similar(similartype(typeof(array), eltype), dims)
8686
end
8787

8888
# NDTensors.similar
@@ -91,7 +91,7 @@ similar(array::AbstractArray, dims::Tuple) = NDTensors.similar(typeof(array), di
9191
# Use the `size` to determine the dimensions
9292
# NDTensors.similar
9393
function similar(array::AbstractArray, eltype::Type)
94-
return NDTensors.similar(typeof(array), eltype, size(array))
94+
return NDTensors.similar(typeof(array), eltype, size(array))
9595
end
9696

9797
# Use the `size` to determine the dimensions

NDTensors/src/adapt.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ adapt_structure(to, x::TensorStorage) = setdata(x, adapt(to, data(x)))
33
adapt_structure(to, x::Tensor) = setstorage(x, adapt(to, storage(x)))
44

55
function GPUArraysCoreExtensions.cpu(eltype::Type{<:Number}, x)
6-
return fmap(x -> adapt(Array{eltype}, x), x)
6+
return fmap(x -> adapt(Array{eltype}, x), x)
77
end
88
GPUArraysCoreExtensions.cpu(x) = fmap(x -> adapt(Array, x), x)
99

@@ -27,11 +27,11 @@ double_precision(x) = fmap(x -> adapt(double_precision(eltype(x)), x), x)
2727
# Used to adapt `EmptyStorage` types
2828
#
2929

30-
using TypeParameterAccessors: specify_type_parameters
30+
using .Vendored.TypeParameterAccessors: specify_type_parameters
3131
function adapt_storagetype(to::Type{<:AbstractVector}, x::Type{<:TensorStorage})
32-
return set_datatype(x, specify_type_parameters(to, eltype, eltype(x)))
32+
return set_datatype(x, specify_type_parameters(to, eltype, eltype(x)))
3333
end
3434

3535
function adapt_storagetype(to::Type{<:AbstractArray}, x::Type{<:TensorStorage})
36-
return set_datatype(x, specify_type_parameters(to, (ndims, eltype), (1, eltype(x))))
36+
return set_datatype(x, specify_type_parameters(to, (ndims, eltype), (1, eltype(x))))
3737
end

0 commit comments

Comments
 (0)