1313# See the License for the specific language governing permissions and
1414# limitations under the License.
1515
16-
17-
1816# Build and test TF-DF.
1917# Options
2018# RUN_TESTS: Run the unit tests e.g. 0 or 1.
2119# PY_VERSION: Version of Python to be used, must be at least 3.9
2220# STARTUP_FLAGS: Any flags given to bazel on startup
2321# 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.
22+ # MAC_INTEL_CROSSCOMPILE: Cross-compile for Intel Macs
23+ # FULL_COMPILATION: If 1, compile all parts of TF-DF. This may take a long time.
2824#
2925# Usage example
3026#
@@ -48,58 +44,72 @@ function is_macos() {
4844 [[ " ${PLATFORM} " == " darwin" ]]
4945}
5046
47+ if is_macos; then
48+ # 1. Check if the current shell is Bash
49+ if [[ $SHELL != " /bin/bash" ]]; then
50+ echo " Error: This script requires Bash. Please run it in a Bash shell."
51+ exit 1 # Exit with an error code
52+ fi
53+ if ! command -v gsort & > /dev/null; then
54+ echo " Error: GNU coreutils is not installed. Please install it with 'brew install coreutils'"
55+ exit 1
56+ fi
57+ if ! command -v ggrep & > /dev/null; then
58+ echo " Error: GNU grep is not installed. Please install it with 'brew install grep'"
59+ exit 1
60+ fi
61+ if ! command -v gsed & > /dev/null; then
62+ echo " Error: GNU sed is not installed. Please install it with 'brew install gnu-sed'"
63+ exit 1
64+ fi
65+ # Tensorflow requires the use of GNU realpath instead of MacOS realpath.
66+ # See https://github.com/tensorflow/tensorflow/issues/60088#issuecomment-1499766349
67+ export PATH=" /opt/homebrew/opt/coreutils/libexec/gnubin:$PATH "
68+ export PATH=" /opt/homebrew/opt/grep/libexec/gnubin:$PATH "
69+ export PATH=" /opt/homebrew/opt/gnu-sed/libexec/gnubin:$PATH "
70+ fi
71+
5172# Install Pip dependencies
5273${PYTHON} -m ensurepip --upgrade || true
53- ${PYTHON} -m pip install pip setuptools --upgrade
54- ${PYTHON} -m pip install numpy pandas scikit-learn
74+ ${PYTHON} -m pip install -q pip setuptools --upgrade
75+ ${PYTHON} -m pip install -q numpy pandas scikit-learn
5576
5677# Install Tensorflow at the chosen version.
5778if [ ${TF_VERSION} == " nightly" ]; then
58- ${PYTHON} -m pip install tf-nightly tf-keras-nightly --force-reinstall
79+ ${PYTHON} -m pip install -q tf-nightly tf-keras-nightly --force-reinstall
5980 TF_MINOR=" nightly"
6081else
61- ${PYTHON} -m pip install tensorflow==${TF_VERSION} --force-reinstall
82+ ${PYTHON} -m pip install -q tensorflow==${TF_VERSION} --force-reinstall
83+ TF_MINOR=$( echo $TF_VERSION | grep -oP ' [0-9]+\.[0-9]+' )
6284 if [[ $TF_VERSION == * " rc" * ]]; then
63- ${PYTHON} -m pip install tf-keras --pre --upgrade
85+ # Unfortunately, the TF-Keras RC may not match the TensorFlow RC (e.g. for 2.16).
86+ # Just install the latest one that's available and hope for the best.
87+ ${PYTHON} -m pip install -q tf-keras --pre --upgrade
6488 else
65- TF_MINOR=$( echo $TF_VERSION | grep -oP ' [0-9]+\.[0-9]+' )
66- ${PYTHON} -m pip install tf-keras==${TF_MINOR}
89+ ${PYTHON} -m pip install -q tf-keras==${TF_MINOR}
6790 fi
6891fi
69- ext=" "
7092
71- pip list
72-
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
93+ ${PYTHON} -m pip list
8094
8195# For Tensorflow versions > 2.15, apply compatibility patches.
8296
8397if [[ ${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
98+ sed -i " s/tensorflow:tf.patch/tensorflow:tf-216.patch/" WORKSPACE
99+ sed -i " s/# patch_args = \[\" -p1\" \],/patch_args = \[\" -p1\" \],/" third_party/yggdrasil_decision_forests/workspace.bzl
100+ 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
87101fi
88102
89103# Get the commit SHA
90104short_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
105+ short_commit_sha=$( echo $short_commit_sha | grep -oP ' (?<=-g)[0-9a-f]*$' )
96106echo " Found tensorflow commit sha: $short_commit_sha "
97107commit_slug=$( curl -s " https://api.github.com/repos/tensorflow/tensorflow/commits/$short_commit_sha " | grep " sha" | head -n 1 | cut -d ' "' -f 4)
98108# 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
109+ sed -E -i " s/strip_prefix = \" tensorflow-2\.[0-9]+\.[0-9]+(-rc[0-9]+)?\" ,/strip_prefix = \" tensorflow-${commit_slug} \" ,/" WORKSPACE
110+ sed -E -i " s|\" https://github.com/tensorflow/tensorflow/archive/v.+\.zip\" |\" https://github.com/tensorflow/tensorflow/archive/${commit_slug} .zip\" |" WORKSPACE
101111prev_shasum=$( grep -A 1 -e " strip_prefix.*tensorflow-" WORKSPACE | tail -1 | awk -F ' "' ' {print $2}' )
102- sed -i $ext " s/sha256 = \" ${prev_shasum} \" ,//" WORKSPACE
112+ sed -i " s/sha256 = \" ${prev_shasum} \" ,//" WORKSPACE
103113
104114# Get build configuration for chosen version.
105115TENSORFLOW_BAZELRC=" tensorflow_bazelrc"
@@ -121,14 +131,18 @@ else
121131 FLAGS=" ${FLAGS} --config=linux"
122132fi
123133
124- if [ ${TF_VERSION } == " mac-intel-crosscompile " ]; then
134+ if [ ${MAC_INTEL_CROSSCOMPILE } == 1 ]; then
125135 TFDF_TMPDIR=" ${TMPDIR} tf_dep"
126136 rm -rf ${TFDF_TMPDIR}
127137 mkdir -p ${TFDF_TMPDIR}
128138 # 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-
139+ if [ ${TF_VERSION} == " nightly" ]; then
140+ ${PYTHON} -m pip download --no-deps --platform=macosx_10_15_x86_64 --dest=$TFDF_TMPDIR tf-nightly
141+ unzip -q $TFDF_TMPDIR /tf_nightly* -d $TFDF_TMPDIR
142+ else
143+ ${PYTHON} -m pip download --no-deps --platform=macosx_10_15_x86_64 --dest=$TFDF_TMPDIR tensorflow
144+ unzip -q $TFDF_TMPDIR /tensorflow* -d $TFDF_TMPDIR
145+ fi
132146 # Find the path to the pre-compiled version of TensorFlow installed in the
133147 # "tensorflow" pip package.
134148 SHARED_LIBRARY_DIR=$( readlink -f $TFDF_TMPDIR /tensorflow)
@@ -171,7 +185,7 @@ STARTUP_FLAGS="${STARTUP_FLAGS} --bazelrc=${TENSORFLOW_BAZELRC}"
171185#
172186# FLAGS="$FLAGS --config=rbe_cpu_linux --config=tensorflow_testing_rbe_linux --config=rbe_linux_py3"
173187
174- if [ ${TF_VERSION } == " mac-intel-crosscompile " ]; then
188+ if [ ${MAC_INTEL_CROSSCOMPILE } == 1 ]; then
175189 # Using darwin_x86_64 fails here, tensorflow expects "darwin".
176190 FLAGS=" ${FLAGS} --cpu=darwin --apple_platform_type=macos"
177191fi
0 commit comments