【问题标题】:Using a PyTorch model in C++ application on Windows在 Windows 上的 C++ 应用程序中使用 PyTorch 模型
【发布时间】:2020-03-19 06:04:29
【问题描述】:

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

我下载了LibTorch 1.3 (stable, Windows, no CUDA, release)并解压,所以我的目录结构是:

│ ├────神器 │ traced_resnet_model.pt │ ├───cmakeapp │ │ CMakeLists.txt │ │ example-app.cpp │ │ ├────libtorch │ │ 构建哈希 │ ├───bin │ ├───cmake │ ├───包括 │ ├───lib │ ├───分享 │ └───测试

我将 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 成功找到了它。

第三,构建Release成功,但是跳过了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 ==========

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

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

【问题讨论】:

    标签: visual-studio cmake libtorch torchscript


    【解决方案1】:

    linked site 上的说明以 Linux 为中心,似乎假设用户在 Linux 环境中操作。在 Linux 上,最后一个命令 make 可以正常工作,但您可能使用 Visual Studio 构建,而不是 make。相反,您应该采用跨平台方法,并告诉 CMake 使用它在配置期间找到的任何构建工具进行构建;尝试使用cmake build . 代替最后一个命令,如您所见,在this other tutorial 中使用:

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

    但是,该教程中提到的内容如下:

    在 Windows 上,调试和发布版本与 ABI 不兼容。如果您计划在调试模式下构建项目,我们建议您从源代码构建 PyTorch。

    这表明发布配置应该可以工作,而您需要下载source code from Github 以在调试模式下构建。由于 MSVC 默认构建 Debug,您应该修改最后一条命令以指示 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)
    

    【讨论】:

    • 感谢您的帮助。不幸的是,它也会导致同样的问题(除非我指定 --config Release): LINK : fatal error LNK1181: cannot open input file 'torch-NOTFOUND.obj'
    • @mentalmushroom 似乎 CMake 仍然存在问题,如 NOTFOUND 所示。进行这些更改后,您是否从头开始运行 CMake? IE。删除 CMake 缓存或移除构建目录并重新运行 CMake?
    • 是的,我是从头开始做的,似乎成功了(我没有看到任何错误)。
    • 我想指出cmake --build . --config Release 成功构建了一个工作应用程序,所以它让我认为问题与配置有关,但指南并没有具体说明。
    • @mentalmushroom 我很高兴它起作用了!我扩展了我的答案,包括对 Debug vs Release 配置的解释。他们的文档表明,按照教程中列出的步骤,通常无法构建 Debug。
    【解决方案2】:

    我无法直接回答之前的答案。 我不确定我是否完全理解正在发生的事情,但我找到了一种方法来避免错误 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"
      )
    

    我可以编译成功。

    【讨论】:

      猜你喜欢
      • 2020-02-01
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      • 2015-10-31
      相关资源
      最近更新 更多