有没有人使用以下配置成功构建tensorflow pythonwheel:
- CPU(非 GPU)
- 操作系统:Windows 7/server 2012
- 使用英特尔MKL和/或mkl-dnn
- Python 3.6
我已经挣扎了好几天,试图调整 bazel 文件和 cmake 文件,但没有成功。
想知道是否有人成功并愿意分享他所做的事情。
谢谢,利奥尔
最佳答案
首先,从早期的 v1.x 版本开始,直到现在的 v2.x 版本,从源代码构建 TensorFlow 一直是一项具有挑战性的壮举。然而,作为我工作的一部分,我不得不先用 cmake 然后用 bazel 来努力构建它。我强烈建议不要使用 cmake 进行构建。由于它没有得到官方支持,由于依赖版本不同,它不会生成与 bazel 版本相同的二进制文件,并且需要对 cmake 文件/脚本甚至源代码进行大量修改才能使其工作。
作为构建 tensorflow 的重要信息来源,我建议两个链接:
[2]:.bazelrc file
第一个链接是来自 google 的官方构建文档,第二个链接经常被忽略,它包含对构建过程的各种有用的提示。其中一个提示是,从版本 2.0 到当前版本(2.9,在撰写本文时),Windows/Mac 不支持 MKL。然而,正如 @Lior-Cohen 的评论中正确指出的那样,在 CPU/GPU 上运行许多运算符需要 MKL。
因此,您需要使用具有 MKL 支持的 bazel 构建 tensorflow 。因此,打开官方构建文档链接并按照说明准备您的环境(Visual Studio、CUDA/CUDNN(如果需要)、msys、python...)。从其 github repo 下载兼容的 bazel 。要检查 bazel 兼容性,请参阅 the table 。在运行bazel build命令之前,您可以设置BAZEL_VC环境变量,因为bazel可能很难找到您的构建工具。因此,请根据预先在命令提示符下安装的构建工具的版本更改以下命令:
set BAZEL_VC=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC
预先警告您在构建过程中可能会遇到许多错误。你必须一一阅读并解决它们。您的情况可能会有所不同,因为许多错误可能是由于下载尝试失败、互联网连接速度慢、在线源停机、由于在线源中的文件更改而导致文件哈希值更改等造成的。因此,请耐心等待,一次解决一个问题,阅读错误并按照错误堆栈检查源链接或脚本。 使用 MKL 构建 TF 的一个主要问题是以下错误:
MASM : warning A4018:invalid command-line option : -B
Assembling: external/local_config_cuda/crosstool/windows/msvc_wrapper_for_nvcc.py
external/local_config_cuda/crosstool/windows/msvc_wrapper_for_nvcc.py(1) : error A2044:invalid character in file
external/local_config_cuda/crosstool/windows/msvc_wrapper_for_nvcc.py(2) : error A2044:invalid character in file
...
提出了两种解决方法,一种是github上建议的 issue 25213另一种是创建一个假的 ML64.exe,它可以处理预期的汇编文件。以下是 C# 代码片段的源代码,如果在您的系统上对其进行编译(作为带有 ML64.exe.config 文件的 exe)并替换为原始 ML64.exe,则可以继续构建过程顺利。将原来的ML64.exe重命名为ML64-org.exe。在 VS 中创建一个简单的 C# 控制台项目:
using System;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
namespace ML64
{
static internal class Program
{
static void Main(string[] args)
{
var apr = new AppSettingsReader();
var dir = apr.GetValue("dir", typeof(string)) as string;
var ml = apr.GetValue("ml", typeof(string)) as string;
var importantArgs = args.Skip(2).Aggregate((a, b) => $"{a} {b}");
var psi = new ProcessStartInfo(ml, importantArgs) { RedirectStandardOutput = true, UseShellExecute = false, WorkingDirectory = dir };
using (var proc = Process.Start(psi))
{
proc.WaitForExit();
Console.Write(proc.StandardOutput.ReadToEnd());
}
}
}
}
并将 app.config 更改为以下内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<appSettings>
<!--Path to your TF source where configure.py resides.-->
<add key="dir" value="G:\2.9.0"/>
<!--Path to your ML64 that TF build complains with errors.-->
<add key="ml" value="C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\ml64-org.exe"/>
</appSettings>
</configuration>
我用于 C++ 项目的 bazel 构建是:bazel --output_base=g:/base build --config=cuda --config=mkl --config=numa --config=monolithic tensorflow:tensorflow.dll
对于您的 python 项目,您可以运行以下命令:bazel build --config=opt --config=cuda --config=mkl --define=no_tensorflow_py_deps=true //tensorflow/tools/pip_package:build_pip_package
关于python - tensorflow 构建 - CPU/MKL/Windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48009243/