Tensorflow代码解析(一)
源码编译tensorflow
源码编译tensorflow1
源码编译tensorflow2
1. 基本概念
-
单元测试
bazel是Google开源的一套编译构建工具,广泛应用于Google内部,包括TensorFlow项目。修改TensorFlow内部源码,需要使用bazel来编译,故有必要了解下bazel。bazel优点很多,主要有
构建快。支持增量编译。对依赖关系进行了优化,从而支持并发执行。
可构建多种语言。bazel可用来构建Java C++ Android ios等很多语言和框架,并支持mac windows linux等不同平台
可伸缩。可处理任意大小的代码库,可处理多个库,也可以处理单个库
可扩展。使用bazel扩展语言可支持新语言和新平台。
2. 安装bazel
安装准备
首先从这里查看你的tensorflow 支持的bazel版本。
在这里可以找到自己所需要的版本
官方教程:Ubuntu
有三种安装方式:
- 使用二进制安装程序(推荐)
- 使用我们的自定义APT存储库
- 从源代码编译Bazel
开始安装
我们采用第一种:使用二进制安装程序
- 安装依赖
sudo apt install g++ unzip zip
- 下载相关版本的
Bazel
我这里是0.15.0
从这里找到bazel-0.15.0-installer-linux-x86_64.sh直接下载好传到指定目录。
或者运行以下命令下载这个版本的 bazel
wget https://github.com/bazelbuild/bazel/releases/download/0.15.0/bazel-0.15.0-installer-linux-x86_64.sh
- 运行安装程序
// 以下是统一写法
chmod +x bazel-<version>-installer-linux-x86_64.sh
./bazel-<version>-installer-linux-x86_64.sh --user
// 以下是对应版本的写法
chmod +x bazel-0.15.0-installer-linux-x86_64.sh
./bazel-0.15.0-installer-linux-x86_64.sh --user
- 环境配置
export PATH="$PATH:$HOME/bin"
也可以把以下命令配置到 ~/.bashrc
或者~/.zshrc
使其永久化。
注:
a. 参数--user
标志将Bazel
安装到$HOME/bin
系统上的目录;
b. 设置.bazelrc
路径$HOME/.bazelrc
;
c. 可以使用--help
命令可以查看其他安装选项。
卸载bazel
# 返回到bazel的bin文件,如:/user/bin/bazel。
find / -name "Bazel"
rm -rf /user/bin/bazel
# 备用
rm -fr ~/.bazel ~/.bazelrc
3. 单元测试
准备
- 从这里下载指定版本的tensorflow源码; 本文的1.12 版本在这里下载。
或者通过wget 下载:
wget https://github.com/tensorflow/tensorflow/archive/v1.12.0.zip --no-check-certificate
wget https://github.com/tensorflow/tensorflow/archive/v1.12.0.tar.gz --no-check-certificate
- 把下载好的包传到自己的工程目录。
- tensorflow 源码框架示意图
开始测试
bazel 测试指导
python 测试
Bazel入门:编译C++项目
bazel 参数文档
使用bazel tensorflow 编译结构
github tensorflow 结构
使用bazel 单元测试
# The easiest way to run the TensorFlow unit tests is using Bazel, assuming you have downloaded the source from Git:
# All tests (for C++ changes).
$ bazel test //tensorflow/...
# All Python tests (for Python front-end changes).
$ bazel test //tensorflow/python/...
# All tests (with GPU support).
$ bazel test -c opt --config=cuda //tensorflow/...
$ bazel test -c opt --config=cuda //tensorflow/python/...
实测:通过使用--distdir
= 参数规避问题
bazel test -c opt --config=cuda --distdir=/home/ --nocache_test_results --test_output=errors --test_verbose_timeout_warnings --local_test_jobs=1 --test_env=LD_LIBRARY_PATH //tensorflow/python/...
# In addition to the above answer, you can run individual tests as shown instead of full packages, which can save a significant amount of time.
bazel run //tensorflow/python/kernel_tests:string_split_op_test
bazel run //tensorflow/python:special_math_ops_test
# Or you can go to the individual directory and run all the tests there
cd python/kernel_tests
bazel run :one_hot_op_test
注意事项1 :安装bazel
的版本
进入自己的解压目录:tensorflow-1.15.0
找到WORKSPACE
查看安装指定的bazel
版本。
80 # We must check the bazel version before trying to parse any other BUILD
81 # files, in case the parsing of those build files depends on the bazel
82 # version we require here.
83 load("//tensorflow:version_check.bzl", "check_bazel_version_at_least")
84 check_bazel_version_at_least("0.19.0")
注意事项2
1 workspace(name = "org_tensorflow")
2
3 load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")
4
5 http_archive(
6 name = "io_bazel_rules_closure",
7 sha256 = "5b00383d08dd71f28503736db0500b6fb4dda47489ff5fc6bed42557c07c6ba9",
8 strip_prefix = "rules_closure-308b05b2419edb5c8ee0471b67a40403df940149",
9 urls = [
10 "https://storage.googleapis.com/mirror.tensorflow.org/github.com/bazelbuild/rules_closure/archive/308b05b2419edb5c8ee0471b67a40403df940149.tar.gz",
11 "https://github.com/bazelbuild/rules_closure/archive/308b05b2419edb5c8ee0471b67a40403df940149.tar.gz", # 2019-06-13
12 ],
其他方法:
错误示例:
2020-03-09 11:30:18.977282: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 AVX512F FMA
E.
======================================================================
ERROR: testDenseLayerOutput (__main__.DenseLayerTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "my_tf_python_test5.py", line 22, in testDenseLayerOutput
output = self.my_dense(input_x)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/engine/base_layer.py", line 753, in __call__
self._assert_input_compatibility(inputs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/engine/base_layer.py", line 1463, in _assert_input_compatibility
if x.shape.ndims is None:
AttributeError: 'tuple' object has no attribute 'ndims'
----------------------------------------------------------------------
Ran 2 tests in 0.088s
FAILED (errors=1)