Windows FAQ#
创建于:2018年4月23日 | 最后更新于:2025年5月20日
从源代码构建#
包含可选组件#
Windows PyTorch 支持两个可选组件:MKL 和 MAGMA。以下是使用它们进行构建的步骤。
REM Make sure you have 7z and curl installed.
REM Download MKL files
curl https://s3.amazonaws.com/ossci-windows/mkl_2020.2.254.7z -k -O
7z x -aoa mkl_2020.2.254.7z -omkl
REM Download MAGMA files
REM version available:
REM 2.5.4 (CUDA 10.1 10.2 11.0 11.1) x (Debug Release)
REM 2.5.3 (CUDA 10.1 10.2 11.0) x (Debug Release)
REM 2.5.2 (CUDA 9.2 10.0 10.1 10.2) x (Debug Release)
REM 2.5.1 (CUDA 9.2 10.0 10.1 10.2) x (Debug Release)
set "CUDA_PREFIX=cuda102"
set "CONFIG=release"
set "HOST=https://s3.amazonaws.com/ossci-windows"
curl -k "%HOST%/magma_2.5.4_%CUDA_PREFIX%_%CONFIG%.7z" -o magma.7z
7z x -aoa magma.7z -omagma
REM Setting essential environment variables
set "CMAKE_INCLUDE_PATH=%cd%\mkl\include"
set "LIB=%cd%\mkl\lib;%LIB%"
set "MAGMA_HOME=%cd%\magma"
加速 Windows 的 CUDA 构建#
Visual Studio 目前不支持并行自定义任务。作为替代,我们可以使用 Ninja
来并行化 CUDA 构建任务。只需输入几行代码即可使用。
REM Let's install ninja first.
pip install ninja
REM Set it as the cmake generator
set CMAKE_GENERATOR=Ninja
扩展#
CFFI 扩展#
CFFI 扩展的支持尚处于实验阶段。您必须在 Extension
对象中指定额外的 libraries
,才能使其在 Windows 上构建。
ffi = create_extension(
'_ext.my_lib',
headers=headers,
sources=sources,
define_macros=defines,
relative_to=__file__,
with_cuda=with_cuda,
extra_compile_args=["-std=c99"],
libraries=['ATen', '_C'] # Append cuda libraries when necessary, like cudart
)
Cpp 扩展#
与前一种扩展相比,此类型的扩展支持更好。然而,它仍然需要一些手动配置。首先,您应该打开 **x86_x64 Cross Tools Command Prompt for VS 2017**。然后,您可以开始编译过程。
安装#
在 win-32 通道中找不到包。#
Solving environment: failed
PackagesNotFoundError: The following packages are not available from current channels:
- pytorch
Current channels:
- https://repo.continuum.io/pkgs/main/win-32
- https://repo.continuum.io/pkgs/main/noarch
- https://repo.continuum.io/pkgs/free/win-32
- https://repo.continuum.io/pkgs/free/noarch
- https://repo.continuum.io/pkgs/r/win-32
- https://repo.continuum.io/pkgs/r/noarch
- https://repo.continuum.io/pkgs/pro/win-32
- https://repo.continuum.io/pkgs/pro/noarch
- https://repo.continuum.io/pkgs/msys2/win-32
- https://repo.continuum.io/pkgs/msys2/noarch
PyTorch 不支持 32 位系统。请使用 Windows 和 Python 64 位版本。
导入错误#
from torch._C import *
ImportError: DLL load failed: The specified module could not be found.
该问题是由缺少必要文件引起的。对于 wheels 包,由于我们没有打包一些库和 VS2017 可再发行组件文件,请确保您手动安装它们。可以下载 VS 2017 可再发行组件安装程序。您还应该注意 NumPy 的安装。请确保它使用的是 MKL 而不是 OpenBLAS。您可以输入以下命令。
pip install numpy mkl intel-openmp mkl_fft
用法(多进程)#
没有 if 子句保护的多进程错误#
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
Windows 上 multiprocessing
的实现有所不同,它使用 spawn
而不是 fork
。因此,我们必须用 if 子句包装代码,以防止代码多次执行。将您的代码重构为以下结构。
import torch
def main()
for i, data in enumerate(dataloader):
# do something here
if __name__ == '__main__':
main()
多进程错误“Broken pipe”#
ForkingPickler(file, protocol).dump(obj)
BrokenPipeError: [Errno 32] Broken pipe
当子进程在父进程完成发送数据之前结束时,会发生此问题。您的代码可能存在问题。您可以尝试将 DataLoader
的 num_worker
减少到零,看看问题是否仍然存在,以此来调试您的代码。
多进程错误“driver shut down”#
Couldn’t open shared file mapping: <torch_14808_1591070686>, error code: <1455> at torch\lib\TH\THAllocator.c:154
[windows] driver shut down
请更新您的显卡驱动程序。如果问题仍然存在,可能是您的显卡太旧,或者计算对您的显卡来说太重。请根据这篇 文章 更新 TDR 设置。
CUDA IPC 操作#
THCudaCheck FAIL file=torch\csrc\generic\StorageSharing.cpp line=252 error=63 : OS call failed or operation not supported on this OS
它们在 Windows 上不受支持。诸如对 CUDA 张量进行多进程操作之类的事情无法成功,对此有两种替代方案。
1. 不要使用 multiprocessing
。将 DataLoader
的 num_worker
设置为零。
2. 改为共享 CPU 张量。确保您的自定义 DataSet
返回 CPU 张量。