# Help with AutoDiff in Metal.jl

**URL:** <https://discourse.julialang.org/t/help-with-autodiff-in-metal-jl/98937>\
**Category:** GPU\
**Created:** [May 16, 2023, 3:13pm UTC](https://discourse.julialang.org/t/help-with-autodiff-in-metal-jl/98937 "2023-05-16T15:13:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![raktim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raktim/32/11188_2.png) [@raktim](https://discourse.julialang.org/u/raktim)\
**Post date:** [May 16, 2023, 3:13pm UTC](https://discourse.julialang.org/t/help-with-autodiff-in-metal-jl/98937/1 "2023-05-16T15:13:41Z")

</div>

I am trying to use Metal.jl for a scientific computation and having difficulty with AutoDiff (ForwardDiff). I am using julia 1.9.

This is the code

```julia
dF = x->ForwardDiff.gradient(x->dot(x,x),x)
N = 7;
x = rand(Float32,N); 
dF(MtlArray(x))

```

The code work for N=7, but not for N=8 and higher.

I get the following warning/error

┌ **Warning** : Compilation of MetalLib to native code failed.  
│ If you think this is a bug, please file an issue and attach /var/folders/l\_/mmy119\_j47b\_k4mtcn0ngfj00000gn/T/jl\_43KX6d0bSl.metallib.  
└ @ Metal ~/.julia/packages/Metal/TtPHW/src/compiler/compilation.jl:77  
**ERROR** : NSError: Threadgroup memory size (36864) exceeds the maximum threadgroup memory allowed (32768) (AGXMetal13\_3, code 3)

What am I doing wrong here?  
Any help in this regard is much appreciated.  
Thanks!

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [May 16, 2023, 5:43pm UTC](https://discourse.julialang.org/t/help-with-autodiff-in-metal-jl/98937/2 "2023-05-16T17:43:04Z")

</div>

That’s strange; your code works here. Feel free to open an issue on Metal.jl, with more details (Manifest, including the metallib file, full backtrace, etc).

---

<div class="post-metadata">

**Author:** ![raktim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raktim/32/11188_2.png) [@raktim](https://discourse.julialang.org/u/raktim)\
**Post date:** [May 16, 2023, 5:56pm UTC](https://discourse.julialang.org/t/help-with-autodiff-in-metal-jl/98937/3 "2023-05-16T17:56:46Z")

</div>

Does it work when you set N = 8?

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [May 16, 2023, 5:59pm UTC](https://discourse.julialang.org/t/help-with-autodiff-in-metal-jl/98937/4 "2023-05-16T17:59:03Z")

</div>

Oh hah, I blindly copied your code. No, it doesn’t, let me have a quick look.

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [May 16, 2023, 6:06pm UTC](https://discourse.julialang.org/t/help-with-autodiff-in-metal-jl/98937/5 "2023-05-16T18:06:54Z")

</div>

[Detect mapreduce threadgroup limits instead of guessing. by maleadt · Pull Request #176 · JuliaGPU/Metal.jl · GitHub](https://github.com/JuliaGPU/Metal.jl/pull/176) should fix this

---

<div class="post-metadata">

**Author:** ![raktim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raktim/32/11188_2.png) [@raktim](https://discourse.julialang.org/u/raktim)\
**Post date:** [May 16, 2023, 10:31pm UTC](https://discourse.julialang.org/t/help-with-autodiff-in-metal-jl/98937/6 "2023-05-16T22:31:26Z")

</div>

I should just upgrade Metal.jl at my end?  
Thanks

---

<div class="post-metadata">

**Author:** ![ayushinav](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ayushinav/32/32687_2.png) [@ayushinav](https://discourse.julialang.org/u/ayushinav)\
**Post date:** [May 17, 2023, 2:38am UTC](https://discourse.julialang.org/t/help-with-autodiff-in-metal-jl/98937/7 "2023-05-17T02:38:28Z")

</div>

@maleadt @raktim  
In the similar spirit, with

```julia
mpu(arr)= MtlArray(Float32.(arr));
N = 7;
x = rand(Float32,N) |>xpu; 

```

this works fine:

```julia
dF = x->gradient(()->sum(broadcast(exp,x)), Flux.params(x))
dF(x)

```

returns `Grads(...)`, but using a `cos` instead of `exp`, not so:

```julia
dF = x->gradient(()->sum(broadcast(cos,x)), Flux.params(x))
dF(x)

```

returns

```julia
InvalidIRError: compiling kernel #broadcast_kernel#28(Metal.mtlKernelContext, MtlDeviceVector{ForwardDiff.Dual{Nothing, Float32, 1}, 1}, Base.Broadcast.Broadcasted{Metal.MtlArrayStyle{1}, Tuple{Base.OneTo{Int64}}, Zygote.var"#1392#1393"{typeof(cos)}, Tuple{Base.Broadcast.Extruded{MtlDeviceVector{Float32, 1}, Tuple{Bool}, Tuple{Int64}}}}, Int64) resulted in invalid LLVM IR
Reason: unsupported call to an unknown function (call to gpu_malloc)

```

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [May 17, 2023, 5:54am UTC](https://discourse.julialang.org/t/help-with-autodiff-in-metal-jl/98937/8 "2023-05-17T05:54:58Z")

</div>

That’s [Support for exceptions · Issue #69 · JuliaGPU/Metal.jl · GitHub](https://github.com/JuliaGPU/Metal.jl/issues/69). You need to avoid exceptions in kernel code, for now.
