aisop/tools/windows/install-d3nano.ps1

104 lines
5.3 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

param(
[string]$InstallRoot = 'D:\D3Nano',
[string]$ModelTar = '',
[string]$WheelsTar = '',
[string]$WheelsDir = 'D:\d3-wheels',
[string]$PythonExe = '',
[int]$Port = 8766,
[int]$SkipModel = 0,
[int]$SkipVenv = 0
)
# 远端 d3-nano 安装脚本:解包模型、建 .venv、离线装依赖、写 djev 配置并校验 CUDA 可用性。
# 由 tools/Deploy-D3Nano.ps1 通过 SSH 调用,参数不含空格。
$ErrorActionPreference = 'Stop'
$python = Join-Path $InstallRoot '.venv\Scripts\python.exe'
$logDir = Join-Path $InstallRoot 'logs'
New-Item -ItemType Directory -Force -Path $InstallRoot, $logDir | Out-Null
# 1. 模型:model/*.safetensors 等与 local-model-manifest.json(启动脚本按清单核对来源)。
if (!$SkipModel) {
if (!$ModelTar) { $ModelTar = Join-Path $InstallRoot 'd3-nano-model.tar' }
if (!(Test-Path -LiteralPath $ModelTar)) { throw "缺少模型包: $ModelTar" }
tar -xf $ModelTar -C $InstallRoot
if ($LASTEXITCODE) { throw "解压模型包失败: $ModelTar" }
}
foreach ($required in @('model\decision_config.json', 'model\model.safetensors', 'model\d3_runtime.py', 'local-model-manifest.json')) {
if (!(Test-Path -LiteralPath (Join-Path $InstallRoot $required))) { throw "模型不完整: $InstallRoot\$required" }
}
# 2. Python 3.13:远端可能只有 WindowsApps 占位程序,需要真正的解释器。
function Find-Python() {
$candidates = @('C:\Program Files\Python313\python.exe', 'C:\Program Files\Python313-3.13\python.exe',
(Join-Path $env:ProgramData 'Python313\python.exe'), (Join-Path $env:LOCALAPPDATA 'Programs\Python\Python313\python.exe'))
foreach ($candidate in $candidates) { if (Test-Path -LiteralPath $candidate) { return $candidate } }
foreach ($name in @('py', 'python')) {
$command = Get-Command $name -ErrorAction SilentlyContinue
if (!$command) { continue }
if ($command.Source -like '*WindowsApps*') { continue }
$detected = (& $name -c "import sys;print(sys.executable)" 2>$null | Out-String).Trim()
if ($detected -and (Test-Path -LiteralPath $detected)) { return $detected }
}
return ''
}
if (!$PythonExe) { $PythonExe = Find-Python }
if (!$PythonExe) {
Write-Output '未找到 Python,使用 winget 安装 Python 3.13'
& winget install --id Python.Python.3.13 --exact --scope machine --silent --accept-package-agreements --accept-source-agreements | Out-Null
$PythonExe = Find-Python
}
if (!$PythonExe) { throw '远端没有可用的 Python 3.13,请先安装后重试' }
$baseVersion = (& $PythonExe -c "import sys;print(sys.version.split()[0])" | Out-String).Trim()
$baseMinor = (& $PythonExe -c "import sys;print('%d.%d' % sys.version_info[:2])" | Out-String).Trim()
if ($baseMinor -ne '3.13') { Write-Output "警告:基础 Python 为 $baseVersion,wheel 包按 cp313 下载,版本不一致会安装失败" }
# 3. 虚拟环境与离线依赖(远端访问 pytorch 源很慢,wheel 由本机打包传入)。
if (!$SkipVenv) {
if (!(Test-Path -LiteralPath $python)) {
& $PythonExe -m venv (Join-Path $InstallRoot '.venv')
if ($LASTEXITCODE) { throw '创建 .venv 失败' }
}
if (!$WheelsDir -and $WheelsTar) {
tar -xf $WheelsTar -C (Split-Path $WheelsDir -Parent)
if ($LASTEXITCODE) { throw "解压 wheel 包失败: $WheelsTar" }
}
if (!(Test-Path -LiteralPath $WheelsDir)) { throw "缺少 wheel 目录: $WheelsDir" }
& $python -m pip install --no-index --find-links $WheelsDir --no-warn-script-location torch torchvision transformers accelerate safetensors huggingface_hub pillow
if ($LASTEXITCODE) { throw '离线安装 d3-nano 依赖失败' }
}
if (!(Test-Path -LiteralPath $python)) { throw "缺少虚拟环境解释器: $python" }
# 4. 配置:djev 档位固定 nano;系统环境变量让桌面程序按此根目录自动拉起。
$config = [ordered]@{ tier = 'nano'; nano_root = $InstallRoot }
$config | ConvertTo-Json | Set-Content -LiteralPath (Join-Path $InstallRoot 'djev.json') -Encoding UTF8
& setx /M SOPHELPER_D3_ROOT $InstallRoot | Out-Null
$machineRoot = [Environment]::GetEnvironmentVariable('SOPHELPER_D3_ROOT', 'Machine')
# 5. 校验:依赖导入、CUDA 可用与目标显卡算力(RTX 5060 需要 cu128 的 sm_120 构建)。
$verify = Join-Path $env:TEMP 'd3nano-verify.py'
@'
import json, sys
import torch, torchvision, transformers, PIL, safetensors, accelerate, huggingface_hub
print(json.dumps({
"python": sys.version.split()[0],
"torch": torch.__version__,
"cudaBuild": torch.version.cuda,
"cudaAvailable": torch.cuda.is_available(),
"device": torch.cuda.get_device_name(0) if torch.cuda.is_available() else None,
"capability": list(torch.cuda.get_device_capability(0)) if torch.cuda.is_available() else None,
"totalMemoryGB": round(torch.cuda.get_device_properties(0).total_memory / 2 ** 30, 2) if torch.cuda.is_available() else None,
"transformers": transformers.__version__,
"torchvision": torchvision.__version__,
}, ensure_ascii=False))
'@ | Set-Content -LiteralPath $verify -Encoding UTF8
& $python $verify
if ($LASTEXITCODE) { throw 'd3-nano 运行环境校验失败' }
Remove-Item -LiteralPath $verify -Force
Write-Output (ConvertTo-Json -InputObject ([ordered]@{
installed = $InstallRoot
python = $python
basePython = $PythonExe
d3RootEnv = $machineRoot
port = $Port
}) -Compress)