从源代码构建PyTorch

3

我想在我的MacOS(版本10.14.3)上从源码安装PyTorch以使用GPU。 我已经按照链接中的文档进行了操作。 当我在终端中运行MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ python setup.py install时,终端会显示以下错误:

[ 69%] Built target caffe2_observers make: *** [all] Error 2 Traceback (most recent
call last): File "setup.py", line 710, in <module>
build_deps() 
File "setup.py", line 282, in build_deps 
build_dir='build')
File "/Users/Desktop/pytorch/tools/build_pytorch_libs.py", line 259, in build_caffe2
check_call(['make', '-j', str(max_jobs), 'install'], cwd=build_dir, env=my_env)
File "/Users/anaconda3/lib/python3.6/subprocess.py", line 291, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['make', '-j', '4', 'install']'
returned non-zero exit status 2.

我试图升级pip并重新安装anaconda和cuda,但没有成功。

在这里,您可以找到错误信息:

[ 69%] Building CXX object modules/observers/CMakeFiles/caffe2_observers.dir/net_observer_reporter_print.cc.o
In file included from <built-in>:1:
In file included from /usr/local/cuda/include/cuda_runtime.h:115:
In file included from /usr/local/cuda/include/crt/common_functions.h:77:
/Library/Developer/CommandLineTools/usr/include/c++/v1/string.h:61:15: fatal error: 'string.h' file not found
#include_next <string.h>
          ^~~~~~~~~~
1 error generated.
CMake Error at caffe2_gpu_generated_THCReduceApplyUtils.cu.o.Release.cmake:219 (message):
    Error generating
/Users/Desktop/pytorch/build/caffe2/CMakeFiles/caffe2_gpu.dir/__/aten/src/THC/./caffe2_gpu_generated_THCReduceApplyUtils.cu.o

make[2]: *** [caffe2/CMakeFiles/caffe2_gpu.dir/__/aten/src/THC/caffe2_gpu_generated_THCReduceApplyUtils.cu.o] Error 1
make[2]: *** Waiting for unfinished jobs....
Scanning dependencies of target torch_shm_manager
1 error generated.

如果你进入源代码目录(即Makefile所在的位置)并运行make -j 4 install,会发生什么? - matt2000
在运行 make -j 4 install 前,我遇到了以下错误 ModuleNotFoundError: No module named '__main__.setup_helpers'; '__main__' is not a package - Jonor
在您提供的信息之前,应该打印有关实际问题的更多详细信息。您能找到这些信息吗? - Sergii Dymchenko
我没有看到它。我已经更新了我的帖子。 - Jonor
2个回答

2

/Library/Developer/CommandLineTools/usr/include/c++/v1/string.h:61:15:fatal error: 'string.h' file not found

看起来你缺少了 C++ 头文件 string.h;我的建议是尝试安装/重新安装 Xcode 命令行工具(如果你还没有安装):

$ xcode-select --install

如果命令行工具已经安装,那么可以尝试通过卸载(删除目录)来解决问题:

如果命令行工具已经安装,则可以尝试通过卸载(删除目录)来解决问题:

"Original Answer" 翻译成 "最初的回答"。
$ rm -rf /Library/Developer/CommandLineTools

完成删除后,请再次尝试第一个命令,然后检查是否已安装string.h





$ find /Library/Developer/CommandLineTools/usr -type f -name string.h
/Library/Developer/CommandLineTools/usr/include/c++/v1/string.h

我重新安装了Xcode并在正确的文件夹中找到了string.h,但是PyTorch的安装仍然给我带来了我描述的相同错误。 - Jonor
2
@Jonor:进入pytorch目录并执行rm -rf build命令。当你说你遇到了相同的错误时,是“文件未找到”,是string.h吗? - l'L'l
没错,我已经执行了 rm -rf build 命令,并重新启动了安装过程,这次安装从0%开始(而之前直接从69%开始)。 - Jonor
1
@Jonor:好的,那就是您想要的;如果这不起作用,我可能还有一些其他建议,但希望这个可以解决问题。 - l'L'l
供参考,当在 macOS 上从源代码安装 pytorch 时遇到不同但相关的问题时,rm -rf build 命令可提供帮助。 - atineoSE
显示剩余4条评论

1
我遇到了这个问题,因为Nvidia与OSX Mojave 10.14+不兼容。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接