1919# Options
2020# RUN_TESTS: Run the unit tests e.g. 0 or 1.
2121# PY_VERSION: Version of Python to be used, must be at least 3.9
22- # STARTUP_FLAGS: Any flags given to bazel on startup
22+ # STARTUP_FLAGS: Any flags given to baze on startup
2323# TF_VERSION: Tensorflow version to use or "nightly".
24- # For cross-compiling with Apple Silicon for Mac Intel, use
25- # mac-intel-crosscompile.
26- # Tests will not work when cross-compiling (obviously).
27- # FULL_COMPILATION: If 1, compile all parts of TF-DF. This may take a long time.
24+ # MAC_INTEL_CROSSCOMPILE: Cross-compile for Intel Macs
25+ # FULL_COMPILATION: If 1, compile all parts of TF-DF. This may take a long time.
2826#
2927# Usage example
3028#
@@ -48,58 +46,72 @@ function is_macos() {
4846 [[ " ${PLATFORM} " == " darwin" ]]
4947}
5048
49+ if is_macos; then
50+ # 1. Check if the current shell is Bash
51+ if [[ $SHELL != " /bin/bash" ]]; then
52+ echo " Error: This script requires Bash. Please run it in a Bash shell."
53+ exit 1 # Exit with an error code
54+ fi
55+ if ! command -v gsort & > /dev/null; then
56+ echo " Error: GNU coreutils is not installed. Please install it with 'brew install coreutils'"
57+ exit 1
58+ fi
59+ if ! command -v ggrep & > /dev/null; then
60+ echo " Error: GNU grep is not installed. Please install it with 'brew install grep'"
61+ exit 1
62+ fi
63+ if ! command -v gsed & > /dev/null; then
64+ echo " Error: GNU sed is not installed. Please install it with 'brew install gnu-sed'"
65+ exit 1
66+ fi
67+ # Tensorflow requires the use of GNU realpath instead of MacOS realpath.
68+ # See https://github.com/tensorflow/tensorflow/issues/60088#issuecomment-1499766349
69+ export PATH=" /opt/homebrew/opt/coreutils/libexec/gnubin:$PATH "
70+ export PATH=" /opt/homebrew/opt/grep/libexec/gnubin:$PATH "
71+ export PATH=" /opt/homebrew/opt/gnu-sed/libexec/gnubin:$PATH "
72+ fi
73+
5174# Install Pip dependencies
5275${PYTHON} -m ensurepip --upgrade || true
53- ${PYTHON} -m pip install pip setuptools --upgrade
54- ${PYTHON} -m pip install numpy pandas scikit-learn
76+ ${PYTHON} -m pip install -q pip setuptools --upgrade
77+ ${PYTHON} -m pip install -q numpy pandas scikit-learn
5578
5679# Install Tensorflow at the chosen version.
5780if [ ${TF_VERSION} == " nightly" ]; then
58- ${PYTHON} -m pip install tf-nightly tf-keras-nightly --force-reinstall
81+ ${PYTHON} -m pip install -q tf-nightly tf-keras-nightly --force-reinstall
5982 TF_MINOR=" nightly"
6083else
61- ${PYTHON} -m pip install tensorflow==${TF_VERSION} --force-reinstall
84+ ${PYTHON} -m pip install -q tensorflow==${TF_VERSION} --force-reinstall
85+ TF_MINOR=$( echo $TF_VERSION | grep -oP ' [0-9]+\.[0-9]+' )
6286 if [[ $TF_VERSION == * " rc" * ]]; then
63- ${PYTHON} -m pip install tf-keras --pre --upgrade
87+ # Unfortunately, the TF-Keras RC may not match the TensorFlow RC (e.g. for 2.16).
88+ # Just install the latest one that's available and hope for the best.
89+ ${PYTHON} -m pip install -q tf-keras --pre --upgrade
6490 else
65- TF_MINOR=$( echo $TF_VERSION | grep -oP ' [0-9]+\.[0-9]+' )
66- ${PYTHON} -m pip install tf-keras==${TF_MINOR}
91+ ${PYTHON} -m pip install -q tf-keras==${TF_MINOR}
6792 fi
6893fi
69- ext=" "
70-
71- pip list
7294
73- if is_macos; then
74- ext=' ""'
75- # Tensorflow requires the use of GNU realpath instead of MacOS realpath.
76- # See https://github.com/tensorflow/tensorflow/issues/60088#issuecomment-1499766349
77- # If missing, install coreutils via homebrew: `brew install coreutils`
78- export PATH=" /opt/homebrew/opt/coreutils/libexec/gnubin:$PATH "
79- fi
95+ ${PYTHON} -m pip list
8096
8197# For Tensorflow versions > 2.15, apply compatibility patches.
8298
8399if [[ ${TF_MINOR} != " 2.15" ]]; then
84- sed -i $ext " s/tensorflow:tf.patch/tensorflow:tf-216.patch/" WORKSPACE
85- sed -i $ext " s/# patch_args = \[\" -p1\" \],/patch_args = \[\" -p1\" \],/" third_party/yggdrasil_decision_forests/workspace.bzl
86- sed -i $ext " s/# patches = \[\" \/\/third_party\/ yggdrasil_decision_forests:ydf.patch\ " \],/patches = \[\ " \/\/third_party\/yggdrasil_decision_forests:ydf.patch\ " \],/" third_party/yggdrasil_decision_forests/workspace.bzl
100+ sed -i " s/tensorflow:tf.patch/tensorflow:tf-216.patch/" WORKSPACE
101+ sed -i " s/# patch_args = \[\" -p1\" \],/patch_args = \[\" -p1\" \],/" third_party/yggdrasil_decision_forests/workspace.bzl
102+ sed -i ' s/# patches = \["@ydf \/\/yggdrasil_decision_forests:ydf.patch"\],/patches = \["\/\/third_party\/yggdrasil_decision_forests:ydf.patch"\],/' third_party/yggdrasil_decision_forests/workspace.bzl
87103fi
88104
89105# Get the commit SHA
90106short_commit_sha=$( ${PYTHON} -c ' import tensorflow as tf; print(tf.__git_version__)' | tail -1)
91- if is_macos; then
92- short_commit_sha=$( echo $short_commit_sha | perl -nle ' print $& while m{(?<=-g)[0-9a-f]*$}g' )
93- else
94- short_commit_sha=$( echo $short_commit_sha | grep -oP ' (?<=-g)[0-9a-f]*$' )
95- fi
107+ short_commit_sha=$( echo $short_commit_sha | grep -oP ' (?<=-g)[0-9a-f]*$' )
96108echo " Found tensorflow commit sha: $short_commit_sha "
97109commit_slug=$( curl -s " https://api.github.com/repos/tensorflow/tensorflow/commits/$short_commit_sha " | grep " sha" | head -n 1 | cut -d ' "' -f 4)
98110# Update TF dependency to the chosen version
99- sed -E -i $ext " s/strip_prefix = \" tensorflow-2\.[0-9]+\.[0-9]+(-rc[0-9]+)?\" ,/strip_prefix = \" tensorflow-${commit_slug} \" ,/" WORKSPACE
100- sed -E -i $ext " s|\" https://github.com/tensorflow/tensorflow/archive/v.+\.zip\" |\" https://github.com/tensorflow/tensorflow/archive/${commit_slug} .zip\" |" WORKSPACE
111+ sed -E -i " s/strip_prefix = \" tensorflow-2\.[0-9]+\.[0-9]+(-rc[0-9]+)?\" ,/strip_prefix = \" tensorflow-${commit_slug} \" ,/" WORKSPACE
112+ sed -E -i " s|\" https://github.com/tensorflow/tensorflow/archive/v.+\.zip\" |\" https://github.com/tensorflow/tensorflow/archive/${commit_slug} .zip\" |" WORKSPACE
101113prev_shasum=$( grep -A 1 -e " strip_prefix.*tensorflow-" WORKSPACE | tail -1 | awk -F ' "' ' {print $2}' )
102- sed -i $ext " s/sha256 = \" ${prev_shasum} \" ,//" WORKSPACE
114+ sed -i " s/sha256 = \" ${prev_shasum} \" ,//" WORKSPACE
103115
104116# Get build configuration for chosen version.
105117TENSORFLOW_BAZELRC=" tensorflow_bazelrc"
@@ -121,14 +133,18 @@ else
121133 FLAGS=" ${FLAGS} --config=linux"
122134fi
123135
124- if [ ${TF_VERSION } == " mac-intel-crosscompile " ]; then
136+ if [ ${MAC_INTEL_CROSSCOMPILE } == 1 ]; then
125137 TFDF_TMPDIR=" ${TMPDIR} tf_dep"
126138 rm -rf ${TFDF_TMPDIR}
127139 mkdir -p ${TFDF_TMPDIR}
128140 # Download the Intel CPU Tensorflow package
129- pip download --no-deps --platform=macosx_10_15_x86_64 --dest=$TFDF_TMPDIR tensorflow
130- unzip -q $TFDF_TMPDIR /tensorflow* -d $TFDF_TMPDIR
131-
141+ if [ ${TF_VERSION} == " nightly" ]; then
142+ ${PYTHON} -m pip download --no-deps --platform=macosx_10_15_x86_64 --dest=$TFDF_TMPDIR tf-nightly
143+ unzip -q $TFDF_TMPDIR /tf_nightly* -d $TFDF_TMPDIR
144+ else
145+ ${PYTHON} -m pip download --no-deps --platform=macosx_10_15_x86_64 --dest=$TFDF_TMPDIR tensorflow
146+ unzip -q $TFDF_TMPDIR /tensorflow* -d $TFDF_TMPDIR
147+ fi
132148 # Find the path to the pre-compiled version of TensorFlow installed in the
133149 # "tensorflow" pip package.
134150 SHARED_LIBRARY_DIR=$( readlink -f $TFDF_TMPDIR /tensorflow)
@@ -171,7 +187,7 @@ STARTUP_FLAGS="${STARTUP_FLAGS} --bazelrc=${TENSORFLOW_BAZELRC}"
171187#
172188# FLAGS="$FLAGS --config=rbe_cpu_linux --config=tensorflow_testing_rbe_linux --config=rbe_linux_py3"
173189
174- if [ ${TF_VERSION } == " mac-intel-crosscompile " ]; then
190+ if [ ${MAC_INTEL_CROSSCOMPILE } == 1 ]; then
175191 # Using darwin_x86_64 fails here, tensorflow expects "darwin".
176192 FLAGS=" ${FLAGS} --cpu=darwin --apple_platform_type=macos"
177193fi
0 commit comments