# Speed Comparison Python v Julia for custom layers

**URL:** <https://discourse.julialang.org/t/speed-comparison-python-v-julia-for-custom-layers/101922>\
**Category:** Machine Learning\
**Tags:** question, flux, python, machine-learning, speed-optimization\
**Created:** [July 22, 2023, 1:38pm UTC](https://discourse.julialang.org/t/speed-comparison-python-v-julia-for-custom-layers/101922 "2023-07-22T13:38:41Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![willleeney](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willleeney/32/51708_2.png) [@willleeney](https://discourse.julialang.org/u/willleeney)\
**Post date:** [July 27, 2023, 1:16pm UTC](https://discourse.julialang.org/t/speed-comparison-python-v-julia-for-custom-layers/101922/21 "2023-07-27T13:16:35Z")

</div>

> All ML libraries do this, because each layer depends on the output of the previous one.

This isn’t true. The example I gave is a model where this isn’t the case? It processes `xi` with one layer and `xj` with one layer, which isn’t sequential?

---

<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:** [July 27, 2023, 1:22pm UTC](https://discourse.julialang.org/t/speed-comparison-python-v-julia-for-custom-layers/101922/22 "2023-07-27T13:22:37Z")

</div>

Yes, that’s what I meant by “branching”. What I thought you meant was wanting to run a sequence of layers in parallel, which wouldn’t work because of the aforementioned data dependency.

> [@willleeney](#):
>
> Or I can implement my own layer in simplechains but it seems like to do this I would basically just be answering my question.

Maybe you could clarify what the question here is? If your custom layer uses SimpleChain layers much like your current implementation uses Flux layers, the library is still helping by providing fast layer building blocks, memory management utils, etc.

---

<div class="post-metadata">

**Author:** ![willleeney](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willleeney/32/51708_2.png) [@willleeney](https://discourse.julialang.org/u/willleeney)\
**Post date:** [July 27, 2023, 1:29pm UTC](https://discourse.julialang.org/t/speed-comparison-python-v-julia-for-custom-layers/101922/23 "2023-07-27T13:29:54Z")

</div>

> wanting to run a sequence of layers in parallel, which wouldn’t work because of the aforementioned data dependency

I had thought that I do want to do this? Can I not run this `(xi * m.Wq)` at the same time as `(xj * m.Wk)` using multiple threads seeing as they don’t use any of the same resources?

> [@ToucheSir](#):
>
> Maybe you could clarify what the question here is? If your custom layer uses SimpleChain layers much like your current implementation uses Flux layers, the library is still helping by providing fast layer building blocks, memory management utils, etc.

So if I can just drop and drag in ` TurboDense{false}(identity, n_in)` in place of `Dense` then that would be great. In the example [GitHub - PumasAI/SimpleChains.jl: Simple chains](https://github.com/PumasAI/SimpleChains.jl), they wrap the layers in a chain and then init the parameters, and also calculate the gradients using `valgrad!($g, $schain, $x, $p)`. Are you saying that I can simply replace the my dense layers within my custom block with this? I was under the impression that it’s not that simple?

---

<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:** [July 27, 2023, 1:34pm UTC](https://discourse.julialang.org/t/speed-comparison-python-v-julia-for-custom-layers/101922/24 "2023-07-27T13:34:17Z")

</div>

> [@willleeney](#):
>
> Can I not run this `(xi * m.Wq)` at the same time as `(xj * m.Wk)` using multiple threads seeing as they don’t use any of the same resources?

Yes you could, I was referring to something like having a 3-layer MLP (a sequence of layers) and wanting to run all 3 layers at the same time concurrently.

> [@willleeney](#):
>
> I was under the impression that it’s not that simple?

I haven’t written custom layers with SimpleChains myself so I’m not equipped to answer this question. Since it doesn’t show up in the docs, you might want to reach out to the maintainers with it.

---

<div class="post-metadata">

**Author:** ![willleeney](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willleeney/32/51708_2.png) [@willleeney](https://discourse.julialang.org/u/willleeney)\
**Post date:** [July 27, 2023, 1:51pm UTC](https://discourse.julialang.org/t/speed-comparison-python-v-julia-for-custom-layers/101922/25 "2023-07-27T13:51:55Z")

</div>

> [@willleeney](#):
>
> So if I can just drop and drag in ` TurboDense{false}(identity, n_in)` in place of `Dense` then that would be great. In the example [GitHub - PumasAI/SimpleChains.jl: Simple chains](https://github.com/PumasAI/SimpleChains.jl), they wrap the layers in a chain and then init the parameters, and also calculate the gradients using `valgrad!($g, $schain, $x, $p)`. Are you saying that I can simply replace the my dense layers within my custom block with this? I was under the impression that it’s not that simple?

@Elrod Please may you shed some insight as the main contributor to SimpleChains.jl

---

<div class="post-metadata">

**Author:** ![willleeney](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willleeney/32/51708_2.png) [@willleeney](https://discourse.julialang.org/u/willleeney)\
**Post date:** [July 28, 2023, 8:56am UTC](https://discourse.julialang.org/t/speed-comparison-python-v-julia-for-custom-layers/101922/26 "2023-07-28T08:56:36Z")

</div>

This issue is related as discusses/requests having the parallel layers which would help me create the custom layers like I want: [Parallel Chains and Layers · Issue #84 · PumasAI/SimpleChains.jl · GitHub](https://github.com/PumasAI/SimpleChains.jl/issues/84)

This issue is related to a broader discussion on Flux compatibility which talks about having drop in turbo layers where a flux layer is expected. I’m not sure how to develop the custom layer using turbo layers still though. [Compatibility with Flux · Issue #59 · PumasAI/SimpleChains.jl · GitHub](https://github.com/PumasAI/SimpleChains.jl/issues/59)

This issue is related to having two simple chains overwrite each other [Two SimpleChains overwrite each other's predictions? · Issue #138 · PumasAI/SimpleChains.jl · GitHub](https://github.com/PumasAI/SimpleChains.jl/issues/138) So I’m guessing that having a SimpleChain of SimpleChains isn’t going to work.

[Previous page](https://discourse.julialang.org/t/speed-comparison-python-v-julia-for-custom-layers/101922.md?page=1)
