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

在搭载Intel芯片的Mac上如何获取Torch MPS设备名称?

获取搭载AMD GPU的Intel芯片Mac的MPS设备名称

首先需要明确:MPS(Metal Performance Shaders)是Apple Silicon(M系列芯片)专属的PyTorch加速后端,搭载AMD GPU的Intel芯片Mac无法使用MPS,PyTorch在这类设备上采用的是Metal后端。目前PyTorch没有提供类似torch.cuda.get_device_name()的直接API来获取AMD GPU名称,但可以通过以下两种方式实现:


方法1:调用系统命令行工具提取

通过system_profiler命令获取系统显示设备信息,从中提取AMD GPU名称:

import subprocess

def get_amd_gpu_name():
    profiler_output = subprocess.run(
        ["system_profiler", "SPDisplaysDataType"],
        capture_output=True,
        text=True
    ).stdout
    for line in profiler_output.splitlines():
        if "Chipset Model" in line and "AMD" in line:
            return line.split(":")[-1].strip()
    return "未检测到AMD GPU"

print(get_amd_gpu_name())

方法2:通过PyObjC调用系统框架获取

借助PyObjC直接调用Apple的IOKit框架读取硬件信息,需先安装依赖:

pip install pyobjc

实现代码:

from Foundation import NSBundle
import objc

# 加载IOKit系统框架
IOKit = NSBundle.bundleWithIdentifier_('com.apple.IOKit')
objc.loadBundleFunctions(IOKit, globals(), [
    ("IOServiceMatching", b"^{__CFDictionary=}*"),
    ("IOServiceGetMatchingServices", b"I^{__CFDictionary=}^{io_iterator_t="),
    ("IOIteratorNext", b"I^{io_iterator_t="),
    ("IORegistryEntryCreateCFProperty", b"^{__CFType=}^{io_registry_entry_t=}^{__CFString=}^{IOOptionBits="),
    ("IOObjectRelease", b"vI"),
])

def get_amd_gpu_name():
    iterator = objc.objc_object(c_void_p=None)
    IOServiceGetMatchingServices(0, IOServiceMatching("IOPCIDevice"), iterator.address())
    gpu_name = None
    while True:
        device = IOIteratorNext(iterator)
        if device == 0:
            break
        # 读取GPU型号属性
        model = IORegistryEntryCreateCFProperty(device, "model", None, 0)
        if model:
            model_str = str(model)
            if "AMD" in model_str:
                gpu_name = model_str
            objc.objc_release(model)
        IOObjectRelease(device)
    IOObjectRelease(iterator)
    return gpu_name if gpu_name else "未检测到AMD GPU"

print(get_amd_gpu_name())

注意事项

  • 可通过torch.backends.metal.is_available()验证当前PyTorch是否支持Intel Mac+AMD GPU的Metal加速(需PyTorch 2.0+、macOS 12.3+)。
  • 以上方法获取的是系统硬件层面的AMD GPU名称,并非PyTorch内部的设备标识(PyTorch未提供该场景下的专属API)。

内容的提问来源于stack exchange,提问作者someguyinafloppyhat

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 21:06:21