sgl-project/sglang · error · ValueError
MlxTensorView requires a Torch MPS tensor, got {owner.device
Error message
MlxTensorView requires a Torch MPS tensor, got {owner.device} What it means
MlxTensorView is a lifetime-bound zero-copy view and only exists for MPS-backed torch tensors. Its constructor detaches the tensor and requires owner.device.type == 'mps', otherwise raising with the offending device.
Source
Thrown at python/sglang/srt/utils/tensor_bridge.py:118
class MlxTensorView:
"""A lifetime-bound, zero-copy MLX view of a Torch MPS tensor.
The view deliberately retains a detached Torch tensor *and* the imported
MLX array. Holding only the array is insufficient: a later parameter
replacement or garbage collection could invalidate the borrowed storage
while MLX still has a lazy graph referring to it. This class is intended
for immutable inference weights; construct a new view after replacing the
source storage.
"""
__slots__ = ("torch_tensor", "array")
def __init__(self, tensor: torch.Tensor, *, synchronize: bool = True):
with _BRIDGE_LOCK:
owner = tensor.detach()
if owner.device.type != "mps":
raise ValueError(
f"MlxTensorView requires a Torch MPS tensor, got {owner.device}"
)
if synchronize:
torch.mps.synchronize()
self.torch_tensor = owner
self.array = _torch_to_mlx(owner, copy=False, synchronize=False)
@classmethod
def _from_synchronized(cls, tensor: torch.Tensor) -> MlxTensorView:
view = object.__new__(cls)
owner = tensor.detach()
if owner.device.type != "mps":
raise ValueError(
f"MlxTensorView requires a Torch MPS tensor, got {owner.device}"
)
view.torch_tensor = owner
view.array = _torch_to_mlx(owner, copy=False, synchronize=False)
return viewView on GitHub (pinned to 0132848349)
Solutions
- Move the tensor to MPS first: MlxTensorView(t.to('mps'))
- Use torch_to_mlx for CPU tensors (it copies) instead of a view
Example fix
# before
view = MlxTensorView(t) # t on cpu
# after
view = MlxTensorView(t.to('mps')) Defensive patterns
Strategy: type-guard
Validate before calling
assert tensor.device.type == "mps", "MlxTensorView needs an MPS tensor"
Type guard
def is_mps(t: torch.Tensor) -> bool:
return t.device.type == "mps" Prevention
- Use the public borrow_torch_tensors/torch_to_mlx instead of constructing views directly
When it happens
Trigger: Constructing MlxTensorView(cpu_tensor) or MlxTensorView(cuda_tensor) directly.
Common situations: Unit tests or helper code constructing views on CPU fixtures; forgetting .to('mps') before wrapping.
Related errors
- SGLANG_USE_MLX requires stable Torch 2.13.x and MLX >= 0.32.
- The MLX tensor bridge supports CPU and MPS tensors, got {ten
- borrow_torch_tensors requires MPS tensors, got {devices}
- SGLANG_USE_MLX requires stable Torch 2.13.x and MLX >= 0.32.
- SGLANG_USE_MLX requires an available PyTorch MPS device
AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28).
Data as JSON: /api/errors/2e1d37be170dc741.
Report an issue: GitHub.