JuliaGPU / JuliaGPU/GPUArrays.jl

Matrix multiplication by static matrix

Open
#432 5 comments 0 reactions 0 assignees View on GitHub
enhancement good first issue
Dominant language
Julia
Stars
450
Forks
104
Avg merge
1d 4h
Merged PRs (30d)
10

Description

It looks like this is falling to the standard `generic_matmatmul!`, which errors on scalar indexing

```julia
julia> using LinearAlgebra, Metal, StaticArrays

julia> Metal.GPUArrays.allowscalar(false)

julia> A = one(SMatrix{3,3}); B = MtlArray(ones(Float32, 3, 1000)); C = copy(B);

julia> C .= A * B
ERROR: Scalar indexing is disallowed.
Invocation of getindex resulted in scalar indexing of a GPU array.
This is typically caused by calling an iterating implementation of a method.
Such implementations *do not* execute on the GPU, but very slowly on the CPU,
and therefore are only permitted from the REPL for prototyping purposes.
If you did intend to index this array, annotate the caller with @allowscalar.
Stacktrace:
[1] error(s::String)
@ Base ./error.jl:35
[2] assertscalar(op::String)
@ GPUArraysCore ~/.julia/packages/GPUArraysCore/lojQM/src/GPUArraysCore.jl:87
[3] getindex
@ ~/.julia/packages/GPUArrays/fqD8z/src/host/indexing.jl:9 [inlined]
[4] _generic_matmatmul!(C::MtlArray{Float64, 2}, tA::Char, tB::Char, A::SMatrix{3, 3, Float64, 9}, B::MtlArray{Float32, 2}, _add::LinearAlgebra.MulAddMul{true, true, Bool, Bool})
@ LinearAlgebra /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/matmul.jl:876
[5] generic_matmatmul!(C::MtlArray{Float64, 2}, tA::Char, tB::Char, A::SMatrix{3, 3, Float64, 9}, B::MtlArray{Float32, 2}, _add::LinearAlgebra.MulAddMul{true, true, Bool, Bool})
@ LinearAlgebra /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/matmul.jl:844
[6] mul!
@ /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/matmul.jl:303 [inlined]
[7] mul!
@ /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/matmul.jl:276 [inlined]
[8] *(A::SMatrix{3, 3, Float64, 9}, B::MtlArray{Float32, 2})
@ LinearAlgebra /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/matmul.jl:141
[9] top-level scope
@ REPL[59]:1
```

It also happens if I skip straight to `mul!`
```julia
julia> mul!(C, A, B, true, false)
ERROR: Scalar indexing is disallowed.
Invocation of getindex resulted in scalar indexing of a GPU array.
This is typically caused by calling an iterating implementation of a method.
Such implementations *do not* execute on the GPU, but very slowly on the CPU,
and therefore are only permitted from the REPL for prototyping purposes.
If you did intend to index this array, annotate the caller with @allowscalar.
Stacktrace:
[1] error(s::String)
@ Base ./error.jl:35
[2] assertscalar(op::String)
@ GPUArraysCore ~/.julia/packages/GPUArraysCore/lojQM/src/GPUArraysCore.jl:87
[3] getindex
@ ~/.julia/packages/GPUArrays/fqD8z/src/host/indexing.jl:9 [inlined]
[4] _generic_matmatmul!(C::MtlArray{Float32, 2}, tA::Char, tB::Char, A::SMatrix{3, 3, Float64, 9}, B::MtlArray{Float32, 2}, _add::LinearAlgebra.MulAddMul{true, true, Bool, Bool})
@ LinearAlgebra /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/matmul.jl:876
[5] generic_matmatmul!(C::MtlArray{Float32, 2}, tA::Char, tB::Char, A::SMatrix{3, 3, Float64, 9}, B::MtlArray{Float32, 2}, _add::LinearAlgebra.MulAddMul{true, true, Bool, Bool})
@ LinearAlgebra /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/matmul.jl:844
[6] mul!(C::MtlArray{Float32, 2}, A::SMatrix{3, 3, Float64, 9}, B::MtlArray{Float32, 2}, alpha::Bool, beta::Bool)
@ LinearAlgebra /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/matmul.jl:303
[7] top-level scope
@ REPL[60]:1
```
Here are the versions:
```julia
julia> versioninfo()
Julia Version 1.8.0
Commit 5544a0fab76 (2022-08-17 13:38 UTC)
Platform Info:
OS: macOS (arm64-apple-darwin21.3.0)
CPU: 8 × Apple M1
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-13.0.1 (ORCJIT, apple-m1)
Threads: 8 on 4 virtual cores
Environment:
JULIA_NUM_THREADS = 8
JULIA_EDITOR = code

(jl_eeJwW1) pkg> st
Status `/private/var/folders/jk/zbhsb6bd25b9swwhb_3d6v340000gn/T/jl_eeJwW1/Project.toml`
[dde4c033] Metal v0.1.1
[90137ffa] StaticArrays v1.5.7
[37e2e46d] LinearAlgebra
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the example with Metal, StaticArrays, and GPUArrays.allowscalar(false), then inspect the LinearAlgebra generic_matmatmul! and mul! stack frames shown in the report. The work is done when multiplication by an SMatrix avoids scalar indexing on the MtlArray and the reported operations complete successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.