Windows 常见问题解答¶
从源构建¶
包括可选组件¶
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
curl -k https://s3.amazonaws.com/ossci-windows/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 目前不支持并行自定义任务。
作为替代方案,我们可以用于并行化 CUDA
构建任务。只需键入几行代码即可使用它。Ninja
REM Let's install ninja first.
pip install ninja
REM Set it as the cmake generator
set CMAKE_GENERATOR=Ninja
外延¶
CFFI 扩展¶
对 CFFI 扩展的支持非常具有实验性。您必须指定
additional 在 object 中构建它
窗户。libraries
Extension
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 扩展¶
这种类型的扩展与 前一个。但是,它仍然需要一些手动操作 配置。首先,您应该打开 VS 2017 的 x86_x64 Cross Tools 命令提示符。 然后,您可以开始编译过程。
安装¶
在 win-32 频道中找不到包。¶
Solving environment: failed
PackagesNotFoundError: The following packages are not available from current channels:
- pytorch
Current channels:
- https://conda.anaconda.org/pytorch/win-32
- https://conda.anaconda.org/pytorch/noarch
- 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.
该问题是由于缺少基本文件引起的。实际上 我们几乎包括 PyTorch 需要的所有 conda 基本文件 包,但 VC2017 redistributable 和一些 mkl 库除外。 您可以通过键入以下命令来解决此问题。
conda install -c peterjc123 vc vs2017_runtime
conda install mkl_fft intel_openmp numpy mkl
至于 wheels 包,因为我们没有打包一些库和 VS2017 redistributable 文件,请确保手动安装它们。 可以下载 VS 2017 可再发行组件安装程序。 您还应该注意 Numpy 的安装。确保它 使用 MKL 而不是 OpenBLAS。您可以输入以下命令。
pip install numpy mkl intel-openmp mkl_fft
另一个可能的原因可能是您使用的是没有 NVIDIA 的 GPU 版本 显卡。请将您的 GPU 包更换为 CPU 包。
from torch._C import *
ImportError: DLL load failed: The operating system cannot run %1.
这实际上是 Anaconda 的一个上游问题。当您初始化 环境,则会出现此问题。你可以修复 intel-openmp 库。
conda install -c defaults intel-openmp -f
用法 (multiprocessing)¶
没有 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 上是不同的,它
使用而不是 .所以我们必须用
if-子句来防止代码多次执行。重构
将代码转换为以下结构。multiprocessing
spawn
fork
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
当子进程在父进程之前结束时,会出现此问题
完成发送数据。您的代码可能有问题。你
可以通过将 OF 减少到 0 来调试您的代码,并查看问题是否仍然存在。
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. 不要使用 .将 of 设置为零。
multiprocessing
num_worker
2. 改为共享 CPU 张量。确保您的自定义返回 CPU 张量。DataSet