TAS

TAS

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)

Showing Posts 1 to 1

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews