Skip to content

Instantly share code, notes, and snippets.

@philipturner
Last active August 10, 2022 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipturner/7aa063af04277d463c14168275878511 to your computer and use it in GitHub Desktop.
Save philipturner/7aa063af04277d463c14168275878511 to your computer and use it in GitHub Desktop.
tensorflow_version="2.9.1"
DESTDIR=${PWD}/Library/tensorflow-${tensorflow_version}
if [[ $(uname -s) == "Darwin" ]]; then
lib_end="dylib" # macOS
else
lib_end="so" # Linux
fi
# ===----------------------------------------------------------------------=== #
# Parse command-line arguments
# ===----------------------------------------------------------------------=== #
args=$@
args_str=" ${args[*]} "
if [[ $args_str =~ " --skip-ctensorflow " ]]; then
compile_ctensorflow=0
else
compile_ctensorflow=1
fi
if [[ $args_str =~ " --skip-tests " ]]; then
run_tests=0
else
run_tests=1
fi
if [[ $args_str =~ " --use-release-toolchain " ]]; then
use_release_toolchain=1
else
use_release_toolchain=0
fi
# ===----------------------------------------------------------------------=== #
# Build X10
# ===----------------------------------------------------------------------=== #
# Command-line flag: --skip-ctensorflow
#
# This makes incremental builds quicker because you don't have to scan Bazel
# build products that are fully built. Also, use this flag if you want to use
# an X10 binary downloaded from the internet instead of a locally built one.
# Just unzip the file into this directory beforehand.
if [[ $compile_ctensorflow == 1 ]]; then
# Clone s4tf/s4tf.
# NOTE: If there were commits to S4TF since you last ran this script, you must
# update the local branch manually.
git clone --depth 1 --no-single-branch https://github.com/s4tf/s4tf
# Clone tensorflow/tensorflow.
# NOTE: If the TF version changed since you last ran this script, you must
# must delete the `tensorflow` directory.
tensorflow_branch="v${tensorflow_version}"
git clone --depth 1 --branch $tensorflow_branch https://github.com/tensorflow/tensorflow
# Install Bazelisk on your system. It will automatically detect what Bazel
# version you need.
if [[ $(uname -s) == "Darwin" ]]; then
brew install bazelisk
else
# If on Linux, install Bazelisk before running this script. See
# https://github.com/bazelbuild/bazelisk for installation instructions.
:
fi
# Ensure that Python dependencies are available.
python3 -m pip install --user numpy six
ln -sf $(pwd)/s4tf/Sources/CX10 $(pwd)/tensorflow/swift_bindings
ln -sf $(pwd)/s4tf/Sources/x10/xla_client $(pwd)/tensorflow/tensorflow/compiler/xla/xla_client
ln -sf $(pwd)/s4tf/Sources/x10/xla_tensor $(pwd)/tensorflow/tensorflow/compiler/tf2xla/xla_tensor
rm $(pwd)/s4tf/Sources/CX10/CX10
rm $(pwd)/s4tf/Sources/x10/xla_client/xla_client
rm $(pwd)/s4tf/Sources/x10/xla_tensor/xla_tensor
export USE_DEFAULT_PYTHON_LIB_PATH=1
export TF_NEED_OPENCL_SYCL=0
export TF_DOWNLOAD_CLANG=0
export TF_SET_ANDROID_WORKSPACE=0
export TF_CONFIGURE_IOS=0
export TF_ENABLE_XLA=1
export TF_NEED_ROCM=0
export TF_NEED_CUDA=0
export TF_CUDA_COMPUTE_CAPABILITIES=7.5
export CC_OPT_FLAGS="-march=native"
export PYTHON_BIN_PATH=$(which python3)
python3 ./tensorflow/configure.py
# Manually delete a cached build product. For an unknown reason, this file can
# get corrupted, causing the OS to kill any process that opens it. Perhaps
# it's because Bazel doesn't properly delete and re-create it during the build
# process.
rm -f tensorflow/bazel-bin/tensorflow/compiler/tf2xla/xla_tensor/libx10.${lib_end}
# Must also delete whatever location the file is copied to, otherwise that
# will also be corrupted.
rm -rf $DESTDIR
cd tensorflow
bazel --output_user_root caches/bazel/tensorflow build -c opt --define \
framework_shared_object=false --config short_logs --nocheck_visibility \
//tensorflow:tensorflow //tensorflow/compiler/tf2xla/xla_tensor:x10
# Terminate bazel daemon.
bazel --output_user_root caches/bazel/tensorflow shutdown
cd ../
fi
# ===----------------------------------------------------------------------=== #
# Construct "Library"
# ===----------------------------------------------------------------------=== #
# This operation should also not run if you're skipping CTensorFlow. Putting
# these commands under `--skip-ctensorflow` lets the script treat X10 binaries
# built locally the same as binaries downloaded from the internet.
if [[ $compile_ctensorflow == 1 ]]; then
mkdir -p ${DESTDIR}/usr/lib
# Permit overwriting the binary on subsequent script runs. Otherwise, there is
# a "permission denied" error on the copy command directly below this line.
chmod 755 ${DESTDIR}/usr/lib/libx10.${lib_end}
cp tensorflow/bazel-bin/tensorflow/compiler/tf2xla/xla_tensor/libx10.${lib_end} ${DESTDIR}/usr/lib/
mkdir -p ${DESTDIR}/usr/include/tensorflow/c
cp tensorflow/tensorflow/c/c_api.h ${DESTDIR}/usr/include/tensorflow/c/
cp tensorflow/tensorflow/c/c_api_experimental.h ${DESTDIR}/usr/include/tensorflow/c/
cp tensorflow/tensorflow/c/c_api_macros.h ${DESTDIR}/usr/include/tensorflow/c/
cp tensorflow/tensorflow/c/tf_attrtype.h ${DESTDIR}/usr/include/tensorflow/c/
cp tensorflow/tensorflow/c/tf_datatype.h ${DESTDIR}/usr/include/tensorflow/c/
cp tensorflow/tensorflow/c/tf_file_statistics.h ${DESTDIR}/usr/include/tensorflow/c/
cp tensorflow/tensorflow/c/tf_status.h ${DESTDIR}/usr/include/tensorflow/c/
cp tensorflow/tensorflow/c/tf_tensor.h ${DESTDIR}/usr/include/tensorflow/c/
cp tensorflow/tensorflow/c/tf_tstring.h ${DESTDIR}/usr/include/tensorflow/c/
mkdir -p ${DESTDIR}/usr/include/tensorflow/core/platform
cp tensorflow/tensorflow/core/platform/ctstring.h ${DESTDIR}/usr/include/tensorflow/core/platform/
cp tensorflow/tensorflow/core/platform/ctstring_internal.h ${DESTDIR}/usr/include/tensorflow/core/platform/
mkdir -p ${DESTDIR}/usr/include/tensorflow/c/eager
cp tensorflow/tensorflow/c/eager/c_api.h ${DESTDIR}/usr/include/tensorflow/c/eager/
fi
# ===----------------------------------------------------------------------=== #
# Build S4TF
# ===----------------------------------------------------------------------=== #
# S4TF should already be present if you ran without --skip-ctensorflow.
if [[ ! -d s4tf ]]; then
git clone --depth 1 --no-single-branch https://github.com/s4tf/s4tf
fi
# Command-line flag: --use-release-toolchain
#
# Setting `TENSORFLOW_USE_RELEASE_TOOLCHAIN` tells the package manifest that
# this is a release toolchain, making it download philipturner/differentiation
# and philipturner/swift-reflection-mirror.
if [[ $use_release_toolchain == 1 ]]; then
export TENSORFLOW_USE_RELEASE_TOOLCHAIN=1
else
# Use the August 9, 2022 development snapshot. It must already be downloaded
# and installed on your system. Newer toolchains should work, but change
# `TOOLCHAINS` and `DYLD_LIBRARY_PATH` to reflect them.
export TOOLCHAINS="org.swift.57202208091a"
fi
cd s4tf
swift build -Xcc -I${DESTDIR}/usr/include -Xlinker -L${DESTDIR}/usr/lib
build_return_code=$?
rm -f .build/debug/libx10.dylib
cp ${DESTDIR}/usr/lib/libx10.dylib .build/debug/libx10.dylib
# ===----------------------------------------------------------------------=== #
# Test S4TF
# ===----------------------------------------------------------------------=== #
# Command-line flag: --skip-tests
#
# Select whether to avoid running the test suite. It floods the terminal, making
# it difficult to view results of `swift build`. Also, tests from a previous
# test compilation may run regardless of whether the current compilation
# succeeded. This happens on macOS with dev toolchains because of the workaround
# below. The return code of `swift test` cannot be used because SR-14008 makes
# it zero, so instead it uses the return code of `swift build`.
if [[ $build_return_code == 0 && $run_tests == 1 ]]; then
# This command fails on macOS on dev toolchains, but it builds the `.xctest`
# file. The workaround below runs the `.xctest` file.
swift test -Xcc -I${DESTDIR}/usr/include -Xlinker -L${DESTDIR}/usr/lib
# (SR-14008) Workaround for `_Differentiation` loading error on development
# toolchains.
if [[ $build_return_code == 0 && $(uname -s) == "Darwin" &&
$use_release_toolchain == 0 ]];
then
toolchain="/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-08-09-a.xctoolchain"
swift_libraries="/usr/lib/swift/macosx"
export DYLD_LIBRARY_PATH="${toolchain}${swift_libraries}"
/Applications/Xcode.app/Contents/Developer/usr/bin/xctest \
.build/$(uname -m)-apple-macosx/debug/TensorFlowPackageTests.xctest
fi
fi
@philipturner
Copy link
Author

philipturner commented May 15, 2022

How to compile S4TF now that it's active again

This script should work on macOS and Linux, but has not been tested on Linux/Docker yet. Read through the script's comments before executing it.

Usage:

build_script.sh [--skip-ctensorflow] [--skip-tests] [--use-release-toolchain]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment