Repository navigation
Closed
Labels
area:coremlxcel-core: MLX FFI, primitives, KV cache, layerspriority:highHigh prioritystatus:doneCompletedtype:enhancementNew features, capabilities, or significant additions
Description
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:3780set_default_device(bool gpu)->Device::gpusrc/lib/mlxcel-core/cpp/mlx_cxx_bridge.cpp:18new_stream_on_device(bool gpu)->Device::gpusrc/lib/mlxcel-core/cpp/mlx_cxx_bridge.cpp:38thread-local stream bound toDevice::gpu- Rust FFI in
src/lib/mlxcel-core/src/lib.rs:45,54,1708exposes 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 examplecudaGetDeviceCount) under thecudabuild; 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 existingnew_thread_local_stream_gpu).copy_array_to_stream(array, stream) -> MlxArray(orcopy_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
cudafeature; 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
cudabuilds. -
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
cudaGetDeviceCountis not reachable through the current build, add the minimal CUDA runtime link needed under thecudafeature 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).
Metadata
Metadata
Assignees
Labels
area:coremlxcel-core: MLX FFI, primitives, KV cache, layerspriority:highHigh prioritystatus:doneCompletedtype:enhancementNew features, capabilities, or significant additions
Activity
feat: device-index-aware device/stream bindings in mlxcel-core (#487) (…
feat: device-index-aware device/stream bindings in mlxcel-core (lablu…