Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiplication with SubArrays #47

Closed
jowch opened this issue Aug 29, 2022 · 2 comments
Closed

Multiplication with SubArrays #47

jowch opened this issue Aug 29, 2022 · 2 comments
Labels
arrays Things about the array abstraction.

Comments

@jowch
Copy link

jowch commented Aug 29, 2022

I have an application where I need to compute pairwise distances between vectors that are part of a larger MtlArray (Gaussian processes; kernel methods). The implementation that finally gets called uses SubArrays (it is designed for CPU work). Since there is no * method for MtlArrays and a view of a MtlArray, Julia dispatches to the CPU implementation of multiplication and is breaking.

Where should this kind of support be added? If this question has a complicated answer, how can I go about working around this? Thanks a bunch in advance!

using Metal

x = MtlArray(rand(Float32, 1, 1))
# 1×1 MtlArray{Float32, 2}:
#  0.7656045

subarray = view(x, :, :)
# 1×1 view(::MtlArray{Float32, 2}, :, :) with eltype Float32:
#  0.7656045

x * x
# 1×1 MtlArray{Float32, 2}:
#  0.5861502

x * subarray
# ERROR: MethodError: unsafe_convert(::Type{Ptr{Float32}}, ::MtlArray{Float32, 2}) is ambiguous. Candidates:
#   unsafe_convert(::Type{Ptr{T}}, a::AbstractArray{T}) where T in Base at pointer.jl:67
#   unsafe_convert(::Type{Ptr{S}}, a::AbstractArray{T}) where {S, T} in Base at pointer.jl:66
#   unsafe_convert(::Type{<:Ptr}, x::MtlArray) in Metal at /Users/me/.julia/packages/Metal/OGOSN/src/array.jl:128
# Possible fix, define
#   unsafe_convert(::Type{Ptr{T}}, ::MtlArray{T}) where T
# Stacktrace:
#  [1] gemm!(transA::Char, transB::Char, alpha::Float32, A::MtlArray{Float32, 2}, B::SubArray{Float32, 2, MtlArray{Float32, 2}, Tuple{Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}}, true}, beta::Float32, C::MtlArray{Float32, 2})
#    @ LinearAlgebra.BLAS ~/.julia/juliaup/julia-1.8.0+0.x64/share/julia/stdlib/v1.8/LinearAlgebra/src/blas.jl:1514
#  [2] gemm_wrapper!(C::MtlArray{Float32, 2}, tA::Char, tB::Char, A::MtlArray{Float32, 2}, B::SubArray{Float32, 2, MtlArray{Float32, 2}, Tuple{Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}}, true}, _add::LinearAlgebra.MulAddMul{true, true, Bool, Bool})
#    @ LinearAlgebra ~/.julia/juliaup/julia-1.8.0+0.x64/share/julia/stdlib/v1.8/LinearAlgebra/src/matmul.jl:674
#  [3] mul!
#    @ ~/.julia/juliaup/julia-1.8.0+0.x64/share/julia/stdlib/v1.8/LinearAlgebra/src/matmul.jl:161 [inlined]
#  [4] mul!
#    @ ~/.julia/juliaup/julia-1.8.0+0.x64/share/julia/stdlib/v1.8/LinearAlgebra/src/matmul.jl:276 [inlined]
#  [5] *(A::MtlArray{Float32, 2}, B::SubArray{Float32, 2, MtlArray{Float32, 2}, Tuple{Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}}, true})
#    @ LinearAlgebra ~/.julia/juliaup/julia-1.8.0+0.x64/share/julia/stdlib/v1.8/LinearAlgebra/src/matmul.jl:148
#  [6] top-level scope
#    @ REPL[30]:1
#  [7] macro expansion
#    @ ~/.julia/packages/CUDA/DfvRa/src/initialization.jl:52 [inlined]
#  [8] top-level scope
#    @ ~/.julia/packages/Metal/OGOSN/src/initialization.jl:25
@maleadt
Copy link
Member

maleadt commented Oct 3, 2022

In CUDA.jl, we represent contiguous SubArrays of a CuArray as another CuArray; that would probably be the easiest way to fix this (as it makes it easy to reuse functionality without having to add additional methods).

@maleadt maleadt added the arrays Things about the array abstraction. label May 22, 2023
@christiangnrd
Copy link
Contributor

Fixed since v0.5.1 by #251.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays Things about the array abstraction.
Projects
None yet
Development

No branches or pull requests

3 participants