参考Build from source和How to build TensorFlow 2.0 on Ubuntu 18.04 (x86_64) with Bazelisk
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout branch_name # r1.15, r2.2, r2.3, etc.
安装 bazelisk,参考 Installing Bazel using Bazelisk
npm install -g @bazel/bazelisk- 安装 npm:
sudo apt install npm
- 安装 npm:
go get github.com/bazelbuild/bazelisk- 安装 Go:
sudo apt install golang - 需要将go加入PATH,如果没有 bazel 只有 bazelisk,需要
ln -s bazelisk bazel
- 安装 Go:
生成 TensorFlow 需要的 bazel 版本配置文件,如果没有的话(一般是 0.26.1,可参考 configure.py 文件_TF_MIN_BAZEL_VERSION ):echo "0.26.1" > .bazelversion
运行 bazel version 下载安装对应的 bazel
编译 TensorFlow r1.15 版本本人使用的 python 版本是 3.6
安装 pip 包,参考 Issue,注意安装正确的版本,不然编译会出现各种问题。
Python 3.6.9
TF-1.14:
pip install 'numpy<1.19.0'
pip install -U pip six wheel mock
pip install future
pip install keras_applications==1.0.4 --no-deps
pip install keras_preprocessing==1.0.2 --no-deps
pip install pandas
bazel version: bazel-0.25.2-installer-linux-x86_64.sh
TF-1.15 & TF-2.0:
pip install 'numpy<1.19.0'
pip install -U pip six wheel mock
pip install future
pip install keras_applications==1.0.8 --no-deps
pip install keras_preprocessing==1.1.2 --no-deps
pip install pandas
bazel version: bazel-0.26.1-installer-linux-x86_64.sh
配置环境
- 使用虚拟环境,运行:
python configure.py - 使用系统环境,运行:
./configure
编译
bazel build //tensorflow/tools/pip_package:build_pip_package- debug信息编译时加上
-c dbg
- 如果是
"external/grpc/src/core/lib/gpr/log_linux.cc:43:13: error: ambiguating new declaration of 'long int gettid()'",参考Issue,进行如下操作:
cd third_party
wget https://nomeroff.net.ua/tf/Rename-gettid-functions.patch
cd ..
vim tensorflow/workspace.bzl
# 找到tf_http_archive( name = "grpc",...
# 添加下面的patch_file一行
tf_http_archive(
name = "grpc",
patch_file = clean_dep("//third_party:Rename-gettid-functions.patch"),
sha256 = "67a6c26db56f345f7cee846e681db2c23f919eba46dd639b09462d1b6203d28c",
strip_prefix = "grpc-4566c2a29ebec0835643b972eb99f4306c4234a3",
system_build_file = clean_dep("//third_party/systemlibs:grpc.BUILD"),
urls = [
"https://storage.googleapis.com/mirror.tensorflow.org/github.com/grpc/grpc/archive/4566c2a29ebec0835643b972eb99f4306c4234a3.tar.gz",
"https://github.com/grpc/grpc/archive/4566c2a29ebec0835643b972eb99f4306c4234a3.tar.gz",
],
)
# 重新运行bazel
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
pip install /tmp/tensorflow_pkg/tensorflow-version-tags.whl