diff --git a/src/host/base.jl b/src/host/base.jl index 28599c24..fc63272f 100644 --- a/src/host/base.jl +++ b/src/host/base.jl @@ -175,10 +175,13 @@ end # hacky overloads to make simple vcat and hcat with numbers work as expected. # we can't really make this work in general without Base providing # a dispatch mechanism for output container type. +# +# Like Base, a leading scalar in `vcat` or `hcat` is one element along the first dimension +# and along the dimension being concatenated, and stretches along every other dimension. @inline Base.vcat(a::Number, b::AbstractGPUArray) = vcat(fill!(similar(b, typeof(a), (1,size(b)[2:end]...)), a), b) @inline Base.hcat(a::Number, b::AbstractGPUArray) = - hcat(fill!(similar(b, typeof(a), (size(b)[1:end-1]...,1)), a), b) + hcat(fill!(similar(b, typeof(a), (1, 1, size(b)[3:end]...)), a), b) ## reshape diff --git a/test/testsuite/base.jl b/test/testsuite/base.jl index 414c4b56..4589af9b 100644 --- a/test/testsuite/base.jl +++ b/test/testsuite/base.jl @@ -331,7 +331,11 @@ end @test compare(hcat, AT, rand(Float32, 3), rand(Float32, 3, 3)) @test compare(hcat, AT, rand(Float32, 3, 3), rand(Float32, 3)) @test compare(hcat, AT, rand(Float32, 3, 3), rand(Float32, 3, 3)) - #@test compare(hcat, AT, rand(Float32, ), rand(Float32, 3, 3)) + # like Base, a leading scalar is one row tall and spans the trailing dimensions + @test compare_exact(hcat, AT, rand(Float32), rand(Float32, 1, 2, 3)) + @test compare_exact(hcat, AT, Int16(1), fill(Int32(2), 1, 0, 3, 2)) + @test_throws DimensionMismatch hcat(1f0, AT(rand(Float32, 3, 3))) + @test_throws DimensionMismatch hcat(1f0, AT(rand(Float32, 2, 0))) #@test compare(hcat, AT, rand(Float32, 3, 3), rand(Float32)) @test compare(vcat, AT, rand(Float32, 3), rand(Float32, 3)) @@ -339,6 +343,7 @@ end @test compare(vcat, AT, rand(Float32, ), rand(Float32, 3)) @test compare(vcat, AT, rand(Float32, 3), rand(Float32)) @test compare(vcat, AT, rand(Float32, ), rand(Float32, 3, 3)) + @test compare(vcat, AT, rand(Float32), rand(Float32, 1, 2, 3)) #@test compare(vcat, AT, rand(Float32, 3, 3), rand(Float32)) @test compare((a,b) -> cat(a, b; dims=4), AT, rand(Float32, 3, 4), rand(Float32, 3, 4))