-
Notifications
You must be signed in to change notification settings - Fork 38
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
Unable to compile trig functions through ForwardDiff #229
Comments
I guess this is essentially a dup of #69. The question is why an exception is being generated here, as we do support Metal.jl/src/device/intrinsics/math.jl Lines 128 to 130 in d6f958c
Can you see using Cthulhu how sincos is invoked?
|
Ah, sorry I didn't notice this the first time. It turns out its related to the New MWE: using Metal, KernelAbstractions
X = Metal.MtlArray(fill(0.3f0, 128))
Y = copy(X)
@kernel function mwe_kernel_sincos(out, a)
I = @index(Global, Linear)
s, c = sincos(a[I])
out[I] = s + c
end
kernel = mwe_kernel_sincos(Metal.MetalBackend())
kernel(Y, X, ndrange = size(Y)) ERROR: InvalidIRError: compiling MethodInstance for gpu_mwe_kernel_sincos(::KernelAbstractions.CompilerMetadata{KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.DynamicCheck, Nothing, CartesianIndices{1, Tuple{Base.OneTo{Int64}}}, KernelAbstractions.NDIteration.NDRange{1, KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.DynamicSize, CartesianIndices{1, Tuple{Base.OneTo{Int64}}}, CartesianIndices{1, Tuple{Base.OneTo{Int64}}}}}, ::MtlDeviceVector{Float32, 1}, ::MtlDeviceVector{Float32, 1}) resulted in invalid LLVM IR
Reason: unsupported call to an unknown function (call to gpu_malloc)
Stacktrace:
[1] malloc
@ ~/.julia/packages/GPUCompiler/YO8Uj/src/runtime.jl:88
[2] macro expansion
@ ~/.julia/packages/GPUCompiler/YO8Uj/src/runtime.jl:183
[3] macro expansion
@ ./none:0
[4] box
@ ./none:0
[5] box_int64
@ ~/.julia/packages/GPUCompiler/YO8Uj/src/runtime.jl:212
[6] indexed_iterate <--
@ ./tuple.jl:97
[7] macro expansion
@ ~/Developer/jl-forward-diff/mwe.jl:9
[8] gpu_mwe_kernel_sincos
@ ~/.julia/packages/KernelAbstractions/cWlFz/src/macros.jl:90
[9] gpu_mwe_kernel_sincos
@ ./none:0 Throws ostensibly the same error as above. So, instead trying @kernel function mwe_kernel_sincos(out, a)
I = @index(Global, Linear)
k = sincos(a[I])
out[I] = k[1] + k[2]
end Throws no error but only has |
It seems to me that the Metal Edit: some examples Kernel: @kernel function mwe_kernel_sincos(out, a)
I = @index(Global, Linear)
k = sincos(a[I])
out[I] = k[1]
end 41 ─ %119 = $(Expr(:foreigncall, "extern air.sincos.f32", Float32, svec(Float32), 0, :(:llvmcall), :(%116), :(%116)))::Float32
└─── goto #43 if not true
42 ─ nothing::Nothing
43 ┄ goto #44
44 ─ goto #49 if not true
45 ─ %124 = Core.tuple(%92)::Tuple{UInt32}
│ %125 = Base.getfield(out, :shape)::Tuple{Int64}
│ %126 = Base.getfield(%125, 1, true)::Int64 Kernel: @kernel function mwe_kernel_sincos(out, a)
I = @index(Global, Linear)
k = sincos(a[I])
out[I] = k[2]
end 41 ─ %119 = $(Expr(:foreigncall, "extern air.sincos.f32", Float32, svec(Float32), 0, :(:llvmcall), :(%116), :(%116)))::Float32
└─── goto #43 if not true
42 ─ Metal.throw(Metal.nothing)::Union{}
└─── unreachable
43 ─ goto #44
44 ─ goto #49 if not true
45 ─ %125 = Core.tuple(%92)::Tuple{UInt32}
│ %126 = Base.getfield(out, :shape)::Tuple{Int64}
│ %127 = Base.getfield(%126, 1, true)::Int64 |
MWE:
Output:
I don't really know the first thing about how the GPU compiler works (though am keen to learn), but I had a look with Cthulhu.jl all the same. I noticed using
@device_code_llvm
that when I have justsincos(a[I])[1]
(without theForwardDiff.derivative
) call, then I getBut with
ForwardDiff.derivative(sin, a[I])
andcode_typed(err; interactive = true)
, I see:which to me looks like it's trying to call the Julia
sincos
function instead of the Metal / air one?I don't know how to diagnose further / how one would fix this, but am happy to help in any way I can!
Edit: it's specifically only the
sincos
function when called in the ForwardDiffsin
function. If I modify ForwardDiff to just dosin
andcos
seperately, it all works fine.The text was updated successfully, but these errors were encountered: