TensorFlow on Apple M1 with 27130 bus error python

I'm having error on the TensorFlow example given here: https://www.tensorflow.org/tutorials/quickstart/beginner with setup given here: https://developer.apple.com/metal/tensorflow-plugin/

-> % python Python 3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 06:05:16) [Clang 12.0.1 ] on darwin Type "help", "copyright", "credits" or "license" for more information.

import tensorflow as tf print("TensorFlow version:", tf.version)

TensorFlow version: 2.9.2

mnist = tf.keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 model = tf.keras.models.Sequential([

... tf.keras.layers.Flatten(input_shape=(28, 28)), ... tf.keras.layers.Dense(128, activation='relu'), ... tf.keras.layers.Dropout(0.2), ... tf.keras.layers.Dense(10) ... ]) Metal device set to: Apple M1

systemMemory: 16.00 GB maxCacheSize: 5.33 GB

2022-10-14 12:48:09.694477: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support. 2022-10-14 12:48:09.695175: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: ) [1] 27130 bus error python

Same problem here:

Metal device set to: Apple M1 Max

systemMemory: 64.00 GB
maxCacheSize: 24.00 GB

2022-10-14 23:13:24.991991: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2022-10-14 23:13:24.992109: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
zsh: bus error  python run_tensorflow_model.py -c test -dt mixed

Disabling the eager execution:

from tensorflow.python.framework.ops import disable_eager_execution
disable_eager_execution()

helps to move forward a little, until the next issue:

2022-10-14 23:20:22.256440: W tensorflow/python/util/util.cc:368] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.
2022-10-14 23:20:28.551177: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2022-10-14 23:20:28.551210: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
2022-10-14 23:20:28.614180: W tensorflow/core/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz
2022-10-14 23:20:28.616659: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:113] Plugin optimizer for device_type GPU is enabled.
2022-10-14 23:20:28.695796: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:113] Plugin optimizer for device_type GPU is enabled.
zsh: bus error  python run_tensorflow_model.py -c test -dt mixed

I managed to solve this by installing Miniforge3 from https://github.com/conda-forge/miniforge for arm64 and following Apple instructions at https://developer.apple.com/metal/tensorflow-plugin/:

# Install Miniforge3
chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh
bash Miniforge3-MacOSX-arm64.sh
conda --version
# Create venv
conda create -n ml-env python=3.9
conda activate ml-env
# Install TensorFlow
conda install -c apple tensorflow-deps
python -m pip install tensorflow-macos
python -m pip install tensorflow-metal
# Test the installation [https://developer.apple.com/metal/tensorflow-plugin/]
python test_tf.py

test_tf.py:

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=True)
model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5, batch_size=64)
TensorFlow on Apple M1 with 27130 bus error python
 
 
Q