macOS CatalinaでTensorflow 1.15.3をビルド

ビルド環境

  • macOS Catalina 10.15.6
  • Python3.8
  • GCC Apple clang version 11.0.3 (clang-1103.0.32.62)
conda create -n tf_38_2 python=3.8
conda activate tf_38_2

pipモジュールのインストール

pip install -U --user pip six 'numpy<1.19.0' wheel setuptools mock 'future>=0.17.1' 'gast==0.2.2' typing_extensions
pip install -U --user keras_applications --no-deps
pip install -U --user keras_preprocessing --no-deps
git clone https://github.com/tensorflow/tensorflow tensorflow_38_2
cd tensorflow_38_2
git checkout v1.15.3

Bazelのインストール

export BAZEL_VERSION=0.26.1
curl -fLO "https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-installer-darwin-x86_64.sh"
chmod +x "bazel-${BAZEL_VERSION}-installer-darwin-x86_64.sh"
./bazel-${BAZEL_VERSION}-installer-darwin-x86_64.sh --user
bazel --version

この2つのパッチを参考にファイルを修正します。

Fix GCC 10.1 compile error. by cbalint13 · Pull Request #40654 · tensorflow/tensorflow · GitHub

py-tensorflow1: fix Python 3.8 build. · macports/macports-ports@f63da02 · GitHub

configureの内容

% ./configure
WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown".
You have bazel 0.26.1 installed.
Please specify the location of python. [Default is /Users/tak/opt/anaconda3/envs/tf_38_2/bin/python]: 


Found possible Python library paths:
  /Users/tak/opt/anaconda3/envs/tf_38_2/lib/python3.8/site-packages
Please input the desired Python library path to use.  Default is [/Users/tak/opt/anaconda3/envs/tf_38_2/lib/python3.8/site-packages]

Do you wish to build TensorFlow with XLA JIT support? [Y/n]: y
XLA JIT support will be enabled for TensorFlow.

Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: n
No OpenCL SYCL support will be enabled for TensorFlow.

Do you wish to build TensorFlow with ROCm support? [y/N]: n
No ROCm support will be enabled for TensorFlow.

Do you wish to build TensorFlow with CUDA support? [y/N]: n
No CUDA support will be enabled for TensorFlow.

Do you wish to download a fresh release of clang? (Experimental) [y/N]: n
Clang will not be downloaded.

Do you wish to build TensorFlow with MPI support? [y/N]: n
No MPI support will be enabled for TensorFlow.

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native -Wno-sign-compare]: 


Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: n
Not configuring the WORKSPACE for Android builds.

Do you wish to build TensorFlow with iOS support? [y/N]: n
No iOS support will be enabled for TensorFlow.

Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details.
    --config=mkl            # Build with MKL support.
    --config=monolithic     # Config for mostly static monolithic build.
    --config=gdr            # Build with GDR support.
    --config=verbs          # Build with libverbs support.
    --config=ngraph         # Build with Intel nGraph support.
    --config=numa           # Build with NUMA support.
    --config=dynamic_kernels    # (Experimental) Build kernels into separate shared objects.
    --config=v2             # Build TensorFlow 2.x instead of 1.x.
Preconfigured Bazel build configs to DISABLE default on features:
    --config=noaws          # Disable AWS S3 filesystem support.
    --config=nogcp          # Disable GCP support.
    --config=nohdfs         # Disable HDFS support.
    --config=noignite       # Disable Apache Ignite support.
    --config=nokafka        # Disable Apache Kafka support.
    --config=nonccl         # Disable NVIDIA NCCL support.
Configuration finished

ビルド実行

bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

ビルド完了したら以下のパッチを参考にソース修正

Fix TensorFlow on Python 3.8 logger issue by yongtang · Pull Request #33953 · tensorflow/tensorflow · GitHub

./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
pip install /tmp/tensorflow_pkg/tensorflow-1.15.3-cp38-cp38-macosx_10_15_x86_64.whl

これでビルドは完了です。