tensorflow是用bazel编译的,有些情景是需要bazel编译的,如定制kernel,编译一些工具如freeze_graph等。如果要编译成功需要找到对应的bazel版本,并不是说使用最高的bazel就能成功。使用tensorflow的过程中不时因为tensorlfow和bazel的版本不匹配出现各种问题,且这些问题不好解决。
怎样找到对应的版本
tensorflow的源码里已经明确告诉你使用那个版本的 bazel
.bazelversion 文件的内容就是对应bazel的版本号
也并不是使用对应的bazel 就能编译成功,有时也有各种问题编译不过,也不是说最新就能编过
git tag
v2.4.1
v2.4.2
v2.4.3
commit 1923123d32ea41d92b70a27a3f6ecf0763b56f6c (HEAD, tag: v2.4.2)
git checkout v2.4.2 当试到v2.4.2时 才使用bazel 编译成功
几个bazel 的基本命令:
bazel run tensorflow/examples/speech_commands:wav_to_features -- --input_wav=/home/ws/code/tensorflow/tensorflow/examples/speech_commands/833a0279_nohash_0.wav --output_c_file=/home/ws/code/tensorflow/tensorflow/examples/speech_commands/bazel_micro_features.c --preprocess=micro
这个问题奇怪的时必须有 上面的 --, 再加上 后面的参数,否则
bazel run tensorflow/examples/speech_commands:wav_to_features --input_wav=/home/ws/code/tensorflow/tensorflow/examples/speech_commands/833a0279_nohash_0.wav --output_c_file=/home/ws/code/tensorflow/tensorflow/examples/speech_commands/bazel_micro_features.c --preprocess=micro
ERROR: Unrecognized option: --input_wav=/home/ws/code/tensorflow/tensorflow/examples/speech_commands/833a0279_nohash_0.wav
bazel build tensorflow/lite/experimental/microfrontend/lib:bazel_native_micro_test
bazel run tensorflow/lite/experimental/microfrontend/lib:bazel_native_micro_test /home/ws/code/tensorflow/tensorflow/lite/experimental/microfrontend/lib/833a0279_nohash_0.wav(参数)
bazel下载的文件在哪里?
/home/ws/.cache/bazel/_bazel_ws/20dc601f9091793a4c2ddf1f27d67a3e/
/home/ws/.cache/bazel/_bazel_ws/20dc601f9091793a4c2ddf1f27d67a3e/external/kissfft
不同编译脚本间转换需要注意什么?两个脚本的内容都要看源文件,特别是宏定义
9 cc_library(
10 name = "kiss_fftr_16",
11 srcs = [
12 "kiss_fft.c",
13 "tools/kiss_fftr.c",
14 ],
15 hdrs = [
16 "_kiss_fft_guts.h",
17 "kiss_fft.h",
18 "tools/kiss_fftr.h",
19 ],
20 copts = [
21 "-DFIXED_POINT=16",
22 ],
23 )
怎样安装bazel
Installing Bazel on Ubuntu - Bazel main
Using the binary installer
Using the binary installer
Generally, you should use the apt repository, but the binary installer can be useful if you don’t have admin permissions on your machine or can’t add custom repositories.
The binary installers can be downloaded from Bazel’s GitHub releases page.
The installer contains the Bazel binary and extracts it into your $HOME/bin
folder. Some additional libraries must be installed manually for Bazel to work.
Step 1: Install required packages
Bazel needs a C++ compiler and unzip / zip in order to work:
sudo apt install g++ unzip zip
If you want to build Java code using Bazel, install a JDK:
# Ubuntu 16.04 (LTS) uses OpenJDK 8 by default:
sudo apt-get install openjdk-8-jdk
# Ubuntu 18.04 (LTS) uses OpenJDK 11 by default:
sudo apt-get install openjdk-11-jdk
Step 2: Run the installer
Next, download the Bazel binary installer named bazel-<version>-installer-linux-x86_64.sh
from the Bazel releases page on GitHub.
Run it as follows:
chmod +x bazel-<version>-installer-linux-x86_64.sh
./bazel-<version>-installer-linux-x86_64.sh --user
加 --user 参数后 bazel 安装到 ~/bin/ 路径 把名字bazel 更改为 含有版本号信息 bazel-3.1.0
3. 建立软连接
删除原来,然后建立软连接
sudo ln -s /home/ws/bin/bazel-3.1.0 /usr/bin/bazel
➜ tensorflow git:(1923123d32e) ✗ whereis bazel
bazel: /usr/bin/bazel /etc/bazel.bazelrc
➜ tensorflow git:(1923123d32e) ✗ ls ~/bin/
bazel-0.29.1 bazel-3.1.0
4. 验证
➜ tensorflow git:(1923123d32e) ✗ bazel --version
bazel 3.1.0
➜ tensorflow git:(heads/v2.4.2) ✗ bazel --version bazel 3.1.0 ➜ tensorflow git:(heads/v2.4.2) ✗ where bazel /usr/bin/bazel ➜ tensorflow git:(heads/v2.4.2) ✗ cd /usr/bin ➜ bin ls -al | grep bazel lrwxrwxrwx 1 root root 24 Sep 2 06:58 bazel -> /home/ws/bin/bazel-3.1.0 -rwxr-xr-x 1 root root 2746 Sep 2 05:45 bazel-0.29.1 -rwxr-xr-x 1 systemd-coredump systemd-coredump 47154615 Dec 17 2020 bazel-3.7.2 -rwxr-xr-x 1 root root 50478648 Dec 31 1999 bazel-real