From 60ec997b5dc03bc82865c7aa66d0dff84e3353c6 Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Thu, 16 Jan 2025 16:07:55 -0500 Subject: [PATCH] Fix tests (#31) --- Project.toml | 4 +-- src/BlockSparseArrays.jl | 1 + .../abstractblocksparsearray.jl | 6 ++++ .../abstractblocksparsearray/adapt.jl | 3 -- src/abstractblocksparsearray/map.jl | 3 +- .../blocksparsearrayinterface.jl | 4 +-- .../TestBlockSparseArraysUtils.jl | 0 test/{basics => }/test_basics.jl | 10 +++---- test/{basics => }/test_gradedunitrangesext.jl | 0 test/{basics => }/test_svd.jl | 28 +++++++++++-------- test/{basics => }/test_tensoralgebraext.jl | 0 11 files changed, 34 insertions(+), 25 deletions(-) rename ext/BlockSparseArraysAdaptExt/BlockSparseArraysAdaptExt.jl => src/abstractblocksparsearray/adapt.jl (52%) rename test/{basics => }/TestBlockSparseArraysUtils.jl (100%) rename test/{basics => }/test_basics.jl (99%) rename test/{basics => }/test_gradedunitrangesext.jl (100%) rename test/{basics => }/test_svd.jl (72%) rename test/{basics => }/test_tensoralgebraext.jl (100%) diff --git a/Project.toml b/Project.toml index 11d5983..df08ba7 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "BlockSparseArrays" uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4" authors = ["ITensor developers and contributors"] -version = "0.2.9" +version = "0.2.10" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" @@ -11,6 +11,7 @@ DerivableInterfaces = "6c5e35bf-e59e-4898-b73c-732dcc4ba65f" DiagonalArrays = "74fd4be6-21e2-4f6f-823a-4360d37c7a77" Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4" FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b" +GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527" GradedUnitRanges = "e2de450a-8a67-46c7-b59c-01d5a3d041c5" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" @@ -24,7 +25,6 @@ LabelledNumbers = "f856a3a6-4152-4ec4-b2a7-02c1a55d7993" TensorAlgebra = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a" [extensions] -BlockSparseArraysAdaptExt = "Adapt" BlockSparseArraysTensorAlgebraExt = ["LabelledNumbers", "TensorAlgebra"] [compat] diff --git a/src/BlockSparseArrays.jl b/src/BlockSparseArrays.jl index f125e2b..276b1f7 100644 --- a/src/BlockSparseArrays.jl +++ b/src/BlockSparseArrays.jl @@ -35,6 +35,7 @@ include("abstractblocksparsearray/broadcast.jl") include("abstractblocksparsearray/map.jl") include("abstractblocksparsearray/linearalgebra.jl") include("abstractblocksparsearray/cat.jl") +include("abstractblocksparsearray/adapt.jl") # functions specifically for BlockSparseArray include("blocksparsearray/defaults.jl") diff --git a/src/abstractblocksparsearray/abstractblocksparsearray.jl b/src/abstractblocksparsearray/abstractblocksparsearray.jl index c3297c1..b03d262 100644 --- a/src/abstractblocksparsearray/abstractblocksparsearray.jl +++ b/src/abstractblocksparsearray/abstractblocksparsearray.jl @@ -57,6 +57,12 @@ function Base.setindex!(a::AbstractBlockSparseArray{<:Any,0}, value) return a end +# Catch zero-dimensional case to avoid scalar indexing. +function Base.setindex!(a::AbstractBlockSparseArray{<:Any,0}, value, ::Block{0}) + blocks(a)[] = value + return a +end + function Base.setindex!( a::AbstractBlockSparseArray{<:Any,N}, value, I::Vararg{Block{1},N} ) where {N} diff --git a/ext/BlockSparseArraysAdaptExt/BlockSparseArraysAdaptExt.jl b/src/abstractblocksparsearray/adapt.jl similarity index 52% rename from ext/BlockSparseArraysAdaptExt/BlockSparseArraysAdaptExt.jl rename to src/abstractblocksparsearray/adapt.jl index b6a3d49..5ed4944 100644 --- a/ext/BlockSparseArraysAdaptExt/BlockSparseArraysAdaptExt.jl +++ b/src/abstractblocksparsearray/adapt.jl @@ -1,5 +1,2 @@ -module BlockSparseArraysAdaptExt using Adapt: Adapt, adapt -using BlockSparseArrays: AbstractBlockSparseArray, map_stored_blocks Adapt.adapt_structure(to, x::AbstractBlockSparseArray) = map_stored_blocks(adapt(to), x) -end diff --git a/src/abstractblocksparsearray/map.jl b/src/abstractblocksparsearray/map.jl index 52aec40..fd5cec7 100644 --- a/src/abstractblocksparsearray/map.jl +++ b/src/abstractblocksparsearray/map.jl @@ -1,6 +1,7 @@ using ArrayLayouts: LayoutArray using BlockArrays: blockisequal using DerivableInterfaces: @interface, AbstractArrayInterface, interface +using GPUArraysCore: @allowscalar using LinearAlgebra: Adjoint, Transpose using SparseArraysBase: SparseArraysBase, SparseArrayStyle @@ -55,7 +56,7 @@ function map_zero_dim! end @interface ::AbstractArrayInterface function map_zero_dim!( f, a_dest::AbstractArray, a_srcs::AbstractArray... ) - a_dest[] = f.(map(a_src -> a_src[], a_srcs)...) + @allowscalar a_dest[] = f.(map(a_src -> a_src[], a_srcs)...) return a_dest end diff --git a/src/blocksparsearrayinterface/blocksparsearrayinterface.jl b/src/blocksparsearrayinterface/blocksparsearrayinterface.jl index 46f95ca..e52c4ac 100644 --- a/src/blocksparsearrayinterface/blocksparsearrayinterface.jl +++ b/src/blocksparsearrayinterface/blocksparsearrayinterface.jl @@ -65,9 +65,7 @@ end @interface ::AbstractBlockSparseArrayInterface function Base.getindex( a::AbstractArray{<:Any,0} ) - # TODO: Use `Block()[]` once https://github.com/JuliaArrays/BlockArrays.jl/issues/430 - # is fixed. - return a[BlockIndex()] + return a[Block()[]] end # a[1:2, 1:2] diff --git a/test/basics/TestBlockSparseArraysUtils.jl b/test/TestBlockSparseArraysUtils.jl similarity index 100% rename from test/basics/TestBlockSparseArraysUtils.jl rename to test/TestBlockSparseArraysUtils.jl diff --git a/test/basics/test_basics.jl b/test/test_basics.jl similarity index 99% rename from test/basics/test_basics.jl rename to test/test_basics.jl index 12b3f8a..1754428 100644 --- a/test/basics/test_basics.jl +++ b/test/test_basics.jl @@ -187,15 +187,15 @@ arrayts = (Array, JLArray) @test iszero(@allowscalar(a[CartesianIndex()])) @test a[Block()] == dev(fill(0)) @test iszero(@allowscalar(a[Block()][])) - @test @allowscalar(a[Block()[]]) == 0 + @test iszero(@allowscalar(a[Block()[]])) @test Array(a) isa Array{elt,0} @test Array(a) == fill(0) for b in ( - (b = copy(a); @allowscalar b[] = 2; b), - (b = copy(a); @allowscalar b[CartesianIndex()] = 2; b), - (b = copy(a); @allowscalar b[CartesianIndex()] = 2; b), + (b = copy(a); @allowscalar(b[] = 2); b), + (b = copy(a); @allowscalar(b[CartesianIndex()] = 2); b), + (b = copy(a); @allowscalar(b[Block()[]] = 2); b), # Regression test for https://github.com/ITensor/BlockSparseArrays.jl/issues/27. - (b = copy(a); @allowscalar b[Block()] = dev(fill(2)); b), + (b = copy(a); b[Block()] = dev(fill(2)); b), ) @test size(b) == () @test isone(length(b)) diff --git a/test/basics/test_gradedunitrangesext.jl b/test/test_gradedunitrangesext.jl similarity index 100% rename from test/basics/test_gradedunitrangesext.jl rename to test/test_gradedunitrangesext.jl diff --git a/test/basics/test_svd.jl b/test/test_svd.jl similarity index 72% rename from test/basics/test_svd.jl rename to test/test_svd.jl index 70dc984..5370d9b 100644 --- a/test/basics/test_svd.jl +++ b/test/test_svd.jl @@ -1,15 +1,13 @@ -using Test -using BlockSparseArrays -using BlockSparseArrays: BlockSparseArray, svd, BlockDiagonal, eachblockstoredindex -using BlockArrays -using Random +using BlockArrays: Block, BlockedMatrix, BlockedVector, blocks, mortar +using BlockSparseArrays: BlockSparseArray, BlockDiagonal, eachblockstoredindex, svd using DiagonalArrays: diagonal using LinearAlgebra: LinearAlgebra +using Random: Random +using Test: @inferred, @testset, @test -function test_svd(a, usv) +function test_svd(a, usv; broken=false) U, S, V = usv - - @test U * diagonal(S) * V' ≈ a + @test U * diagonal(S) * V' ≈ a broken = broken @test U' * U ≈ LinearAlgebra.I @test V' * V ≈ LinearAlgebra.I end @@ -41,9 +39,17 @@ end @testset "($m, $n) BlockDiagonal{$T}" for ((m, n), T) in Iterators.product(blockszs, eltypes) a = BlockDiagonal([rand(T, i, j) for (i, j) in zip(m, n)]) - usv = svd(a) - # TODO: `BlockDiagonal * Adjoint` errors - test_svd(a, usv) + if VERSION ≥ v"1.11" + usv = svd(a) + # TODO: `BlockDiagonal * Adjoint` errors + # TODO: This is broken because of https://github.com/JuliaLang/julia/issues/57034, + # fix and reenable. + test_svd(a, usv; broken=true) + else + # `svd(a)` depends on `diagind(::AbstractMatrix, ::IndexStyle)` + # being defined, but it was only introduced in Julia v1.11. + @test svd(a) broken = true + end end # blocksparse diff --git a/test/basics/test_tensoralgebraext.jl b/test/test_tensoralgebraext.jl similarity index 100% rename from test/basics/test_tensoralgebraext.jl rename to test/test_tensoralgebraext.jl