PowerShell 7 环境已加载 (版本: 7.5.2)
PS C:\Users\Administrator\Desktop> cd E:\PyTorch_Build\pytorch
PS E:\PyTorch_Build\pytorch> python -m venv rtx5070_env
PS E:\PyTorch_Build\pytorch> .\rtx5070_env\Scripts\activate
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 正确加载 Visual Studio 构建环境
(rtx5070_env) PS E:\PyTorch_Build\pytorch> $vsPath = "C:\Program Files\Microsoft Visual Studio\2022\Community"
(rtx5070_env) PS E:\PyTorch_Build\pytorch> & "$vsPath\VC\Auxiliary\Build\vcvars64.bat" x64
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.14.13
** Copyright (c) 2025 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 验证环境加载
(rtx5070_env) PS E:\PyTorch_Build\pytorch> cl.exe /?
cl.exe: The term 'cl.exe' 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.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> link.exe /?
link.exe: The term 'link.exe' 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.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 正确设置 CMake 参数数组
(rtx5070_env) PS E:\PyTorch_Build\pytorch> $cmakeArgs = @(
>> "-G", "Ninja",
>> "-DCMAKE_BUILD_TYPE=Release",
>> "-DCMAKE_C_COMPILER=cl.exe",
>> "-DCMAKE_CXX_COMPILER=cl.exe",
>> "-DCMAKE_CUDA_COMPILER=E:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.0/bin/nvcc.exe",
>> "-DCMAKE_CUDA_HOST_COMPILER=cl.exe",
>> "-DCMAKE_SYSTEM_VERSION=10.0.22621.0",
>> "-DCUDA_NVCC_FLAGS=-Xcompiler /wd4819 -gencode arch=compute_89,code=sm_89",
>> "-DTORCH_CUDA_ARCH_LIST=8.9",
>> "-DUSE_CUDA=ON",
>> "-DUSE_NCCL=OFF",
>> "-DUSE_DISTRIBUTED=OFF",
>> "-DBUILD_TESTING=OFF",
>> "-DBLAS=OpenBLAS",
>> "-DCUDA_TOOLKIT_ROOT_DIR=E:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.0",
>> "-DCUDNN_ROOT_DIR=E:/Program Files/NVIDIA/CUDNN/v9.12",
>> "-DPYTHON_EXECUTABLE=$((Get-Command python).Source)"
>> )
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 运行 CMake
(rtx5070_env) PS E:\PyTorch_Build\pytorch> cmake .. @cmakeArgs
CMake Warning:
Ignoring extra path from command line:
".."
CMake Error: The source directory "E:/PyTorch_Build" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 设置执行策略
(rtx5070_env) PS E:\PyTorch_Build\pytorch> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 加载 VS 环境
(rtx5070_env) PS E:\PyTorch_Build\pytorch> .\vs_env_setup.ps1
.\vs_env_setup.ps1: The term '.\vs_env_setup.ps1' 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.
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 设置 CUDA 路径
(rtx5070_env) PS E:\PyTorch_Build\pytorch> $env:CUDA_PATH = "E:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.0"
(rtx5070_env) PS E:\PyTorch_Build\pytorch> $env:CUDNN_PATH = "E:/Program Files/NVIDIA/CUDNN/v9.12"
(rtx5070_env) PS E:\PyTorch_Build\pytorch> $env:PATH = "$env:CUDA_PATH/bin;$env:CUDNN_PATH/bin;$env:PATH"
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 准备构建目录
(rtx5070_env) PS E:\PyTorch_Build\pytorch> Set-Location E:\PyTorch_Build\pytorch
(rtx5070_env) PS E:\PyTorch_Build\pytorch> Remove-Item build -Recurse -Force -ErrorAction SilentlyContinue
(rtx5070_env) PS E:\PyTorch_Build\pytorch> New-Item -Path build -ItemType Directory | Out-Null
(rtx5070_env) PS E:\PyTorch_Build\pytorch> Set-Location build
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build>
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build> # 配置 CMake
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build> .\cmake_config.ps1
.\cmake_config.ps1: The term '.\cmake_config.ps1' 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.
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build>
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build> # 编译和安装
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build> if ($LASTEXITCODE -eq 0) {
>> cmake --build . --config Release --parallel 8
>> if ($LASTEXITCODE -eq 0) {
>> cmake --install .
>> }
>> }
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build>
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build> # 验证安装
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build> python -c "import torch; print(f'PyTorch版本: {torch.__version__}'); print(f'CUDA可用: {torch.cuda.is_available()}')"
PyTorch版本: 2.8.0+cpu
CUDA可用: False
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build> python -c @"
>> import torch
>> import platform
>> import os
>>
>> print('===== 系统信息 =====')
>> print(f'操作系统: {platform.system()} {platform.release()}')
>> print(f'Python版本: {platform.python_version()}')
>> print(f'PyTorch版本: {torch.__version__}')
>>
>> print('\n===== CUDA 支持 =====')
>> print(f'CUDA可用: {torch.cuda.is_available()}')
>> if torch.cuda.is_available():
>> print(f'CUDA版本: {torch.version.cuda}')
>> print(f'cuDNN版本: {torch.backends.cudnn.version()}')
>> print(f'GPU数量: {torch.cuda.device_count()}')
>>
>> for i in range(torch.cuda.device_count()):
>> props = torch.cuda.get_device_properties(i)
>> print(f'\n设备 {i}: {props.name}')
>> print(f' 计算能力: {props.major}.{props.minor}')
>> print(f' 内存: {props.total_memory/1024**3:.2f} GB')
>>
>> # GPU 计算测试
>> try:
>> a = torch.randn(10000, 10000, device='cuda')
>> b = torch.randn(10000, 10000, device='cuda')
>> c = torch.matmul(a, b)
>> print('\n===== 计算测试 =====')
>> print(f'矩阵乘法完成 | 结果形状: {c.shape}')
>> except Exception as e:
>> print(f'计算测试失败: {str(e)}')
>> else:
>> print('\n===== 错误诊断 =====')
>> print('编译配置信息:')
>> print(torch.__config__.show())
>>
>> print('\n环境变量检查:')
>> print(f'CUDA_HOME: {os.environ.get("CUDA_HOME", "未设置")}')
>> print(f'PATH包含CUDA: {"CUDA" in os.environ.get("PATH", "")}')
>> print(f'NVCC路径: {os.environ.get("CUDA_PATH", "未设置")}')
>> "@
===== 系统信息 =====
操作系统: Windows 10
Python版本: 3.10.10
PyTorch版本: 2.8.0+cpu
===== CUDA 支持 =====
CUDA可用: False
===== 错误诊断 =====
编译配置信息:
PyTorch built with:
- C++ Version: 201703
- MSVC 193833145
- Intel(R) oneAPI Math Kernel Library Version 2025.2-Product Build 20250620 for Intel(R) 64 architecture applications
- Intel(R) MKL-DNN v3.7.1 (Git Hash 8d263e693366ef8db40acc569cc7d8edf644556d)
- OpenMP 2019
- LAPACK is enabled (usually provided by MKL)
- CPU capability usage: AVX2
- Build settings: BLAS_INFO=mkl, BUILD_TYPE=Release, COMMIT_SHA=a1cb3cc05d46d198467bebbb6e8fba50a325d4e7, CXX_COMPILER=C:/actions-runner/_work/pytorch/pytorch/pytorch/.ci/pytorch/windows/tmp_bin/sccache-cl.exe, CXX_FLAGS=/DWIN32 /D_WINDOWS /EHsc /Zc:__cplusplus /bigobj /FS /utf-8 -DUSE_PTHREADPOOL -DNDEBUG -DUSE_KINETO -DLIBKINETO_NOCUPTI -DLIBKINETO_NOROCTRACER -DLIBKINETO_NOXPUPTI=ON -DUSE_FBGEMM -DUSE_XNNPACK -DSYMBOLICATE_MOBILE_DEBUG_HANDLE /wd4624 /wd4068 /wd4067 /wd4267 /wd4661 /wd4717 /wd4244 /wd4804 /wd4273, LAPACK_INFO=mkl, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, TORCH_VERSION=2.8.0, USE_CUDA=0, USE_CUDNN=OFF, USE_CUSPARSELT=OFF, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_GLOO=ON, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=OFF, USE_NNPACK=OFF, USE_OPENMP=ON, USE_ROCM=OFF, USE_ROCM_KERNEL_ASSERT=OFF, USE_XCCL=OFF, USE_XPU=OFF,
环境变量检查:
CUDA_HOME: E:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1
PATH包含CUDA: True
NVCC路径: E:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.0
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build>
"PowerShell 7 环境已加载 (版本: 7.5.2)
PS C:\Users\Administrator\Desktop> cd E:\PyTorch_Build\pytorch
PS E:\PyTorch_Build\pytorch> python -m venv rtx5070_env
PS E:\PyTorch_Build\pytorch> .\rtx5070_env\Scripts\activate
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 创建 CUDA 测试程序
(rtx5070_env) PS E:\PyTorch_Build\pytorch> @'
>> #include <cuda_runtime.h>
>> #include <iostream>
>>
>> __global__ void addKernel(int *c, const int *a, const int *b) {
>> int i = threadIdx.x;
>> c[i] = a[i] + b[i];
>> }
>>
>> int main() {
>> const int arraySize = 5;
>> const int a[arraySize] = {1, 2, 3, 4, 5};
>> const int b[arraySize] = {10, 20, 30, 40, 50};
>> int c[arraySize] = {0};
>>
>> int *dev_a, *dev_b, *dev_c;
>> cudaMalloc(&dev_a, arraySize * sizeof(int));
>> cudaMalloc(&dev_b, arraySize * sizeof(int));
>> cudaMalloc(&dev_c, arraySize * sizeof(int));
>>
>> cudaMemcpy(dev_a, a, arraySize * sizeof(int), cudaMemcpyHostToDevice);
>> cudaMemcpy(dev_b, b, arraySize * sizeof(int), cudaMemcpyHostToDevice);
>>
>> addKernel<<<1, arraySize>>>(dev_c, dev_a, dev_b);
>> cudaMemcpy(c, dev_c, arraySize * sizeof(int), cudaMemcpyDeviceToHost);
>>
>> std::cout << "CUDA 测试结果: ";
>> for (int i = 0; i < arraySize; i++) {
>> std::cout << c[i] << " ";
>> }
>>
>> cudaFree(dev_a);
>> cudaFree(dev_b);
>> cudaFree(dev_c);
>> return 0;
>> }
>> '@ | Set-Content cuda_test.cpp
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 编译测试
(rtx5070_env) PS E:\PyTorch_Build\pytorch> nvcc cuda_test.cpp -o cuda_test
nvcc fatal : Cannot find compiler 'cl.exe' in PATH
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 运行测试
(rtx5070_env) PS E:\PyTorch_Build\pytorch> if (Test-Path cuda_test.exe) {
>> .\cuda_test.exe
>> } else {
>> Write-Host "CUDA 编译失败,请检查环境"
>> }
CUDA 编译失败,请检查环境
(rtx5070_env) PS E:\PyTorch_Build\pytorch> .\full_build.ps1
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.14.13
** Copyright (c) 2025 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
cl.exe: E:\PyTorch_Build\pytorch\vs_env_setup.ps1:6
Line |
6 | cl.exe /?
| ~~~~~~
| The term 'cl.exe' 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.
link.exe: E:\PyTorch_Build\pytorch\vs_env_setup.ps1:7
Line |
7 | link.exe /?
| ~~~~~~~~
| The term 'link.exe' 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.
.\cmake_config.ps1: E:\PyTorch_Build\pytorch\full_build.ps1:19
Line |
19 | .\cmake_config.ps1
| ~~~~~~~~~~~~~~~~~~
| The term '.\cmake_config.ps1' 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.
Error: not a CMake build directory (missing CMakeCache.txt)
PyTorch版本: 2.8.0+cpu
CUDA可用: False
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build> .\verify_pytorch.ps1
.\verify_pytorch.ps1: The term '.\verify_pytorch.ps1' 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.
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build> .\compile_test.ps1
.\compile_test.ps1: The term '.\compile_test.ps1' 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.
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build>"