# 运行时错误：MPS设备上未分配占位符存储

- **ID:** `huggingface/mps-fp16-cast-error`
- **领域:** huggingface
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

模型权重或张量在Apple MPS后端上被转换为float16，但MPS不完全支持FP16，导致分配失败。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| transformers>=4.30.0 | active | — | — |
| torch>=2.0.0 | active | — | — |
| macOS>=13.0 | active | — | — |

## 解决方案

1. ```
   Load model with torch_dtype=torch.float32 and use model.to('mps') without half precision.
   ```
2. ```
   Set environment variable PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0 to disable MPS memory allocation optimization.
   ```
3. ```
   Use CPU fallback by setting device='cpu' and use float16 with CPU if memory is a concern.
   ```

## 无效尝试

- **** — MPS backend has limited FP16 support; explicit FP16 casting triggers the error. (95% 失败率)
- **** — Half-precision conversion on MPS is not fully implemented and causes memory allocation errors. (90% 失败率)
