# My Flux Application painfully slow

**URL:** https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705
**Category:** General Usage
**Tags:** question
**Created:** [October 20, 2020, 8:34pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705 "2020-10-20T20:34:54Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [October 20, 2020, 8:34pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/1 "2020-10-20T20:34:55Z")

</div>

My Flux application runs painfully slowly.

My training set is about 250,000 with about 100 inputs with the following setup:

```julia
Chain(Dense(100,40,relu), Dense(40,40,relu), Dense(40,1,identity))

```

The mini-batches are about 6000 and are taking about 15 seconds each training using ADAM or RMSProp, which seems painfully slow.

My colleague wrote a PyTorch version which runs nearly 100 times faster on the same machine.

There must be a bottleneck somewhere, but I can’t seem to find it.

I am using a cross-entropy loss function (slightly modified, but compatible with Zygote).  
I timed the evaluation of the loss function for the entire dataset and it was only about 0.3 seconds

Does anyone have any idea why it should be running so slowly?

Thanks for any hints!

---

<div class="post-metadata">

### Author: ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)
#### Post date: [October 20, 2020, 8:40pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/2 "2020-10-20T20:40:35Z")

</div>

Could you post some more code so others could test it?  
Are you using a GPU?

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [October 20, 2020, 8:42pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/3 "2020-10-20T20:42:06Z")

</div>

No, I’m not using GPU. I know that when I first loaded Flux, there was an error relating to GPU but I tried again and it seemed to work. Maybe it didn’t load properly.

If I restart and type using Flux, I get a complaint about CUDA and NVidia drivers:

\u250c Warning: CUDA.jl only supports NVIDIA drivers for CUDA 9.0 or higher (yours is for CUDA 6.5.0)  
\u2514 @ CUDA /home/davide/.julia/packages/CUDA/dZvbp/src/initialization.jl:107

InitError: Could not find a suitable CUDA installation  
during initialization of module Flux

Stacktrace:  
[1] error(::String) at ./error.jl:33  
[2] **runtime\_init** () at /home/davide/.julia/packages/CUDA/dZvbp/src/initialization.jl:110  
[3] (::CUDA.var"#609#610"{Bool})() at /home/davide/.julia/packages/CUDA/dZvbp/src/initialization.jl:32  
[4] lock(::CUDA.var"#609#610"{Bool}, ::ReentrantLock) at ./lock.jl:161  
[5] \_functional(::Bool) at /home/davide/.julia/packages/CUDA/dZvbp/src/initialization.jl:26  
[6] functional(::Bool) at /home/davide/.julia/packages/CUDA/dZvbp/src/initialization.jl:19  
[7] functional at /home/davide/.julia/packages/CUDA/dZvbp/src/initialization.jl:18 [inlined]  
[8] **init** () at /home/davide/.julia/packages/Flux/05b38/src/Flux.jl:53  
[9] \_include\_from\_serialized(::String, ::Array{Any,1}) at ./loading.jl:697  
[10] \_require\_search\_from\_serialized(::Base.PkgId, ::String) at ./loading.jl:782  
[11] \_require(::Base.PkgId) at ./loading.jl:1007  
[12] require(::Base.PkgId) at ./loading.jl:928  
[13] require(::Module

But if I type using Flux again, there is no complaint. I ignored this because I don’t use GPU, but could it have other ramifications? Any idea about how to do a clean install?

---

<div class="post-metadata">

### Author: ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)
#### Post date: [October 20, 2020, 8:53pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/4 "2020-10-20T20:53:21Z")

</div>

I think it should be fine but this is a performance problem. If your colleague is using a GPU that could explain thedifference.  
Edit: sorry didnt See same machine

It’s kind of a fishing expedition without the code.

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [October 20, 2020, 8:56pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/5 "2020-10-20T20:56:55Z")

</div>

I can clean up the code and include it here in a few minutes, but there is only the model (given above), the loss function (which I have benchmarked) and the call to train! with ADAM or RMSProp

```julia
function xent_loss(x0,y0)
    mc=m1(x0).-mean(m1(x0))
    pden=exp.(mc)*y0[2:end,:]
    p=exp.(mc)./pden
    lossc=-sum(y0[1,:]'.*log.(p))
    return lossc
end

for epoch_idx in 1:maxiters
    Flux.train!(xent_loss, Flux.params(m1),tr_data, opt;cb=cb)
    cb()
end

```

---

<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: [October 20, 2020, 8:59pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/6 "2020-10-20T20:59:25Z")

</div>

> [@compleat](#):
>
> Warning: CUDA.jl only supports NVIDIA drivers for CUDA 9.0 or higher (yours is for CUDA 6.5.0)

You’ll need to upgrade your NVIDIA driver to something more recent.

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [October 20, 2020, 9:02pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/7 "2020-10-20T21:02:43Z")

</div>

OK - thanks. I didn’t think that would matter.

I didn’t think I was using GPU’s, but am I?

Maybe PyTorch (in Python) was using a compatible system.

I don’t know how to upgrade on Ubuntu, but I can take some time and figure it out.

Thanks!

---

<div class="post-metadata">

### Author: ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)
#### Post date: [October 20, 2020, 9:04pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/8 "2020-10-20T21:04:06Z")

</div>

I think the slices in the loss allocate so maybe using views would be faster. Check out the section on allocation here

> **[Julia 1.5 Highlights](https://julialang.org/blog/2020/08/julia-1.5-highlights/)**
>
> Julia version 1.5 has been released, featuring many performance improvements and new capabilities.

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [October 20, 2020, 9:09pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/9 "2020-10-20T21:09:13Z")

</div>

Thanks, but I am sorry, but I didn’t follow any of what you said, and I couldn’t see any reference to ‘views’ in the link.

Also, I checked and updating the CUDA driver in Ubuntu is very difficult (7 steps involving patches, setting environment variables, etc.). If I had a system administrator he/she could do it, but I don’t

Will that slow me down?

---

<div class="post-metadata">

### Author: ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)
#### Post date: [October 20, 2020, 9:12pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/10 "2020-10-20T21:12:09Z")

</div>

Sorry. This I think will allocate new memory ie be slow

```julia
y0[2:end,:]

```

Alternatively you can use

```julia
@view(y0[2:end,:])

```

Which does not allocate but reference the original memory.

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [October 20, 2020, 9:12pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/11 "2020-10-20T21:12:44Z")

</div>

Awesome!

---

<div class="post-metadata">

### Author: ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)
#### Post date: [October 20, 2020, 9:14pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/12 "2020-10-20T21:14:08Z")

</div>

This is the example from the blog so it seems the () are unnecessary

```julia
function sum_neighborhoods(A, n::Int)
    return [sum(@view A[i:i+n-1, j:j+n-1]) for i = 1:n:size(A,1), j = 1:n:size(A,2) ]
end

```

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [October 20, 2020, 9:17pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/13 "2020-10-20T21:17:23Z")

</div>

Unfortunately Zygote didn’t like it ☹

I have found it to be very temperamental (doesn’t even allow element-wise operations!)

---

<div class="post-metadata">

### Author: ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)
#### Post date: [October 20, 2020, 9:18pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/14 "2020-10-20T21:18:30Z")

</div>

Ah not good. I have no idea how the Zygote magic works. Sorry.

---

<div class="post-metadata">

### Author: ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)
#### Post date: [October 20, 2020, 9:21pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/15 "2020-10-20T21:21:44Z")

</div>

Two more possible ideas:

Compute and store `m1(x0)` and `exp.(mc)` only once depending on how long that takes.

In have never used it but maybe

[https://github.com/oscardssmith/BetterExp.jl](https://github.com/oscardssmith/BetterExp.jl)

Could improve speed?

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [October 20, 2020, 9:24pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/16 "2020-10-20T21:24:05Z")

</div>

But in any case, the whole evaluation of the loss function _even on the entire training set_ takes only 0.3 seconds. Why should one gradient step take 15 seconds?

BTW I would feel better about Flux loading properly. Any idea how I might achieve that? [Barring upgrading CUDA drivers which is too hard for me]

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [October 20, 2020, 9:29pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/17 "2020-10-20T21:29:02Z")

</div>

As the author of BetterExp, don’t use it here (at least not yet). The performance difference between base exp and the version in this package is only a maximum of 10x, so this isn’t the main problem. The main problem is almost certainly cpu vs gpu. A secondary problem is the memory allocation, which should be fixed with `@views`. Furthermore, with a little bit of luck BetterExp will have it’s improvements merged into Base by 1.6, at which point the library will be obsolete.

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [October 20, 2020, 9:32pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/18 "2020-10-20T21:32:16Z")

</div>

OK - thanks for your help. Maybe I will hire someone to install CUDA

---

<div class="post-metadata">

### Author: ![cojua8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cojua8/32/11882_2.png) [@cojua8](https://discourse.julialang.org/u/cojua8)
#### Post date: [October 20, 2020, 10:16pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/19 "2020-10-20T22:16:19Z")

</div>

Have you tried using `Float32` instead of `Float64`?

Have a look at this: [Performance Tips · Flux](https://fluxml.ai/Flux.jl/stable/performance/)

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [October 20, 2020, 10:17pm UTC](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705/20 "2020-10-20T22:17:54Z")

</div>

Hi. No, I haven’t but I am only using 12% of available memory on my machine. Would it help in any case?

[Next page](https://discourse.julialang.org/t/my-flux-application-painfully-slow/48705.md?page=2)
