Repository navigation
feat: device-index-aware device/stream bindings in mlxcel-core (#487) - #505
Merged
Merged
Conversation
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.
1 of 8 tasks
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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() -> i32viamlx::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_indexvia theDevice(DeviceType, index)constructor.copy_array_to_streamviamlx::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 theextern "C++"block; ergonomic safe wrappers instreams(new_stream_on_gpu,set_default_gpu_device,new_thread_local_stream_on_gpu) validate the index againstgpu_device_count()and return a typedInvalidGpuIndexerror.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
cudaGetDeviceCountfor the device count. MLX at the pinned commit already exposes a portabledevice_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-targetsandcargo fmt --check: clean.The multi-GPU behavior (index
> 0) is not reachable on a single-GPU Metal machine, so index-> 0placement and the cross-GPUcopypath 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; thecudafeature 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
gpu_device_count()returns>= 1on all targets and reports the true count on a multi-GPU CUDA host.>= 1; a stream on each valid index succeeds; an out-of-range index returns the typed error.cudafeature; runs on a multi-GPU CUDA host).gpu_device_count()is surfaced and logged at server and CLI startup.Closes #487