本文在windows系统下使用visual studio 2019编译cpu版本的PyTorch。为了不影响本地已有的python环境,使用miniconda来管理python以及pytorch的安装。
1 安装miniconda
- 从https://docs.conda.io/en/latest/miniconda.html安装miniconda
- 创建虚拟环境
conda create -n pytorch-debug python=3.82 下载PyTorch源码
git clone --recursive https://github.com/pytorch/pytorch
3 安装依赖
conda install numpy ninja pyyaml mkl mkl-include setuptools cmake cffi typing_extensions future six requests dataclasses
4 配置
如果是vs2017,则set CMAKE_GENERATOR=Visual Studio 15 2017set "VS150COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build"set CMAKE_GENERATOR=Visual Studio 16 2019set DISTUTILS_USE_SDK=1set NO_CUDA=1set DEBUG=1set USE_NINJA=OFFcall "%VS150COMNTOOLS%\vcvarsall.bat" x64
5 编译安装
5.1 编译
编译完成会显示下面的信息 ```bash Finished processing dependencies for torch==1.8.0a0python setup.py build
| | | It is no longer necessary to use the ‘build’ or ‘rebuild’ targets | | | | To install: | | $ python setup.py install | | To develop locally: | | $ python setup.py develop | | To force cmake to re-generate native build files (off by default): | | $ python setup.py develop —cmake |
| |
<a name="2H9wL"></a>## 5.2 以开发者模式安装```bashpython setup.py develop
5.3 测试
import torchprint(torch.__version__)
6 使用visual studio进入PyTorch源码
6.1 创建项目
6.2 配置Python环境
6.3 调试配置
在解决方案资源管理器中,右键单击 Python 项目,依次选择“属性”、“调试”选项卡,然后选择“启用本机代码调试” ,参考在 Python 项目中启用混合模式调试
6.4 调试

- 编写代码
- 设置断点
- F5开始调试
- F11逐语句调试(可能需要等待vs加载符号)
- 进入底层源码(如果如下图所示,则成功进入底层源码)

7 常见错误
7.1 无法打开文件“python38_d.lib”
LINK : fatal error LNK1104: 无法打开文件“python38_d.lib” [D:\pytorch\build\caffe2\torch\torch_python.vcxproj]
报错表示打不开python38_d.lib,该lib是debug版本的python
解决思路:先找到python的安装路径,查看是否存在python38_d.lib ,如果存在,则可以尝试https://discuss.pytorch.org/t/fail-to-build-debug-version-on-windows/81127/4,否则可以尝试以下三种方法
- 复制
python38_d.lib重命名为python38_d.lib - 修改
pyconfig.h(最终使用这个方法解决问题),参考https://www.itread01.com/content/1548179478.html```c // 修改两处 // 1. 把227行的python38_d.lib替换为python38.libpragma comment(lib,”python38.lib”)
// 2. 注释321行
ifdef _DEBUG
//# define Py_DEBUG
endif
- 编译debug版本的python<a name="WhFvm"></a>## 7.2 import torch报错```python---------------------------------------------------------------------------ImportError Traceback (most recent call last)<ipython-input-1-eb42ca6e4af3> in <module>----> 1 import torchD:\pytorch\torch\__init__.py in <module>216 $ python setup.py develop && python -c "import torch" # This should succeed217 or by running Python from a different directory.--> 218 ''').strip()) from None219 raise # If __file__ is not None the cause is unknown, so just re-raise.220ImportError: Failed to load PyTorch C extensions:It appears that PyTorch has loaded the `torch/_C` folderof the PyTorch repository rather than the C extensions whichare expected in the `torch._C` namespace. This can occur whenusing the `install` workflow. e.g.$ python setup.py install && python -c "import torch"This error can generally be solved using the `develop` workflow$ python setup.py develop && python -c "import torch" # This should succeedor by running Python from a different directory.
根据报错信息可以有两种解决方法

