2023年11月29日发(作者:)

pytorch0.4错误总结

0.4.0 发布说明

错误修复:

修复多进程下的内存泄漏问题 PR #5585

使⽤多线程版本 MKL 替代顺序版 MKL ,在 CPU 上带来10%的速度提升 PR #6416

重新添加 Compute Capability 5.0 显卡的⽀持

新功能:

在编译中加⼊ MAGMA

添加 CUDA 9.1 build

提供 Wheels 包

⽀持新的cpp拓展 PR #5548

已知问题:

使⽤CPU来跑模型⽐Linux/Mac要慢很多,具体视模型复杂度和CPU核⼼数⽽定。原因是MSVC对新版本OpenMP的⽀持很差,因此我们

会在下⼀个版本对⼀些组件进⾏替换,从⽽解决这个问题。使⽤GPU速度不受影响,与 Linux/Mac 没有区别。

包括分布式, NCCL等在内的⼀些组件在Windows下不受⽀持,详细可见:

常见问题

安装

1 Conda 依赖项冲突

Solving environment: failed

UnsatisfiableError: The following specifications were found to be in conflict:

- libxml2 -> libiconv -> *[track_features=vc14]

- libxml2 -> libiconv -> vc==14

- pytorch

Solving environment: failed

PackagesNotFoundError: The following packages are not available from current channels:

- pytorch

Current channels:

- https:///pytorch/win-32

- https:///pytorch/noarch

- https:///pkgs/main/win-32

- https:///pkgs/main/noarch

- https:///pkgs/free/win-32

- https:///pkgs/free/noarch

- https:///pkgs/r/win-32

- https:///pkgs/r/noarch

- https:///pkgs/pro/win-32

- https:///pkgs/pro/noarch

- https:///pkgs/msys2/win-32

- https:///pkgs/msys2/noarch

PyTorch 在32位系统上不受⽀持。请使⽤64位的 Windows 和 Python。

3 从我的通道迁移⾄官⽅通道

如果你使⽤ peterjc123 通道的 0.3.1 版本 , 那么你可以直接进⾏升级。

conda install -c pytorch pytorch

否则请先卸载所有与 PyTorch 相关的包。

conda uninstall pytorch

# 如果是CUDA版本的话

conda uninstall cuda80 cuda90

# 如果 已安装的 PyTorch 版本 >= 0.3

conda install -c defaults vc=14

# 安装官⽅的包

conda install -c pytorch pytorch

4 为什么没有 Python 2 的包?

因为它还不够稳定,在正式发布之前,我们需要解决⼀系列的问题。实在需要的话,你可以⾃⼰进⾏编译。

5 怎么安装torchvision?

你可以通过 PyPI 进⾏安装。

pip install torchvision

6.导⼊错误 1

from torch._C import *

ImportError: DLL load failed: The specified module could not be found.

这个问题说明缺少了必要的⼀些动态链接库。其实我们在发布时已经将⼏乎所有PyTorch需要的⽂件全部打包了,但是没有包含VC 2017

运⾏时。你可以尝试通过下⾯的命令进⾏安装:

conda install -c peterjc123 vc vs2017_runtime

from torch._C import *

ModuleNotFoundError: No module named 'torch._C'

这个错误与上⼀个不同。这通常会在以下情况发⽣:你的代码⽬录中有⼀个名为的⽂件。对⽂件做⼀下重命名就能解决。

8导⼊错误 3

ModuleNotFoundError: No module named 'torch'

这通常是你安装PyTorch的Python环境(pip/conda)和使⽤的Python环境不同造成的。你可以通过键⼊以下的指令进⾏检查。

where

# 如果使⽤ conda 进⾏安装

where

# 如果使⽤ pip 进⾏安装

where

注意这些命令输出的第⼀⾏,他们应该指向同⼀个Python环境。

C:Anaconda2Scriptsconda.exe

C:Anaconda2python.exe

C:Anaconda2Scriptspip.exe

9.⽹络错误

CondaError: EOFError('Compressed file ended before the end-of-stream marker was reached',)

从 [Anaconda dir]pkgs 删除损坏的PyTorch包并重试。

使⽤(多进程)

1.多进程错误 1

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.

Python的 multiprocessing 库在 Windows 下的实现与 Linux 不同, 它使⽤的是 spawn ,⽽不是 fork 。所以我们需要在代码⼊⼝加上

⼀个判断来防⽌程序多次执⾏。可以将代码重构为如下结构:

import torch

def main()

for i, data in enumerate(dataloader):

# do something here

if __name__ == '__main__':

main()

2.多进程错误 2

ForkingPickler(file, protocol).dump(obj)

BrokenPipeError: [Errno 32] Broken pipe

这个错误当⼦进程过早结束时会发⽣。你的代码可能存在⼀些问题,可以将 DataLoader 的 num_worker 设置为0来进⾏调试。

3.多进程错误 3

Couldn’t open shared file mapping: , error code: <1455> at D:Downloadspytorch-master-1torchlibTHTHAllocator.c:154

[windows] driver shut down

请升级你的显卡驱动。如果升级后问题仍然存在,要么是你的显卡太⽼了,或者是计算太过复杂。可以尝试根据这个修改TDR设置。

IPC 操作

THCudaCheck FAIL file= line=252 error=63 : OS call failed or operation not supported on this OS

这些在Windows上是不受⽀持的。有两个替代⽅案:

a). 不要使⽤ multiprocessing 。

b). 共享 CPU tensors。

CFFI拓展

1 这种类型的拓展在 Windows 下⽀持⽐较差,⽽且这些拓展太多了,只靠⼏个⼈是没法改完的。如果硬要使⽤的话,可以看下⾯的说明。

2.在Windows下编译,你需要在Extension对象中指明 libraries 。

ffi = create_extension(

'__lib',

headers=headers,

sources=sources,

define_macros=defines,

relative_to=__file__,

with_cuda=with_cuda,

extra_compile_args=["-std=c99"],

libraries=['ATen'] # 在必要时添加CUDA相关的库,如cudart

)

3 ⽆法解析的外部链接 extern THCState *state;

error LNK2001: unresolved external symbol state caused by extern THCState *state;

将代码后缀名从.c改为.cpp,并对代码做如下改动。

#include

#include

THCState *state = at::globalContext().thc_state;

extern "C" int my_lib_add_forward_cuda(THCudaTensor *input1, THCudaTensor *input2,

THCudaTensor *output)

{

if (!THCudaTensor_isSameSizeAs(state, input1, input2))

return 0;

THCudaTensor_resizeAs(state, output, input1);

THCudaTensor_cadd(state, output, input1, 1.0, input2);

return 1;

}

extern "C" int my_lib_add_backward_cuda(THCudaTensor *grad_output, THCudaTensor *grad_input)

{

THCudaTensor_resizeAs(state, grad_input, grad_output);

THCudaTensor_fill(state, grad_input, 1);

return 1;

}

C++拓展

这个类型的拓展在Windows下的⽀持⽐上⾯要好的多。但是他还是需要⼀些⼿⼯配置。⾸先,你需要打开x86_x64 Cross Tools

Command Prompt for VS 2017,然后在其中打开git-bash,他⼀般的位置在C:Program ,最后你可以开始

进⾏编译。

其他

1 MAGMA 和 NCCL ⽀持

我们编译的包中包含了 MAGMA,但是当你的矩阵很⼤的时候 ,程序很可能不能正常⼯作。原因是在代码中它使⽤了 long ⽽不是

int64_t ,这就可能会在Windows上造成溢出问题。⾄于 NCCL,Nvidia只提供了Linux系统的⼆进制⽂件,也没有开源相应的代码,因此

在NCCL for Windows发布之前,我们没有办法对其进⾏编译。

2 分布式⽀持

我们会在未来对其进⾏研究和实现。

3.⽼显卡架构⽀持

THCudaCheck FAIL file=torchlibthcunngeneric/ line=34 error=48 : no kernel image is available for execution on the device

为了和其他系统保持同步,我们⽆法对⽼显卡提供⽀持。可以安装 中的旧版本的包。