TAS

TAS

Nx.Defn.compile - errors when using Nx.gather and a tensor as a compile-time option.

According to the Nx documentation, it seems possible to store a “constant” tensor in a compiled anonymous function by passing them as options at compile time. I realize that in many cases this is a bad idea due to the caching mechanisms but in this case, the tensor would be used as a configurable lookup table. In some cases, “storing” Tensors in the function works fine but Nx.gather generates a compile-time error. Adding two tensors of which one is stored as a “option” is fine;

  defn anon_fn1(arg, opts) do
    t2 = opts[:t2]
    arg + t2
  end

  def create_anon_fn1() do
    t2 = Nx.tensor([1, 3, 5], type: :f64) |> Nx.backend_copy()

    Nx.Defn.compile(
      &anon_fn1/2,
      [
        Nx.template({1}, :f64),
        [t2: t2]
      ],
      compiler: EXLA
    )
  end

iex(12)> fn1 = create_anon_fn1()
#Function<135.35555145/1 in Nx.Defn.Compiler.fun/2>
iex(13)> fn1.(Nx.tensor([2], type: :f64))
#Nx.Tensor<
  f64[3]
  EXLA.Backend<host:0, 0.1447861285.840040468.11335>
  [3.0, 5.0, 7.0]
>

However, using Nx.gather creates a compile error….

  defn anon_fn2(arg, opts) do
    t2 = opts[:t2]
    Nx.gather(t2, arg)
  end

def create_anon_fn2() do
    t2 = Nx.iota({10}, type: :f64) |> Nx.backend_copy()

    Nx.Defn.compile(
      &anon_fn2/2,
      [
        Nx.template({3, 1}, :s32),
        [t2: t2]
      ],
      compiler: EXLA
    )
  end

iex(16)> fn2 = create_anon_fn2()
** (FunctionClauseError) no function clause matching in Nx.BinaryBackend.to_binary/1

    The following arguments were given to Nx.BinaryBackend.to_binary/1:

        # 1
        #Nx.Tensor<
          s32[3][1]

          Nx.Defn.Expr
          parameter a:0   s32[3][1]
        >

    Attempted function clauses (showing 1 out of 1):

        defp to_binary(%Nx.Tensor{data: %{state: data}})

    (nx 0.6.4) Nx.BinaryBackend.to_binary/1
    (nx 0.6.4) lib/nx/binary_backend.ex:2120: Nx.BinaryBackend.gather/3
    (nx 0.6.4) lib/nx.ex:14575: Nx.gather/3
    (nx 0.6.4) lib/nx/defn/compiler.ex:158: Nx.Defn.Compiler.runtime_fun/3
    (exla 0.6.4) lib/exla/defn.ex:387: anonymous fn/4 in EXLA.Defn.compile/8
    (exla 0.6.4) lib/exla/defn/locked_cache.ex:36: EXLA.Defn.LockedCache.run/2
    (stdlib 5.0.2) timer.erl:270: :timer.tc/2
    iex:16: (file)
iex(16)>

Using iEx to verify that the tensors are correctly applied to Nx.gather;

iex(18)> t2 = Nx.iota({10}, type: :f64)
#Nx.Tensor<
  f64[10]
  EXLA.Backend<host:0, 0.1447861285.840040468.11339>
  [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
>
iex(19)> arg = Nx.tensor([[3], [2], [7]], type: :s32)
#Nx.Tensor<
  s32[3][1]
  EXLA.Backend<host:0, 0.1447861285.840040472.11967>
  [
    [3],
    [2],
    [7]
  ]
>
iex(20)> Nx.gather(t2, arg)
#Nx.Tensor<
  f64[3]
  EXLA.Backend<host:0, 0.1447861285.840040468.11340>
  [3.0, 2.0, 7.0]
>

Any thoughts on what is going on would be appreciated!

Thanks!!!

Environment:
Nx 0.6.4, EXLA 0.6.4
Erlang/OTP 26 [erts-14.0.2] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit] [dtrace]
Elixir (1.15.7)

Most Liked

josevalim

josevalim

Creator of Elixir

The docs for Nx.Defn explain this with more detail but you cannot pass tensor as options. Options are not abstracted away into expressions and we need to convert all tensors into expressions in order to JIT them.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement