Skip to content

feat: device-index-aware device and stream bindings in mlxcel-core (multi-GPU foundation) #487

Closed

Description

@inureyes

Part of #486.

Problem

mlxcel-core's device API is binary CPU vs GPU and always targets GPU index 0, so there is no way to place work on a specific GPU on a multi-GPU node:

  • src/lib/mlxcel-core/cpp/mlx_cxx_bridge.cpp:3780 set_default_device(bool gpu) -> Device::gpu
  • src/lib/mlxcel-core/cpp/mlx_cxx_bridge.cpp:18 new_stream_on_device(bool gpu) -> Device::gpu
  • src/lib/mlxcel-core/cpp/mlx_cxx_bridge.cpp:38 thread-local stream bound to Device::gpu
  • Rust FFI in src/lib/mlxcel-core/src/lib.rs:45,54,1708 exposes only the boolean forms.

mlx::core::Device already carries (DeviceType, int index), so the underlying capability exists upstream; it is simply not bound. This is the foundation for real single-node multi-GPU tensor parallelism (see the epic).

Scope

Add an index-aware device and stream surface, gated so non-CUDA builds behave sanely (Metal exposes a single GPU).

C++ bridge (mlx_cxx_bridge.h / .cpp):

  • gpu_device_count() -> int: number of usable GPUs. On CUDA, query the runtime (for example cudaGetDeviceCount) under the cuda build; on Metal return 1.
  • new_stream_on_gpu_index(index: i32) -> MlxStream: mlx::core::new_stream(Device(Device::gpu, index)).
  • set_default_device_index(index: i32): mlx::core::set_default_device(Device(Device::gpu, index)).
  • new_thread_local_stream_gpu_index(index: i32) -> MlxThreadLocalStream: per-thread stream pinned to a specific GPU (mirrors the existing new_thread_local_stream_gpu).
  • copy_array_to_stream(array, stream) -> MlxArray (or copy_to_device(array, index)): materialize an array on a target device. Confirm the correct MLX primitive (evaluating a copy/contiguous op on a target-device stream) during implementation; this is needed by sub-issue 2 for cross-device movement.

Rust FFI and safe wrappers (src/lib/mlxcel-core/src/lib.rs and the crate's public API):

  • Mirror the above in the extern "C++" block and add ergonomic safe wrappers.
  • Validate index bounds against gpu_device_count() and return a typed error for out-of-range indices.

Feature gating and portability:

  • Index greater than 0 is meaningful only under the cuda feature; on Metal, gpu_device_count() returns 1 and any index other than 0 is rejected with a clear message.

Acceptance criteria

  • New bridge functions compile on both the default (Metal/Apple) and cuda builds.
  • gpu_device_count() returns >= 1 on all targets and the true count on a multi-GPU CUDA host.
  • Rust unit tests: device-count is >= 1; creating a stream on each valid index succeeds; an out-of-range index returns the typed error.
  • CUDA-gated test (ignored or skipped on Metal CI): run a trivial op (for example an add) on a non-default GPU index and verify the result is correct and that the op actually executed on that device.
  • Integration (not orphaned): gpu_device_count() is called at server/CLI startup and logged (for example "detected N GPUs"), so the new API is exercised in a real code path even before sub-issue 2 lands.
  • No clippy or fmt regressions; cross-platform Rust checks pass.

Risks and notes

  • MLX CUDA device-count and per-index stream behavior at the pinned commit must be confirmed; if cudaGetDeviceCount is not reachable through the current build, add the minimal CUDA runtime link needed under the cuda feature only.
  • Cross-device array movement semantics need verification; document the chosen primitive in the PR.

Out of scope

  • The tensor-parallel runtime changes that consume this API (sub-issue 2).
  • Binding MLX's distributed/NCCL collectives (evaluated in sub-issue 2).

Activity

  1. added
    status:ready
    type:enhancement
    priority:high
    area:core
    status:in-progress
    and removed
    status:ready
    on Jun 30, 2026
  2. added
    status:done
    and removed
    status:in-progress
    on Jun 30, 2026
  3. self-assigned this
    on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:corepriority:highstatus:donetype:enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions