Install Tensorflow on Mac m1

Since most developers use Mac m1 and all ML developers use TensorFlow why does TensorFlow now work on Mac M1 never?

The so-called latest instructions from Apple don't work on Mac OS Ventura 13.2.1 M1

https://developer.apple.com/metal/tensorflow-plugin/

Get this error:

RuntimeError: module compiled against API version 0x10 but this version of numpy is 0xf RuntimeError: module compiled against API version 0x10 but this version of numpy is 0xf ImportError: numpy.core._multiarray_umath failed to import ImportError: numpy.core.umath failed to import Traceback (most recent call last): File "/Users/thomasmann/Downloads/junk.py", line 1, in <module> import tensorflow as tf File "/Users/thomasmann/miniconda3/lib/python3.10/site-packages/tensorflow/init.py", line 37, in <module> from tensorflow.python.tools import module_util as _module_util File "/Users/thomasmann/miniconda3/lib/python3.10/site-packages/tensorflow/python/init.py", line 42, in <module> from tensorflow.python import data File "/Users/thomasmann/miniconda3/lib/python3.10/site-packages/tensorflow/python/data/init.py", line 21, in <module> from tensorflow.python.data import experimental File "/Users/thomasmann/miniconda3/lib/python3.10/site-packages/tensorflow/python/data/experimental/init.py", line 96, in <module> from tensorflow.python.data.experimental import service File "/Users/thomasmann/miniconda3/lib/python3.10/site-packages/tensorflow/python/data/experimental/service/init.py", line 419, in <module> from tensorflow.python.data.experimental.ops.data_service_ops import distribute File "/Users/thomasmann/miniconda3/lib/python3.10/site-packages/tensorflow/python/data/experimental/ops/data_service_ops.py", line 22, in <module> from tensorflow.python.data.experimental.ops import compression_ops File "/Users/thomasmann/miniconda3/lib/python3.10/site-packages/tensorflow/python/data/experimental/ops/compression_ops.py", line 16, in <module> from tensorflow.python.data.util import structure File "/Users/thomasmann/miniconda3/lib/python3.10/site-packages/tensorflow/python/data/util/structure.py", line 22, in <module> from tensorflow.python.data.util import nest File "/Users/thomasmann/miniconda3/lib/python3.10/site-packages/tensorflow/python/data/util/nest.py", line 34, in <module> from tensorflow.python.framework import sparse_tensor as _sparse_tensor File "/Users/thomasmann/miniconda3/lib/python3.10/site-packages/tensorflow/python/framework/sparse_tensor.py", line 24, in <module> from tensorflow.python.framework import constant_op File "/Users/thomasmann/miniconda3/lib/python3.10/site-packages/tensorflow/python/framework/constant_op.py", line 25, in <module> from tensorflow.python.eager import execute File "/Users/thomasmann/miniconda3/lib/python3.10/site-packages/tensorflow/python/eager/execute.py", line 21, in <module> from tensorflow.python.framework import dtypes File "/Users/thomasmann/miniconda3/lib/python3.10/site-packages/tensorflow/python/framework/dtypes.py", line 34, in <module> _np_bfloat16 = _pywrap_bfloat16.TF_bfloat16_type() TypeError: Unable to convert function return value to a Python type! The signature was () -> handle

I updated numpy. This code runs fine on Debian. Here it latest error:

2023-03-21 12:44:36.438409: W tensorflow&#x2F;core&#x2F;framework&#x2F;op_kernel.cc:1830] OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id: 0x127460260
2023-03-21 12:44:36.438442: W tensorflow&#x2F;core&#x2F;framework&#x2F;op_kernel.cc:1830] OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id: 0x127460260
---------------------------------------------------------------------------
NotFoundError                             Traceback (most recent call last)
Cell In[1], line 124
    116 # Train and eval the model
    117 model.compile(
    118     optimizer=tf.keras.optimizers.Adam(learning_rate=3e-5, epsilon=1e-08),
    119     loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    120     metrics=[&#039;acc&#039;],
    121 )
--&gt; 124 model.fit(
    125     train_dataset,
    126     epochs=EPOCHS,
    127     steps_per_epoch=train_size,  
    128     validation_data=test_dataset,
    129     validation_steps=test_size
    130 )
    133 loss, eval_accuracy = model.evaluate(test_dataset)
    134 print("Loss: {}\t Test Accuracy: {}".format(loss, eval_accuracy))

File ~&#x2F;miniconda3&#x2F;lib&#x2F;python3.10&#x2F;site-packages&#x2F;keras&#x2F;utils&#x2F;traceback_utils.py:70, in filter_traceback.&lt;locals&gt;.error_handler(*args, **kwargs)
     67     filtered_tb = _process_traceback_frames(e.__traceback__)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---&gt; 70     raise e.with_traceback(filtered_tb) from None
     71 finally:
     72     del filtered_tb

File ~&#x2F;miniconda3&#x2F;lib&#x2F;python3.10&#x2F;site-packages&#x2F;tensorflow&#x2F;python&#x2F;eager&#x2F;execute.py:52, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     50 try:
     51   ctx.ensure_initialized()
---&gt; 52   tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
     53                                       inputs, attrs, num_outputs)
     54 except core._NotOkStatusException as e:
     55   if name is not None:

NotFoundError: Graph execution error:

Detected at node &#039;StatefulPartitionedCall_102&#039; defined at (most recent call last):
    File "&#x2F;Users&#x2F;walkerrowe&#x2F;miniconda3&#x2F;lib&#x2F;python3.10&#x2F;runpy.py", line 196, in _run_module_as_main
      return _run_code(code, main_globals, None,
    File "&#x2F;Users&#x2F;walkerrowe&#x2F;miniconda3&#x2F;lib&#x2F;python3.10&#x2F;runpy.py", line 86, in _run_code
      exec(code, run_globals)
    

Hello! It took me a very long time to come up with a solution for installing Tensorflow on an Apple Mac M1, but i finally did it. I have written instructions, so that others maybe do not have to go through this tedious frustration ;). My approach is different to the mentioned one, as I already have Anaconda installed and I did not want to delete it.

Prerequesites:

  • python installed, preferably between 3.8 and 3.10
  • anaconda installed

Steps:

  • create new environment in anaconda navigator, with python version 3.9 (I called the environment tf)
  • activate the terminal of the environment
  • write conda install -c apple tensorflow-deps
  • write conda install pip in terminal
  • figure out where the pip executable is (in my case it is /Users/Alexander/opt/anaconda3/envs/tf/bin/pip)
  • now to the crucial part: the newest releases don't really work in my experience. Therefore previous versions are installed. Creds to @dweilert.
  • write "/Users/Alexander/opt/anaconda3/envs/tf/bin/pip install "tensorflow-macos==2.9"" in terminal
  • write "/Users/Alexander/opt/anaconda3/envs/tf/bin/pip install "tensorflow-metal==0.5"" in terminal

That should be it. I hope I could help.

@ThomasMann, sorry you had to go through so much trouble. There were couple of issues.

RuntimeError: module compiled against API version 0x10 but this version of numpy is 0xf RuntimeError: module compiled against API version 0x10 but this version of numpy is 0xf ImportError: 

This was a numpy mismatch issue between the Conda env and the numpy used to build TF env. If you update the numpy

pip install --upgrade numpy

It should resolve the issue. We have support for venv now as well

python3 -m venv ~&#x2F;venv-metal
source ~&#x2F;venv-metal&#x2F;bin&#x2F;activate
python -m pip install -U pip

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

should work as well.

This issue was due to regression in Keras Optimizers support in Base tensor flow. We have fixed this issue in our latest release of Tensorflow v2.12. Can you please update your TF to latest and see if this resolves your issue?

Install Tensorflow on Mac m1
 
 
Q