Skip to content

Commit 8dd8e24

Browse files
authored
Merge branch 'main' into show
2 parents c5107fa + e4c91aa commit 8dd8e24

File tree

12 files changed

+298
-127
lines changed

12 files changed

+298
-127
lines changed

.github/workflows/IntegrationTest.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: "IntegrationTest"
2+
3+
on:
4+
push:
5+
branches:
6+
- 'main'
7+
tags: '*'
8+
paths:
9+
- 'Project.toml'
10+
pull_request:
11+
paths:
12+
- 'Project.toml'
13+
14+
jobs:
15+
integration-test:
16+
name: "IntegrationTest"
17+
strategy:
18+
matrix:
19+
repo:
20+
- 'ITensor/ITensorBase.jl'
21+
- 'ITensor/NamedDimsArrays.jl'
22+
- 'ITensor/QuantumOperatorDefinitions.jl'
23+
- 'ITensor/TensorAlgebra.jl'
24+
uses: "ITensor/ITensorActions/.github/workflows/IntegrationTest.yml@main"
25+
with:
26+
localregistry: "https://github.com/ITensor/ITensorRegistry.git"
27+
repo: "${{ matrix.repo }}"

.github/workflows/Registrator.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@ on:
99
branches:
1010
- 'master'
1111
- 'main'
12+
13+
permissions:
14+
contents: write
15+
pull-requests: write
16+
1217
jobs:
1318
Register:
1419
if: github.event_name == 'workflow_dispatch' || github.event.pull_request.merged == true
1520
uses: "ITensor/ITensorActions/.github/workflows/Registrator.yml@main"
1621
with:
1722
localregistry: ITensor/ITensorRegistry
23+
secrets:
24+
REGISTRATOR_KEY: ${{ secrets.REGISTRATOR_KEY }}

Project.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
name = "BlockSparseArrays"
22
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.2.18"
4+
version = "0.2.26"
5+
56

67
[deps]
78
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
@@ -45,7 +46,7 @@ MacroTools = "0.5.13"
4546
MapBroadcast = "0.1.5"
4647
SparseArraysBase = "0.2.10"
4748
SplitApplyCombine = "1.2.3"
48-
TensorAlgebra = "0.1.0"
49+
TensorAlgebra = "0.1.0, 0.2"
4950
Test = "1.10"
5051
TypeParameterAccessors = "0.2.0, 0.3"
5152
julia = "1.10"

ext/BlockSparseArraysGradedUnitRangesExt/BlockSparseArraysGradedUnitRangesExt.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module BlockSparseArraysGradedUnitRangesExt
22

33
using BlockSparseArrays: AnyAbstractBlockSparseArray, BlockSparseArray, blocktype
44
using GradedUnitRanges: AbstractGradedUnitRange
5-
using TypeParameterAccessors: set_eltype, set_ndims, unwrap_array_type
5+
using TypeParameterAccessors: similartype, unwrap_array_type
66

77
# A block spare array similar to the input (dense) array.
88
# TODO: Make `BlockSparseArrays.blocksparse_similar` more general and use that,
@@ -15,9 +15,7 @@ function similar_blocksparse(
1515
# TODO: Probably need to unwrap the type of `a` in certain cases
1616
# to make a proper block type.
1717
return BlockSparseArray{
18-
elt,
19-
length(axes),
20-
set_eltype(set_ndims(unwrap_array_type(blocktype(a)), length(axes)), elt),
18+
elt,length(axes),similartype(unwrap_array_type(blocktype(a)), elt, axes)
2119
}(
2220
axes
2321
)
@@ -52,6 +50,13 @@ function Base.similar(
5250
end
5351

5452
# Fix ambiguity error with `BlockSparseArrays.jl`.
53+
function Base.similar(
54+
a::AnyAbstractBlockSparseArray,
55+
elt::Type,
56+
axes::Tuple{AbstractGradedUnitRange,Vararg{AbstractGradedUnitRange}},
57+
)
58+
return similar_blocksparse(a, elt, axes)
59+
end
5560
function Base.similar(
5661
a::AnyAbstractBlockSparseArray,
5762
elt::Type,

ext/BlockSparseArraysTensorAlgebraExt/BlockSparseArraysGradedUnitRangesExt.jl

Lines changed: 0 additions & 19 deletions
This file was deleted.

ext/BlockSparseArraysTensorAlgebraExt/BlockSparseArraysTensorAlgebraExt.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,15 @@ function TensorAlgebra.splitdims(
9494
groupreducewhile(tensor_product, split_axes, ndims(a); init=OneToOne()) do i, axis
9595
return length(axis) length(axes(a, i))
9696
end
97-
blockperms = invblockperm.(blocksortperm.(axes_prod))
97+
blockperms = blocksortperm.(axes_prod)
98+
sorted_axes = map((r, I) -> only(axes(r[I])), axes_prod, blockperms)
99+
98100
# TODO: This is doing extra copies of the blocks,
99101
# use `@view a[axes_prod...]` instead.
100102
# That will require implementing some reindexing logic
101103
# for this combination of slicing.
102-
a_unblocked = a[axes_prod...]
103-
a_blockpermed = a_unblocked[blockperms...]
104+
a_unblocked = a[sorted_axes...]
105+
a_blockpermed = a_unblocked[invblockperm.(blockperms)...]
104106
return splitdims(BlockReshapeFusion(), a_blockpermed, split_axes...)
105107
end
106108

src/abstractblocksparsearray/abstractblocksparsearray.jl

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -78,41 +78,52 @@ function Base.setindex!(
7878
return a
7979
end
8080

81-
using TypeParameterAccessors: unspecify_type_parameters
82-
function show_typeof_blocksparse(io::IO, a::AbstractBlockSparseArray)
83-
Base.show(io, unspecify_type_parameters(typeof(a)))
84-
print(io, '{')
85-
show(io, eltype(a))
86-
print(io, ", ")
87-
show(io, ndims(a))
88-
print(io, ", ")
89-
show(io, blocktype(a))
90-
print(io, ", …")
91-
print(io, '}')
92-
return nothing
81+
# Copy of `Base.dims2string` defined in `show.jl`.
82+
function dims_to_string(d)
83+
isempty(d) && return "0-dimensional"
84+
length(d) == 1 && return "$(d[1])-element"
85+
return join(map(string, d), '×')
9386
end
9487

95-
# Copied from `BlockArrays.jl`.
96-
block2string(b, s) = string(join(map(string, b), '×'), "-blocked ", Base.dims2string(s))
88+
# Copy of `BlockArrays.block2string` from `BlockArrays.jl`.
89+
block_to_string(b, s) = string(join(map(string, b), '×'), "-blocked ", dims_to_string(s))
9790

98-
function summary_blocksparse(io::IO, a::AbstractArray)
99-
print(io, block2string(blocksize(a), size(a)))
100-
print(io, ' ')
101-
show_typeof_blocksparse(io, a)
102-
return nothing
91+
using TypeParameterAccessors: type_parameters, unspecify_type_parameters
92+
function concretetype_to_string_truncated(type::Type; param_truncation_length=typemax(Int))
93+
isconcretetype(type) || throw(ArgumentError("Type must be concrete."))
94+
alias = Base.make_typealias(type)
95+
base_type, params = if isnothing(alias)
96+
unspecify_type_parameters(type), type_parameters(type)
97+
else
98+
base_type_globalref, params_svec = alias
99+
base_type_globalref.name, params_svec
100+
end
101+
str = string(base_type)
102+
if isempty(params)
103+
return str
104+
end
105+
str *= '{'
106+
param_strings = map(params) do param
107+
param_string = string(param)
108+
if length(param_string) > param_truncation_length
109+
return ""
110+
end
111+
return param_string
112+
end
113+
str *= join(param_strings, ", ")
114+
str *= '}'
115+
return str
103116
end
104117

105118
function Base.summary(io::IO, a::AbstractBlockSparseArray)
106-
summary_blocksparse(io, a)
119+
print(io, block_to_string(blocksize(a), size(a)))
120+
print(io, ' ')
121+
print(io, concretetype_to_string_truncated(typeof(a); param_truncation_length=40))
107122
return nothing
108123
end
109124

110125
function Base.showarg(io::IO, a::AbstractBlockSparseArray, toplevel::Bool)
111-
if toplevel
112-
show_typeof_blocksparse(io, a)
113-
else
114-
print(io, "::")
115-
show_typeof_blocksparse(io, a)
116-
end
126+
!toplevel && print(io, "::")
127+
print(io, concretetype_to_string_truncated(typeof(a); param_truncation_length=40))
117128
return nothing
118129
end

src/blocksparsearray/blocksparsearray.jl

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -208,31 +208,3 @@ TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(blocktype)) =
208208
function TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(blockstype))
209209
return Position(4)
210210
end
211-
212-
# TODO: Make this generic to `AbstractBlockSparseVector` using
213-
# TypeParameterAccessors.jl, for example using:
214-
# `set_ndims(unspecify_type_parameters(typeof(a)), 1)`.
215-
function show_typeof_blocksparse(io::IO, a::BlockSparseVector)
216-
print(io, "BlockSparseVector")
217-
print(io, '{')
218-
show(io, eltype(a))
219-
print(io, ", ")
220-
show(io, blocktype(a))
221-
print(io, ", …")
222-
print(io, '}')
223-
return nothing
224-
end
225-
226-
# TODO: Make this generic to `AbstractBlockSparseMatrix` using
227-
# TypeParameterAccessors.jl, for example using:
228-
# `set_ndims(unspecify_type_parameters(typeof(a)), 2)`.
229-
function show_typeof_blocksparse(io::IO, a::BlockSparseMatrix)
230-
print(io, "BlockSparseMatrix")
231-
print(io, '{')
232-
show(io, eltype(a))
233-
print(io, ", ")
234-
show(io, blocktype(a))
235-
print(io, ", …")
236-
print(io, '}')
237-
return nothing
238-
end

src/blocksparsearrayinterface/broadcast.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ function Broadcast.BroadcastStyle(
3131
return DefaultArrayStyle{N}()
3232
end
3333

34-
function Base.similar(bc::Broadcasted{<:BlockSparseArrayStyle}, elt::Type)
35-
# TODO: Make sure this handles GPU arrays properly.
34+
function Base.similar(bc::Broadcasted{<:BlockSparseArrayStyle}, elt::Type, ax)
35+
# TODO: Make this more generic, base it off sure this handles GPU arrays properly.
3636
m = Mapped(bc)
37-
return similar(first(m.args), elt, combine_axes(axes.(m.args)...))
37+
return similar(first(m.args), elt, ax)
3838
end
3939

4040
# Catches cases like `dest .= value` or `dest .= value1 .+ value2`.

test/test_basics.jl

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,24 @@ arrayts = (Array, JLArray)
337337
@test blockstoredlength(a) == 1
338338
@test storedlength(a) == 2 * 4
339339

340+
# Test similar on broadcasted expressions.
341+
a = dev(BlockSparseArray{elt}(undef, ([2, 3], [3, 4])))
342+
bc = Broadcast.broadcasted(+, a, a)
343+
a′ = similar(bc, Float32)
344+
@test a′ isa BlockSparseArray{Float32}
345+
@test blocktype(a′) <: arrayt{Float32,2}
346+
@test axes(a) == (blockedrange([2, 3]), blockedrange([3, 4]))
347+
348+
# Test similar on broadcasted expressions with axes specified.
349+
a = dev(BlockSparseArray{elt}(undef, ([2, 3], [3, 4])))
350+
bc = Broadcast.broadcasted(+, a, a)
351+
a′ = similar(
352+
bc, Float32, (blockedrange([2, 4]), blockedrange([2, 5]), blockedrange([2, 2]))
353+
)
354+
@test a′ isa BlockSparseArray{Float32}
355+
@test blocktype(a′) <: arrayt{Float32,3}
356+
@test axes(a′) == (blockedrange([2, 4]), blockedrange([2, 5]), blockedrange([2, 2]))
357+
340358
a = dev(BlockSparseArray{elt}(undef, ([2, 3], [3, 4])))
341359
@views for b in [Block(1, 2), Block(2, 1)]
342360
a[b] = dev(randn(elt, size(a[b])))
@@ -1090,34 +1108,30 @@ arrayts = (Array, JLArray)
10901108
arrayt_elt = arrayt{elt,3}
10911109

10921110
a = BlockSparseVector{elt,arrayt{elt,1}}([2, 2])
1111+
res = sprint(summary, a)
1112+
function ref_vec(elt, arrayt, prefix="")
1113+
return "2-blocked 4-element $(prefix)BlockSparseVector{$(elt), $(arrayt), …, …}"
1114+
end
10931115
# Either option is possible depending on namespacing.
1094-
@test (
1095-
sprint(summary, a) ==
1096-
"2-blocked 4-element BlockSparseVector{$(elt), $(vectort_elt), …}"
1097-
) || (
1098-
sprint(summary, a) ==
1099-
"2-blocked 4-element BlockSparseArrays.BlockSparseVector{$(elt), $(vectort_elt), …}"
1100-
)
1116+
@test (res == ref_vec(elt, vectort_elt)) ||
1117+
(res == ref_vec(elt, vectort_elt, "BlockSparseArrays."))
11011118

11021119
a = BlockSparseMatrix{elt,arrayt{elt,2}}([2, 2], [2, 2])
1120+
res = sprint(summary, a)
1121+
function ref_mat(elt, arrayt, prefix="")
1122+
return "2×2-blocked 4×4 $(prefix)BlockSparseMatrix{$(elt), $(arrayt), …, …}"
1123+
end
11031124
# Either option is possible depending on namespacing.
1104-
@test (
1105-
sprint(summary, a) == "2×2-blocked 4×4 BlockSparseMatrix{$(elt), $(matrixt_elt), …}"
1106-
) || (
1107-
sprint(summary, a) ==
1108-
"2×2-blocked 4×4 BlockSparseArrays.BlockSparseMatrix{$(elt), $(matrixt_elt), …}"
1109-
)
1125+
@test (res == ref_mat(elt, matrixt_elt)) ||
1126+
(res == ref_mat(elt, matrixt_elt, "BlockSparseArrays."))
11101127

11111128
a = BlockSparseArray{elt,3,arrayt{elt,3}}([2, 2], [2, 2], [2, 2])
1112-
1113-
# Either option is possible depending on namespacing.
1114-
@test (
1115-
sprint(summary, a) ==
1116-
"2×2×2-blocked 4×4×4 BlockSparseArray{$(elt), 3, $(arrayt_elt), …}"
1117-
) || (
1118-
sprint(summary, a) ==
1119-
"2×2×2-blocked 4×4×4 BlockSparseArrays.BlockSparseArray{$(elt), 3, $(arrayt_elt), …}"
1120-
)
1129+
res = sprint(summary, a)
1130+
function ref_arr(elt, arrayt, prefix="")
1131+
return "2×2×2-blocked 4×4×4 $(prefix)BlockSparseArray{$(elt), 3, $(arrayt), …, …}"
1132+
end
1133+
@test (res == ref_arr(elt, arrayt_elt)) ||
1134+
(res == ref_arr(elt, arrayt_elt, "BlockSparseArrays."))
11211135

11221136
if elt === Float64
11231137
# Not testing other element types since they change the

0 commit comments

Comments
 (0)