7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Apple M4 MacでTensorFlowが爆速!GPU有効化etcで機械学習が10倍速くなる方法を解説

Last updated at Posted at 2025-09-02

はじめに

GMOコネクトの永田です。

業務で機械学習にかかわるタイミングがあり、手元のPCがMacBook Pro(Apple M4 Max, 36GB)でしたので、GPUを有効にするとどの程度機械学習が早くなるのか実測してみました。
TensorflowのGPU有効化の手順は割と情報があったのですが、実測値がまとまっているサイトが少なかったため、実際に測定してまとめようと思いました。

まとめ

  • MacOS上のTensorflowでGPUを有効化する場合、Python3.12以下を利用する必要がある
    • tensorflow-macos tensorflow-metal をpip installするだけでGPU利用になる
  • GPU有効化と @tf.function で桁違いに(10x)速くなる

Mac上でのTensorflow GPU利用時のポイント

最初、Pythonを最新(記事執筆時点)の3.13で試していたのですが、インストールに失敗していました。

$  python --version
Python 3.13.7
$ python3.13 -m venv venv
$ source ./venv/bin/activate
$ python -m pip install tensorflow-macos tensorflow-metal
ERROR: Could not find a version that satisfies the requirement tensorflow-macos (from versions: none)
ERROR: No matching distribution found for tensorflow-macos

Python3.13リリースされたの結構前よね?って思いながら調べていましたが、結論だけ書くとPython3.12までダウングレードする必要がありました。

Python3.12にダウングレードしたら、以下ですんなりと動作しました。

$ python3.12 --version
Python 3.12.11

$ python -m pip install tensorflow-macos tensorflow-metal

$ python -c "import tensorflow as tf; print(tf.config.list_physical_devices())"
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'), PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

また、version指定無しでインストールされたtensorflowが tensorflow-2.16.2 だったため、ライブラリも古めなものが必要そうです。(記事執筆時点でtensorflowの最新は2.20.0)

GPUを有効化しての実測

今回、実測のサンプルとしてTensorflowチュートリアルからDCGANを持ってきました。

なおこのサンプルは、@tf.function を使っているため、@tf.function あり・なし合わせて、4パターンを試してみます。

なお、GPU有効化後、CPUのみにするのも簡単に変更できます。

# Notice the use of `tf.function`
# This annotation causes the function to be "compiled".
@tf.function
def train_step(images, generator, discriminator, generator_optimizer, discriminator_optimizer, noise_dim, batch_size):
    """Executes one training step."""
    with tf.device('/CPU:0'): # <--ここを /GPU:0 にするか tf.device を指定しないとGPU利用
        noise = tf.random.normal([batch_size, noise_dim])

        with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
          # (略)

計測結果: MacBook Pro(Apple M4 Max, 36GB)

リソース @tf.function OFF @tf.function ON
CPU 130〜170sec/1epoch 60〜70sec/1epoch
GPU 30〜40sec/1epoch 8〜20sec/1epoch
  • GPUを利用している様子
    スクリーンショット 2025-09-01 15.41.25.png

桁違いに(10x)速くなっています!
これで快適なdebugができそうですね😊

(再掲)まとめ

  • MacOS上のTensorflowでGPUを有効化する場合、Python3.12以下を利用する必要がある
    • tensorflow-macos tensorflow-metal をpip installするだけでGPU利用になる
  • GPU有効化と @tf.function で桁違いに(10x)速くなる

弊社では、機械学習・AI・LLMなどを使ったサービスの開発や技術支援をはじめ、幅広い支援を行っておりますので、何かありましたらお気軽にお問合せください。

お問合せ: https://gmo-connect.jp/contactus/

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?