在舊款 CPU 編譯 Tensorflow v2.1
紀錄在舊款 CPU 上編譯 Tensorflow v2.1 的過程,包含安裝 Python 虛擬環境、下載 Tensorflow 源碼、安裝 Bazel、設定環境並編譯等步驟。
參考官網: https://www.tensorflow.org/install/source
硬體版本: OS: Ubuntu 18.04 Server CPU: Intel X5670
軟體版本: Python: Anaconda Python 3.7.5 Bazel: 0.27.1 Tensorflow: r2.1
安裝 Python 虛擬環境
Conda
conda create -n tf2 python=3.7.5
conda activate tf2
conda install pip six numpy wheel setuptools mock
conda install -c conda-forge keras-applications
conda install -c conda-forge keras-preprocessing
Pip
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip setuptools wheel
pip install -U six 'numpy<1.19.0' mock 'future>=0.17.1' 'gast==0.3.3' typing_extensions
pip install -U keras_applications --no-deps
pip install -U keras_preprocessing --no-deps
下載 Tensorflow 源碼並切換 branch
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout r2.1
安裝 Bazel
參考官網對bazel版本限制參考: https://www.tensorflow.org/install/source#tested_build_configurations
要準確知道 tensorflow 限制的 bazel 版本,可參考 tensorflow/configure.py
內的 _TF_MIN_BAZEL_VERSION
和 _TF_MAX_BAZEL_VERSION
2個常數
bazel安裝參考: https://docs.bazel.build/versions/master/install-ubuntu.html
wget https://github.com/bazelbuild/bazel/releases/download/0.27.1/bazel-0.27.1-installer-linux-x86_64.sh
chmod +x bazel-0.27.1-installer-linux-x86_64.sh
./bazel-0.27.1-installer-linux-x86_64.sh --user
更新 .bashrc
PATH=$PATH:$HOME/bin
設定環境並編譯
設定環境
source ~/.bashrc
conda activate tf2
cd tensorflow
bazel clean
$ ./configure # 基本上所有安裝選項都選**N**
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.
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=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=nonccl # Disable NVIDIA NCCL support.
Configuration finished
開始編譯
bazel build tensorflow/tools/pip_package:build_pip_package
bazel build --local_cpu_resources=32 --config=opt tensorflow/tools/pip_package:build_pip_package
時間紀錄
CPU: Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz * 2
INFO: Elapsed time: 5665.291s, Critical Path: 4042.33s
INFO: 14283 processes: 14283 local.
INFO: Build completed successfully, 15315 total actions
安裝 tensorflow
用 pip 安裝
./tensorflow/tools/pip_package/build_pip_package.sh /tmp/tensorflow_pkg
pip install /tmp/tensorflow_pkg/tensorflow-2.1.0rc0-cp37-cp37m-linux_x86_64.whl
測試以下代碼
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test, verbose=2)