Installing TensorFlow on an Intel Mac with AMD Radeon R9 M395 2 GB GPU and CPU versus GPU testing
I have a 2015 Intel based iMac available and wonder how good its AMD Radeon GPU would be on machine learning tasks compared to its CPU. To fin out, we install Tensorflow on this iMac and run some CPU & GPU tests.
1. Download my Yaml file for conda
I constructed a conda environment yaml file and published it here on Github for you to download. You can also download from Github this TensorFlowMNistTest.py script, (which is an evolved version of the code on this page). You will also need to download the EagerCpuGpuConfig.py script which contains a class to make your Gpu visible or invisible to ML computations.
2. How I generated the yaml file
I generated this yaml file as follows:
$ sw_vers -productVersion11.6
¹
From the very beginning of https://developer.apple.com/metal/tensorflow-plugin/ we learn we need at least OS X 12 for this tensorflow installation to work.
So I upgraded to 12.5.1 by using Apples software update (started by pressing the left upper apple icon on my screen and then ‘about this Mac’ and ‘software update’’ and following instructions. OS X version 12 is the minimum needed for the sequel. This then gave me:
$ sw_vers -productVersion12.5.1
Ok. https://developer.apple.com/metal/tensorflow-plugin/ Step 1, mentions to use a virtual environment via venv, but we will do this via conda.
So go create a conda envrionment that will shield installed python libraries from your general system as well as from other conda projects where you may prefer or have to stick to other version of the same libraries:
$ conda create --name TensorFlowMacIntel python=3
$ conda activate TensorFlowMacIntel
$ (TensorFlowMacIntel) python -m pip install --upgrade pip
$ (TensorFlowMacIntel) python -m pip install tensorflow_datasets
$ (TensorFlowMacIntel) python -m pip install protobuf==3.19.4
¹
You need to have at least python 3.8 so put python=3.8 if the above would have given you something lower than that.
The same page, in Step 2, says that |”If using conda environment built against pre-macOS 11 SDK use, you should add the SYSTEM_VERSION_COMPAT=0 prefix”. I did build my conda env with OS X 11, and if I leave out the prefix, I get an error, so lets try this then:
$ (TensorFlowMacIntel) python -m pip install -U pip
$ (TensorFlowMacIntel) SYSTEM_VERSION_COMPAT=0 python -m pip install tensorflow-macos
this gave:
(TensorFlowMacIntel) Dogmatix:TensorFlowM1 peter$ SYSTEM_VERSION_COMPAT=0 python -m pip install tensorflow-macosCollecting tensorflow-macosDownloading tensorflow_macos-2.9.2-cp38-cp38-macosx_11_0_x86_64.whl (237.5 MB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 237.5/237.5 MB 4.1 MB/s eta 0:00:00Requirement already satisfied: absl-py>=1.0.0 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from tensorflow-macos) (1.2.0)
...
Requirement already satisfied: oauthlib>=3.0.0 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.10,>=2.9->tensorflow-macos) (3.2.0)Installing collected packages: tensorflow-macosWARNING: The scripts estimator_ckpt_converter, import_pb_to_tensorboard, saved_model_cli, tensorboard, tf_upgrade_v2, tflite_convert, toco and toco_from_protos are installed in '/Library/Frameworks/Python.framework/Versions/3.8/bin' which is not on PATH.Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.Successfully installed tensorflow-macos-2.9.2(TensorFlowMacIntel) Dogmatix:TensorFlowM1 peter$
Ok, so this worked out fine.
The same page, in Step 3 mentions does not mention setting the same environment variable, but I needed to, so I did:
(TensorFlowMacIntel) Dogmatix:TensorFlowM1 peter$ SYSTEM_VERSION_COMPAT=0 python -m pip install tensorflow-metalCollecting tensorflow-metalDownloading tensorflow_metal-0.5.0-cp38-cp38-macosx_11_0_x86_64.whl (1.5 MB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.5/1.5 MB 8.7 MB/s eta 0:00:00Requirement already satisfied: wheel~=0.35 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from tensorflow-metal) (0.37.1)Requirement already satisfied: six~=1.15.0 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from tensorflow-metal) (1.15.0)Installing collected packages: tensorflow-metalSuccessfully installed tensorflow-metal-0.5.0(TensorFlowMacIntel) Dogmatix:TensorFlowM1 peter$
After the above, I then, from within the TensorFlowMacIntel, exported the environment to a yaml file via:
$ (TensorFlowMacIntel) conda env export > TensorFlowMacIntel.yml
3. Testing the GPU versus the CPU
If you did not install the above mentioned tensorflow-macos or tensorflow-macos in section 2, and you run the below test, you will see that it does not detect the GPU and so the code still runs, regardless wether you disable or enable the GPU, but it will only execute on the CPU, so you will not notice any significant change in performance.
When you now download, from my GitHub repo, the 2 python files TensorFlowMNistTest.py and EagerCpuGpuConfig.py, you can run the following
$ (TensorFlowMacIntel) python TensorFlowMNistTest.py
To see how much faster tensorflow runs on a GPU than on a CPU we ran 4 tests, where we varied switching the GPU on (by making it visible/invisible to tensorflow code) or off and switching a mode called ‘eager’ on or off. You switch these on line 9 via setting the booleans
eagerly=False
disable_gpu_visibility=False
in the file TensorFlowMNistTest.py. (Best leave disable_cpu_visibility to False though.)
The summary² of the output of the 4 runs is:
---
disable_cpu_visibility = False
disable_gpu_visibility = False
eagerly = False
0:02:51.319595
---
disable_cpu_visibility = False
disable_gpu_visibility = False
eagerly = True
0:04:50.110275
---
disable_cpu_visibility = False
disable_gpu_visibility = True
eagerly = False
0:07:30.041130
---
disable_cpu_visibility = False
disable_gpu_visibility = True
eagerly = True
0:10:38.660720
---
Putting these results in a google spreadsheet, together with the experiments we did earlier on an Apple M1 Mac and on a Wintel machine with NVIDIA Quadro T1000 GPU, gives:
and making bar graphs of it
shows that for the older iMac, the GPU helps by a factor 3 in non-eager mode and 5 in eager mode. It’s quite similar, on average, for the newer machines (running OS X or Windows), where mapping the tensorflow computations on the GPU rather than CPU gave a speedup of about a factor 4.
Footnotes:
- The installation of tensorflow_datasets gave an error reporting a conflict with the, today, latest version of protobuf.
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.tensorflow 2.9.2 requires protobuf<3.20,>=3.9.2, but you have protobuf 3.20.1 which is incompatible.tensorboard 2.9.1 requires protobuf<3.20,>=3.9.2, but you have protobuf 3.20.1 which is incompatible.
This could be fixed by installing the version 3.19.4 of protobuf i.o. the latest, as follows:
$ (TensorFlowMacIntel) python -m pip install protobuf==3.19.4(TensorFlowMacIntel) Dogmatix:TensorFlowM1 peter$ python -m pip install protobuf==3.19.4
Collecting protobuf==3.19.4
Downloading protobuf-3.19.4-cp38-cp38-macosx_10_9_x86_64.whl (961 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 961.7/961.7 kB 7.5 MB/s eta 0:00:00
Installing collected packages: protobuf
Attempting uninstall: protobuf
Found existing installation: protobuf 3.20.1
Uninstalling protobuf-3.20.1:
Successfully uninstalled protobuf-3.20.1
Successfully installed protobuf-3.19.4
2. The logs of the 4 runs are on GitHub as Maci7AmdRadeon*Eager*Gpu.log
Written by Peter Sels on Sep 10th 2022.