Last active
July 7, 2023 05:29
-
-
Save tsdeng/0b24289cc9e799c2dbe52920c933303d to your computer and use it in GitHub Desktop.
Install tensorflow, tensorflow-text and tensorflow-models on MacOS. The major PITA is tensorflow-text which does not release a wheel for apple silicon. So the script build the wheel from source.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function usage() { | |
cat <<EOF | |
Usage: $(basename $0) [-h|--help] | |
Description: | |
Install tensorflow, tensorflow-text and tf-models-official on macos. | |
Notice tensorflow-addon is deprecated: https://github.com/tensorflow/addons/issues/2807 | |
-h Show help. | |
-v Show tf versions. | |
EOF | |
} | |
function pip_show_versions() { | |
package=$1 | |
echo "===${package} versions===" | |
pip index versions ${package} | |
} | |
function ls_tf_versions() { | |
pip_show_versions "tensorflow" | |
pip_show_versions "tensorflow-metal" | |
pip_show_versions "tf-models-official" | |
echo "===tensorflow-text versions===" | |
curl https://github.com/tensorflow/text/tags 2>/dev/null | grep -oE 'tag/v([^"]*)'| sed 's/tag\///' | sort | uniq | grep "$TF_VERSION" | |
} | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
-h|--help) | |
usage | |
exit 0 | |
;; | |
-v) | |
ls_tf_versions | |
exit 0 | |
;; | |
-*|--*) | |
echo "Unknown option $1" | |
usage | |
exit 1 | |
;; | |
*) # No argument accepted for this script | |
usage | |
exit 1 | |
;; | |
esac | |
done | |
cat <<EOF | |
!!!!!!!!!!!!!!!!!!!!!!!! | |
For latest info, visit https://developer.apple.com/metal/tensorflow-plugin/. | |
!!!!!!!!!!!!!!!!!!!!!!!! | |
EOF | |
: "${PYTHON_VERSION:=3.11}" | |
: "${TF_VERSION:=2.13.0}" # User `pip index versions` to list available versions. | |
# Check packages of the speicified version exist. | |
(curl https://github.com/tensorflow/text/tags 2>/dev/null | grep -oE 'tag/v([^"]*)'| sed 's/tag\///' | sort | uniq | grep "$TF_VERSION" > /dev/null ) || (echo "tensorflow-text does not have tag ${TF_VERSION}."; exit 1) | |
(pip3 index versions tensorflow | grep "Available versions" | grep "${TF_VERSION}" > /dev/null ) || (echo "tensorflow version ${TF_VERSION} not available."; pip3 index versions tensorflow; exit 1) | |
(pip3 index versions tf-models-official | grep "Available versions" | grep "${TF_VERSION}" > /dev/null ) || (echo "tf-models-official version ${TF_VERSION} not available."; pip3 index versions tf-models-official; exit 1) | |
# Check python version is correct | |
INSTALLED_PYTHON_VERSION="$(python -c 'import sys; print(".".join(map(str, sys.version_info[0:2])))')" | |
[[ "$INSTALLED_PYTHON_VERSION" == "$PYTHON_VERSION" ]] || (echo "Wrong python version, expected $PYTHON_VERSION, got $INSTALLED_PYTHON_VERSION." && exit 1) | |
# Make sure bazel is installed | |
(brew list | grep bazelisk > /dev/null) || brew install bazelisk | |
# Install tensorflow first. Should not install tensorflow-text directly because | |
# it pulls in a wrong/older version of tf and will fail the build. | |
pip3 install tensorflow==${TF_VERSION} | |
# Install tensorflow-metal | |
pip3 install tensorflow-metal | |
# Install tensorflow-text | |
TMP_DIR=`mktemp -d` | |
# deletes the temp directory | |
function cleanup { | |
rm -rf "$TMP_DIR" | |
echo "Deleted temp working directory $TMP_DIR" | |
} | |
trap cleanup EXIT | |
pushd ${TMP_DIR} | |
git clone https://github.com/tensorflow/text.git | |
cd text | |
git checkout v${TF_VERSION} | |
bazel clean --expunge # Clear bazel build cache to get rid of any weird issues | |
# caused by previous build with different python version. | |
./oss_scripts/run_build.sh | |
pip3 install ./tensorflow_text*.whl | |
popd ${TMP_DIR} | |
# Install tensorflow-models | |
pip3 install tf-models-official==${TF_VERSION} | |
# All done. | |
echo "Finished installation. Following is a list of tf packages:" | |
pip list | grep -E "^(tensor|tf)" | |
echo "Testing GPU support from tf..." | |
python -c 'import tensorflow as tf;print("GPUs found by tf:", tf.config.list_physical_devices(device_type="GPU"))' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment