From 969d34b93b852ea0fd24d09684a3cfe430ed372a Mon Sep 17 00:00:00 2001 From: Jack Dunham Date: Mon, 19 Oct 2026 10:50:15 -0400 Subject: [PATCH 1/2] Read `scalar` of a rank-0 GPU array with `@allowscalar` `scalar(a) = a[]` is a scalar index, which GPU array packages disallow, so reading the result of a full contraction of `CuArray`s threw. A `GPUArraysCore` extension adds `scalar(a::AnyGPUArray{<:Any, 0}) = @allowscalar a[]`, which copies the element to the host; other arrays keep the generic `a[]`. Bump the version to 0.23.3. Co-Authored-By: Claude Opus 5.5 --- Project.toml | 5 ++++- ext/TensorAlgebraGPUArraysCoreExt.jl | 9 +++++++++ test/test_scalar.jl | 6 +++++- 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 ext/TensorAlgebraGPUArraysCoreExt.jl diff --git a/Project.toml b/Project.toml index bc6956e8..76bfe1ab 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "TensorAlgebra" uuid = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a" -version = "0.23.2" +version = "0.23.3" authors = ["ITensor developers and contributors"] [workspace] @@ -15,6 +15,7 @@ StridedViews = "4db3bf67-4bd7-4b4e-b153-31dc3fb37143" TupleTools = "9d95972d-f1c8-5527-a6e0-b4b365fa01f6" [weakdeps] +GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527" Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6" TensorKit = "07d1fe3e-3e46-537d-9eac-e9e13d0d4cec" TensorKitSectors = "13a9c161-d5da-41f0-bcbd-e1a08ae0647f" @@ -22,12 +23,14 @@ TensorOperations = "6aa20fa7-93e2-5fca-9bc0-fbd0db3c71a2" VectorInterface = "409d34a3-91d5-4945-b6ec-7529ddf182d8" [extensions] +TensorAlgebraGPUArraysCoreExt = "GPUArraysCore" TensorAlgebraMooncakeExt = "Mooncake" TensorAlgebraTensorKitExt = ["TensorKit", "TensorOperations", "VectorInterface"] TensorAlgebraTensorKitSectorsExt = "TensorKitSectors" TensorAlgebraTensorOperationsExt = "TensorOperations" [compat] +GPUArraysCore = "0.2" LinearAlgebra = "1.10" MatrixAlgebraKit = "0.6" Mooncake = "0.4.202, 0.5" diff --git a/ext/TensorAlgebraGPUArraysCoreExt.jl b/ext/TensorAlgebraGPUArraysCoreExt.jl new file mode 100644 index 00000000..076ea8ec --- /dev/null +++ b/ext/TensorAlgebraGPUArraysCoreExt.jl @@ -0,0 +1,9 @@ +module TensorAlgebraGPUArraysCoreExt + +using GPUArraysCore: AnyGPUArray, @allowscalar +using TensorAlgebra: TensorAlgebra + +# `a[]` on a GPU array is a scalar index, which GPU array packages disallow by default. +TensorAlgebra.scalar(a::AnyGPUArray{<:Any, 0}) = @allowscalar a[] + +end diff --git a/test/test_scalar.jl b/test/test_scalar.jl index 89098c5a..90baec15 100644 --- a/test/test_scalar.jl +++ b/test/test_scalar.jl @@ -1,5 +1,6 @@ +using JLArrays: JLArray using TensorAlgebra: scalar -using Test: @test, @testset +using Test: @test, @test_throws, @testset @testset "scalar" begin @test scalar(fill(3.0)) === 3.0 @@ -7,4 +8,7 @@ using Test: @test, @testset a[] = 5.0 @test scalar(a) === 5.0 @test scalar(fill(2 + 3im)) === 2 + 3im + @test scalar(JLArray(fill(3.0))) === 3.0 + @test scalar(reshape(JLArray([4.0]), ())) === 4.0 + @test_throws ErrorException scalar(JLArray([3.0])) end From 8efd57bae1191f0003633755dd668b6db20c5a27 Mon Sep 17 00:00:00 2001 From: Jack Dunham Date: Mon, 19 Oct 2026 11:00:18 -0400 Subject: [PATCH 2/2] Formatting. --- ext/TensorAlgebraGPUArraysCoreExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/TensorAlgebraGPUArraysCoreExt.jl b/ext/TensorAlgebraGPUArraysCoreExt.jl index 076ea8ec..2ec60bab 100644 --- a/ext/TensorAlgebraGPUArraysCoreExt.jl +++ b/ext/TensorAlgebraGPUArraysCoreExt.jl @@ -1,6 +1,6 @@ module TensorAlgebraGPUArraysCoreExt -using GPUArraysCore: AnyGPUArray, @allowscalar +using GPUArraysCore: @allowscalar, AnyGPUArray using TensorAlgebra: TensorAlgebra # `a[]` on a GPU array is a scalar index, which GPU array packages disallow by default.