Skip to content

feat: share compilation cache for host devices - #1901

Merged
polvalente merged 3 commits into
elixir-nx:mainfrom
danmarcab:feat/share-compilation-cache
Nov 27, 2026
Merged

polvalente merged 3 commits into
elixir-nx:mainfrom
danmarcab:feat/share-compilation-cache

Conversation

@danmarcab

Copy link
Copy Markdown
Contributor

First of all, thanks for all the work on the libraries!

We've increased the number of host devices and got performance gains. We've run into high memory usage from our NX programs. This seems to be due to how the compiled programs are cached in memory.

This patch drops the device id from the cache key and ensures only one copy for each program exists.

Change

For host clients without a mesh:

  • device_id is dropped from the compilation cache key, so all devices share
    one compiled executable per function, shape and options.
  • The executable returned from the cache is re-bound to the device requested
    by the current call (bind_device/2). That covers __compile__/jit as
    well as module_compilation: :to_executable.
    Other platforms (CUDA, ROCm, etc.) and mesh/sharded executables are
    unchanged: their executables are tied to a device, so device_id stays in
    the key. Executables with device_id < 0 are not portable and are left
    alone.

Repro

# XLA_FLAGS=--xla_force_host_platform_device_count=8 elixir repro.exs
#
# Set EXLA_PATH to a local nx checkout's exla directory to run against a branch.
deps =
  case System.get_env("EXLA_PATH") do
    nil -> [{:exla, "1.0.0"}, {:fine, "0.1.6"}]
    path -> [{:exla, path: path}, {:nx, path: Path.join(path, "../nx"), override: true}]
  end
Mix.install(deps)
defmodule Repro do
  import Nx.Defn
  defn affine(x), do: x * 2 + 1
  def executables do
    :ets.foldl(
      fn {_key, value}, n -> if executable?(value), do: n + 1, else: n end,
      0,
      EXLA.Defn.LockedCache
    )
  end
  defp executable?(%EXLA.Executable{}), do: true
  defp executable?(value) when is_tuple(value), do: value |> Tuple.to_list() |> Enum.any?(&executable?/1)
  defp executable?(_), do: false
end
devices = EXLA.Client.fetch!(:host).device_count
x = Nx.iota({1000})
for device_id <- 0..(devices - 1) do
  Nx.Defn.jit_apply(&Repro.affine/1, [x], compiler: EXLA, device_id: device_id)
end
IO.puts("host devices: #{devices}, executables for one function and shape: #{Repro.executables()}")

Comment thread exla/lib/exla/defn.ex
{executable, {used_inputs, outputs, outfeed, _input_typespecs?}} =
compile(key, vars, fun, compile_options, 0, [], callback)

executable = bind_device(executable, compile_options)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have a test that requires multi device? We have a flag for this in test_helper I think.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll give it a go

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added one, I wasn't sure where the right place for the test was, let me know if you'd want is somewhere else!

danmarcab and others added 2 commits November 23, 2026 12:21
The test compiled add_two/2 with scalar s32 inputs, the same function and
shapes as the debug logging test. With the device-agnostic cache key, that
test could already have cached the compilation, so the initial refute
failed depending on test order.

Co-authored-by: Paulo Valente <polvalente@users.noreply.github.com>
@polvalente
polvalente merged commit c1a558f into elixir-nx:main Nov 27, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants