PyTorch Windows 常見問題

2020-09-16 14:44 更新

原文:PyTorch Windows 常見問題

從源頭建造

包括可選組件

Windows PyTorch 支持兩個組件:MKL 和 MAGMA。 以下是使用它們構(gòu)建的步驟。

REM Make sure you have 7z and curl installed.


REM Download MKL files
curl https://s3.amazonaws.com/ossci-windows/mkl_2018.2.185.7z -k -O
7z x -aoa mkl_2018.2.185.7z -omkl


REM Download MAGMA files
REM cuda100/cuda101 is also available for `CUDA_PREFIX`. There are also 2.4.0 binaries for cuda80/cuda92.
REM The configuration could be `debug` or `release` for 2.5.0\. Only `release` is available for 2.4.0.
set CUDA_PREFIX=cuda90
set CONFIG=release
curl -k https://s3.amazonaws.com/ossci-windows/magma_2.5.0_%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 構(gòu)建

Visual Studio 當(dāng)前不支持并行自定義任務(wù)。 或者,我們可以使用Ninja并行化 CUDA 構(gòu)建任務(wù)。 只需輸入幾行代碼即可使用它。

REM Let's install ninja first.
pip install ninja


REM Set it as the cmake generator
set CMAKE_GENERATOR=Ninja

一鍵安裝腳本

您可以看看這組腳本。 它將為您帶路。

延期

CFFI 擴(kuò)展

CFFI Extension 的支持是試驗(yàn)性的。 在 Windows 下啟用它通常需要兩個步驟。

首先,在Extension對象中指定其他libraries以使其在 Windows 上構(gòu)建。

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 libaries when necessary, like cudart
)

其次,這里是“ extern THCState *state;導(dǎo)致的外部符號狀態(tài)無法解析”的工作場所

將源代碼從 C 更改為 C ++。 下面列出了一個示例。

#include <THC/THC.h>
#include <ATen/ATen.h>


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;
}

Cpp 擴(kuò)展

與前一個擴(kuò)展相比,這種擴(kuò)展具有更好的支持。 但是,它仍然需要一些手動配置。 首先,您應(yīng)該打開 x86_x64 VS 2017 的交叉工具命令提示符。 然后,您可以開始編譯過程。

安裝

在 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 位系統(tǒng)上不起作用。 請使用 Windows 和 Python 64 位版本。

為什么 Windows 沒有 Python 2 軟件包?

因?yàn)樗粔蚍€(wěn)定。 在我們正式發(fā)布之前,有一些問題需要解決。 您可以自己構(gòu)建。

導(dǎo)入錯誤

from torch._C import *


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

該問題是由于缺少基本文件引起的。 實(shí)際上,除了 VC2017 可再發(fā)行和一些 mkl 庫,我們幾乎包含了 PyTorch 的 conda 軟件包所需的所有基本文件。 您可以通過鍵入以下命令來解決此問題。

conda install -c peterjc123 vc vs2017_runtime
conda install mkl_fft intel_openmp numpy mkl

至于 wheel 軟件包,由于我們沒有打包一些庫和 VS2017 可再發(fā)行文件,因此請確保手動安裝它們。 可以下載 VS 2017 可再發(fā)行安裝程序。 而且您還應(yīng)該注意安裝 Numpy。 確保它使用 MKL 而不是 OpenBLAS。 您可以輸入以下命令。

pip install numpy mkl intel-openmp mkl_fft

另一個可能的原因可能是您使用的是沒有 NVIDIA 顯卡的 GPU 版本。 請用 CPU 之一替換您的 GPU 軟件包。

from torch._C import *


ImportError: DLL load failed: The operating system cannot run %1.

這實(shí)際上是 Anaconda 的上游問題。 當(dāng)您使用 conda-forge 頻道初始化環(huán)境時,就會出現(xiàn)此問題。 您可以通過此命令修復(fù) intel-openmp 庫。

conda install -c defaults intel-openmp -f

用法(并行處理)

沒有 if 子句保護(hù)的并行處理錯誤

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.

multiprocessing的實(shí)現(xiàn)在 Windows 上有所不同,Windows 使用spawn代替fork。 因此,我們必須用 if 子句包裝代碼,以防止代碼多次執(zhí)行。 將代碼重構(gòu)為以下結(jié)構(gòu)。

import torch


def main()
    for i, data in enumerate(dataloader):
        # do something here


if __name__ == '__main__':
    main()

并行處理錯誤“管道破裂”

ForkingPickler(file, protocol).dump(obj)


BrokenPipeError: [Errno 32] Broken pipe

當(dāng)子進(jìn)程在父進(jìn)程完成發(fā)送數(shù)據(jù)之前結(jié)束時,就會發(fā)生此問題。 您的代碼可能有問題。 您可以通過將 DataLoadernum_worker減小為零來調(diào)試代碼,然后查看問題是否仍然存在。

并行處理錯誤“驅(qū)動程序關(guān)閉”

Couldn't open shared file mapping: <torch_14808_1591070686>, error code: <1455> at torch\lib\TH\THAllocator.c:154


[windows] driver shut down

請更新您的圖形驅(qū)動程序。 如果這種情況持續(xù)存在,則可能是圖形卡太舊或計算量太大。 請根據(jù)此帖子更新 TDR 設(shè)置。

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 張量執(zhí)行并行處理之類的操作無法成功,有兩種選擇。

1.不要使用multiprocessing。 將 DataLoadernum_worker設(shè)置為零。

2.改為共享 CPU 張量。 確保自定義DataSet返回 CPU 張量。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號