94057c3d3e
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
229 lines
6.6 KiB
Python
229 lines
6.6 KiB
Python
# Copied and adapted from: https://github.com/vllm-project/vllm-metal
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
"""Tensor bridge between MLX and PyTorch.
|
|
|
|
Provides zero-copy conversion when possible using Apple Silicon's unified memory.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from functools import lru_cache
|
|
from typing import TYPE_CHECKING, Literal
|
|
|
|
import torch
|
|
|
|
from sglang.srt.environ import envs
|
|
|
|
if TYPE_CHECKING:
|
|
import mlx.core as mx
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_MLX_AVAILABLE: bool = False
|
|
try:
|
|
import mlx.core as mx # noqa: F811
|
|
|
|
_MLX_AVAILABLE = True
|
|
except ImportError:
|
|
pass
|
|
|
|
|
|
def is_mlx_available() -> bool:
|
|
"""Return True when the ``mlx`` package can be imported."""
|
|
return _MLX_AVAILABLE
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def use_mlx() -> bool:
|
|
"""Return True when the user opted-in via ``SGLANG_USE_MLX=1`` **and** MLX is importable."""
|
|
return bool(envs.SGLANG_USE_MLX.get()) and _MLX_AVAILABLE
|
|
|
|
|
|
# MPS has a 4GB (2^32 bytes) limit for MPSTemporaryNDArray allocations.
|
|
# Metal may allocate multiple temporary buffers internally, so we use a
|
|
# conservative threshold of 1GB to avoid hitting the limit.
|
|
# See: https://github.com/anthropics/vllm-metal/issues/43
|
|
_MPS_SAFE_SIZE_BYTES = 1 << 30 # 1GB
|
|
|
|
# MLX to PyTorch dtype mapping
|
|
# TODO(perf): float64 is CPU-only in MLX (see ml-explore/mlx#1843).
|
|
# When the target device is GPU/MPS we should auto-downcast float64 → float32
|
|
# to avoid a runtime error; when the target is CPU we can keep float64.
|
|
# For now float64 is omitted from the mapping so it hits the ValueError
|
|
# fallback in mlx_to_torch().
|
|
MLX_TO_TORCH_DTYPE = (
|
|
{
|
|
mx.float32: torch.float32,
|
|
mx.float16: torch.float16,
|
|
mx.bfloat16: torch.bfloat16,
|
|
mx.int32: torch.int32,
|
|
mx.int64: torch.int64,
|
|
mx.int16: torch.int16,
|
|
mx.int8: torch.int8,
|
|
mx.uint8: torch.uint8,
|
|
mx.bool_: torch.bool,
|
|
}
|
|
if _MLX_AVAILABLE
|
|
else {}
|
|
)
|
|
|
|
# PyTorch to MLX dtype mapping
|
|
TORCH_TO_MLX_DTYPE = {v: k for k, v in MLX_TO_TORCH_DTYPE.items()}
|
|
|
|
|
|
def get_torch_device() -> torch.device:
|
|
"""Get the PyTorch device for Metal/MPS.
|
|
|
|
Returns:
|
|
torch.device for MPS if available, else CPU
|
|
"""
|
|
if torch.backends.mps.is_available():
|
|
return torch.device("mps")
|
|
return torch.device("cpu")
|
|
|
|
|
|
def _get_tensor_size_bytes(array: mx.array) -> int:
|
|
"""Calculate the size of an MLX array in bytes.
|
|
|
|
Args:
|
|
array: MLX array
|
|
|
|
Returns:
|
|
Size in bytes
|
|
"""
|
|
return array.size * array.dtype.size
|
|
|
|
|
|
def _is_safe_for_mps(array: mx.array) -> bool:
|
|
"""Check if an array is safe to transfer to MPS without hitting size limits.
|
|
|
|
MPS has a 4GB limit for MPSTemporaryNDArray, but Metal may allocate
|
|
multiple temporary buffers internally. We use a conservative threshold.
|
|
|
|
Args:
|
|
array: MLX array to check
|
|
|
|
Returns:
|
|
True if safe to transfer to MPS, False if should stay on CPU
|
|
"""
|
|
return _get_tensor_size_bytes(array) < _MPS_SAFE_SIZE_BYTES
|
|
|
|
|
|
def torch_to_mlx(tensor: torch.Tensor) -> mx.array:
|
|
"""Convert PyTorch tensor to MLX array.
|
|
|
|
Uses numpy as an intermediate to enable zero-copy on unified memory.
|
|
|
|
Args:
|
|
tensor: PyTorch tensor (can be on any device)
|
|
|
|
Returns:
|
|
MLX array with the same data
|
|
"""
|
|
# Move to CPU if on MPS for numpy conversion
|
|
if tensor.device.type != "cpu":
|
|
tensor = tensor.cpu()
|
|
|
|
tensor = tensor.detach()
|
|
|
|
# Note: numpy does not support bfloat16.
|
|
if tensor.dtype == torch.bfloat16:
|
|
return mx.array(tensor)
|
|
|
|
return mx.array(tensor.numpy())
|
|
|
|
|
|
# TODO(perf): accept a list/batch of arrays and convert them in one pass
|
|
# to reduce the Python ↔ MLX round-trip overhead.
|
|
def mlx_to_torch(
|
|
array: mx.array,
|
|
device: torch.device | Literal["mps", "cpu"] | None = None,
|
|
already_contiguous: bool = False,
|
|
) -> torch.Tensor:
|
|
"""Convert MLX array to PyTorch tensor.
|
|
|
|
Uses numpy as an intermediate to enable zero-copy on unified memory.
|
|
|
|
Args:
|
|
array: MLX array
|
|
device: Target PyTorch device (default: MPS if available)
|
|
already_contiguous: Skip contiguity check if array is known contiguous
|
|
|
|
Returns:
|
|
PyTorch tensor with the same data
|
|
"""
|
|
if device is None:
|
|
device = get_torch_device()
|
|
elif isinstance(device, str):
|
|
device = torch.device(device)
|
|
|
|
# Use memoryview for zero-copy conversion (bypasses numpy for bfloat16)
|
|
# reference: https://github.com/ml-explore/mlx/issues/403
|
|
torch_dtype = MLX_TO_TORCH_DTYPE.get(array.dtype)
|
|
if torch_dtype is not None:
|
|
if already_contiguous:
|
|
# Fast path: skip contiguity check, single eval
|
|
mx.eval(array)
|
|
buffer = memoryview(array)
|
|
else:
|
|
# MLX views / non-contiguous arrays expose a non-contiguous buffer (or
|
|
# sometimes no usable buffer), which `torch.frombuffer` can't consume.
|
|
# Make contiguous first, then eval once
|
|
array = mx.contiguous(array)
|
|
mx.eval(array)
|
|
buffer = memoryview(array)
|
|
|
|
tensor = torch.frombuffer(buffer, dtype=torch_dtype).reshape(array.shape)
|
|
else:
|
|
# Fallback to numpy path for unsupported dtypes
|
|
raise ValueError(f"Unsupported MLX dtype: {array.dtype}")
|
|
|
|
# Move to target device, but check for MPS size limits first
|
|
if device.type == "mps":
|
|
if _is_safe_for_mps(array):
|
|
tensor = tensor.to(device)
|
|
else:
|
|
# Large tensor - keep on CPU to avoid MPS 4GB limit crash
|
|
# See: https://github.com/anthropics/vllm-metal/issues/43
|
|
logger.debug(
|
|
"Tensor too large for MPS (%d bytes > %d limit), keeping on CPU",
|
|
_get_tensor_size_bytes(array),
|
|
_MPS_SAFE_SIZE_BYTES,
|
|
)
|
|
elif device.type != "cpu":
|
|
tensor = tensor.to(device)
|
|
|
|
return tensor
|
|
|
|
|
|
def sync_mlx() -> None:
|
|
"""Synchronize MLX operations.
|
|
|
|
Call this before converting MLX arrays to ensure all operations complete.
|
|
"""
|
|
# Prefer an explicit MLX barrier when available; otherwise force evaluation.
|
|
# `mx.eval([])` is a no-op, so we evaluate a tiny scalar as a safe fallback.
|
|
try:
|
|
mx.synchronize()
|
|
except (AttributeError, TypeError):
|
|
mx.eval(mx.array(0, dtype=mx.int32))
|
|
|
|
|
|
def sync_torch() -> None:
|
|
"""Synchronize PyTorch MPS operations.
|
|
|
|
Call this before converting PyTorch tensors to ensure all operations complete.
|
|
"""
|
|
if torch.backends.mps.is_available():
|
|
torch.mps.synchronize()
|
|
|
|
|
|
__all__ = [
|
|
"is_mlx_available",
|
|
"use_mlx",
|
|
"mlx_to_torch",
|
|
"torch_to_mlx",
|
|
"get_torch_device",
|
|
]
|