Apple Silicon機へのGPU対応PyTorch/TensorFlowのインストール
Apple Silocon機でPytorchおよびTensorFlowをGPU対応させてインストールする方法について.TensorFlowは追加のプラグインをインストールする必要があり,一方PyTorchはその必要はない.
PyTorchインストール
通常通りPyTorchをインストールする.torchvisionとtorchaudioは使う場合は入れる.Pythonは3.9以降であれば良い.
pip install torch torchvision torchaudio
Pytorchバックエンドの名前はmps
になる.以下のコードでGPU対応できているか確認できる.
import torch
if torch.backends.mps.is_available():
device = torch.device("mps")
print("MPS device is available and being used.")
TensorFlowインストール
TensorFlowの場合,通常のtensorflow
パッケージに加えてtensorflow-metal
プラグインが必要.
pip install tensorflow
pip install tensorflow-metal
以下のコードでGPU対応できているか確認できる.
import tensorflow as tf
print(tf.config.list_physical_devices('GPU'))
まとめ
2025年現在ではpytorchもtensorflowも非常に簡単に設定できる.