評價此頁

Windows FAQ#

創建於:2018年4月23日 | 最後更新於:2025年5月20日

從原始碼構建#

包含可選元件#

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"
set "HOST=https://s3.amazonaws.com/ossci-windows"
curl -k "%HOST%/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 擴充套件#

與前一種擴充套件相比,此型別的擴充套件支援更好。然而,它仍然需要一些手動配置。首先,您應該開啟 **x86_x64 Cross Tools Command Prompt for VS 2017**。然後,您可以開始編譯過程。

安裝#

在 win-32 通道中找不到包。#

Solving environment: failed

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

- pytorch

Current channels:
- 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.

該問題是由缺少必要檔案引起的。對於 wheels 包,由於我們沒有打包一些庫和 VS2017 可再發行元件檔案,請確保您手動安裝它們。可以下載 VS 2017 可再發行元件安裝程式。您還應該注意 NumPy 的安裝。請確保它使用的是 MKL 而不是 OpenBLAS。您可以輸入以下命令。

pip install numpy mkl intel-openmp mkl_fft

用法(多程序)#

沒有 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 張量進行多程序操作之類的事情無法成功,對此有兩種替代方案。

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

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