Windows PyTorch 支持兩個(gè)組件: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"
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 Extension 的支持是試驗(yàn)性的。 在 Windows 下啟用它通常需要兩個(gè)步驟。
首先,在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 ++。 下面列出了一個(gè)示例。
#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;
}
與前一個(gè)擴(kuò)展相比,這種擴(kuò)展具有更好的支持。 但是,它仍然需要一些手動(dòng)配置。 首先,您應(yīng)該打開 x86_x64 VS 2017 的交叉工具命令提示符。 然后,您可以開始編譯過程。
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 位版本。
因?yàn)樗粔蚍€(wěn)定。 在我們正式發(fā)布之前,有一些問題需要解決。 您可以自己構(gòu)建。
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ā)行文件,因此請確保手動(dòng)安裝它們。 可以下載 VS 2017 可再發(fā)行安裝程序。 而且您還應(yīng)該注意安裝 Numpy。 確保它使用 MKL 而不是 OpenBLAS。 您可以輸入以下命令。
pip install numpy mkl intel-openmp mkl_fft
另一個(gè)可能的原因可能是您使用的是沒有 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)境時(shí),就會(huì)出現(xiàn)此問題。 您可以通過此命令修復(fù) intel-openmp 庫。
conda install -c defaults intel-openmp -f
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é)束時(shí),就會(huì)發(fā)生此問題。 您的代碼可能有問題。 您可以通過將 DataLoader
的num_worker
減小為零來調(diào)試代碼,然后查看問題是否仍然存在。
Couldn't open shared file mapping: <torch_14808_1591070686>, error code: <1455> at torch\lib\TH\THAllocator.c:154
[windows] driver shut down
請更新您的圖形驅(qū)動(dòng)程序。 如果這種情況持續(xù)存在,則可能是圖形卡太舊或計(jì)算量太大。 請根據(jù)此帖子更新 TDR 設(shè)置。
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
。 將 DataLoader
的num_worker
設(shè)置為零。
2.改為共享 CPU 張量。 確保自定義DataSet
返回 CPU 張量。
更多建議: