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

sqrt(::Complex) unsupported due to conversion exceptions #364

Closed
kmp5VT opened this issue Jun 13, 2024 · 3 comments · Fixed by #374
Closed

sqrt(::Complex) unsupported due to conversion exceptions #364

kmp5VT opened this issue Jun 13, 2024 · 3 comments · Fixed by #374
Labels
good first issue Good for newcomers

Comments

@kmp5VT
Copy link

kmp5VT commented Jun 13, 2024

Hello,

I am trying to compute the square root of a matrix with values of ComplexF32 and I run into a internal Metal.jl error. Here is a small example

julia> using Metal, Test
julia> d = randn(ComplexF32, (2,2))
julia> md = mtl(d)
julia> dd = sqrt.(d)
julia> mdd = sqrt.(md)

This is the error I am seeing

ERROR: InvalidIRError: compiling MethodInstance for (::Metal.var"#broadcast_2d#203")(::MtlDeviceMatrix{…}, ::Base.Broadcast.Broadcasted{…}) resulted in invalid LLVM IR
Reason: unsupported call to an unknown function (call to gpu_malloc)
Stacktrace:
 [1] malloc
   @ ~/.julia/packages/GPUCompiler/kqxyC/src/runtime.jl:88
 [2] macro expansion
   @ ~/.julia/packages/GPUCompiler/kqxyC/src/runtime.jl:183
 [3] macro expansion
   @ ./none:0
 [4] box
   @ ./none:0
 [5] box_float32
   @ ~/.julia/packages/GPUCompiler/kqxyC/src/runtime.jl:212
 [6] multiple call sites
   @ unknown:0

And this is my version info

pkg> st
  [dde4c033] Metal v1.1.0

julia> versioninfo()
Julia Version 1.10.4
Commit 48d4fd48430 (2024-06-04 10:41 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 10 × Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)
Environment:
  LD_LIBRARY_PATH = /Users/kpierce/Software/triqs/triqs_install/lib::/opt/intel/oneapi/mkl/latest/lib
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 

Thanks!

@maleadt
Copy link
Member

maleadt commented Jun 14, 2024

Dup of #69. You need to avoid exceptions in your code.

@maleadt maleadt closed this as completed Jun 14, 2024
@mtfishman
Copy link
Contributor

mtfishman commented Jun 19, 2024

@maleadt to clarify a bit, is the issue that sqrt(::Complex) can potentially throw an exception, and that isn't allowed in broadcasting in Metal.jl right now? I don't see any exception throwing in sqrt(::Complex): https://github.com/JuliaLang/julia/blob/6f39acb9fb2b34b43d6b455d196b40f1759c9031/base/complex.jl#L530. I can't even guess from a mathematical perspective why it might throw an error, it seems like square root would be defined for any complex number but I guess there is exception throwing farther down the call stack in one of the math functions being called inside of sqrt(::Complex).

Would something actionable here be to add a @device_override definition for sqrt(::Complex), as a stopgap until exceptions are supported in Metal.jl? If so I would request to keep this open so we can track it and maybe try to fix this case, since it would be quite useful for us.

@maleadt
Copy link
Member

maleadt commented Jun 19, 2024

is the issue that sqrt(::Complex) can potentially throw an exception, and that isn't allowed in broadcasting in Metal.jl right now?

Yeah, in fact, exceptions aren't allowed at all, not only in broadcasting code.

julia> function f(x)
       @inbounds x[1] = sqrt(x[1])
       return
       end
f (generic function with 1 method)

julia> @metal f(mtl([1f0+1f0im]))
ERROR: InvalidIRError: compiling MethodInstance for f(::MtlDeviceVector{ComplexF32, 1}) resulted in invalid LLVM IR
Reason: unsupported call to an unknown function (call to gpu_malloc)

I don't see any exception throwing in sqrt(::Complex)

If you inspect the generated LLVM IR you can spot the calls to ijl_box_float32 as mentioned above. sqrt(::Complex) calls ssqs which contains a check conversion to Int.

Would something actionable here be to add a @device_override definition for sqrt(::Complex), as a stopgap until exceptions are supported in Metal.jl?

Sure, that, or a quirk for ssqs, throwing a GPU-compatible exception instead.

@maleadt maleadt reopened this Jun 19, 2024
@maleadt maleadt changed the title [bug] square root of array of ComplexF32 type fails sqrt(::Complex) unsupported due to conversion exceptions Jun 19, 2024
@maleadt maleadt added bug good first issue Good for newcomers labels Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants