You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apple Silicon设备MPS已检测可用但PyTorch模型仍在CPU运行的问题求助

Apple Silicon设备MPS已检测可用但PyTorch模型仍在CPU运行的问题求助

大家好,我最近在Apple Silicon Mac上尝试用MPS(Metal Performance Shaders)运行HuggingFace Transformers的模型,明明系统已经检测到MPS可用了,但模型却始终在CPU上运行,性能拉胯得厉害,想问问有没有朋友遇到过类似问题或者知道怎么解决?

下面是我的模型初始化代码:

def instantiate_model(cfg: dict):
    model_fp = cfg['model']['model_fp']
    batch_size = cfg['model']['batch_size']
    default_threshold = float(cfg['postprocessing']['threshold'])

    # Clear any cached memory
    torch.mps.empty_cache()
    
    # Set device
    if torch.backends.mps.is_available():
        device = torch.device("mps")
        print(f"MPS device found: {device}")
    else:
        device = torch.device("cpu")
        print(f"MPS not available, using: {device}")

    # Load model
    model = AutoModelForTokenClassification.from_pretrained(
        model_fp,
        torch_dtype=torch.float32
    )
    model.eval()
    # 后续代码省略...

我已经确认MPS是可用的,控制台也正常打印出了MPS device found: mps,但模型运行时CPU占用率直接拉满,GPU完全没动静,实在搞不懂哪里出问题了,求各位大佬支招!

备注:内容来源于stack exchange,提问作者Davide Bassi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 13:34:30