aisop/tools/Deploy-D3Nano.ps1

70 lines
4.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]$Target = 'Administrator@192.168.0.184',
[string]$InstallRoot = 'D:\D3Nano',
[string]$ModelSource = 'E:\D3Nano',
[string]$WheelsSource = 'E:\00-Inbox\aisop\scratch\wheels\d3nano',
[string]$WheelsRemote = 'D:\d3-wheels',
[string]$ServiceScript = 'C:\sophelper\current\services\start_d3_lite_local.py',
[int]$Port = 8766,
[switch]$SkipWheels,
[switch]$SkipStart
)
# 把 d3-nano(模型 + 离线 wheel + Python 环境)部署到远端 Windows 工位机,并实机验证服务。
# 远端访问 pytorch 官方源很慢,wheel 由本机 pip download 后打包传入。
$ErrorActionPreference = 'Stop'
if ($Target -notmatch '^[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+$') { throw 'Target must be user@host' }
if ($InstallRoot -notmatch '^[A-Za-z]:\\[A-Za-z0-9_.\\-]+$') { throw 'InstallRoot must be an absolute Windows path without spaces' }
if ($WheelsRemote -notmatch '^[A-Za-z]:\\[A-Za-z0-9_.\\-]+$') { throw 'WheelsRemote must be an absolute Windows path without spaces' }
$repo = Split-Path $PSScriptRoot -Parent
$remoteFwd = $InstallRoot.Replace('\', '/')
# 1. 模型包:model/ 与 local-model-manifest.json,跳过 __pycache__ 与下载缓存。
$modelTar = Join-Path $repo 'artifacts\d3-nano-model.tar'
Push-Location $ModelSource
try {
$items = Get-ChildItem -LiteralPath $ModelSource -Force | Where-Object { $_.Name -in @('model', 'local-model-manifest.json', 'local-serving-settings.json') } | Select-Object -ExpandProperty Name
if (!$items) { throw "模型源目录缺少 model/:$ModelSource" }
if (Test-Path -LiteralPath $modelTar) { Remove-Item -LiteralPath $modelTar -Force }
& tar -cf $modelTar $items
if ($LASTEXITCODE) { throw '打包模型失败' }
} finally { Pop-Location }
Write-Host ('模型包: ' + $modelTar + ' (' + [math]::Round((Get-Item $modelTar).Length/1GB,2) + ' GB)')
# 2. wheel 包:本机 pip download 得到的 cp313 / win_amd64 / cu128 依赖。
$wheelsTar = ''
if (!$SkipWheels) {
if (!(Test-Path -LiteralPath $WheelsSource)) { throw "缺少 wheel 目录: $WheelsSource" }
$wheelCount = @(Get-ChildItem -LiteralPath $WheelsSource -Filter *.whl).Count
if (!$wheelCount) { throw "目录中没有 wheel: $WheelsSource" }
$wheelsTar = Join-Path $repo 'artifacts\d3-nano-wheels.tar'
if (Test-Path -LiteralPath $wheelsTar) { Remove-Item -LiteralPath $wheelsTar -Force }
& tar -cf $wheelsTar -C $WheelsSource .
if ($LASTEXITCODE) { throw '打包 wheel 失败' }
Write-Host ('wheel 包: ' + $wheelsTar + ' (' + [math]::Round((Get-Item $wheelsTar).Length/1MB,1) + ' MB, ' + $wheelCount + ' 个)')
}
# 3. 传入远端并安装。
$payload = @($modelTar, (Join-Path $PSScriptRoot 'windows\install-d3nano.ps1'), (Join-Path $PSScriptRoot 'windows\d3nano-service.py'), (Join-Path $PSScriptRoot 'windows\probe-d3nano.py'))
if ($wheelsTar) { $payload += $wheelsTar }
& ssh -o BatchMode=yes $Target "New-Item -ItemType Directory -Force -Path $InstallRoot, $WheelsRemote, $InstallRoot\benchmark | Out-Null"
if ($LASTEXITCODE) { throw 'SSH failed' }
& scp -o BatchMode=yes $payload "${Target}:$remoteFwd/"
if ($LASTEXITCODE) { throw 'Transfer failed' }
$images = Get-ChildItem -LiteralPath (Join-Path $repo 'artifacts\imajev-benchmark') -Filter *-input.jpg
& scp -o BatchMode=yes $images.FullName "${Target}:$remoteFwd/benchmark/"
if ($LASTEXITCODE) { throw 'Transfer failed' }
if ($wheelsTar) {
& ssh -o BatchMode=yes $Target "tar -xf $InstallRoot\d3-nano-wheels.tar -C $WheelsRemote"
if ($LASTEXITCODE) { throw '远端解包 wheel 失败' }
}
& ssh -o BatchMode=yes $Target "powershell -NoProfile -ExecutionPolicy Bypass -File $InstallRoot\install-d3nano.ps1 -InstallRoot $InstallRoot -WheelsDir $WheelsRemote"
if ($LASTEXITCODE) { throw '安装 d3-nano 失败' }
# 4. 在同一 SSH 会话中临时启动并验收,完成后释放模型。日常服务由桌面程序直接拉起。
$venvPython = Join-Path $InstallRoot '.venv\Scripts\python.exe'
if (!$SkipStart) {
& ssh -o BatchMode=yes $Target "$venvPython $InstallRoot\d3nano-service.py verify --root $InstallRoot --port $Port --script $ServiceScript --image-root $InstallRoot\benchmark"
if ($LASTEXITCODE) { throw 'd3-nano 真实图片请求验证失败' }
}
Write-Host "已部署 d3-nano 到 $Target:$InstallRoot;SOPHELPER_D3_ROOT 已写入系统环境变量。"