pytorch在Mac上实现像cuda一样的加速

1. 参考:https://developer.apple.com/metal/pytorch/ 

2. 具体实现:
  2.1 Requirements
      Mac M芯片或者AMD的GPU
      macOS 12.3 or later
      Python 3.7 or later
      Xcode command-line tools: xcode-select --install
  2.2  准备anaconda或者miniconda或者自带的pip3

    conda安装:
conda install pytorch torchvision torchaudio -c pytorch-nightly
    pip安装:
pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu

3. 应用检验:
import torch
if torch.backends.mps.is_available():
    mps_device = torch.device("mps")
    x = torch.ones(1, device=mps_device)
    print (x)
else:
    print ("MPS device not found.")

# if not torch.backends.mps.is_available():
#     if not torch.backends.mps.is_built():
#         print("MPS not available because the current PyTorch install was not "
#               "built with MPS enabled.")
#     else:
#         print("MPS not available because the current MacOS version is not 12.3+ "
#               "and/or you do not have an MPS-enabled device on this machine.")
# mps_device = torch.device("mps")
# x = torch.ones(5, device=mps_device)
# # 或者 x = torch.ones(5, device="mps")
# y = x * 2
# model = YourFavoriteNet()
# model.to(mps_device)
# pred = model(x)
posted @ 2024-04-01 21:52  王力艳  阅读(340)  评论(0)    收藏  举报