• 文件 >
  • Windows 常見問題
捷徑

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 目前不支援平行自訂工作。或者,我們可以使用 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 物件中指定其他 函式庫 才能在 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 擴展

與前一個相比,這種擴展有更好的支援。但是,它仍然需要一些手動配置。首先,您應該打開適用於 VS 2017 的 x86_x64 交叉工具命令提示字元。然後,您可以開始編譯過程。

安裝

在 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.

問題是由於缺少必要的檔案引起的。實際上,我們在 conda 套件中包含了 PyTorch 需要的幾乎所有必要檔案,除了 VC2017 可再發行的檔案和一些 mkl 函式庫。您可以透過輸入以下命令來解決此問題。

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

至於 wheels 套件,由於我們沒有打包一些函式庫和 VS2017 可再發行的檔案,請確保您手動安裝它們。VS 2017 可再發行元件安裝程式 可供下載。您還需要注意 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.

這實際上是 Anaconda 的上游問題。當您使用 conda-forge 通道初始化您的環境時,就會出現這個問題。您可以透過以下命令修復 intel-openmp 函式庫。

conda install -c defaults intel-openmp -f

用法(多程序)

沒有 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.

multiprocessing 的實現在 Windows 上有所不同,它使用 spawn 而不是 fork。因此,我們必須使用 if 子句包裝程式碼,以保護程式碼不被多次執行。將您的程式碼重構為以下結構。

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

當子程序在父程序完成發送資料之前結束時,就會發生此問題。您的程式碼可能出現問題。您可以透過將 DataLoadernum_worker 減少到零來除錯您的程式碼,並查看問題是否仍然存在。

多程序錯誤「驅動程式關閉」

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。將 DataLoadernum_worker 設定為零。

2. 改為共用 CPU 張量。確保您的自訂 DataSet 返回 CPU 張量。

文件

訪問 PyTorch 的全面開發人員文檔

查看文檔

教程

獲取針對初學者和高級開發人員的深入教程

查看教程

資源

查找開發資源並獲得問題的解答

查看資源