Tensorflow Compile

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,324评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,356评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,328评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,147评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,160评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,115评论 1 296
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,025评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,867评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,307评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,528评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,688评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,409评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,001评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,657评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,811评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,685评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,573评论 2 353

推荐阅读更多精彩内容