Skip to content

Show the array type in 2-arg show of GPU arrays - #429

Merged
maleadt merged 1 commit into
JuliaGPU:mainfrom
mcabbott:patch-1
Oct 16, 2026
Merged

maleadt merged 1 commit into
JuliaGPU:mainfrom
mcabbott:patch-1

Conversation

@mcabbott

@mcabbott mcabbott commented Sep 23, 2022 •

Copy link
Copy Markdown
Contributor

Two-argument show printed GPU arrays exactly like their CPU copy, so a GPU array inside a tuple, NamedTuple or struct looked like an Array:

julia> (x = CuArray([1.0]), y = cu([2.0 3.0]))
(x = [1.0], y = Float32[2.0 3.0])                    # before
(x = CuArray([1.0]), y = CuArray(Float32[2.0 3.0]))  # after

The output is a call to the storage type around the materialized CPU copy, so it evaluates to an equal array, wrapped arrays included:

julia> m = cu([1 2; 3 4]);

julia> repr(m')
"CuArray([1 3; 2 4])"

julia> repr(view(m, 1, :)), repr(similar(m, 0, 2)), repr(CuArray(fill(1.0)))
("CuArray([1, 2])", "CuArray(Matrix{Int64}(undef, 0, 2))", "CuArray(fill(1.0))")

Wrappers with a printer of their own keep it, e.g. on Julia 1.12+ Tridiagonal(CuArray([3.0]), CuArray([1.0, 2.0]), CuArray([4.0])) and adjoint(CuArray([1.0])). Like Base's eltype prefix, the call is left out when the context's :typeinfo is exactly the array's type, as for the elements of a concretely typed container:

julia> repr([m, m])
"CuArray{Int64, 2, CUDACore.DeviceMemory}[[1 2; 3 4], [1 2; 3 4]]"

julia> repr(Any[m])
"Any[CuArray([1 2; 3 4])]"

3-arg (REPL) display is unchanged, except where Base delegates it to 2-arg show: empty arrays in a :compact context, and on Julia 1.13+ empty vectors, which now display as e.g. CuArray(Int64[]) rather than Int64[].

The type parameters of the storage are not shown, so a CuArray in unified memory shows as CuArray(...) too. Zero-dimensional arrays used to throw a scalar-indexing error and now show as above. Wrapped arrays in a container typed on the wrapper ([m']) show as Adjoint{...}[CuArray(...)], which does not convert back; Base has the same limitation for CPU wrappers.

Implementation: show(::IO, ::AbstractGPUArray) handles unwrapped arrays and decides the :typeinfo elision, because Base replaces :typeinfo before calling its internal helpers. Wrapped arrays still go through Base's show and the existing _show_nonempty, _show_empty and show_vector overrides, so wrappers with their own show method keep it.

Supersedes JuliaGPU/CUDA.jl#1197.

Comment thread test/testsuite/base.jl Resolved
Comment thread test/testsuite/base.jl Resolved
@christiangnrd
christiangnrd force-pushed the patch-1 branch 4 times, most recently from 49f7bb3 to 9555eb4 Compare July 26, 2026 15:34
@christiangnrd

Copy link
Copy Markdown
Member

I fixed up this PR and it's pretty simple is this something we still want?

@maleadt maleadt changed the title 2-arg show for GPUArray Show the array type in 2-arg show of GPU arrays Oct 15, 2026
@maleadt

maleadt commented Oct 15, 2026

Copy link
Copy Markdown
Member

Rebased and reworked, this should be good to go once CI passes.

Two-argument `show` printed GPU arrays exactly like their CPU copy, so
`(x = CuArray([1.0]),)` was indistinguishable from `(x = [1.0],)`. Show a call
to the storage type around the copy instead, e.g. `CuArray([1.0])`.

The name comes from the unwrapped storage type, and the copy is materialized,
so wrapped arrays show as an expression for an equal array: an `Adjoint` of a
`CuMatrix` shows as `CuArray(<transposed contents>)` rather than
`Adjoint(...)`, which would evaluate to the transpose. Wrappers with a printer
of their own, such as LinearAlgebra's `Diagonal` and adjoint vectors on Julia
1.12+, keep it and show their GPU parents this way. Zero-dimensional arrays,
which used to error on scalar indexing, now show as `CuArray(fill(1.0))`.

Like Base's eltype prefix, the call is left out when the context's `:typeinfo`
is exactly the array's type, e.g. for the elements of a
`Vector{CuArray{Float32, 1, DeviceMemory}}`. Base replaces `:typeinfo` before
calling the internal helpers that wrapped matrices go through, so this is
decided in an override of `show` for the storage type.

Co-authored-by: Christian Guinard <28689358+christiangnrd@users.noreply.github.com>
@maleadt
maleadt merged commit 9447b8b into JuliaGPU:main Oct 16, 2026
17 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants