Skip to content

Commit

Permalink
[cicd]Enhance project's compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
poinwater committed Sep 17, 2023
1 parent 0ff7384 commit 0f6c231
Show file tree
Hide file tree
Showing 37 changed files with 318 additions and 155 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/make_wheel_Linux_x86.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ else
export TF_NAME='tensorflow'
fi

if [ $TF_VERSION == "2.6.3" ] || [ $TF_VERSION == "2.8.3" ] ; then
# if tensorflow version == 2.12.0 or == 2.13.0
if [[ "$TF_VERSION" =~ ^2\.(12|13)\.0$ ]] ; then
export BUILD_IMAGE="tfra/nosla-cuda11.8.1-cudnn8-ubuntu20.04-manylinux2014-python$PY_VERSION"
export TF_CUDA_VERSION="11.8"
export TF_CUDNN_VERSION="8.6"
# else if tensorflow version >= 2.6.0 and <= 2.11.9
elif [[ "$TF_VERSION" =~ ^2\.([6-9]|10|11)\.[0-9]$ ]] ; then
export BUILD_IMAGE="tfra/nosla-cuda11.2.1-cudnn8-ubuntu20.04-manylinux2014-python$PY_VERSION"
export TF_CUDA_VERSION="11.2"
export TF_CUDNN_VERSION="8.1"
Expand Down
30 changes: 27 additions & 3 deletions .github/workflows/make_wheel_macOS_arm64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,37 @@
#
# Making wheel for macOS arm64 architecture
# Requirements:
# MacOS Monterey 12.0.0 +, Tensorflow-macos 2.6.0 or 2.8.0, ARM64 Apple Silicon, Bazel 4.1.0 +
# MacOS Monterey 12.0.0 +, Tensorflow-macos 2.6.0 - 2.11.0, ARM64 Apple Silicon, Bazel 5.1.1
# Please don't install tensorflow-metal, it may cause incorrect GPU devices detection issue.
set -e -x

export TF_NEED_CUDA=0
if [ -z $HOROVOD_VERSION ] ; then
export HOROVOD_VERSION='0.28.1'
fi

# For TensorFlow version 2.12 or earlier:
export PROTOBUF_VERSION=3.19.6
export TF_NAME="tensorflow-macos"

python --version
python -m pip install --default-timeout=1000 delocate==0.10.3 wheel setuptools
# For TensorFlow version 2.13 or later:
if [[ "$TF_VERSION" =~ ^2\.1[3-9]\.[0-9]$ ]] ; then
export PROTOBUF_VERSION=3.20.3
export TF_NAME="tensorflow"
fi

python -m pip install \
--platform=macosx_12_0_arm64 \
--target=$(python -c 'import site; print(site.getsitepackages()[0])') \
--upgrade \
--only-binary=:all: \
protobuf~=$PROTOBUF_VERSION $TF_NAME==$TF_VERSION

python configure.py
# Setting DYLD_LIBRARY_PATH to help delocate finding tensorflow after the rpath invalidation
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$(python -c 'import configure; print(configure.get_tf_shared_lib_dir())')

bazel build \
--cpu=darwin_arm64 \
Expand All @@ -20,5 +44,5 @@ bazel build \
--test_output=errors \
build_pip_pkg

# Output the wheel file to the artifacts directory
bazel-bin/build_pip_pkg artifacts $NIGHTLY_FLAG
bazel-bin/build_pip_pkg artifacts "--plat-name macosx_11_0_arm64 $NIGHTLY_FLAG"
delocate-wheel -w wheelhouse -v --ignore-missing-dependencies artifacts/*.whl
22 changes: 16 additions & 6 deletions .github/workflows/make_wheel_macOS_x86.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,40 @@ set -e -x

export TF_NEED_CUDA=0
if [ -z $HOROVOD_VERSION ] ; then
export HOROVOD_VERSION='0.23.0'
export HOROVOD_VERSION='0.28.1'
fi
python --version

brew install open-mpi
python -m pip install numpy==1.20.0
python -m pip install --default-timeout=1000 delocate==0.9.1 wheel==0.36.2 setuptools tensorflow==$TF_VERSION
python -m pip install --default-timeout=1000 delocate==0.10.3 wheel==0.36.2 setuptools tensorflow==$TF_VERSION
python -m pip install tensorflow-io
python -m pip install --upgrade protobuf==3.20.0

bash tools/docker/install/install_horovod.sh $HOROVOD_VERSION --only-cpu

bash tools/testing/build_and_run_tests.sh
# Test
bash tools/testing/build_and_run_tests.sh $SKIP_CUSTOM_OP_TESTS

# Clean
bazel clean

# Build
python configure.py

bazel build \
-c opt \
--copt -mmacosx-version-min=10.13 \
--linkopt -mmacosx-version-min=10.13 \
--copt -mmacosx-version-min=10.15 \
--linkopt -mmacosx-version-min=10.15 \
--noshow_progress \
--noshow_loading_progress \
--verbose_failures \
--test_output=errors \
build_pip_pkg

bazel-bin/build_pip_pkg artifacts $NIGHTLY_FLAG
delocate-wheel -w wheelhouse artifacts/*.whl

# Setting DYLD_LIBRARY_PATH to help delocate finding tensorflow after the rpath invalidation
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$(python -c 'import configure; print(configure.get_tf_shared_lib_dir())')
delocate-wheel -w wheelhouse -v --ignore-missing-dependencies artifacts/*.whl

46 changes: 21 additions & 25 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ jobs:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v1
- uses: actions/setup-python@v2
with:
python-version: ${{ env.MIN_PY_VERSION }}
python-version: ${{ env.MAX_PY_VERSION }}
- name: Build wheels
run: |
pip install --default-timeout=1000 -r tools/install_deps/pytest.txt -r tools/install_deps/tensorflow-cpu.txt -r requirements.txt
pip install --default-timeout=1000 -r tools/install_deps/pytest.txt -r tools/install_deps/tensorflow.txt -r requirements.txt
sudo apt install -y redis > /dev/null 2> /dev/null
bash tools/install_deps/install_bazelisk.sh ./
python -m pip install tensorflow-io
Expand All @@ -39,29 +39,21 @@ jobs:
bazel test --local_ram_resources=4096 -c opt -k --test_timeout 300,450,1200,3600 --test_output=errors //tensorflow_recommenders_addons/...
release-wheel:
name: Build release wheels
runs-on: ${{ matrix.os }}
strategy:
matrix:
# TODO: add back 'windows-latest' when it can be compiled.
os: ['macos-12', 'ubuntu-20.04']
py-version: ['3.7', '3.8', '3.9']
tf-version: ['2.6.3', '2.8.3']
tf-need-cuda: ['1', '0']
tf-version: ['2.6.3', '2.11.0']
tf-need-cuda: ['0', '1']
tf-cuda-version: ['11.2']
tf-cudnn-version: ['8.1']
cpu: ['x86']
# TODO(poinwater): add macOS CI once GitHub supports macOS 12.0.0 +
# include:
# - os: 'macos-11'
# cpu: 'arm64'
# tf-version: '2.5.0'
# py-version: '3.9'
# tf-need-cuda: '0'
exclude:
# excludes cuda on macOS
- os: 'macos-12'
tf-need-cuda: '1'
fail-fast: false
runs-on: ${{ matrix.os }}
steps:
- name: clear cache folder
run: rm -rf /usr/share/dotnet /opt/ghc "/usr/local/share/boost"
Expand All @@ -72,11 +64,14 @@ jobs:
script: |
const commit_details = await github.git.getCommit({owner: context.repo.owner, repo: context.repo.repo, commit_sha: context.sha});
return commit_details.data.author.date
- if: matrix.tf-version != '2.11.0'
shell: bash
run: echo "SKIP_CUSTOM_OP_TESTS=--skip-custom-ops" >> $GITHUB_ENV
- if: github.event_name == 'push'
shell: bash
run: echo "NIGHTLY_FLAG=--nightly" >> $GITHUB_ENV
- uses: actions/checkout@v2
- uses: actions/setup-python@v1
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.py-version }}
- name: Setup Bazel
Expand All @@ -94,7 +89,11 @@ jobs:
NIGHTLY_TIME: ${{ steps.author-date.outputs.result }}
CPU: ${{ matrix.cpu }}
shell: bash
run: bash .github/workflows/make_wheel_${OS}_${CPU}.sh
run: |
if [[ "$TF_VERSION" =~ ^2\.(9|10|11)\.[0-9]$ ]] ; then
export HOROVOD_VERSION="0.28.1"
fi
bash .github/workflows/make_wheel_${OS}_${CPU}.sh
- uses: haya14busa/action-cond@v1
id: device
with:
Expand All @@ -114,16 +113,13 @@ jobs:
# TODO: add back 'Windows' when it can be compiled.
os: ['macOS', 'Linux']
py-version: ['3.7', '3.8', '3.9']
tf-version: ['2.8.3']
tf-need-cuda: ['1', '0']
tf-need-cuda: ['0', '1']
cpu: ['x86']
# TODO(poinwater): add macOS CI once GitHub supports macOS 12.0.0 +
# include:
# - os: 'macOS'
# cpu: 'arm64'
# tf-version: '2.5.0'
# py-version: '3.9'
# tf-need-cuda: '0'
include:
- os: 'macos-12'
cpu: 'arm64'
tf-version: '2.9.0'
py-version: '3.9'
exclude:
# excludes cuda on macOS
- os: 'macOS'
Expand Down
26 changes: 8 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,36 +192,26 @@ pip install artifacts/tensorflow_recommenders_addons_gpu-*.whl
Requirements:

- macOS 12.0.0+
- Python 3.8 or 3.9
- tensorflow-macos 2.6.0
- bazel 4.1.0+
- Python 3.9
- tensorflow-macos 2.9.0
- bazel 5.1.1

The natively supported TensorFlow is maintained by Apple. Please see the instruction [Get started with tensorflow-metal](https://developer.apple.com/metal/tensorflow-plugin/) to install the Tensorflow on apple silicon devices.


```sh
# Install TensorFlow macOS dependencies
conda install -c apple tensorflow-deps==2.6.0

# Install base TensorFlow
python -m pip install tensorflow-macos==2.6.0
```

If you see any issue with installing `tensorflow-macos`, please contact the [Apple Developer Forums: tensorflow-metal](https://developer.apple.com/forums/tags/tensorflow-metal) for help.

**Install TFRA on Apple Silicon via PIP**
```sh
python -m pip install tensorflow-recommenders-addons --no-deps
```

**Install TFRA on Apple Silicon from Source**
**Build TFRA on Apple Silicon from Source**

```sh
export TF_VERSION="2.6.0" # Specify your Tensorflow version here, 2.8.0 is well tested.
export PY_VERSION="3.8" # Specify your python version here, "3.9" is well tested.
# Install bazelisk
brew install bazelisk

# Building TFRA wheel
PY_VERSION=$PY_VERSION TF_VERSION=$TF_VERSION TF_NEED_CUDA="0" sh .github/workflows/make_wheel_macOS_arm64.sh
# Build wheel from source
PY_VERSION=3.9.0 TF_VERSION=2.9.0 TF_NEED_CUDA="0" sh .github/workflows/make_wheel_macOS_arm64.sh

# Install the wheel
python -m pip install --no-deps ./artifacts/*.whl
Expand Down
36 changes: 21 additions & 15 deletions configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,24 @@
import logging

import tensorflow as tf
try:
from packaging.version import Version
except: # make it compatible for python 3.7
from distutils.version import LooseVersion as Version

_TFRA_BAZELRC = ".bazelrc"


# Maping TensorFlow version to valid Bazel version.
def _VALID_BAZEL_VERSION(tf_version):
if is_macos() and is_arm64():
target_bazel = "4.1.0"
return target_bazel
elif tf_version < "2.0.0":
if Version(tf_version) < Version("2.0.0"):
target_bazel = "0.26.1"
logging.warn(
'There is only limited support for TensorFlow under version 2.0.0 '
'because its Bazel version, and requiring users to make some Bazel script changes '
'refering to the previous COMMIT to compile properly by themselves.')
return target_bazel
elif tf_version >= "2.0.0":
elif Version(tf_version) >= Version("2.0.0"):
target_bazel = "5.1.1"
logging.info(
'To ensure code compatibility with Bazel rules_foreign_cc component, '
Expand Down Expand Up @@ -78,6 +79,13 @@ def is_raspi_arm():
return os.uname()[4] == "armv7l"


def get_cpp_version():
cpp_version = "c++14"
if Version(tf.__version__) >= Version("2.10"):
cpp_version = "c++17"
return cpp_version


def get_tf_header_dir():
if get_tf_version_integer() >= 2000:
tf_header_dir = tf.sysconfig.get_compile_flags()[0][2:]
Expand All @@ -104,7 +112,10 @@ def get_tf_shared_lib_dir():
# Converts the linkflag namespec to the full shared library name
def get_shared_lib_name():
namespec = tf.sysconfig.get_link_flags()
if is_macos():
if is_macos() and is_arm64():
# MacOS arm64
return "libtensorflow_framework.2.dylib"
elif is_macos():
# MacOS
return "libtensorflow_framework.dylib"
elif is_windows():
Expand Down Expand Up @@ -177,14 +188,9 @@ def _get_installed_and_valid_bazel_version():
def check_bazel_version(is_macos_arm64: bool = False):
installed_bazel_version, valid_bazel_version = _get_installed_and_valid_bazel_version(
)
if installed_bazel_version < valid_bazel_version:
if is_macos_arm64:
raise ValueError(
'Bazel version is {}. For macOS arm64 platform, the Bazel version must be at least {}.'
.format(installed_bazel_version, valid_bazel_version))
else:
raise ValueError('Bazel version is {}, but {} is needed.'.format(
installed_bazel_version, valid_bazel_version))
if Version(installed_bazel_version) < Version(valid_bazel_version):
raise ValueError('Bazel version is {}, but {} is needed.'.format(
installed_bazel_version, valid_bazel_version))


def extract_tf_header():
Expand Down Expand Up @@ -227,7 +233,7 @@ def create_build_configuration():
else:
tf_cxx_standard_compile_flag = None
if tf_cxx_standard_compile_flag is None:
tf_cxx_standard = "c++14"
tf_cxx_standard = get_cpp_version()
else:
tf_cxx_standard = tf_cxx_standard_compile_flag.split("-std=")[-1]
write_action_env("TF_CXX_STANDARD", tf_cxx_standard)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import tensorflow as tf

from tensorflow_recommenders_addons import dynamic_embedding as de
try:
from tensorflow.keras.optimizers.legacy import Adam
except:
from tensorflow.keras.optimizers import Adam

from absl import flags
from absl import app
Expand All @@ -29,7 +33,7 @@ def train(num_steps):
# Create a model
model = video_game_model.VideoGameDnn(batch_size=FLAGS.batch_size,
embedding_size=FLAGS.embedding_size)
optimizer = tf.keras.optimizers.Adam(1E-3, clipnorm=None)
optimizer = Adam(1E-3, clipnorm=None)
optimizer = de.DynamicEmbeddingOptimizer(optimizer)
auc = tf.keras.metrics.AUC(num_thresholds=1000)
accuracy = tf.keras.metrics.BinaryAccuracy(dtype=tf.float32)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from absl import flags
from absl import app
from tensorflow_recommenders_addons import dynamic_embedding as de
try:
from tensorflow.keras.optimizers.legacy import Adam
except:
from tensorflow.keras.optimizers import Adam

flags = tf.compat.v1.app.flags
FLAGS = flags.FLAGS
Expand Down Expand Up @@ -141,7 +145,7 @@ def train(self):
model = DualChannelsDeepModel(
self.ps_devices, self.embedding_size, self.embedding_size,
tf.keras.initializers.RandomNormal(0.0, 0.5))
optimizer = tf.keras.optimizers.Adam(1E-3)
optimizer = Adam(1E-3)
optimizer = de.DynamicEmbeddingOptimizer(optimizer)

auc = tf.keras.metrics.AUC(num_thresholds=1000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from absl import flags
from absl import app
from tensorflow_recommenders_addons import dynamic_embedding as de
try:
from tensorflow.keras.legacy.optimizers import Adam
except:
from tensorflow.keras.optimizers import Adam

os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "true" #VERY IMPORTANT!

Expand Down Expand Up @@ -157,7 +161,7 @@ def train():
dataset = get_dataset(batch_size=32)
model = DualChannelsDeepModel(FLAGS.embedding_size, FLAGS.embedding_size,
tf.keras.initializers.RandomNormal(0.0, 0.5))
optimizer = tf.keras.optimizers.Adam(1E-3)
optimizer = Adam(1E-3)
optimizer = de.DynamicEmbeddingOptimizer(optimizer)

auc = tf.keras.metrics.AUC(num_thresholds=1000)
Expand Down
Loading

0 comments on commit 0f6c231

Please sign in to comment.