3

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

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

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

我将 CMake 安装为组件的 Visual Studio 2019,因此我运行了 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.

现在第一个问题,既不工作make也不nmake工作:

'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 ==========

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

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

4

2 回答 2

5

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

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

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

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

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

cmake --build . --config Release

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

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)
于 2019-11-23T14:48:03.427 回答
0

我无法直接回答之前的答案。我不确定我是否完全理解正在发生的事情,但我找到了一种避免错误 torch-NOTFOUND.obj 并让我的项目在 RelWithDebugInfo 中编译的方法。该错误似乎是 IMPORTED_LOCATION 错误问题IMPORTED_LOCATION 和 -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-05-18T17:01:04.430 回答