Skip to content

feat: device-index-aware device/stream bindings in mlxcel-core (#487) - #505

Merged
inureyes merged 1 commit into
mainfrom
feat/487-device-index-bindings
Jun 30, 2026
Merged

inureyes merged 1 commit into
mainfrom
feat/487-device-index-bindings

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Adds an index-aware GPU device and stream surface to mlxcel-core, the foundation for single-node multi-GPU tensor parallelism (epic #486, sub-issue 1 of 2). The device API was binary CPU/GPU and always targeted GPU index 0, so there was no way to place work on a specific GPU of a multi-GPU node. Sub-issue #488 (the multi-GPU TP runtime) consumes this surface and lands on CUDA hardware.

What changed

C++ bridge (mlx_cxx_bridge.{h,cpp}): new entry points built on MLX's portable device API, so they compile identically on Metal, a CUDA multi-GPU host, and a CPU-only build with no CUDA headers and no preprocessor gating.

  • gpu_device_count() -> i32 via mlx::core::device_count(Device::gpu), clamped to >= 1 (Metal reports 1, a CUDA build reports the real adapter count).
  • new_stream_on_gpu_index, set_default_device_index, new_thread_local_stream_gpu_index via the Device(DeviceType, index) constructor.
  • copy_array_to_stream via mlx::core::copy(array, stream) for cross-device materialization (used by sub-issue feat: multi-GPU tensor-parallel runtime with per-rank device placement and cross-GPU collectives #488 to move shards between GPUs).

Rust FFI and safe wrappers (lib.rs, streams.rs): the raw bridge functions are mirrored in the extern "C++" block; ergonomic safe wrappers in streams (new_stream_on_gpu, set_default_gpu_device, new_thread_local_stream_on_gpu) validate the index against gpu_device_count() and return a typed InvalidGpuIndex error.

Integration: the server startup banner (log_endpoints) and the CLI model-load path both log the detected GPU count, exercising the new API in a real code path before sub-issue #488 lands.

Design note

The issue suggested cudaGetDeviceCount for the device count. MLX at the pinned commit already exposes a portable device_count(DeviceType) (mlx/device.h), so no CUDA runtime header or link is needed: the same code is correct across Metal, CUDA, and CPU-only builds. This removes the only part of the issue that would otherwise have required CUDA-host-only code.

Testing

Verified on macOS / Apple Silicon (Metal), where gpu_device_count() is 1.

  • cargo build (default/Metal features): green.
  • cargo test -p mlxcel-core --lib streams: 12/12 pass, including device-count >= 1, stream creation on every valid index, out-of-range rejection (at-count and negative), default-device and thread-local validation, and the typed-error message.
  • cargo clippy --all-targets and cargo fmt --check: clean.

The multi-GPU behavior (index > 0) is not reachable on a single-GPU Metal machine, so index-> 0 placement and the cross-GPU copy path are validated in sub-issue #488 on CUDA hardware. A #[cfg(feature = "cuda")] test (trivial_op_runs_on_non_default_gpu_index) is included for that host; the cuda feature cannot be built on macOS (no CUDA toolkit), so the cuda build path is verified on a CUDA box as part of #488.

Acceptance criteria

  • New bridge functions compile on the default (Metal/Apple) build. The cuda build is verified on a CUDA host as part of feat: multi-GPU tensor-parallel runtime with per-rank device placement and cross-GPU collectives #488.
  • gpu_device_count() returns >= 1 on all targets and reports the true count on a multi-GPU CUDA host.
  • Rust unit tests: device-count >= 1; a stream on each valid index succeeds; an out-of-range index returns the typed error.
  • CUDA-gated test runs a trivial op on a non-default GPU index (compiled under the cuda feature; runs on a multi-GPU CUDA host).
  • gpu_device_count() is surfaced and logged at server and CLI startup.
  • No clippy or fmt regressions.

Closes #487

Add an index-aware GPU device and stream surface to mlxcel-core so work can
be placed on a specific GPU on a multi-GPU node. This is the foundation for
single-node tensor parallelism (epic #486); the boolean device API only
targeted GPU index 0.

The new bridge entry points use MLX's portable device API
(device_count(DeviceType::gpu) and the Device(DeviceType, index)
constructor), so the same code compiles on Metal (one GPU), a CUDA
multi-GPU host, and a CPU-only build with no CUDA headers:

- gpu_device_count() -> i32 (clamped to >= 1)
- new_stream_on_gpu_index / set_default_device_index /
  new_thread_local_stream_gpu_index
- copy_array_to_stream: cross-device materialize via mlx::core::copy,
  consumed by the multi-GPU runtime in sub-issue #488

Safe wrappers in streams.rs (new_stream_on_gpu, set_default_gpu_device,
new_thread_local_stream_on_gpu) validate the index against the device count
and return a typed InvalidGpuIndex error.

Integration: the server startup banner and the CLI model-load path both log
the detected GPU count, exercising the new API in a real code path before
sub-issue #488 lands.

Tests cover device-count >= 1, stream creation on every valid index,
out-of-range rejection, the typed error message, and default-device and
thread-local validation. A cuda-gated test runs a trivial op on a
non-default GPU index, compiled only under the cuda feature.

Verified on macOS/Metal: build, 12 streams tests, clippy, and fmt all
green. The cuda build path and the multi-GPU runtime are validated in
sub-issue #488 on CUDA hardware.
@inureyes inureyes added type:enhancement New features, capabilities, or significant additions priority:high High priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers status:review Under review labels Jun 30, 2026
@inureyes
inureyes merged commit 38dbaad into main Jun 30, 2026
4 checks passed
@inureyes
inureyes deleted the feat/487-device-index-bindings branch June 30, 2026 16:28
@inureyes inureyes added status:done Completed and removed status:review Under review labels Jun 30, 2026
@inureyes inureyes self-assigned this Aug 31, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
…p#487) (lablup#505)

Add an index-aware GPU device and stream surface to mlxcel-core so work can
be placed on a specific GPU on a multi-GPU node. This is the foundation for
single-node tensor parallelism (epic lablup#486); the boolean device API only
targeted GPU index 0.

The new bridge entry points use MLX's portable device API
(device_count(DeviceType::gpu) and the Device(DeviceType, index)
constructor), so the same code compiles on Metal (one GPU), a CUDA
multi-GPU host, and a CPU-only build with no CUDA headers:

- gpu_device_count() -> i32 (clamped to >= 1)
- new_stream_on_gpu_index / set_default_device_index /
  new_thread_local_stream_gpu_index
- copy_array_to_stream: cross-device materialize via mlx::core::copy,
  consumed by the multi-GPU runtime in sub-issue lablup#488

Safe wrappers in streams.rs (new_stream_on_gpu, set_default_gpu_device,
new_thread_local_stream_on_gpu) validate the index against the device count
and return a typed InvalidGpuIndex error.

Integration: the server startup banner and the CLI model-load path both log
the detected GPU count, exercising the new API in a real code path before
sub-issue lablup#488 lands.

Tests cover device-count >= 1, stream creation on every valid index,
out-of-range rejection, the typed error message, and default-device and
thread-local validation. A cuda-gated test runs a trivial op on a
non-default GPU index, compiled only under the cuda feature.

Verified on macOS/Metal: build, 12 streams tests, clippy, and fmt all
green. The cuda build path and the multi-GPU runtime are validated in
sub-issue lablup#488 on CUDA hardware.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers priority:high High priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant