(pytorch_env) PS E:\PyTorch_Build\pytorch> # 接受所有必要的许可协议
(pytorch_env) PS E:\PyTorch_Build\pytorch> conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main
accepted Terms of Service for https://repo.anaconda.com/pkgs/main
(pytorch_env) PS E:\PyTorch_Build\pytorch> conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r
accepted Terms of Service for https://repo.anaconda.com/pkgs/r
(pytorch_env) PS E:\PyTorch_Build\pytorch> conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/msys2
accepted Terms of Service for https://repo.anaconda.com/pkgs/msys2
(pytorch_env) PS E:\PyTorch_Build\pytorch>
(pytorch_env) PS E:\PyTorch_Build\pytorch> # 安装所有依赖
(pytorch_env) PS E:\PyTorch_Build\pytorch> conda install -c conda-forge -y `
>> libuv=1.46 `
>> openssl=3.1 `
>> numpy `
>> mkl=2024.1 `
>> mkl-include=2024.1 `
>> libblas=*=*mkl
3 channel Terms of Service accepted
Retrieving notices: done
Channels:
- conda-forge
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- defaults
Platform: win-64
Collecting package metadata (repodata.json): done
Solving environment: failed
PackagesNotFoundError: The following packages are not available from current channels:
- libuv=1.46
Current channels:
- https://conda.anaconda.org/conda-forge
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- defaults
To search for alternate channels that may provide the conda package you're
looking for, navigate to
https://anaconda.org
and use the search bar at the top of the page.
(pytorch_env) PS E:\PyTorch_Build\pytorch>
(pytorch_env) PS E:\PyTorch_Build\pytorch> # 验证 MKL 安装
(pytorch_env) PS E:\PyTorch_Build\pytorch> python -c "import mkl; print(f'MKL version: {mkl.__version__}')"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'mkl'
(pytorch_env) PS E:\PyTorch_Build\pytorch> # 设置正确的 cuDNN 路径
(pytorch_env) PS E:\PyTorch_Build\pytorch> $cudnn_path = "E:\Program Files\NVIDIA\CUNND\v9.12"
(pytorch_env) PS E:\PyTorch_Build\pytorch>
(pytorch_env) PS E:\PyTorch_Build\pytorch> # 清理之前的构建
(pytorch_env) PS E:\PyTorch_Build\pytorch> Remove-Item -Recurse -Force build -ErrorAction SilentlyContinue
(pytorch_env) PS E:\PyTorch_Build\pytorch>
(pytorch_env) PS E:\PyTorch_Build\pytorch> # 设置环境变量
(pytorch_env) PS E:\PyTorch_Build\pytorch> $env:CUDNN_ROOT_DIR = $cudnn_path
(pytorch_env) PS E:\PyTorch_Build\pytorch> $env:CUDNN_INCLUDE_DIR = "$cudnn_path\include"
(pytorch_env) PS E:\PyTorch_Build\pytorch> $env:CUDNN_LIBRARY = "$cudnn_path\lib\x64\cudnn.lib"
(pytorch_env) PS E:\PyTorch_Build\pytorch>
(pytorch_env) PS E:\PyTorch_Build\pytorch> # 永久生效
(pytorch_env) PS E:\PyTorch_Build\pytorch> [Environment]::SetEnvironmentVariable("CUDNN_ROOT_DIR", $cudnn_path, "Machine")
(pytorch_env) PS E:\PyTorch_Build\pytorch> [Environment]::SetEnvironmentVariable("CUDNN_INCLUDE_DIR", "$cudnn_path\include", "Machine")
(pytorch_env) PS E:\PyTorch_Build\pytorch> [Environment]::SetEnvironmentVariable("CUDNN_LIBRARY", "$cudnn_path\lib\x64\cudnn.lib", "Machine")
(pytorch_env) PS E:\PyTorch_Build\pytorch> # 切换到 PyTorch 源码目录
(pytorch_env) PS E:\PyTorch_Build\pytorch> Set-Location E:\PyTorch_Build\pytorch
(pytorch_env) PS E:\PyTorch_Build\pytorch>
(pytorch_env) PS E:\PyTorch_Build\pytorch> # 创建缺失的 CMakeLists.txt
(pytorch_env) PS E:\PyTorch_Build\pytorch> @"
>> cmake_minimum_required(VERSION 3.18)
>> project(torch)
>> add_subdirectory(csrc)
>> "@ | Out-File -FilePath torch\CMakeLists.txt -Encoding ASCII
(pytorch_env) PS E:\PyTorch_Build\pytorch>
(pytorch_env) PS E:\PyTorch_Build\pytorch> # 创建缺失的 Python 文件
(pytorch_env) PS E:\PyTorch_Build\pytorch> New-Item -Path torch\_utils_internal.py -ItemType File -Force
Directory: E:\PyTorch_Build\pytorch\torch
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2025/9/1 19:07 0 _utils_internal.py
(pytorch_env) PS E:\PyTorch_Build\pytorch> New-Item -Path torch\csrc\api\include\torch\version.h.in -ItemType File -Force
Directory: E:\PyTorch_Build\pytorch\torch\csrc\api\include\torch
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2025/9/1 19:07 0 version.h.in
(pytorch_env) PS E:\PyTorch_Build\pytorch>
(pytorch_env) PS E:\PyTorch_Build\pytorch> # 添加基本内容到 version.h.in
(pytorch_env) PS E:\PyTorch_Build\pytorch> @"
>> #pragma once
>>
>> #define TORCH_VERSION_MAJOR 2
>> #define TORCH_VERSION_MINOR 9
>> #define TORCH_VERSION_PATCH 0
>> "@ | Out-File -FilePath torch\csrc\api\include\torch\version.h.in -Encoding ASCII
(pytorch_env) PS E:\PyTorch_Build\pytorch> # 设置构建参数
(pytorch_env) PS E:\PyTorch_Build\pytorch> $env:USE_CUDNN = "1"
(pytorch_env) PS E:\PyTorch_Build\pytorch> $env:MAX_JOBS = [Environment]::ProcessorCount
(pytorch_env) PS E:\PyTorch_Build\pytorch>
(pytorch_env) PS E:\PyTorch_Build\pytorch> # 添加 MKL 路径
(pytorch_env) PS E:\PyTorch_Build\pytorch> $mkl_include = "$env:CONDA_PREFIX\Library\include"
(pytorch_env) PS E:\PyTorch_Build\pytorch> $mkl_lib = "$env:CONDA_PREFIX\Library\lib\mkl_rt.lib"
(pytorch_env) PS E:\PyTorch_Build\pytorch>
(pytorch_env) PS E:\PyTorch_Build\pytorch> # 构建命令
(pytorch_env) PS E:\PyTorch_Build\pytorch> python setup.py install --cmake `
>> -DMKL_INCLUDE_DIR="$mkl_include" `
>> -DMKL_LIBRARIES="$mkl_lib" `
>> -DCUDNN_INCLUDE_DIR="$env:CUDNN_INCLUDE_DIR" `
>> -DCUDNN_LIBRARY="$env:CUDNN_LIBRARY" `
>> 2>&1 | Tee-Object -FilePath build_log.txt
Building wheel torch-2.9.0a0+git2d31c3d
option -D not recognized
(pytorch_env) PS E:\PyTorch_Build\pytorch> # 设置构建参数
(pytorch_env) PS E:\PyTorch_Build\pytorch> $env:USE_CUDNN = "1"
(pytorch_env) PS E:\PyTorch_Build\pytorch> $env:MAX_JOBS = [Environment]::ProcessorCount
(pytorch_env) PS E:\PyTorch_Build\pytorch>
(pytorch_env) PS E:\PyTorch_Build\pytorch> # 添加 MKL 路径
(pytorch_env) PS E:\PyTorch_Build\pytorch> $mkl_include = "$env:CONDA_PREFIX\Library\include"
(pytorch_env) PS E:\PyTorch_Build\pytorch> $mkl_lib = "$env:CONDA_PREFIX\Library\lib\mkl_rt.lib"
(pytorch_env) PS E:\PyTorch_Build\pytorch>
(pytorch_env) PS E:\PyTorch_Build\pytorch> # 构建命令
(pytorch_env) PS E:\PyTorch_Build\pytorch> python setup.py install --cmake `
>> -DMKL_INCLUDE_DIR="$mkl_include" `
>> -DMKL_LIBRARIES="$mkl_lib" `
>> -DCUDNN_INCLUDE_DIR="$env:CUDNN_INCLUDE_DIR" `
>> -DCUDNN_LIBRARY="$env:CUDNN_LIBRARY" `
>> 2>&1 | Tee-Object -FilePath build_log.txt
Building wheel torch-2.9.0a0+git2d31c3d
option -D not recognized
(pytorch_env) PS E:\PyTorch_Build\pytorch> # Dockerfile
(pytorch_env) PS E:\PyTorch_Build\pytorch> FROM nvidia/cuda:13.0-devel-ubuntu22.04
ParserError:
Line |
1 | FROM nvidia/cuda:13.0-devel-ubuntu22.04
| ~~~~
| The 'from' keyword is not supported in this version of the language.
(pytorch_env) PS E:\PyTorch_Build\pytorch>
(pytorch_env) PS E:\PyTorch_Build\pytorch> RUN apt-get update && \
RUN: The term 'RUN' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
(pytorch_env) PS E:\PyTorch_Build\pytorch> apt-get install -y git ninja-build python3-pip
apt-get: The term 'apt-get' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
(pytorch_env) PS E:\PyTorch_Build\pytorch>
(pytorch_env) PS E:\PyTorch_Build\pytorch> RUN pip3 install cmake
RUN: The term 'RUN' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
(pytorch_env) PS E:\PyTorch_Build\pytorch>
(pytorch_env) PS E:\PyTorch_Build\pytorch> RUN git clone https://github.com/pytorch/pytorch
RUN: The term 'RUN' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
(pytorch_env) PS E:\PyTorch_Build\pytorch> WORKDIR pytorch
WORKDIR: The term 'WORKDIR' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
(pytorch_env) PS E:\PyTorch_Build\pytorch>
(pytorch_env) PS E:\PyTorch_Build\pytorch> RUN git submodule update --init --recursive
RUN: The term 'RUN' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
(pytorch_env) PS E:\PyTorch_Build\pytorch> RUN python3 setup.py install
RUN: The term 'RUN' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
(pytorch_env) PS E:\PyTorch_Build\pytorch>