Windows10下bazel编译tensorflow的graph_transforms源码用于.pb模型压缩,过程之艰辛让你不敢置信

  • Post author:
  • Post category:其他


1、安装bazel 3.7.2版本

以下是我完整的编译过程,记录下来各位少踩点坑

1、安装bazel=3.7.2 从官网下载

https://github.com/bazelbuild/bazel/tags

,版本可以选择,直接下载.exe,并把路径配置到环境变量中,可以使用bazel –version查看版本,

前期先确保电脑已安装JDK 8和Microsort Visual C++ Redistributable for Visual Studio 2015

2、安装MSYS2,从

http://www.msys2.org/

下载安装,如果慢可以使用镜像源网站下载,一路默认安装即可,最后会打开命令行输入:pacman -Syuu(更新包),等待完成配置环境变量即可

3、查看自己电脑的硬件cuda版本,从官网下载安装CUDA 11.1和cuDNN 8.2

4、git clone

https://github.com/tensorflow/tensorflow

5、进入源码根目录

6、执行python ./configure.py配置自己的电脑情况

7、下面每一步的询问根据自己电脑的情况仔细回答:

(pytorch) D:\pycharm\homework\icc\tensorflow>python ./configure.py
You have bazel 3.7.2 installed.
Please specify the location of python. [Default is C:\Users\miao\Anaconda3\envs\pytorch\python.exe]:


Found possible Python library paths:
  C:\Users\miao\Anaconda3\envs\pytorch\lib\site-packages
Please input the desired Python library path to use.  Default is [C:\Users\miao\Anaconda3\envs\pytorch\lib\site-packages]

Do you wish to build TensorFlow with ROCm support? [y/N]: n
No ROCm support will be enabled for TensorFlow.

Do you wish to build TensorFlow with CUDA support? [y/N]: y
CUDA support will be enabled for TensorFlow.

Could not find any cuda.h matching version '' in any subdirectory:
        ''
        'include'
        'include/cuda'
        'include/*-linux-gnu'
        'extras/CUPTI/include'
        'include/cuda/CUPTI'
        'local/cuda/extras/CUPTI/include'
of:


Asking for detailed CUDA configuration...

Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 10]: 11.1


Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7]: 8.2


Please specify the comma-separated list of base paths to look for CUDA libraries and headers. [Leave empty to use the default]: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1


Found CUDA 11.1 in:
    C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/lib/x64
    C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/include
Found cuDNN 8 in:
    C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/lib/x64
    C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/include


Please specify a list of comma-separated CUDA compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus. Each capability can be specified as "x.y" or "compute_xy" to include both virtual and binary GPU code, or as "sm_xy" to only include the binary code.
Please note that each additional compute capability significantly increases your build time and binary size, and that TensorFlow only supports compute capabilities >= 3.5 [Default is: 3.5,7.0]: 7.5


Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]: -march=native


Would you like to override eigen strong inline for some C++ compilation to reduce the compilation time? [Y/n]: n
Not overriding eigen strong inline, some compilations could take more than 20 mins.

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=mkl_aarch64    # Build with oneDNN and Compute Library for the Arm Architecture (ACL).
        --config=monolithic     # Config for mostly static monolithic build.
        --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=nogcp          # Disable GCP support.
        --config=nonccl         # Disable NVIDIA NCCL support.

其中Please note that each additional compute capability significantly increases your build time and binary size, and that TensorFlow only supports compute capabilities >= 3.5 [Default is: 3.5,7.0]: 7.5,这个


7.5是自己电脑的gpu算力,可以在英伟达官网查询到,我的GPU是RTX2060

8、执行bazel build tensorflow/tools/graph_transforms:transform_graph开始编译ransform_graph,这个过程以比较耗费gpu资源,基本上是在占用率100%,我是晚上8点多开始编译,12点睡觉之前没完成,早上6点醒了已经编译完成,具体时间不知道,但是至少要4个小时以上,在4~10个小时之间,所以建议晚上编译,或者找个电脑空闲的时间,期间尽量少操作电脑

9、第8步极易出错,如果出现ERROR: ‘sandboxed’ was requested for default strategies but no strategy with that identifier was registered. Valid values are: [standalone, worker, local],网上各种搜索找不到解决问题的办法,最终还是要靠自己,然后在官网

https://docs.bazel.build/versions/4.1.0/user-manual.html

上查到

--spawn_strategy

的设置情况,然后将第8步骤的编译命令改为:


bazel build tensorflow/tools/graph_transforms:transform_graph –spawn_strategy=local,可以最终编译成功

INFO: Options provided by the client:
  Inherited 'common' options: --isatty=1 --terminal_columns=120
INFO: Reading rc options for 'build' from d:\pycharm\homework\icc\tensorflow\.bazelrc:
  Inherited 'common' options: --experimental_repo_remote_exec
INFO: Options provided by the client:
  'build' options: --python_path=C:/Users/miao/Anaconda3/python.exe
INFO: Reading rc options for 'build' from d:\pycharm\homework\icc\tensorflow\.bazelrc:
  'build' options: --define framework_shared_object=true --java_toolchain=@tf_toolchains//toolchains/java:tf_java_toolchain --host_java_toolchain=@tf_toolchains//toolchains/java:tf_java_toolchain --define=use_fast_cpp_protos=true --define=allow_oversize_protos=true --spawn_strategy=sandboxed -c opt --announce_rc --define=grpc_no_ares=true --noincompatible_remove_legacy_whole_archive --noincompatible_prohibit_aapt1 --enable_platform_specific_config --define=with_xla_support=true --config=short_logs --config=v2 --define=no_aws_support=true --define=no_hdfs_support=true
INFO: Reading rc options for 'build' from d:\pycharm\homework\icc\tensorflow\.tf_configure.bazelrc:
  'build' options: --action_env PYTHON_BIN_PATH=C:/Users/miao/Anaconda3/python.exe --action_env PYTHON_LIB_PATH=C:/Users/miao/Anaconda3/lib/site-packages --python_path=C:/Users/miao/Anaconda3/python.exe --action_env CUDA_TOOLKIT_PATH=C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1 --action_env TF_CUDA_COMPUTE_CAPABILITIES=7.5 --config=cuda --copt=/d2ReducedOptimizeHugeFunctions --host_copt=/d2ReducedOptimizeHugeFunctions
INFO: Found applicable config definition build:short_logs in file d:\pycharm\homework\icc\tensorflow\.bazelrc: --output_filter=DONT_MATCH_ANYTHING
INFO: Found applicable config definition build:v2 in file d:\pycharm\homework\icc\tensorflow\.bazelrc: --define=tf_api_version=2 --action_env=TF2_BEHAVIOR=1
INFO: Found applicable config definition build:cuda in file d:\pycharm\homework\icc\tensorflow\.bazelrc: --repo_env TF_NEED_CUDA=1 --crosstool_top=@local_config_cuda//crosstool:toolchain --@local_config_cuda//:enable_cuda
INFO: Found applicable config definition build:windows in file d:\pycharm\homework\icc\tensorflow\.bazelrc: --copt=/W0 --copt=/D_USE_MATH_DEFINES --host_copt=/D_USE_MATH_DEFINES --cxxopt=/std:c++14 --host_cxxopt=/std:c++14 --config=monolithic --copt=-DWIN32_LEAN_AND_MEAN --host_copt=-DWIN32_LEAN_AND_MEAN --copt=-DNOGDI --host_copt=-DNOGDI --copt=/experimental:preprocessor --host_copt=/experimental:preprocessor --linkopt=/DEBUG --host_linkopt=/DEBUG --linkopt=/OPT:REF --host_linkopt=/OPT:REF --linkopt=/OPT:ICF --host_linkopt=/OPT:ICF --verbose_failures --distinct_host_configuration=false
INFO: Found applicable config definition build:monolithic in file d:\pycharm\homework\icc\tensorflow\.bazelrc: --define framework_shared_object=false
DEBUG: C:/users/miao/_bazel_miao/5mod3muh/external/tf_runtime/third_party/cuda/dependencies.bzl:51:10: The following command will download NVIDIA proprietary software. By using the software you agree to comply with the terms of the license agreement that accompanies the software. If you do not agree to the terms of the license agreement, do not use the software.
INFO: Analyzed target //tensorflow/tools/graph_transforms:transform_graph (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //tensorflow/tools/graph_transforms:transform_graph up-to-date:
  bazel-bin/tensorflow/tools/graph_transforms/transform_graph.exe
INFO: Elapsed time: 20150.407s, Critical Path: 14128.95s
INFO: 963 processes: 1 internal, 962 local.
INFO: Build completed successfully, 963 total actions

10、下面开始压缩模型,未完待续。。。



版权声明:本文为miao3266原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。