Skip to content

Commit 632d813

Browse files
Merge pull request #211 from tensorflow:mac-release-updates
PiperOrigin-RevId: 615049008
2 parents f2ed042 + ade64cb commit 632d813

File tree

6 files changed

+85
-82
lines changed

6 files changed

+85
-82
lines changed

.bazelrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ build --strategy=Genrule=standalone
6060
build --action_env TF_NEED_CUDA="0"
6161

6262
# Host specifics options.
63-
build:macos --config=release_base
6463
build:macos --features=-supports_dynamic_linker
6564
# On Intel machines, AVX2 is available
6665
build:macos_intel --config=avx_linux

configure/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def get_tag(self):
6868
if platform.system() == "Darwin":
6969
# Uncomment on of the lines below to adapt the platform string when
7070
# cross-compiling.
71-
# plat = "macosx_12_0_arm64"
71+
plat = "macosx_12_0_arm64"
7272
# plat = "macosx_10_15_x86_64"
7373
pass
7474
return python, abi, plat

documentation/installation.md

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ cd decision-forests
7373

7474
**Optional:** TensorFlow Decision Forests depends on
7575
[Yggdrasil Decision Forests](https://github.com/google/yggdrasil-decision-forests)
76-
. If you want to edit the Yggdrasil code, you can clone the Yggdrasil github and
77-
change the path accordingly in
76+
. If you want to edit the Yggdrasil code, you can clone the Yggdrasil repository
77+
and change the path accordingly in
7878
`third_party/yggdrasil_decision_forests/workspace.bzl`.
7979

8080
**Optional:** If you want to use the docker option, run the
@@ -145,59 +145,38 @@ of python using pyenv by running the following command. See the header of
145145

146146
- XCode command line tools
147147
- Bazel (recommended [Bazelisk](https://github.com/bazelbuild/bazelisk))
148-
- Python >= 3.9
149-
- Git
148+
- Homebrew packages: GNU coreutils, GNU sed, GNU grep
150149
- Pyenv (for building the Pip packages with multiple Python versions)
151150

152-
#### Building / Packaging (Apple CPU)
151+
#### Arm64 CPU
153152

154-
If you have a MacOS machine with Apple CPU, you can build with the following
155-
instructions.
153+
For MacOS systems with ARM64 CPU, follow these steps:
156154

157-
1. Clone the three repositories and adjust paths.
155+
1. Prepare your environment
158156

159157
```
160158
git clone https://github.com/tensorflow/decision-forests.git
161-
git clone https://github.com/google/yggdrasil-decision-forests.git
162-
git clone --branch boost-1.75.0 https://github.com/boostorg/boost.git
163-
(cd boost && git submodule update --init --checkout --force)
164-
# Adjust path TF-DF --> YDF
165-
perl -0777 -i.original -pe 's/ http_archive\(\n name = "ydf",\n urls = \["https:\/\/github.com\/google\/yggdrasil-decision-forests\/archive\/refs\/heads\/main.zip"\],\n strip_prefix = "yggdrasil-decision-forests-main",\n \)/ native.local_repository\(\n name = "ydf",\n path = "..\/yggdrasil-decision-forests",\n \)/igs' decision-forests/third_party/yggdrasil_decision_forests/workspace.bzl
166-
# Adjust path YDF --> Boost
167-
perl -0777 -i.original -pe 's/ new_git_repository\(\n name = "org_boost",\n branch = branch,\n build_file_content = build_file_content,\n init_submodules = True,\n recursive_init_submodules = True,\n remote = "https:\/\/github.com\/boostorg\/boost",\n \)/ native.new_local_repository\(\n name = "org_boost",\n path = "..\/boost",\n build_file_content = build_file_content,\n \)/igs' yggdrasil-decision-forests/third_party/boost/workspace.bzl
168-
```
169-
170-
You may need to adjust the test_bazel.sh script manually to fix the
171-
Tensorflow commit hash, since it is sometimes broken for MacOS builds.
172-
173-
1. (Optional) Create a fresh virtual environment and activate it
174-
175-
```
176159
python3 -m venv venv
177160
source venv/source/activate
178161
```
179162
180-
1. Adjust the TensorFlow dependency for Apple CPUs
181-
182-
```
183-
perl -0777 -i.original -pe 's/tensorflow~=/tensorflow-macos~=/igs' decision-forests/configure/setup.py
184-
```
185-
186-
1. Decide which Python version you want to use and run
163+
1. Decide which Python version and TensorFlow version you want to use and run
187164
188165
```
189166
cd decision-forests
190-
# This will compile with the latest Tensorflow version in the tensorflow-macos repository.
191-
RUN_TESTS=1 PY_VERSION=3.9 TF_VERSION=mac-arm64 ./tools/test_bazel.sh
167+
export TF_VERSION=2.15.0 # Change to the TensorFlow Version you need.
168+
export PY_VERSION=3.9 # Change to the Python you need.
169+
export RUN_TESTS=1 # Change to 0 if you want to skip tests.
170+
./tools/test_bazel.sh # Takes ~15 minutes on a modern Mac.
192171
```
193172
194-
1. Build the Pip Packages
173+
1. Package the code.
195174
196175
```
197-
# First, we deactivate our virtualenv, since the Pip script uses a different one.
176+
# Building the packages uses different virtualenvs through Pyenv.
198177
deactivate
199178
# Build the packages.
200-
./tools/build_pip_package.sh ALL_VERSIONS_MAC_ARM64
179+
./tools/build_pip_package.sh ALL_VERSIONS
201180
```
202181
203182
1. The packages can be found in `decision-forests/dist/`.
@@ -207,22 +186,29 @@ instructions.
207186
If you have a MacOS machine with Apple CPU, cross-compile TF-DF for MacOS
208187
machines with Intel CPUs as follows.
209188
210-
1. Follow Steps 1-3 and 5 of the guide for Apple CPUs, **skip Step 4**.
211-
You may need to run `bazel --bazelrc=tensorflow_bazelrc clean --expunge` to
212-
clean your build directory.
189+
1. Prepare your environment
190+
191+
```
192+
git clone https://github.com/tensorflow/decision-forests.git
193+
python3 -m venv venv
194+
source venv/source/activate
195+
```
213196
214197
1. Decide which Python version you want to use and run
215198
216199
```
217200
cd decision-forests
218-
# This will compile with the latest Tensorflow version in the tensorflow-macos repository.
219-
RUN_TESTS=0 PY_VERSION=3.9 TF_VERSION=mac-intel-crosscompile ./tools/test_bazel.sh
201+
export TF_VERSION=2.15.0 # Change to the TensorFlow Version you need.
202+
export PY_VERSION=3.9 # Change to the Python you need.
203+
export RUN_TESTS=0 # Cross-compiled packages cannot be tested.
204+
export MAC_INTEL_CROSSCOMPILE=1
205+
./tools/test_bazel.sh # Takes ~15 minutes on a modern Mac.
220206
```
221207
222-
1. Build the Pip Packages
208+
1. Package the code.
223209
224210
```
225-
# First, we deactivate our virtualenv, since the Pip script uses a different one.
211+
# Building the packages uses different virtualenvs through Pyenv.
226212
deactivate
227213
# Build the packages.
228214
./tools/build_pip_package.sh ALL_VERSIONS_MAC_INTEL_CROSSCOMPILE

third_party/yggdrasil_decision_forests/workspace.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def deps(from_git_repo = True):
1313
)
1414
else:
1515
# You can also clone the YDF repository manually.
16+
# Note that you need to manually apply the patch for Tensorflow >= 2.16 or nightly.
1617
native.local_repository(
1718
name = "ydf",
1819
# When downloading from Github, you might need - instead of _ as folder name

tools/build_pip_package.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ function assemble_files() {
102102

103103
# When cross-compiling, adapt the platform string.
104104
if [ ${ARG} == "ALL_VERSIONS_MAC_INTEL_CROSSCOMPILE" ]; then
105+
sed -i'.bak' -e "s/plat = \"macosx_12_0_arm64\"/# plat = \"macosx_12_0_arm64\"/" ${SRCPK}/setup.py
105106
sed -i'.bak' -e "s/# plat = \"macosx_10_15_x86_64\"/plat = \"macosx_10_15_x86_64\"/" ${SRCPK}/setup.py
106107
fi
107108

tools/test_bazel.sh

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
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.
5780
if [ ${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"
6083
else
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
6893
fi
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

8399
if [[ ${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
87103
fi
88104

89105
# Get the commit SHA
90106
short_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]*$')
96108
echo "Found tensorflow commit sha: $short_commit_sha"
97109
commit_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
101113
prev_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.
105117
TENSORFLOW_BAZELRC="tensorflow_bazelrc"
@@ -121,14 +133,18 @@ else
121133
FLAGS="${FLAGS} --config=linux"
122134
fi
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"
177193
fi

0 commit comments

Comments
 (0)