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 擴展¶
與前一個擴展相比,此類型的擴展具有更好的支援。 但是,它仍然需要一些手動配置。 首先,您應該開啟 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://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 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
另一個可能的原因是您正在使用 GPU 版本,但沒有 NVIDIA 顯示卡。請將您的 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.
multiprocessing
在 Windows 上的實作方式不同,它使用 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
當子進程在父進程完成資料發送之前結束時,會發生此問題。您的程式碼可能存在問題。您可以通過將 DataLoader
的 num_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
。將 DataLoader
的 num_worker
設為零。
2. 改為共享 CPU 張量。確保您的自訂 DataSet
回傳 CPU 張量。