• 文件 >
  • 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 物件中指定額外的 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 擴充套件

與前一種擴充套件相比,此類擴充套件支援更佳。但是,它仍然需要一些手動配置。首先,您應該開啟 適用於 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 位系統。請使用 64 位版本的 Windows 和 Python。

匯入錯誤

from torch._C import *

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

此問題是由於缺少必要檔案引起的。實際上,我們為 conda 軟體包包含了 PyTorch 所需的幾乎所有必要檔案,但 VC2017 redistributable 和一些 mkl 庫除外。您可以透過輸入以下命令解決此問題。

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

對於 wheels 軟體包,由於我們沒有包含一些庫和 VS2017 redistributable 檔案,請確保手動安裝它們。可以下載 VS 2017 redistributable 安裝程式。您還應注意 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 的一個上游問題。當您使用 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.

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

當子程序在父程序完成傳送資料之前結束時,會出現此問題。您的程式碼可能存在問題。您可以透過將 DataLoadernum_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 IPC 操作。例如,對 CUDA 張量進行多程序處理將無法成功,以下是兩種替代方法。

1. 不要使用 multiprocessing。將 DataLoadernum_worker 設定為零。

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

文件

查閱 PyTorch 的完整開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深入教程

檢視教程

資源

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

檢視資源