Skip to content

TensorFlow on Apple Silicon

Problem

Installing TensorFlow on Apple Silicon (M1/M2) Macs presents several challenges:

  1. Outdated information: Many online tutorials recommend outdated methods like Miniforge instead of the now M1-native Conda
  2. Package confusion: Users encounter errors like "No matching distribution found for tensorflow" when using standard installation methods
  3. Unclear packages: The relationship between tensorflow-macos, tensorflow-deps, and tensorflow-metal is confusing
  4. Version compatibility: Finding compatible versions of TensorFlow and its dependencies can be difficult

The root issue is that official TensorFlow distributions don't natively support Apple Silicon, requiring Apple's specialized packages for optimal performance.

Solution

As of late 2023, Apple and TensorFlow have simplified the installation process:

bash
# Create and activate a new conda environment
conda create -n tf-metal python=3.11
conda activate tf-metal

# Install TensorFlow and Metal plugin
pip install tensorflow
pip install tensorflow-metal

INFO

For TensorFlow 2.13 and later, you no longer need tensorflow-macos or tensorflow-deps - the standard tensorflow package works natively on Apple Silicon.

Conda Environment Method

For a more reproducible setup, use a Conda environment YAML file:

yaml
name: tf-metal
channels:
  - conda-forge
  - nodefaults
dependencies:
  - python=3.11
  - pip
  - pip:
    - tensorflow
    - tensorflow-metal

Create the environment with:

bash
conda env create -f tf-metal-arm64.yaml

Verifying Installation

Test your installation with this script:

python
import tensorflow as tf

# Check versions and devices
print(f"TensorFlow Version: {tf.__version__}")
print(f"GPU Devices: {tf.config.list_physical_devices('GPU')}")

# Simple computation to verify GPU acceleration
tf.debugging.set_log_device_placement(True)
a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
c = tf.matmul(a, b)
print(c)

Alternative: Virtual Environment Method

If you prefer not to use Conda:

bash
# Create virtual environment
python -m venv ~/tensorflow-metal
source ~/tensorflow-metal/bin/activate

# Install TensorFlow and Metal plugin
pip install tensorflow
pip install tensorflow-metal

Explanation

Package Purpose

  • tensorflow-macos: Apple's optimized TensorFlow distribution for macOS (required for versions < 2.13)
  • tensorflow-metal: Plugin that enables TensorFlow to use Apple's Metal framework for GPU acceleration
  • tensorflow-deps: Dependency package containing required libraries (largely obsolete for TensorFlow 2.13+)

Version Compatibility

WARNING

TensorFlow and tensorflow-metal versions must be compatible. Check the tensorflow-metal PyPI page for version matching.

Common compatible version pairs:

  • TensorFlow 2.13+ + tensorflow-metal 1.0+
  • TensorFlow 2.12 + tensorflow-metal 0.8
  • TensorFlow 2.11 + tensorflow-metal 0.7
  • TensorFlow 2.10 + tensorflow-metal 0.6

Troubleshooting Common Issues

Issue: "platform is already registered with name: METAL"

bash
# Fix: Install in user directory instead of system
pip uninstall tensorflow-macos tensorflow-metal
pip install --user tensorflow-macos tensorflow-metal

Issue: Optimizer compatibility problems with TF 2.11+

python
# Use legacy optimizer
model.compile(
    optimizer=tf.keras.optimizers.legacy.Adam(learning_rate=1e-3),
    loss=loss_fn,
    metrics=["accuracy"]
)

Issue: NumPy compatibility errors

bash
# Downgrade protobuf if needed
pip install protobuf==3.20.*

Performance Testing

Use Apple's CIFAR test to verify GPU acceleration:

python
import tensorflow as tf

cifar = tf.keras.datasets.cifar100
(x_train, y_train), (x_test, y_test) = cifar.load_data()
model = tf.keras.applications.ResNet50(
    include_top=True,
    weights=None,
    input_shape=(32, 32, 3),
    classes=100,
)

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)
model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5, batch_size=64)

Run with TF_MLC_LOGGING=1 python script.py to see detailed Metal performance logging.

Conclusion

The installation process for TensorFlow on Apple Silicon has significantly improved:

  1. For TensorFlow 2.13+, use standard pip install tensorflow followed by tensorflow-metal
  2. Always verify version compatibility between TensorFlow and tensorflow-metal
  3. Use Conda environments for better dependency management
  4. Test with the CIFAR example to confirm GPU acceleration

Apple Silicon now provides excellent TensorFlow performance with proper configuration, often matching or exceeding Intel-based Mac performance for machine learning workloads.