# Help with Flux.jl, Metal.jl (Apple Silicon) and Conv layers

**URL:** <https://discourse.julialang.org/t/help-with-flux-jl-metal-jl-apple-silicon-and-conv-layers/105632>\
**Category:** Machine Learning\
**Created:** [October 31, 2023, 9:44pm UTC](https://discourse.julialang.org/t/help-with-flux-jl-metal-jl-apple-silicon-and-conv-layers/105632 "2023-10-31T21:44:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JakobPeterlin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobpeterlin/32/3945_2.png) [@JakobPeterlin](https://discourse.julialang.org/u/JakobPeterlin)\
**Post date:** [October 31, 2023, 9:44pm UTC](https://discourse.julialang.org/t/help-with-flux-jl-metal-jl-apple-silicon-and-conv-layers/105632/1 "2023-10-31T21:44:46Z")

</div>

Hello,  
after trying to use Flux.jl to reimplement some PyTorch code (SpinalVGG), I encountered some difficulties when trying to run my code on an M2 Max’s GPU with Metal.jl. The error boils down to the following code, which first produces a warning (which is btw. identical to the warning that I get when I attempt to run this model on a 2070 Super) and then ends with an error.

Code (minimal):

```julia
using Flux, Metal
layer = gpu(Conv((3, 3), 1=>16, relu))
x = gpu(rand(28, 28, 1, 1, 1))
layer(x)

```

Warning (beginning of):

```julia
┌ Warning: Performing scalar indexing on task Task (runnable) @0x00000003671e8c90.
│ Invocation of getindex resulted in scalar indexing of a GPU array.

```

Error:

```julia
        nested task error: ArgumentError: cannot take the CPU address of a MtlArray{Float32, 5, Metal.MTL.MTLResourceStorageModePrivate}

```

Based on the above error I suspect that the Conv layer (or its parameters) are not transferred to the GPU with Metal.jl. But I am even more puzzled by the warning (which persists even when running this with CUDA instead of Metal on a Nvidia GPU). What am I doing wrong/misunderstanding and is Conv layer not supported by Metal.jl?

I would be glad if anyone could help me solve this issue. I have tried running this with Julia 1.9 (and 1.10 beta3) with newly updated packages.

---

<div class="post-metadata">

**Author:** ![joelreymont](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joelreymont/32/206998_2.png) [@joelreymont](https://discourse.julialang.org/u/joelreymont)\
**Post date:** [February 23, 2024, 10:11am UTC](https://discourse.julialang.org/t/help-with-flux-jl-metal-jl-apple-silicon-and-conv-layers/105632/2 "2024-02-23T10:11:01Z")

</div>

I get a different warning

```julia
julia> using Flux, Metal

julia> layer = gpu(Conv((3, 3), 1=>16, relu))
┌ Info: The CUDA functionality is being called but
│ `CUDA.jl` must be loaded to access it.
└ Add `using CUDA` or `import CUDA` to your code.
Conv((3, 3), 1 => 16, relu) # 160 parameters

```

and an error

```julia
julia> layer(x)
ERROR: DimensionMismatch: Rank of x and w must match! (5 vs. 4)

```

Flux does have the Meta backend

```julia
julia> device = Flux.get_device(; verbose=true)
[ Info: Using backend: Metal.
(::Flux.FluxMetalDevice) (generic function with 1 method)

julia> device.deviceID
<AGXG14CDevice: 0x125850400>
    name = Apple M2 Max

```

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [February 23, 2024, 4:27pm UTC](https://discourse.julialang.org/t/help-with-flux-jl-metal-jl-apple-silicon-and-conv-layers/105632/3 "2024-02-23T16:27:15Z")

</div>

Per [Missing functionalities - Metal with Conv and ConvTranspose layers · Issue #2278 · FluxML/Flux.jl · GitHub](https://github.com/FluxML/Flux.jl/issues/2278), convolutions are not supported on the Metal backend because nobody has stepped up to wrap the relevant functionality in Julia.
