本文來自:
https://pytorch.org/docs/stable/notes/mps.html
https://pytorch.ac.cn/docs/stable/notes/mps.html
MPS 后端
mps
設備支持 在使用 Metal 編程框架的 MacOS 設備上,進行高性能 GPU 訓練。
它引入了新的設備,將機器學習計算圖和原語映射到 Metal Performance Shaders 圖框架和 Metal Performance Shaders 框架提供的經過優化的內核上。
新的 MPS 后端擴展了 PyTorch 生態系統,并為現有腳本提供在 GPU 上設置和運行操作的功能。
要開始使用,只需將您的張量和模塊移動到 mps
設備。
# Check that MPS is available
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.")else:mps_device = torch.device("mps")# Create a Tensor directly on the mps devicex = torch.ones(5, device=mps_device)# Orx = torch.ones(5, device="mps")# Any operation happens on the GPUy = x * 2# Move your model to mps just like any other devicemodel = YourFavoriteNet()model.to(mps_device)# Now every call runs on the GPUpred = model(x)
2024-07-16(二)