Skip to content

fix(exla): use per-partition device id when preparing shard_jit input buffers - #1833

Merged
polvalente merged 4 commits into
mainfrom
fix/exla-shard-jit-device-id-bug
Sep 10, 2026
Merged

polvalente merged 4 commits into
mainfrom
fix/exla-shard-jit-device-id-bug

Conversation

@polvalente

Copy link
Copy Markdown
Member

EXLA.MLIR.Module.compile/5 sets executable.device_id to -1 for sharded
(SPMD) executables, since they are not pinned to one device. maybe_outfeed
passed that -1 straight into EXLA.Defn.Buffers.from_nx!/2, which compares
it against each input buffer's device id and, on a mismatch, calls
EXLA.DeviceBuffer.copy_to_device/3 with device_id -1. The NIF rejects -1,
raising "No matching device found for device_id -1" any time a shard_jit
input tensor already lived on an EXLA device (e.g. default_backend set to
EXLA.Backend) instead of Nx.BinaryBackend.

from_nx!/4 now takes the real target device id for the buffer it is
preparing. maybe_outfeed passes each partition's index as that id when
the run is sharded, matching PjRt's default per-partition device
assignment, and keeps using executable.device_id otherwise.

Added a regression test in sharding_test.exs that reproduces the crash
with EXLA.Backend-resident inputs and checks the fix returns correct
per-partition results with the right device ids.

… buffers

EXLA.MLIR.Module.compile/5 sets executable.device_id to -1 for sharded
(SPMD) executables, since they are not pinned to one device. maybe_outfeed
passed that -1 straight into EXLA.Defn.Buffers.from_nx!/2, which compares
it against each input buffer's device id and, on a mismatch, calls
EXLA.DeviceBuffer.copy_to_device/3 with device_id -1. The NIF rejects -1,
raising "No matching device found for device_id -1" any time a shard_jit
input tensor already lived on an EXLA device (e.g. default_backend set to
EXLA.Backend) instead of Nx.BinaryBackend.

from_nx!/4 now takes the real target device id for the buffer it is
preparing. maybe_outfeed passes each partition's index as that id when
the run is sharded, matching PjRt's default per-partition device
assignment, and keeps using executable.device_id otherwise.

Added a regression test in sharding_test.exs that reproduces the crash
with EXLA.Backend-resident inputs and checks the fix returns correct
per-partition results with the right device ids.
Replaces the two optional positional args (transfer?, target_device_id)
with a single opts list, so call sites read as flags instead of bare
booleans/nils.
@polvalente
polvalente force-pushed the fix/exla-shard-jit-device-id-bug branch from fd2160f to cc8266c Compare September 10, 2026 20:24
@polvalente
polvalente merged commit 8b4e123 into main Sep 10, 2026
9 checks passed
@polvalente
polvalente deleted the fix/exla-shard-jit-device-id-bug branch September 10, 2026 20:27
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.

1 participant