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> # 创建虚拟环境
(rtx5070_env) PS E:\PyTorch_Build\pytorch> python -m venv rtx5070_env
Error: [Errno 13] Permission denied: 'E:\\PyTorch_Build\\pytorch\\rtx5070_env\\Scripts\\python.exe'
(rtx5070_env) PS E:\PyTorch_Build\pytorch> .\rtx5070_env\Scripts\activate
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 设置执行策略
(rtx5070_env) PS E:\PyTorch_Build\pytorch> Set-ExecutionPolicy RemoteSigned -Scope Process -Force
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 创建所有脚本文件
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # [粘贴上面的四个脚本创建命令]
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 运行完整构建流程
(rtx5070_env) PS E:\PyTorch_Build\pytorch> .\full_build.ps1
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 验证CUDA编译
(rtx5070_env) PS E:\PyTorch_Build\pytorch> .\compile_cuda_test.ps1
ParserError: E:\PyTorch_Build\pytorch\compile_cuda_test.ps1:56
Line |
56 | '@ | Set-Content compile_cuda_test.ps1
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| The string is missing the terminator: '.
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 验证PyTorch安装
(rtx5070_env) PS E:\PyTorch_Build\pytorch> 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> PowerShell 7 环境已加载 (版本: 7.5.2)
版本:: The term '版本:' 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> PS C:\Users\Administrator\Desktop> cd E:\PyTorch_Build\pytorch
Get-Process: A positional parameter cannot be found that accepts argument 'cd'.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> PS E:\PyTorch_Build\pytorch> python -m venv rtx5070_env
Get-Process: A positional parameter cannot be found that accepts argument 'python'.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> PS E:\PyTorch_Build\pytorch> .\rtx5070_env\Scripts\activate
Get-Process: A positional parameter cannot be found that accepts argument '.\rtx5070_env\Scripts\activate'.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 创建虚拟环境
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 创建虚拟环境
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> python -m venv rtx5070_env
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch> python -m venv rtx5070_env
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> Error: [Errno 13] Permission denied: 'E:\\PyTorch_Build\\pytorch\\rtx5070_env\\Scripts\\python.exe'
Error:: The term 'Error:' 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> .\rtx5070_env\Scripts\activate
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch> .\rtx5070_env\Scripts\acti …
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch>
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch>
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 设置执行策略
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 设置执行策略
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> Set-ExecutionPolicy RemoteSigned -Scope Process -Force
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch> Set-ExecutionPolicy Remote …
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch>
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch>
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 创建所有脚本文件
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 创建所有脚本文件
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # [粘贴上面的四个脚本创建命令]
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch> # [粘贴上面的四个脚本创建命令]
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch>
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch>
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 运行完整构建流程
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 运行完整构建流程
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> .\full_build.ps1
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch> .\full_build.ps1
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch>
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch>
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 验证CUDA编译
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 验证CUDA编译
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> .\compile_cuda_test.ps1
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch> .\compile_cuda_test.ps1
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> ParserError: E:\PyTorch_Build\pytorch\compile_cuda_test.ps1:56
ParserError:: The term 'ParserError:' 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> Line |
>> 56 | '@ | Set-Content compile_cuda_test.ps1
>> | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> | The string is missing the terminator: '.
>> (rtx5070_env) PS E:\PyTorch_Build\pytorch>
ParserError:
Line |
2 | 56 | '@ | Set-Content compile_cuda_test.ps1
| ~~
| Expressions are only allowed as the first element of a pipeline.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 验证PyTorch安装
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 验证PyTorch安装
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> python -c "import torch; print(f'PyTorch版本: {torch.__version__}'); print(f'CUDA可用: {torch.cuda.is_available()}')"
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch> python -c "import torch; p …
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> PyTorch版本: 2.8.0+cpu
PyTorch版本:: The term 'PyTorch版本:' 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> CUDA可用: False
CUDA可用:: The term 'CUDA可用:' 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>
ParserError:
Line |
1 | (rtx5070_env) PS E:\PyTorch_Build\pytorch>
| ~~
| Unexpected token 'PS' in expression or statement.
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 1. 环境设置脚本
(rtx5070_env) PS E:\PyTorch_Build\pytorch> @"
>> # setup_environment.ps1
>> Set-ExecutionPolicy RemoteSigned -Scope Process -Force
>>
>> `$vsPath = "C:\Program Files\Microsoft Visual Studio\2022\Community"
>> `$vcPath = "`$vsPath\VC\Tools\MSVC"
>> `$sdkPath = "C:\Program Files (x86)\Windows Kits\10"
>>
>> # 获取最新MSVC版本
>> `$msvcVersions = Get-ChildItem -Path `$vcPath |
>> Where-Object { `$_.Name -match '^\d+\.\d+\.\d+' } |
>> Sort-Object { [version]`$_.Name } -Descending
>> `$latestMsvc = `$msvcVersions | Select-Object -First 1
>> `$msvcVersion = `$latestMsvc.Name
>> `$env:VCToolsInstallDir = "`$vcPath\`$msvcVersion"
>>
>> # 获取最新Windows SDK
>> `$sdkVersions = Get-ChildItem -Path "`$sdkPath\Include" |
>> Where-Object { `$_.Name -match '^\d+\.\d+\.\d+' } |
>> Sort-Object { [version]`$_.Name } -Descending
>> `$latestSdk = `$sdkVersions | Select-Object -First 1
>> `$sdkVersion = `$latestSdk.Name
>>
>> # 设置环境变量
>> `$env:PATH = @(
>> "`$env:VCToolsInstallDir\bin\Hostx64\x64",
>> "`$sdkPath\bin\`$sdkVersion\x64",
>> "E:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.0\bin",
>> "E:\Program Files\NVIDIA\CUDNN\v9.12\bin",
>> `$env:PATH
>> ) -join ';'
>>
>> `$env:INCLUDE = @(
>> "`$env:VCToolsInstallDir\include",
>> "`$sdkPath\Include\`$sdkVersion\um",
>> "`$sdkPath\Include\`$sdkVersion\ucrt",
>> "`$sdkPath\Include\`$sdkVersion\shared",
>> "E:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.0\include",
>> `$env:INCLUDE
>> ) -join ';'
>>
>> `$env:LIB = @(
>> "`$env:VCToolsInstallDir\lib\x64",
>> "`$sdkPath\Lib\`$sdkVersion\um\x64",
>> "`$sdkPath\Lib\`$sdkVersion\ucrt\x64",
>> "E:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.0\lib\x64",
>> `$env:LIB
>> ) -join ';'
>>
>> # 设置CUDA编译器
>> `$env:CUDA_HOST_COMPILER = "`$env:VCToolsInstallDir\bin\Hostx64\x64\cl.exe"
>> "@ | Set-Content setup_environment.ps1
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 2. CMake配置脚本
(rtx5070_env) PS E:\PyTorch_Build\pytorch> @"
>> # cmake_config.ps1
>> `$sourceDir = (Get-Item -Path "..").FullName
>> `$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)"
>> )
>>
>> Write-Host "运行 CMake: cmake `$sourceDir @cmakeArgs"
>> cmake `$sourceDir @cmakeArgs
>> "@ | Set-Content cmake_config.ps1
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 3. 完整构建脚本
(rtx5070_env) PS E:\PyTorch_Build\pytorch> @"
>> # full_build.ps1
>> # 初始化环境
>> .\setup_environment.ps1
>>
>> # 准备构建目录
>> Set-Location E:\PyTorch_Build\pytorch
>> Remove-Item build -Recurse -Force -ErrorAction SilentlyContinue
>> New-Item -Path build -ItemType Directory | Out-Null
>> Set-Location build
>>
>> # 配置 CMake
>> .\cmake_config.ps1
>>
>> # 编译和安装
>> if (`$LASTEXITCODE -eq 0) {
>> cmake --build . --config Release --parallel 8
>> if (`$LASTEXITCODE -eq 0) {
>> cmake --install .
>> }
>> }
>>
>> # 验证安装
>> python -c "import torch; print(f'PyTorch版本: {torch.__version__}'); print(f'CUDA可用: {torch.cuda.is_available()}')"
>> "@ | Set-Content full_build.ps1
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 4. CUDA测试脚本(修复终止符问题)
(rtx5070_env) PS E:\PyTorch_Build\pytorch> @"
>> # compile_cuda_test.ps1
>> # 创建CUDA测试程序
>> `$cudaTest = @'
>> #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;
>> }
>> '@
>>
>> `$cudaTest | Set-Content cuda_test.cpp
>>
>> # 编译测试
>> `$ccbinPath = "`$env:VCToolsInstallDir\bin\Hostx64\x64"
>> nvcc -ccbin "`$ccbinPath" cuda_test.cpp -o cuda_test
>>
>> # 运行测试
>> if (Test-Path cuda_test.exe) {
>> .\cuda_test.exe
>> } else {
>> Write-Host "CUDA编译失败,请检查环境"
>> }
>> "@ | Set-Content compile_cuda_test.ps1
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 1. 确保在正确目录
(rtx5070_env) PS E:\PyTorch_Build\pytorch> cd E:\PyTorch_Build\pytorch
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 2. 清理并创建虚拟环境
(rtx5070_env) PS E:\PyTorch_Build\pytorch> Get-Process python* | Stop-Process -Force -ErrorAction SilentlyContinue
(rtx5070_env) PS E:\PyTorch_Build\pytorch> Remove-Item rtx5070_env -Recurse -Force -ErrorAction SilentlyContinue
(rtx5070_env) PS E:\PyTorch_Build\pytorch> python -m venv rtx5070_env
(rtx5070_env) PS E:\PyTorch_Build\pytorch> .\rtx5070_env\Scripts\activate
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 3. 设置执行策略
(rtx5070_env) PS E:\PyTorch_Build\pytorch> Set-ExecutionPolicy RemoteSigned -Scope Process -Force
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 4. 创建所有脚本文件
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # [粘贴上面的脚本创建命令]
(rtx5070_env) PS E:\PyTorch_Build\pytorch>
(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 5. 执行构建流程
(rtx5070_env) PS E:\PyTorch_Build\pytorch> .\full_build.ps1
.\cmake_config.ps1: E:\PyTorch_Build\pytorch\full_build.ps1:12
Line |
12 | .\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)
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build>
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build> # 6. 验证CUDA编译
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build> .\compile_cuda_test.ps1
.\compile_cuda_test.ps1: The term '.\compile_cuda_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>
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build> # 7. 验证PyTorch安装
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build> python -c "import torch; print(f'PyTorch版本: {torch.__version__}'); print(f'CUDA可用: {torch.cuda.is_available()}')"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build> if ($?) {
>> python -c @"
>> import torch
>> print(f'CUDA设备数量: {torch.cuda.device_count()}')
>> if torch.cuda.is_available():
>> print(f'设备0名称: {torch.cuda.get_device_name(0)}')
>> "@
>> }
(rtx5070_env) PS E:\PyTorch_Build\pytorch\build>