Nx performance

40 views
Skip to first unread message

Kyle White

unread,
Sep 19, 2022, 8:55:22 PM9/19/22
to Numerical Elixir (Nx)
Hi,

I'm just getting my feet wet with both Elixir and Nx and came across a striking performance difference between Nx and the Stream module for a computation.

My simple example is compute the sum of all odd numbers from 1 to N. Nx is taking ~20x as long as doing the computation without Nx.

Is there some way to optimize this for Nx? any tips? I'm of course looking to do a lot more with Nx but want to be sure I understand how to use it correctly first.

My example code is attached.

Thanks,
Kyle

comparison.exs

Sean Moriarity

unread,
Sep 19, 2022, 8:58:47 PM9/19/22
to elix...@googlegroups.com
Hi Kyle,

You're running Nx with the default backend (BinaryBackend), which is pure Elixir and very, very slow. You should try EXLA or Torchx as your backend. Try re-running your benchmark, but instead calling `Nx.Defn.global_default_options(compiler: EXLA)` before the call to `Benchmark.add_nx`

Sean

--
You received this message because you are subscribed to the Google Groups "Numerical Elixir (Nx)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-nx+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-nx/82d26ec0-04df-43ae-88b6-cf14bb831a40n%40googlegroups.com.

Kyle White

unread,
Sep 19, 2022, 9:19:05 PM9/19/22
to elix...@googlegroups.com
Hi Sean,

Thanks for the quick response on the lame question -- I should have read more of the docs before coming here. ~600ms down to <5 after compilation. Thanks!

Kyle

You received this message because you are subscribed to a topic in the Google Groups "Numerical Elixir (Nx)" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-nx/cxIA5DjxAYo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-nx+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-nx/CAMBO%2BDZ-34dcLiA%2Bh4jK81_h5J2_6AtTPmcU46GtMr%2BJnwWMvA%40mail.gmail.com.

smoria...@gmail.com

unread,
Sep 19, 2022, 11:04:47 PM9/19/22
to elix...@googlegroups.com
No problem, glad to help, and glad you’re seeing speedups now! 🙂

Sean

On Sep 19, 2022, at 9:19 PM, Kyle White <kylew...@gmail.com> wrote:


Reply all
Reply to author
Forward
0 new messages