Windows 在 C++ 应用程序中使用 PyTorch 模型

Using a PyTorch model in C++ application on Windows

按照官方PyTorch tutorial,我在Python中创建了模型,通过跟踪将其转换为Torch Script,并将脚本模块保存到.pt文件中。加载模型和 CMakeLists 的 C++ 代码与教程中的相同。

我下载了 LibTorch 1.3(稳定版,Windows,无 CUDA,发布)并解压,所以我的目录结构是:

│
├───artifact
│       traced_resnet_model.pt
│       
├───cmakeapp
│   │   CMakeLists.txt
│   │   example-app.cpp
│   │   
├───libtorch
│   │   build-hash   
│   ├───bin
│   ├───cmake
│   ├───include
│   ├───lib
│   ├───share
│   └───test

我有 Visual Studio 2019,CMake 作为组件安装,所以我 运行 VS2019 的开发人员命令提示和 cd 到项目目录 (cmakeapp)。

根据指南,我运行使用以下命令来构建应用程序:

mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=..\libtorch ..
make

CMake 似乎成功了,除了一些警告:

CMake Warning (dev) at D:/dox/projects/AI/torchscript/libtorch/share/cmake/Caffe
2/public/utils.cmake:57 (if):
  Policy CMP0054 is not set: Only interpret if() arguments as variables or
  keywords when unquoted.  Run "cmake --help-policy CMP0054" for policy
  details.  Use the cmake_policy command to set the policy and suppress this
  warning.

  Quoted variables like "MSVC" will no longer be dereferenced when the policy
  is set to NEW.  Since the policy is not set the OLD behavior will be used.
Call Stack (most recent call first):
  D:/dox/projects/AI/torchscript/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:
121 (caffe2_interface_library)
  D:/dox/projects/AI/torchscript/libtorch/share/cmake/Torch/TorchConfig.cmake:40
 (find_package)
  CMakeLists.txt:4 (find_package)
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at D:/dox/projects/AI/torchscript/libtorch/share/cmake/Torch
/TorchConfig.cmake:90 (if):
  Policy CMP0054 is not set: Only interpret if() arguments as variables or
  keywords when unquoted.  Run "cmake --help-policy CMP0054" for policy
  details.  Use the cmake_policy command to set the policy and suppress this
  warning.

  Quoted variables like "MSVC" will no longer be dereferenced when the policy
  is set to NEW.  Since the policy is not set the OLD behavior will be used.
Call Stack (most recent call first):
  CMakeLists.txt:4 (find_package)
This warning is for project developers.  Use -Wno-dev to suppress it.

现在 第一期 makenmake 都不起作用:

'make' is not recognized as an internal or external command, operable program or batch file.

D:\dox\projects\AI\torchscript\cmakeapp\build>nmake

Microsoft (R) Program Maintenance Utility Version 14.23.28107.0 Copyright (C) Microsoft Corporation.  All rights reserved.

NMAKE : fatal error U1064: MAKEFILE not found and no target specified Stop.

我是不是漏掉了什么?

其次,找到生成的custom_ops.sln文件,所以在Visual Studio中打开。该项目提供 4 种不同的配置:Debug、MinSizeRel、Release 和 RelWithDebInfo。构建除 Release 之外的任何东西都失败了:

LINK : fatal error LNK1181: cannot open input file 'torch-NOTFOUND.obj'
2>Done building project "example-app.vcxproj" -- FAILED.

我对这个错误感到非常惊讶,因为指定了 libtorch 路径并且 CMake 成功找到了它。

第三,构建发布成功,但跳过ALL_BUILD项目:

3>------ Skipped Build: Project: ALL_BUILD, Configuration: Release x64 ------
3>Project not selected to build for this solution configuration 
========== Build: 2 succeeded, 0 failed, 0 up-to-date, 1 skipped ==========

不确定应该选择什么解决方案配置来构建所有这些。

如能澄清这些令人困惑的问题,我将不胜感激。

linked site are Linux-centric, and appear to assume the user is operating in a Linux environment. On Linux, the last command make would work without any issue, but you are likely building with Visual Studio, not make. You should instead take the cross-platform approach, and tell CMake to build with whatever build tool it found during configuration; Try using cmake build . for the last command instead, as you can see is used in this other tutorial上的说明:

mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch ..
cmake --build .

但是,该教程中提到的是:

On Windows, debug and release builds are not ABI-compatible. If you plan to build your project in debug mode, we recommend building PyTorch from source.

这表明发布配置应该可以工作,而您需要下载 source code from Github 以在调试模式下构建。由于 MSVC 默认构建调试,您应该修改最后一条命令以指示 Release 配置:

cmake --build . --config Release

此外,当使用 MSVC 在 Windows 上构建时,他们的安装教程建议将以下行附加到您的 CMake 文件中以避免此 issue thread 中讨论的错误,这也可能有助于解决您遇到的问题正在看:

if (MSVC)
  file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
  add_custom_command(TARGET example-app
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${TORCH_DLLS}
                     $<TARGET_FILE_DIR:example-app>)
endif (MSVC)

我不能直接回答前面的答案。 我不确定我是否完全理解正在发生的事情,但我找到了一种方法来避免错误 torch-NOTFOUND.obj 并让我的项目在 RelWithDebugInfo 中编译。该错误似乎是 IMPORTED_LOCATION 错误问题 IMPORTED_LOCATION and -NOTFOUND。 如果你进入 libtorch\share\cmake\Caffe2\Caffe2Targets-release.cmake 最后(1.5.0 版本的第 51-53 行)你会发现:

set_target_properties(torch PROPERTIES
  IMPORTED_IMPLIB_RELEASE "${_IMPORT_PREFIX}/lib/torch.lib"
  IMPORTED_LOCATION_RELEASE "${_IMPORT_PREFIX}/lib/torch.dll"
  )

我替换为:

set_target_properties(torch PROPERTIES
  IMPORTED_IMPLIB "${_IMPORT_PREFIX}/lib/torch.lib"
  IMPORTED_LOCATION "${_IMPORT_PREFIX}/lib/torch.dll"
  )

编译成功