tensorflow 构建失败并出现 "missing dependency" 错误

tensorflow build fails with "missing dependency" error

我对 bazel 和 tensorflow 完全陌生,所以对于有一定经验的人来说,解决这个问题的方法可能很明显。我的 tensorflow bazel 构建失败并显示 "missing dependency" 错误消息。以下是构建命令和输出的相关序列:

(tf-gpu)kss@linux-9c32:~/projects> git clone --recurse-submodules https://github.com/tensorflow/tensorflow tensorflow-nogpu
Cloning into 'tensorflow-nogpu'...
remote: Counting objects: 16735, done.
remote: Compressing objects: 100% (152/152), done.
remote: Total 16735 (delta 73), reused 0 (delta 0), pack-reused 16583
Receiving objects: 100% (16735/16735), 25.25 MiB | 911.00 KiB/s, done.
Resolving deltas: 100% (10889/10889), done.
Checking connectivity... done.
Submodule 'google/protobuf' (https://github.com/google/protobuf.git) registered for path 'google/protobuf'
Cloning into 'google/protobuf'...
remote: Counting objects: 30266, done.
remote: Compressing objects: 100% (113/113), done.
remote: Total 30266 (delta 57), reused 0 (delta 0), pack-reused 30151
Receiving objects: 100% (30266/30266), 28.90 MiB | 1.98 MiB/s, done.
Resolving deltas: 100% (20225/20225), done.
Checking connectivity... done.
Submodule path 'google/protobuf': checked out '0906f5d18a2548024b511eadcbb4cfc0ca56cd67'
(tf-gpu)kss@linux-9c32:~/projects> cd tensorflow-nogpu/
(tf-gpu)kss@linux-9c32:~/projects/tensorflow-nogpu> ./configure 
Please specify the location of python. [Default is /home/kss/.venv/tf-gpu/bin/python]: 
Do you wish to build TensorFlow with GPU support? [y/N] 
No GPU support will be enabled for TensorFlow
Configuration finished
(tf-gpu)kss@linux-9c32:~/projects/tensorflow-nogpu> bazel build -c opt //tensorflow/tools/pip_package:build_pip_package                                                        
Sending SIGTERM to previous Bazel server (pid=8491)... done.
....
INFO: Found 1 target...
ERROR: /home/kss/.cache/bazel/_bazel_kss/b97e0e942a10977a6b42467ea6712cbf/external/re2/BUILD:9:1: undeclared inclusion(s) in rule '@re2//:re2':
this rule is missing dependency declarations for the following files included by 'external/re2/re2/perl_groups.cc':
  '/usr/lib64/gcc/x86_64-suse-linux/4.8/include/stddef.h'
  '/usr/lib64/gcc/x86_64-suse-linux/4.8/include/stdarg.h'
  '/usr/lib64/gcc/x86_64-suse-linux/4.8/include/stdint.h'
  '/usr/lib64/gcc/x86_64-suse-linux/4.8/include/x86intrin.h'
  '/usr/lib64/gcc/x86_64-suse-linux/4.8/include/ia32intrin.h'
  '/usr/lib64/gcc/x86_64-suse-linux/4.8/include/mmintrin.h'
  '/usr/lib64/gcc/x86_64-suse-linux/4.8/include/xmmintrin.h'
  '/usr/lib64/gcc/x86_64-suse-linux/4.8/include/mm_malloc.h'
  '/usr/lib64/gcc/x86_64-suse-linux/4.8/include/emmintrin.h'
  '/usr/lib64/gcc/x86_64-suse-linux/4.8/include/immintrin.h'
  '/usr/lib64/gcc/x86_64-suse-linux/4.8/include/fxsrintrin.h'
  '/usr/lib64/gcc/x86_64-suse-linux/4.8/include/adxintrin.h'.
Target //tensorflow/tools/pip_package:build_pip_package failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 144.661s, Critical Path: 1.18s
(tf-gpu)kss@linux-9c32:~/projects/tensorflow-nogpu> 

我使用的 bazel 版本是 release 0.1.4,我在 openSUSE 13.2 上是 运行。我确认头文件确实存在,这可能是预期的:

(tf-gpu)kss@linux-9c32:~/projects/tensorflow-nogpu> ll /usr/lib64/gcc/x86_64-suse-linux/4.8/include/stddef.h
-rw-r--r-- 1 root root 13619 Oct  6  2014 /usr/lib64/gcc/x86_64-suse-linux/4.8/include/stddef.h

发现此问题的人请注意

除了必须使用 --crosstool_top 而不是 --crosstool 外,使用下面 Damien 的回答。此外,如果您正在构建 GPU 加速,您还需要修改 tensorflow 存储库中的 CROSSTOOL 文件,例如:

(tf-gpu)kss@linux-9c32:~/projects/tensorflow-gpu> git diff third_party/gpus/crosstool/CROSSTOOL | cat
diff --git a/third_party/gpus/crosstool/CROSSTOOL b/third_party/gpus/crosstool/CROSSTOOL
index dfde7cd..b63f950 100644
--- a/third_party/gpus/crosstool/CROSSTOOL
+++ b/third_party/gpus/crosstool/CROSSTOOL
@@ -56,6 +56,7 @@ toolchain {
   cxx_builtin_include_directory: "/usr/lib/gcc/"
   cxx_builtin_include_directory: "/usr/local/include"
   cxx_builtin_include_directory: "/usr/include"
+  cxx_builtin_include_directory: "/usr/lib64/gcc"
   tool_path { name: "gcov" path: "/usr/bin/gcov" }

   # C(++) compiles invoke the compiler (as that is the one knowing where

您应该调整 C++ 编译器。

为此,最好的方法是: 编辑文件 tools/cpp/CROSSTOOL (https://github.com/bazelbuild/bazel/blob/master/tools/cpp/CROSSTOOL) from your package path directory (should be in ~/.bazel/base_workspace, can be found with bazel info package_path) to add a line cxx_builtin_include_directory: /usr/lib64/gcc around line 100 (see https://github.com/bazelbuild/bazel/blob/master/tools/cpp/CROSSTOOL#L101).

然后 echo "build --crosstool=//tools/cpp:toolchain" >>~/.bazelrc 然后重试构建。

抱歉造成混乱,我们正在努力使 C++ 工具链开箱即用地更好地工作。

Bazel 抱怨系统头文件,因为编译器在生成依赖项时使用 -MD(而不是 -MMD)标志。虽然使用 -MD 对于经常变化的环境是合理的,但列出对系统头文件的依赖会导致 'missing dependency declarations' 错误。

对我有帮助的是将编译器包装文件 third_party/gpus/crosstool/clang/bin/crosstool_wrapper_driver_is_not_gcc.tpl 中的“-MD”标志转换为“-MMD”标志,就在 'subprocess.call([CPU_COMPILER]...)':

之前
cpu_compiler_flags = ['-MMD' if flag == '-MD' else flag for flag in cpu_compiler_flags]

和third_party/sycl/crosstool/computecpp.tpl,相似的地方:

computecpp_device_compiler_flags = ['-MMD' if flag == '-MD' else flag for flag in computecpp_device_compiler_flags]