Error when importing TensorFlow

I installed TensorFlow according to this link: https://developer.apple.com/metal/tensorflow-plugin/

Mac M1 Pro
macOS Monterey 12.3

When I try to import tensorflow: import tensorflow

I get the following error:

RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd

RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd

ImportError: numpy.core._multiarray_umath failed to import

ImportError: numpy.core.umath failed to import

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/__init__.py", line 37, in <module>

    from tensorflow.python.tools import module_util as _module_util

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/__init__.py", line 42, in <module>

    from tensorflow.python import data

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/data/__init__.py", line 21, in <module>

    from tensorflow.python.data import experimental

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/data/experimental/__init__.py", line 95, in <module>

    from tensorflow.python.data.experimental import service

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/data/experimental/service/__init__.py", line 387, in <module>

    from tensorflow.python.data.experimental.ops.data_service_ops import distribute

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/data/experimental/ops/data_service_ops.py", line 23, in <module>

    from tensorflow.python.data.experimental.ops import compression_ops

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/data/experimental/ops/compression_ops.py", line 16, in <module>

    from tensorflow.python.data.util import structure

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/data/util/structure.py", line 22, in <module>

    from tensorflow.python.data.util import nest

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/data/util/nest.py", line 36, in <module>

    from tensorflow.python.framework import sparse_tensor as _sparse_tensor

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/framework/sparse_tensor.py", line 24, in <module>

    from tensorflow.python.framework import constant_op

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/framework/constant_op.py", line 25, in <module>

    from tensorflow.python.eager import execute

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/eager/execute.py", line 23, in <module>

    from tensorflow.python.framework import dtypes

  File "/Users/fryderykkogl/miniforge3/lib/python3.9/site-packages/tensorflow/python/framework/dtypes.py", line 29, in <module>

    _np_bfloat16 = _pywrap_bfloat16.TF_bfloat16_type()

TypeError: Unable to convert function return value to a Python type! The signature was

	() -> handle


Any ideas on what I can do? I couldn't find this error anywhere

This happens as a result of Numpy version mismatch between what's installed in the environment and the numpy version used to build TF. Can you make sure that tensorflow-deps version 2.7.0 or 2.8.0 matches the tensorflow-macos version you are trying to install? You will have to force-reinstall the dependencies as mentioned on the instructions:

When upgrading to new base TensorFlow version, we recommend:

# uninstall existing tensorflow-macos and tensorflow-metal
python -m pip uninstall tensorflow-macos
python -m pip uninstall tensorflow-metal
# Upgrade tensorflow-deps
conda install -c apple tensorflow-deps --force-reinstall
# or point to specific conda environment
conda install -c apple tensorflow-deps --force-reinstall -n my_env 

I have the same question, how to solve it?

I had a same problem that the NumPy version mismatches, spending days to figure it out. I solved this with this link below: https://caffeinedev.medium.com/how-to-install-tensorflow-on-m1-mac-8e9b91d93706

I hope this can help you

Uninstall Tensorflow and Metal

python -m pip uninstall tensorflow-macos
python -m pip uninstall tensorflow-metal

(Leave out -n my_env if you are already in target environment)

conda install -c apple tensorflow-deps==2.10.0 --force-reinstall -n my_env 

(2.10.0 is current version, update this for future versions.)

Re-install Tensorflow and Metal

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

I had the same problem. What solved it for me was to use python -m pip instead of pip install and do sanity checks after every line, ie which python and python -c 'import tensorflow' at earliest point possible.

Example, starting from a new environment:

conda create --name test-env -y python=3.8
conda activate test-env
which python # make note and compare for later

conda install -c apple -y tensorflow-deps
which python # should remain same

python -m pip install tensorflow-macos # notice the python -m pip variant
which python # should remain same
python -c 'import tensorflow'  # an empty output should indicate successful import

python -m pip install tensorflow-metal
which python # should remain same
python -c 'import tensorflow'  # should still work
...

Continue like this for every conda and pip package.

For me, the python path changed after a call to pip install ..., which ultimately caused the issue while importing tensorflow.

For me it was solved when ensuring that the dependencies and tensor flow itself are the same version! Right now that's actually 2.9.0 (tensorflow 2.10.0 is being installed even though no 2.10. version of the dependencies exists.

  1. Delete miniconda
  2. Follow these steps almost fully: https://developer.apple.com/metal/tensorflow-plugin/
  3. However, ensure that you type:

conda install -c apple tensorflow-deps=2.9.0  python -m pip install tensorflow-macos==2.9.0

Hope this helps someone...

Tl;dr: Apple's installation instructions appear to be broken. If you're happy with TensorFlow 2.9.0, set up a Miniconda environment, and run:

conda install -c apple tensorflow-deps ==2.9.0
python -m pip install tensorflow-macos==2.9.0
python -m pip install tensorflow-metal==0.5.0

The latest version of TensorFlow that's usable with Metal acceleration is TensorFlow 2.10.0. For this, set up a Miniforge environment, and run:

conda install -c apple tensorflow-deps ==2.10.0
python -m pip install tensorflow-macos==2.10.0
python -m pip install tensorflow-metal==0.6.0

10 months later, this appears to still be broken. Here's what happens for me.

  1. I set up a fresh Conda environment with https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh.
  2. conda search -c apple tensorflow-deps --info suggests that the latest version of tensorflow-deps is 2.10.0. But it requires exactly NumPy 1.23.2:
tensorflow-deps 2.10.0 0
------------------------
file name   : tensorflow-deps-2.10.0-0.tar.bz2
name        : tensorflow-deps
version     : 2.10.0
build       : 0
build number: 0
size        : 2 KB
license     : Apache2
subdir      : osx-arm64
url         : https://conda.anaconda.org/apple/osx-arm64/tensorflow-deps-2.10.0-0.tar.bz2
md5         : 93ab322b1297b4fde0dd1f7071a51652
timestamp   : 2022-09-15 21:15:58 UTC
dependencies:
  - grpcio >=1.37.0,<2.0
  - h5py >=3.6.0,<3.7
  - numpy >=1.23.2,<1.23.3
  - protobuf >=3.19.1,<3.20
  - python

Unfortunately, NumPy 1.23.2 is not actually available from the official Anaconda repository:

$ conda search numpy --info
...
numpy 1.23.1 py39h42add53_0
---------------------------
file name   : numpy-1.23.1-py39h42add53_0.conda
name        : numpy
version     : 1.23.1
build       : py39h42add53_0
build number: 0
size        : 11 KB
license     : BSD-3-Clause
subdir      : osx-arm64
url         : https://repo.anaconda.com/pkgs/main/osx-arm64/numpy-1.23.1-py39h42add53_0.conda
md5         : 836f58d0108ccd6d56f3325ef870717e
timestamp   : 2022-08-02 09:40:05 UTC
dependencies:
  - blas * openblas
  - libcxx >=12.0.0
  - libopenblas >=0.3.20,<1.0a0
  - numpy-base 1.23.1 py39hadd41eb_0
  - python >=3.9,<3.10.0a0


numpy 1.23.3 py310h220015d_0
----------------------------
file name   : numpy-1.23.3-py310h220015d_0.conda
name        : numpy
version     : 1.23.3
build       : py310h220015d_0
build number: 0
size        : 11 KB
license     : BSD-3-Clause
subdir      : osx-arm64
url         : https://repo.anaconda.com/pkgs/main/osx-arm64/numpy-1.23.3-py310h220015d_0.conda
md5         : 9db0662d7f232643e2e98d3464e39d1e
timestamp   : 2022-10-14 18:54:16 UTC
dependencies:
  - blas * openblas
  - libcxx >=12.0.0
  - libopenblas >=0.3.20,<1.0a0
  - numpy-base 1.23.3 py310h742c864_0
  - python >=3.10,<3.11.0a0
...
  1. I try installing tensorflow-deps anyway with conda install -c apple tensorflow-deps. This installs tensorflow-deps 2.9.0 and NumPy 1.22.3.
  2. Next, tensorflow-macos. https://pypi.org/project/tensorflow-macos/#history suggests that the latest version is 2.11.0. Sure enough, when I run python -m pip install tensorflow-macos, this is what gets installed. I guess this is unsurprising, because tensorflow-deps doesn't seem to show up in pip's package list, so there's no way for tensorflow-macos to depend on the correct version.
  3. Ok, so let's try installing a version of tensorflow-macos that matches tensorflow-deps: python -m pip install tensorflow-macos==2.9.0.
  4. python -m pip install tensorflow-metal. This installs 0.7.0, the latest version.
  5. python train.py (the train script given at https://developer.apple.com/metal/tensorflow-plugin/). Nope:
$ python train.py
Traceback (most recent call last):
  File ".../train.py", line 1, in <module>
    import tensorflow as tf
  File ".../miniconda/lib/python3.10/site-packages/tensorflow/__init__.py", line 443, in <module>
    _ll.load_library(_plugin_dir)
  File ".../handwriting/miniconda/lib/python3.10/site-packages/tensorflow/python/framework/load_library.py", line 151, in load_library
    py_tf.TF_LoadLibrary(lib)
tensorflow.python.framework.errors_impl.NotFoundError: dlopen(.../miniconda/lib/python3.10/site-packages/tensorflow-plugins/libmetal_plugin.dylib, 0x0006): symbol not found in flat namespace '__ZN3tsl8internal10LogMessage16VmoduleActivatedEPKci'
  1. The error seems to be related to tensorflow-metal. Maybe we need an older version? https://pypi.org/project/tensorflow-metal/#history suggests that the version corresponding to tensorflow-macos 2.9.0 should be tensorflow-metal 0.5.0. So: python -m pip uninstall tensorflow-metal; python -m pip install tensorflow-metal==0.5.0
  2. train.py now runs successfully. (But it doesn't seem to be using the ANE? Is this intentional? At least according to powermetrics, which seems to be the only way to measure ANE activity (https://eclecticlight.co/2022/03/30/the-hunt-for-the-m1s-neural-engine); powermetrics reports CPU at about 3 W and GPU at 7 W but ANE at 0 mW. It does at least run faster than if I only install tensorflow-macos, taking about 2 minutes per epoch vs 8 minutes per epoch.)
  3. But what if I want to use the latest version? If I set up conda-forge, which does have NumPy 1.23.2, in a fresh environment from https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-arm64.sh. When I conda install -c apple tensorflow-deps, version 2.10.0 is now installed as expected. python -m pip install tensorflow-macos still installs 2.11.0, so I have to force it with python -m pip install tensorflow-macos==2.10.0. Then python -m pip install tensorflow-metal, which installs 0.7.0, as before. But train.py is broken like before:
$ python train.py
Traceback (most recent call last):
  File ".../train.py", line 1, in <module>
    import tensorflow as tf
  File ".../miniconda_forge/lib/python3.10/site-packages/tensorflow/__init__.py", line 439, in <module>
    _ll.load_library(_plugin_dir)
  File ".../miniconda_forge/lib/python3.10/site-packages/tensorflow/python/framework/load_library.py", line 151, in load_library
    py_tf.TF_LoadLibrary(lib)
tensorflow.python.framework.errors_impl.NotFoundError: dlopen(.../miniconda_forge/lib/python3.10/site-packages/tensorflow-plugins/libmetal_plugin.dylib, 0x0006): symbol not found in flat namespace '__ZN3tsl8internal10LogMessage16VmoduleActivatedEPKci'
  1. So again, I have to manually specify the version of tensorflow-metal: python -m pip install tensorflow-metal==0.6.0. Now train.py works.

So I think the full list of problems here are:

  1. There appears to be no CI of the instructions at https://developer.apple.com/metal/tensorflow-plugin/.
  2. tensorflow-macos does not check to see whether it's installed with the same version of tensorflow-deps.
  3. tensorflow-metal similarly does not depend on the specific version of tensorflow-macos that is required.

Hi all,

Do you know if there is a clean release for tf 2.11 of the devs ?

Official link https://developer.apple.com/metal/tensorflow-plugin/ is not updated properly since v2.9 0.5.0.

Would be nice to properly document those in the official page so we don't need to search in many area, tutorial, etc.. to be sure tf:2.X goes with tf-devs:0.Y.y and tf-metal:0.Z.z

After searching the solution for a whole night, i have tried to follow the each step from the youtube channel and websites, but sadly none of them actually works. I think the main issue is we are dealing with mutiple python versions, and the packages are not downloaded in the correct path with corresponding python. for exmample, the miniconda from apple official website contian a 3.10 ... version of python, if default python version is 3.9... . in my case, the 'python -m pip install -U pip' downloaded packages assosiated with python 3.9, which is not matching the python version in the conda environment.

however, i am new to this, this is just my thoughts. i cant guarantee 100% correct. but it is worth to try. it explains the error tensorflow not found.

Error when importing TensorFlow
 
 
Q