apple / tensorflow_macos

TensorFlow for macOS 11.0+ accelerated using Apple's ML Compute framework.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Apple M1 TensorFlow

Niclas70 opened this issue · comments

Does anyone know if TensorFlow for Mac M1 is, or will be, supported in Anaconda? I.e. possible to install from the Anaconda environment user interface?

I tried installing from GitHub via terminal, but did not succeed.
If possible, it would be great to be able to use Spyder for development.

I was able to set it up by installing from GitHub via terminal by following instructions here: https://medium.com/gft-engineering/macbook-m1-tensorflow-on-jupyter-notebooks-6171e1f48060

Thanks! Looks promising.
Got stuck though on "Step 3: Install Miniforge" doing the final "conda" as it shows

"
Traceback (most recent call last):
File "/usr/local/bin/conda", line 6, in
from pkg_resources import load_entry_point
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/init.py", line 3241, in
@_call_aside
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/init.py", line 3225, in _call_aside
f(*args, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/init.py", line 3254, in _initialize_master_working_set
working_set = WorkingSet._build_master()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/init.py", line 583, in _build_master
ws.require(requires)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/init.py", line 900, in require
needed = self.resolve(parse_requirements(requirements))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/init.py", line 786, in resolve
raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'ruamel.yaml>=0.11.14' distribution was not found and is required by conda
"

will investigate this error further.

These two linked articles might help:
#153

https://github.com/conda-forge/miniforge#miniforge3
Check install instructions in this article for miniforge

Thanks!!! You saved my day. Actually "week", since I've struggled with this several days.

Now I only need yo get used to Jupyter coding. Used to Spyder.
Is it possible to code in Xcode (having access to TensorFlow)?

Here what got so far with Tensorflow 2.5.0

  • Successfully built from source pip package for arm64 native arch
  • Pip install to conda (miniforge3)
  • Got rid of terrible accuracy of LSTM this apple GitHub version of Tensorflow 2.4.1

It was time consuming to get this done.

Steps like in https://www.tensorflow.org/install/source but with important amendments

  1. First you need to install basilisk via Homebrew (use rosetta or arch -x86_64 important) check with uname -m
  2. Then download from clone Tensorflow latest git. Not from apple git clone https://github.com/tensorflow/tensorflow.git
  3. After you need to prepare environment to build this from source, for that we need python 3.8 from Xcode and new env of that where you have to install numpy (I couldn't install from clear venv so, I just used this
    " /bin/bash -c "$(curl -fsSLhttps://raw.githubusercontent.com/apple/tensorflow_macos/master/scripts/download_and_install.sh)" ! double check your python version and arch ( file $(which python3) or file $(which python) in env it should be arm64e for first and arm64 for second respectively) - from here we need numpy to build it from source otherwise you will get errors before build starts
  4. Configure your Tensorflow and choose your new python3.8 env ! other configs I've chosen 'no' enevryware and blank for --configs
  5. Then start building with this flags USE_BAZEL_VERSION=3.7.2 bazel build --jobs 7 --config=macos_arm64 --config=noaws --config=nogcp --config=nohdfs --config=nonccl //tensorflow/tools/pip_package:build_pip_package (flags I get from comment this repo issues I believe)
    jobs stands roughly for cores. its pretty intensive task and takes 3-4 hours with 8 cores working
  6. Finally you can install it on Conda env which you get from this guide or better this article. its important to complete fully <- this guide Bec you can't get dependencies from scratch. You just overwriting Tensorflow using wheel you have built

So u use Xcode python which is arm64 and x86_64 Bazel to build Tensorflow from source and then pip it in Conda env

After all got performance lost like 10% ( in Colab with new 2.5.0. performance is +10%) but accuracy of LSTM in particular (checked in Google Colab before and after) Before was not acceptable at all. And some errors while import Tensorflow
but everything works fine. I don't know how to install this wheel from scratch so) the problem is numpy and some dependencies. May be someone can help (now testing --config=monolithic may be that's the solution)

2021-05-26 15:51:22.533915: E tensorflow/core/lib/monitoring/collection_registry.cc:77] Cannot register 2 metrics with the same name: /tensorflow/core/saved_model/write/count
2021-05-26 15:51:22.533935: E tensorflow/core/lib/monitoring/collection_registry.cc:77] Cannot register 2 metrics with the same name: /tensorflow/core/saved_model/read/count
2021-05-26 15:51:22.533938: E tensorflow/core/lib/monitoring/collection_registry.cc:77] Cannot register 2 metrics with the same name: /tensorflow/core/saved_model/write/api
2021-05-26 15:51:22.533942: E tensorflow/core/lib/monitoring/collection_registry.cc:77] Cannot register 2 metrics with the same name: /tensorflow/core/saved_model/read/api
2021-05-26 15:51:23.089208: E tensorflow/core/lib/monitoring/collection_registry.cc:77] Cannot register 2 metrics with the same name: /tensorflow/core/saved_model/write/count
2021-05-26 15:51:23.089227: E tensorflow/core/lib/monitoring/collection_registry.cc:77] Cannot register 2 metrics with the same name: /tensorflow/core/saved_model/read/count
2021-05-26 15:51:23.089230: E tensorflow/core/lib/monitoring/collection_registry.cc:77] Cannot register 2 metrics with the same name: /tensorflow/core/saved_model/write/api
2021-05-26 15:51:23.089235: E tensorflow/core/lib/monitoring/collection_registry.cc:77] Cannot register 2 metrics with the same name: /tensorflow/core/saved_model/read/api

Hey @Niclas70, I wrote the guide on issue #153, I hope it helps!

You should be able to use ANY editor to write Python code, even in this unique environment. The only point you'll want to make is that whatever you're using to run your code, should use the Python you specially installed with Miniforge -- not the default Python, not Anacona Python, not Homebrew's Python.

For example for me, if I'm in the terminal, I can run ~/miniforge3/bin/python myscript.py and it'll run myscript.py using miniforge's Python, which is where I installed my Apple TensorFlow. And I can fact check that since if I run ~/miniforge3/bin/python on my computer and then run import tensorflow, I get the Apple TensorFlow. Of course, your paths may change, but this practice should let you use any editor you'd like for creating code.