美文网首页
Tensorflow Compile

Tensorflow Compile

作者: XBruce | 来源:发表于2022-06-15 10:19 被阅读0次

    Error building TensorFlow 2.8 in Windows 10 - Special Interest Groups / Build - TensorFlow Forum

    Build from source TensorFlow 2.8 C/C++ DLLs on Windows 10 & 11.

    Here is my latest notes to build TensorFlow C/C++ DLLs on Windows.

    1. Set Windows 10 to Developer Mode !!!
      Why: Symlinks in Windows 10! - Windows Developer Blog 6
      How: Enable your device for development - Windows apps | Microsoft Docs 2
    2. Set Windows Registry to long path names (Python scripts reference long path names):
      See: Maximum Path Length Limitation - Win32 apps | Microsoft Docs 1

    RegEdit
    Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem
    LongPathsEnabled = 1

    1. Follow the steps to install and build TensorFlow from this link:
      Build from source on Windows | TensorFlow 4
      a. Install Visual Studio Community 2019
      i. Check Desktop development with C++
      ii. Check C++ Clang tools for Windows
      iii. Check Individual components: Git for Windows
      iv. Check Individual components: GitHub extension for Visual Studio
      b. Install Go Programming Language
      c. Install Bazelisk
      GitHub - bazelbuild/bazelisk: A user-friendly launcher for Bazel. 2
      i. Set environment variable USE_BAZEL_VERSION=4.2.1
      ii. go install github.com/bazelbuild/bazelisk@latest 1
      iii. rename bazelisk.exe to bazel.exe
      iv. Make sure path to bazel.exe is added to PATH env var.
      v. Check version is 4.2.1: bazel –version
      d. Install Python 3.9.10 for Windows
      Python Releases for Windows | Python.org 2
      i. Check pip
      ii. Check py launcher
      iii. After installed, check version and where it is located:
      python –version
      py –version
      where python
      where py
      iv. Upgrade pip
      py -m pip install --upgrade pip
      e. Install the TensorFlow pip package dependencies:
      i. pip3 install -U six numpy wheel packaging
      ii. pip3 install -U keras_preprocessing --no-deps
      f. Install MSYS2. Add C:\msys64\usr\bin to PATH.
      g. Reopen command prompt and enter:
      pacman -S git patch unzip
      h. Install CUDA SDK 11.2 (if Windows 11, see below item iii.)
      i. Check tested build configurations
      Build from source | TensorFlow 4
      ii. Check version of CUDA:
      nvcc –version

    iii. Note: If Windows 11 , install Cuda SDK 11.6.1, and CuDNN 8.3.3) Also download zlibwapi.dll and copy to C:\Windows\System32. See zlib required for cuDNN 8.3.0 on Windows · Issue #6226 · cupy/cupy · GitHub 1 and Installation Guide :: NVIDIA Deep Learning cuDNN Documentation 1

    1. After you clone TensorFlow, git checkout r2.8

    2. Make sure to use the tensorFlow, python, compiler, bazel, cuDNN, and CUDA versions as listed in the table “Tested build configurations.” (Windows 11 and CUDA 11.6 is not listed as “tested” but seems to work.)
      Build from source on Windows | TensorFlow 4

    3. Configure the build for CPU:
      Build from source on Windows | TensorFlow 2
      If you configure for GPU, you will need to set the env variable with forward slashes:
      CUDNN_INSTALL_PATH=C:/Program Files/NVIDIA/CUDNN/v8.1

    *Note: If Windows 11,
    CUDNN_INSTALL_PATH=C:/Program Files/NVIDIA/CUDNN/v8.3

    1. Open the BUILD file to see the package names you can build:
      repos\GitHub\tensorflow\tensorflow\BUILD. You should see:
      tf_cc_shared_object(
      name = “tensorflow_cc”,

      genrule(
      name = “install_headers”,

    The interface library (tensorflow_cc.dll.if.lib) for linking tensorflow DLL library (tensorflow_cc.dll) on Windows.

    To learn more about import library (called interface library in Bazel):

    Link an executable to a DLL | Microsoft Docs 1

    filegroup(
    name = “get_tensorflow_cc_dll_import_lib”,
    srcs = ["//tensorflow:tensorflow_cc.dll"],
    output_group = “interface_library”,
    visibility = ["//visibility:public"],
    )

    Rename the import library for tensorflow.dll from tensorflow_cc.dll.if.lib to tensorflow.lib

    genrule(
    name = “tensorflow_cc_dll_import_lib”,
    srcs = [":get_tensorflow_cc_dll_import_lib"],
    outs = [“tensorflow_cc.lib”],
    cmd = select({
    “//tensorflow:windows”: “cp -f <@”,
    “//conditions:default”: “touch $@”, # Just a placeholder for Unix platforms
    }),
    visibility = ["//visibility:public"],
    )

    genrule(
    name = “install_headers”,
    srcs = [
    “//tensorflow/c:headers”,
    “//tensorflow/c/eager:headers”,
    “//tensorflow/cc:headers”,
    “//tensorflow/core:headers”,
    ],
    outs = [“include”],

    1. Disable anti- virus checker while compiling !!!

    2. Open the Command Prompt – Run as Admin !!!

    3. Change directory to:
      repos\GitHub\tensorflow

    4. Build the C/C++ Dynamic-link library (.dll):
      bazel build --config=opt tensorflow:tensorflow_cc

    5. Build the import library (.lib)
      bazel build --config=opt tensorflow:get_tensorflow_cc_dll_import_lib

    6. Rename the import library for tensorflow_cc.dll from tensorflow_cc.dll.if.lib to tensorflow_cc.lib
      bazel build --config=opt tensorflow:tensorflow_cc_dll_import_lib

    7. Install headers
      bazel build --config=opt tensorflow:install_headers

    8. Build an example project that links to tensorflow_cc.lib (see Ref1 or Ref2 reference below for example project)

    9. If you get linking errors, then follow this link below: (see also Ref2 and Ref3 for TF_EXPORT below)
      Tensorflow 2.3 unresolved external symbols in machine-generated files when building C++ project on Windows - Stack Overflow 2
      a. Add missing symbols to the file tensorflow_filtered_def_file.def
      b. Delete tensorflow_cc.dll and tensorflow_cc.lib (they are read-only).
      c. After you updated .def file, open the following command prompt:
      “x64 Native Tools Command Prompt for VS 2019”
      d. Change directory to:
      C:\Users\pbagwell\repos\GitHub\tensorflow

    e. Rebuild the DLL by calling the following command:
    link.exe /nologo /DLL /SUBSYSTEM:CONSOLE -defaultlib:advapi32.lib -DEFAULTLIB:advapi32.lib -ignore:4221 /FORCE:MULTIPLE /MACHINE:X64 @bazel-out/x64_windows-opt/bin/tensorflow/tensorflow_cc.dll-2.params /OPT:ICF /OPT:REF /DEF:bazel-out/x64_windows-opt/bin/tensorflow/tensorflow_filtered_def_file.def /ignore:4070
    17. To change the name of the output library, for example to tensorflow_cc_gpu.dll, modify the file, “C:\Users\pbagwell\repos\GitHub\tensorflow_gpu\bazel-out\x64_windows-opt\bin\tensorflow\tensorflow_cc.dll-2.params”.

    From:
    /OUT:bazel-out/x64_windows-opt/bin/tensorflow/tensorflow_cc.dll
    /IMPLIB:bazel-out/x64_windows-opt/bin/tensorflow/tensorflow_cc.dll.if.lib

    To:
    /OUT:bazel-out/x64_windows-opt/bin/tensorflow/tensorflow_cc_gpu.dll
    /IMPLIB:bazel-out/x64_windows-opt/bin/tensorflow/tensorflow_cc_gpu.dll.if.lib

    Then relink as in step 15e.
    References:
    Example project with TF_EXPORT:
    Tensorflow 2.x C++ API for object detection (inference)
    Ref1. 3 dogs object detection: https://medium.com/@reachraktim/using-the-new-tensorflow-2-x-c-api-for-object-detection-inference-ad4b7fd5fecc 3

    TF_EXPORT
    Ref2. https://medium.com/vitrox-publication/deep-learning-frameworks-tensorflow-build-from-source-on-windows-python-c-cpu-gpu-d3aa4d0772d8 3
    Ref3. https://ashley-tharp.medium.com/btw-if-you-enjoy-my-tutorial-i-always-appreciate-endorsements-on-my-linkedin-https-www-linkedin-a6d6fcba1e44 1

    *Note: clean bazel between cpu and gpu builds by calling:
    bazel clean –expunge


    I got TensorFlow 2.8 built on the Windows 11 Alienware (CPU and GPU). When I went to the nVidia site to get the CUDA SDK for Windows 11, CUDA SDK 11.2 was not listed. The earliest version for W11 was 11.4.3, which was not listed in TensorFlow as “tested”. I went ahead and installed the latest CUDA SDK 11.6.1 (which includes the latest display driver 511.65) and the latest cuDNN 8.3.3, and so far, it works for the 3 dogs example. I updated the document “Techmah CMF\Product Development - General\Engineering\SW\TensorFlow\How_to_Build_TensorFlow2.8_2022_03_25.docx”.

    Paul Bagwell

    Bellow solution Not work for me.

    I have built tensorflow2.8.0rc0 with bazel 4.2.2 successfully, where I
    only used Visual Studio 2022 but with old msvc v142 tools set installed.

    My solution is:

    Firstly, make a soft link named "C:\Program Files\Microsoft Visual Studio\2019" to “C:\Program Files\Microsoft Visual Studio\2022”
    Then set 2 env variable like

    $env:BAZEL_VC="C:\Program Files\Microsoft Visual Studio\2019\Community\VC"
    $env:BAZEL_VC_FULL_VERSION="14.29.30133"
    mklink /d C:\Program Files\Microsoft Visual Studio\2019  C:\Program Files\Microsoft Visual Studio\2022
    vcpkg install tensorflow-cc:x64-windows
    

    相关文章

      网友评论

          本文标题:Tensorflow Compile

          本文链接:https://www.haomeiwen.com/subject/zaxxvrtx.html