Files

128 lines
7.7 KiB
PowerShell

# Copy the model volume from a WSL2 distribution to a Windows folder, lay it out the way `leleka` expects, and verify the
# SHA-256 pins through `leleka.exe verify`.
#
# powershell -ExecutionPolicy Bypass -File scripts\windows\stage-model.ps1 -Dest D:\leleka-assets [-Distro Ubuntu]
# [-Source /home/me/leleka-assets] [-BinSrc dist\bin] [-LelekaExe dist\bin\leleka.exe] [-CublasDir DIR] [-SkipVerify]
#
# Source: a Linux path inside the distribution (default: ~/leleka-assets, else ~/laptop). Both layouts are understood: the
# archive at the volume root (what `leleka` makes) or inside model\ (the older laptop volume). It is read through
# \\wsl.localhost\<distro>\... and nothing in the distribution is changed. Copying is resumable: robocopy skips files that
# already match, so rerun the script after an interruption.
#
# Layout written to -Dest:
# model\*.gguf (2 shards) qwen-iq3-s-l2-v4.kar (+ .provenance.json) AT THE ROOT as a regular single-link file
# data\mtp-rt\* data\expert-profile.bin (when the source has it) tokenizer-assets\ (when the source has it)
#
# Verification: `leleka.exe up --native --mode none` (prepares the state, installs the programs, extracts the tokenizer when
# it was not copied) and then `leleka.exe verify --native`, which hashes both shards and the archive in full against the pins.
# That needs the five programs (scripts\windows\build-release.ps1) and the cuBLAS DLLs (scripts\windows\fetch-cublas.ps1).
param(
[Parameter(Mandatory = $true)][string]$Dest,
[string]$Distro = "Ubuntu",
[string]$Source = "",
[string]$BinSrc = "",
[string]$LelekaExe = "",
[string]$CublasDir = "",
[switch]$SkipVerify
)
$ErrorActionPreference = "Stop"
$root = (Resolve-Path (Join-Path $PSScriptRoot "..\..")).Path
$Shards = @(
@{ Name = "Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00001-of-00002.gguf"; Bytes = 54817524224 },
@{ Name = "Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00002-of-00002.gguf"; Bytes = 28800138432 }
)
$KarName = "qwen-iq3-s-l2-v4.kar"
$KarBytes = 55295529472
$MtpFiles = @("dense.bin", "dense.txt", "draft_vocab.bin", "experts.bin")
function Say([string]$m) { Write-Host "stage-model: $m" }
# --- where is the source -----------------------------------------------------------------------------------------------
$home_ = (& wsl.exe -d $Distro --exec printenv HOME).Trim()
if (-not $home_) { throw "cannot reach the WSL distribution '$Distro' (wsl -l -v lists them)" }
function ToUnc([string]$linux) { "\\wsl.localhost\$Distro" + ($linux -replace "/", "\") }
if (-not $Source) {
foreach ($candidate in @("$home_/leleka-assets", "$home_/laptop")) {
if (Test-Path (ToUnc "$candidate/model")) { $Source = $candidate; break }
}
if (-not $Source) { throw "no volume found in $Distro (looked for ~/leleka-assets and ~/laptop); pass -Source" }
}
$src = ToUnc $Source
Say "source $src"
$srcModel = Join-Path $src "model"
$karDir = if (Test-Path (Join-Path $src $KarName)) { $src } elseif (Test-Path (Join-Path $srcModel $KarName)) { $srcModel } else { throw "$KarName is neither in $src nor in $srcModel" }
foreach ($shard in $Shards) {
$path = Join-Path $srcModel $shard.Name
if (-not (Test-Path $path)) { throw "$path is missing" }
if ((Get-Item $path).Length -ne $shard.Bytes) { throw "$path has the wrong length" }
}
if ((Get-Item (Join-Path $karDir $KarName)).Length -ne $KarBytes) { throw "$KarName has the wrong length" }
if (-not (Test-Path (Join-Path $karDir "$KarName.provenance.json"))) { throw "$KarName.provenance.json is missing next to the archive" }
$srcMtp = Join-Path $src "data\mtp-rt"
foreach ($name in $MtpFiles) { if (-not (Test-Path (Join-Path $srcMtp $name))) { throw "$srcMtp\$name is missing (the MTP pack)" } }
# --- room for it --------------------------------------------------------------------------------------------------------
New-Item -ItemType Directory -Force -Path $Dest | Out-Null
$Dest = (Resolve-Path $Dest).Path
$need = $KarBytes + ($Shards | ForEach-Object { $_.Bytes } | Measure-Object -Sum).Sum + 4GB
$drive = Get-PSDrive -Name $Dest.Substring(0, 1)
$have = $drive.Free
$already = (Get-ChildItem -Recurse -File $Dest -ErrorAction SilentlyContinue | Measure-Object Length -Sum).Sum
if ($have + $already -lt $need) {
throw ("{0} has {1:N0} GiB free; the volume needs about {2:N0} GiB" -f $drive.Name, (($have + $already) / 1GB), ($need / 1GB))
}
# --- copy (robocopy prints a percentage per file; it skips files that already match) ----------------------------------------
function Copy-Tree([string]$from, [string]$to, [string[]]$files = @(), [switch]$Recurse) {
$arguments = @($from, $to) + $files + @("/J", "/R:3", "/W:5", "/NJH", "/NJS", "/NDL")
if ($Recurse) { $arguments += "/E" }
# Redirected to a file, robocopy would write one line per percent point: drop the progress there.
if ([Console]::IsOutputRedirected) { $arguments += "/NP" }
Say "copying $from -> $to"
& robocopy.exe @arguments
if ($LASTEXITCODE -ge 8) { throw "robocopy failed ($LASTEXITCODE) for $from" }
}
Copy-Tree $srcModel (Join-Path $Dest "model") ($Shards | ForEach-Object { $_.Name })
Copy-Tree $karDir $Dest @($KarName, "$KarName.provenance.json")
Copy-Tree $srcMtp (Join-Path $Dest "data\mtp-rt") -Recurse
if (Test-Path (Join-Path $src "data\expert-profile.bin")) {
Copy-Tree (Join-Path $src "data") (Join-Path $Dest "data") @("expert-profile.bin")
} else {
Say "the source has no data\expert-profile.bin (optional: without it the engine runs without the ranked VRAM expert tier)"
}
if (Test-Path (Join-Path $src "tokenizer-assets")) {
Copy-Tree (Join-Path $src "tokenizer-assets") (Join-Path $Dest "tokenizer-assets") -Recurse
}
# --- the layout checks the engine will make -----------------------------------------------------------------------------------
$kar = Join-Path $Dest $KarName
foreach ($shard in $Shards) {
if ((Get-Item (Join-Path $Dest "model\$($shard.Name)")).Length -ne $shard.Bytes) { throw "$($shard.Name) was not copied completely" }
}
if ((Get-Item $kar).Length -ne $KarBytes) { throw "$KarName was not copied completely" }
$links = @(& fsutil.exe hardlink list $kar).Count
if ($links -ne 1) { throw "$kar has $links hard links; the engine admits a single-link regular file only" }
Set-ItemProperty -Path $kar -Name IsReadOnly -Value $true
Say "layout ok: shards in model\, $KarName at the root (regular file, one link, read-only)"
if ($SkipVerify) { Say "verification skipped (-SkipVerify); run: leleka.exe verify --native --assets $Dest"; exit 0 }
# --- verify the pins through leleka.exe ------------------------------------------------------------------------------------------
if (-not $LelekaExe) {
foreach ($candidate in @("dist\bin\leleka.exe", "target\release\leleka.exe")) {
if (Test-Path (Join-Path $root $candidate)) { $LelekaExe = Join-Path $root $candidate; break }
}
}
if (-not $LelekaExe) { throw "leleka.exe not found; build it (scripts\windows\build-release.ps1) or pass -LelekaExe" }
if (-not $BinSrc) { $BinSrc = Split-Path -Parent $LelekaExe }
$common = @("--native", "--assets", $Dest)
if ($CublasDir) { $common += @("--cublas-dir", $CublasDir) }
Say "preparing the state (programs, tokens, tokenizer, configuration); this does not start anything"
& $LelekaExe up @common "--mode" "none" "--bin-src" $BinSrc
if ($LASTEXITCODE -ne 0) { throw "leleka up --mode none failed ($LASTEXITCODE); run 'leleka doctor' for what is missing" }
Say "hashing both shards and the archive in full against the pinned SHA-256 (about 5-10 minutes, disk bound)"
& $LelekaExe verify @common
if ($LASTEXITCODE -ne 0) { throw "verification FAILED ($LASTEXITCODE): a file does not match its pinned digest; copy it again" }
Say "verified. Start the service: $LelekaExe up --native --foreground --assets $Dest"