From 7776c467ed43da3692f6ef6eff4ba93380ea2557 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 25 Sep 2026 18:08:57 +0200 Subject: [PATCH] Don't launch a kernel to permute empty arrays permutedims! launched its kernel with the destination's size as the ndrange, which Metal's KernelAbstractions back-end does not support when it is empty (JuliaGPU/Metal.jl#980): permutedims of an MtlMatrix with a zero dimension threw a DivideError. There is nothing to permute anyway. --- src/host/linalg.jl | 2 ++ test/testsuite/linalg.jl | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/host/linalg.jl b/src/host/linalg.jl index a198c3de..fefe1f86 100644 --- a/src/host/linalg.jl +++ b/src/host/linalg.jl @@ -1057,6 +1057,8 @@ function _permutedims!(::Type{IT}, dest::AbstractGPUArray, src::AbstractGPUArray, perm::NTuple{N}) where {IT,N} @assert length(src) <= typemax(IT) Base.checkdims_perm(dest, src, perm) + # not every back-end supports launching an empty kernel (JuliaGPU/Metal.jl#980) + isempty(dest) && return dest dest_strides = ntuple(k->k==1 ? 1 : prod(i->size(dest, i), 1:k-1), N) dest_strides_perm = ntuple(i->IT(dest_strides[findfirst(==(i), perm)]), N) size_src = IT.(size(src)) diff --git a/test/testsuite/linalg.jl b/test/testsuite/linalg.jl index 0df896c7..c08ba3a9 100644 --- a/test/testsuite/linalg.jl +++ b/test/testsuite/linalg.jl @@ -26,6 +26,8 @@ @test compare(x -> permutedims(x, (2, 1)), AT, rand(Float32, 2, 3)) @test compare(x -> permutedims(x, (2, 1, 3)), AT, rand(Float32, 4, 5, 6)) @test compare(x -> permutedims(x, (3, 1, 2)), AT, rand(Float32, 4, 5, 6)) + @test compare(x -> permutedims(x, (2, 1)), AT, rand(Float32, 0, 3)) + @test compare(x -> permutedims(x, (3, 1, 2)), AT, rand(Float32, 4, 0, 6)) if ComplexF32 in eltypes @test compare(x -> permutedims(x, [2,1,4,3]), AT, randn(ComplexF32,3,4,5,1)) # test UInt64 version to make sure it works properly when array length is larger than typemax of UInt32.