# Help with CUDA and Flux. DeviceMemory issue

**URL:** <https://discourse.julialang.org/t/help-with-cuda-and-flux-devicememory-issue/125481>\
**Category:** General Usage\
**Tags:** question, cuda, flux\
**Created:** [February 2, 2025, 7:46pm UTC](https://discourse.julialang.org/t/help-with-cuda-and-flux-devicememory-issue/125481 "2025-02-02T19:46:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [February 2, 2025, 7:46pm UTC](https://discourse.julialang.org/t/help-with-cuda-and-flux-devicememory-issue/125481/1 "2025-02-02T19:46:33Z")

</div>

Hello, good evening.

Im having an issue trying to run the Flux get started example.

Always got

```julia
ArgumentError: Illegal conversion of a CUDA.DeviceMemory to a Ptr{Float32}

```

It brokes in line 17:

```julia
    out1 = model(noisy |> device) # 2x1000 Matrix{Float32}, or CuArray{Float32}

```

Source code:

```julia
# Install everything, including CUDA, and load packages:
using Pkg; Pkg.add(["Flux", "CUDA", "cuDNN", "ProgressMeter"])
using Flux, Statistics, ProgressMeter
using CUDA # optional
device = gpu_device() # function to move data and model to the GPU

# Generate some data for the XOR problem: vectors of length 2, as columns of a matrix:
noisy = rand(Float32, 2, 1000) # 2×1000 Matrix{Float32}
truth = [xor(col[1]>0.5, col[2]>0.5) for col in eachcol(noisy)] # 1000-element Vector{Bool}

# Define our model, a multi-layer perceptron with one hidden layer of size 3:
model = Chain(
    Dense(2 => 3, tanh), # activation function inside layer
    BatchNorm(3),
    Dense(3 => 2)) |> device # move model to GPU, if one is available

# The model encapsulates parameters, randomly initialised. Its initial output is:
out1 = model(noisy |> device) # 2×1000 Matrix{Float32}, or CuArray{Float32}
probs1 = softmax(out1) |> cpu # normalise to get probabilities (and move off GPU)

# To train the model, we use batches of 64 samples, and one-hot encoding:
target = Flux.onehotbatch(truth, [true, false]) # 2×1000 OneHotMatrix
loader = Flux.DataLoader((noisy, target), batchsize=64, shuffle=true);

opt_state = Flux.setup(Flux.Adam(0.01), model) # will store optimiser momentum, etc.

# Training loop, using the whole data set 1000 times:
losses = []
@showprogress for epoch in 1:1_000
    for xy_cpu in loader
        # Unpack batch of data, and move to GPU:
        x, y = xy_cpu |> device
        loss, grads = Flux.withgradient(model) do m
            # Evaluate model and loss inside gradient context:
            y_hat = m(x)
            Flux.logitcrossentropy(y_hat, y)
        end
        Flux.update!(opt_state, model, grads[1])
        push!(losses, loss) # logging, outside gradient context
    end
end

opt_state # parameters, momenta and output have all changed

out2 = model(noisy |> device) # first row is prob. of true, second row p(false)
probs2 = softmax(out2) |> cpu # normalise to get probabilities
mean((probs2[1,:] .> 0.5) .== truth) # accuracy 94% so far!

```

---

<div class="post-metadata">

**Author:** ![mohamed.d180](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed.d180/32/52028_2.png) [@mohamed.d180](https://discourse.julialang.org/u/mohamed.d180)\
**Post date:** [February 2, 2025, 10:52pm UTC](https://discourse.julialang.org/t/help-with-cuda-and-flux-devicememory-issue/125481/2 "2025-02-02T22:52:04Z")

</div>

I can confirm that every thing works OK, here is a screen shot from Google colab, Please check your CUDA installation using

```julia
using CUDA
CUDA.versioninfo()

```

 ![Screen Shot 2025-02-03 at 12.48.48 AM](https://global.discourse-cdn.com/julialang/original/3X/3/f/3ff5ee863d87fd22a4a1200a2ad1dce8fa9360a4.png)

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [February 2, 2025, 11:17pm UTC](https://discourse.julialang.org/t/help-with-cuda-and-flux-devicememory-issue/125481/3 "2025-02-02T23:17:44Z")

</div>

Yep, Missing cuda. I was pretty sure that was installed. Thank you Mohamed.
