Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(exla): respect device id when automatic transfers are disabled #1592

Merged
merged 4 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion exla/config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import Config
config :exla, :clients,
cuda: [platform: :cuda, memory_fraction: 0.8],
rocm: [platform: :rocm, memory_fraction: 0.8],
other_host: [platform: :host]
other_host: [platform: :host],
no_automatic_transfers_host: [platform: :host, automatic_transfers: false]

config :exla, default_client: String.to_atom(System.get_env("EXLA_TARGET", "host"))

Expand Down
4 changes: 2 additions & 2 deletions exla/lib/exla/backend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,14 @@ defmodule EXLA.Backend do
def concatenate(out, tensors, axis) do
copied = Enum.map(tensors, &Nx.backend_copy(&1, Nx.BinaryBackend))
result = Nx.BinaryBackend.concatenate(out, copied, axis)
Nx.backend_transfer(result, {EXLA.Backend, jit_opts([], tensors)})
Nx.backend_transfer(result, {EXLA.Backend, jit_opts(tensors, [])})
end

@impl true
def stack(out, tensors, axis) do
copied = Enum.map(tensors, &Nx.backend_copy(&1, Nx.BinaryBackend))
result = Nx.BinaryBackend.stack(out, copied, axis)
Nx.backend_transfer(result, {EXLA.Backend, jit_opts([], tensors)})
Nx.backend_transfer(result, {EXLA.Backend, jit_opts(tensors, [])})
end

@impl true
Expand Down
27 changes: 27 additions & 0 deletions exla/test/exla/backend_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,33 @@ defmodule EXLA.BackendTest do
assert %{device_id: 1, client_name: :other_host} = Nx.reshape(a, {1}).data.buffer
end

@tag :multi_device
test "stack and concatenate should end up in the same client" do
t_0 =
Nx.tensor([1], backend: {EXLA.Backend, client: :no_automatic_transfers_host, device_id: 0})

t_1 =
Nx.tensor([1], backend: {EXLA.Backend, client: :no_automatic_transfers_host, device_id: 1})

t_stack_0 = Nx.stack([t_0, t_1])
t_concat_0 = Nx.concatenate([t_0, t_1])

assert t_stack_0.data.buffer.client_name == :no_automatic_transfers_host
assert t_stack_0.data.buffer.device_id == 1

assert t_concat_0.data.buffer.client_name == :no_automatic_transfers_host
assert t_concat_0.data.buffer.device_id == 1

t_stack_1 = Nx.stack([t_1, t_0])
t_concat_1 = Nx.concatenate([t_1, t_0])

assert t_stack_1.data.buffer.client_name == :no_automatic_transfers_host
assert t_stack_1.data.buffer.device_id == 0

assert t_concat_1.data.buffer.client_name == :no_automatic_transfers_host
assert t_concat_1.data.buffer.device_id == 0
end

test "Kernel.inspect/2" do
t = Nx.tensor([1, 2, 3, 4], backend: EXLA.Backend)

Expand Down