Files
leleka/benchmarks/tools/Compare-PrefillWorkers.ps1
T

138 lines
7.6 KiB
PowerShell

<#
.SYNOPSIS
Paired, alternating comparison of two workers (default: the CPU worker and the GPU-prefill worker; -ArmA/-ArmB
choose others, such as leleka-gpu-npu-worker with -OpenvinoRoot) on an agent-shaped episode,
through the control plane's /v1/responses.
.DESCRIPTION
Each arm starts leleka-api with one worker, waits for readiness, then replays the same scripted episode:
turn k sends the user task plus k (function_call, function_call_output) pairs whose outputs are large,
deterministic pseudo-source listings (-ToolTokens tokens each, roughly), with a small output cap. Earlier turns
are an exact prefix of later ones, so after turn 1 each request only has the newest tool output to process
(the cached prefix is reused). Wall time per turn is dominated by prefill of that new text; usage reports
input/cached/output tokens. The model's own answers are discarded (history is replayed, not generated).
Arms alternate in order (cpu,gpu,gpu,cpu,...) for -Pairs pairs; only processes this script starts are stopped.
Warm model file, cold worker cache per arm (a fresh process), one untimed 1-token request per start.
Build first: cargo build --release -p leleka-control-plane -p leleka-cpu-worker -p leleka-igpu-worker
It is a measurement of this scripted episode, not of real agent traffic. Record machine state yourself
(Capture-MachineState.ps1). Results are appended as JSON lines to -Output (outside Git).
#>
param(
[Parameter(Mandatory)] [string] $Model,
[Parameter(Mandatory)] [string] $Output,
[string] $RepoRoot,
[int] $Port = 8081,
[int] $Context = 16384,
[int] $Threads = 10,
[int] $Turns = 4,
[int] $ToolTokens = 1500,
[int] $MaxOutput = 32,
[int] $Pairs = 3,
[int] $GpuMinTokens = 256,
[string] $ArmA = 'leleka-cpu-worker',
[string] $ArmB = 'leleka-igpu-worker',
[string] $OpenvinoRoot = $env:LELEKA_OPENVINO_ROOT,
[double] $NpuShare = 0.45,
[int] $SplitMinTokens = 1024,
# keep each arm's control-plane and worker standard error here (as <worker>-pair<p>.err.txt); also turns on
# the GPU+NPU worker's split timing log
[string] $LogDir
)
$ErrorActionPreference = 'Stop'
if (-not $RepoRoot) { $RepoRoot = (Resolve-Path (Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Path) '..\..')).Path }
$bin = Join-Path $RepoRoot 'target\release'
# deterministic filler that tokenizes to roughly 4 characters per token
function New-ToolOutput([int]$turn, [int]$tokens) {
$rng = New-Object System.Random (1000 + $turn)
$words = 'fn','let','mut','self','return','match','impl','struct','Vec','String','Result','Option','usize','for','in','if','else','pub','use','mod','const','loop','while','break'
$sb = New-Object Text.StringBuilder
$line = 0
while ($sb.Length -lt $tokens * 4) {
$n = $rng.Next(4, 10)
$parts = 1..$n | ForEach-Object { $words[$rng.Next(0, $words.Count)] + $rng.Next(0, 99) }
[void]$sb.Append("$line " + ($parts -join ' ') + ";`n")
$line++
}
$sb.ToString()
}
$outputs = 1..$Turns | ForEach-Object { New-ToolOutput $_ $ToolTokens }
$tool = @{ type = 'function'; name = 'read_file'; description = 'Read a source file.'
parameters = @{ type = 'object'; properties = @{ path = @{ type = 'string' } }; required = @('path') } }
function Start-Arm([string]$worker) {
$work = Join-Path ([IO.Path]::GetTempPath()) ("leleka-ab-" + [Guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory $work | Out-Null
$workerArgs = @('--model', $Model, '--ctx', "$Context", '--threads', "$Threads")
if ($worker -in 'leleka-igpu-worker', 'leleka-gpu-npu-worker') { $workerArgs += @('--gpu-min-tokens', "$GpuMinTokens") }
if ($worker -in 'leleka-npu-worker', 'leleka-gpu-npu-worker') { $workerArgs += @('--openvino-root', $OpenvinoRoot) }
if ($worker -eq 'leleka-gpu-npu-worker') {
$workerArgs += @('--npu-share', "$NpuShare", '--split-min-tokens', "$SplitMinTokens")
}
$config = @{ bind = "127.0.0.1:$Port"; model = 'gpt-oss-20b'
engine = @{ executable = (Join-Path $bin "$worker.exe"); args = $workerArgs; gptoss_gguf = $Model
startup_timeout_s = 300; generation_timeout_s = 1200 } } | ConvertTo-Json -Depth 5
[IO.File]::WriteAllText((Join-Path $work 'config.json'), $config, (New-Object Text.UTF8Encoding($false)))
$server = Start-Process (Join-Path $bin 'leleka-api.exe') -ArgumentList @('serve', '--config', (Join-Path $work 'config.json')) `
-PassThru -WindowStyle Hidden -RedirectStandardOutput (Join-Path $work 'out.txt') -RedirectStandardError (Join-Path $work 'err.txt')
$ready = $false
foreach ($i in 1..100) {
Start-Sleep 3
if ($server.HasExited) { throw "the control plane exited; see $work\err.txt" }
try { if ((Invoke-WebRequest "http://127.0.0.1:$Port/ready" -UseBasicParsing -TimeoutSec 3).StatusCode -eq 200) { $ready = $true; break } } catch {}
}
if (-not $ready) { throw "not ready; see $work\err.txt" }
[pscustomobject]@{ Server = $server; Work = $work; Worker = $worker; Pair = $script:pair }
}
function Stop-Arm($arm) {
Get-CimInstance Win32_Process -Filter "ParentProcessId=$($arm.Server.Id)" -ErrorAction SilentlyContinue |
ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }
if (-not $arm.Server.HasExited) { Stop-Process $arm.Server -Force }
Start-Sleep 2
if ($LogDir) {
Copy-Item (Join-Path $arm.Work 'err.txt') (Join-Path $LogDir "$($arm.Worker)-pair$($arm.Pair).err.txt") -ErrorAction SilentlyContinue
}
Remove-Item $arm.Work -Recurse -Force -ErrorAction SilentlyContinue
}
function Send([object]$body) {
$uri = "http://127.0.0.1:$Port/v1/responses"
Invoke-RestMethod $uri -Method Post -Body ($body | ConvertTo-Json -Depth 10 -Compress) -ContentType 'application/json' -TimeoutSec 1500
}
function Run-Episode([string]$worker) {
$arm = Start-Arm $worker
try {
$null = Send @{ model = 'gpt-oss-20b'; input = 'Say OK.'; max_output_tokens = 8; reasoning = @{ effort = 'low' } }
$rows = @()
foreach ($k in 1..$Turns) {
$items = @(@{ role = 'user'; content = 'Review the repository and summarize what each file does.' })
foreach ($i in 1..$k) {
$items += @{ type = 'function_call'; call_id = "call_$i"; name = 'read_file'; arguments = "{`"path`":`"src/file$i.rs`"}" }
$items += @{ type = 'function_call_output'; call_id = "call_$i"; output = $outputs[$i - 1] }
}
$sw = [Diagnostics.Stopwatch]::StartNew()
$r = Send @{ model = 'gpt-oss-20b'; input = $items; tools = @($tool); max_output_tokens = $MaxOutput; reasoning = @{ effort = 'low' } }
$sw.Stop()
$rows += [pscustomobject]@{ worker = $worker; turn = $k; wall_s = [math]::Round($sw.Elapsed.TotalSeconds, 2)
input_tokens = $r.usage.input_tokens; cached_tokens = $r.usage.input_tokens_details.cached_tokens; output_tokens = $r.usage.output_tokens }
}
$rows
} finally { Stop-Arm $arm }
}
$meta = @{ kind = 'meta'; date = (Get-Date -Format 'yyyy-MM-dd HH:mm'); model = $Model; turns = $Turns; tool_tokens = $ToolTokens
max_output = $MaxOutput; pairs = $Pairs; context = $Context; threads = $Threads; gpu_min_tokens = $GpuMinTokens
arm_a = $ArmA; arm_b = $ArmB; npu_share = $NpuShare; split_min_tokens = $SplitMinTokens }
($meta | ConvertTo-Json -Compress) | Add-Content $Output
if ($LogDir) { New-Item -ItemType Directory -Force $LogDir | Out-Null; $env:LELEKA_SPLIT_LOG = '1' }
foreach ($p in 1..$Pairs) {
$script:pair = $p
$order = if ($p % 2 -eq 1) { $ArmA, $ArmB } else { $ArmB, $ArmA }
foreach ($w in $order) {
"pair $p, $w"
foreach ($row in (Run-Episode $w)) {
$row | Add-Member pair $p -PassThru | ConvertTo-Json -Compress | Add-Content $Output
" turn $($row.turn): $($row.wall_s) s, input $($row.input_tokens), cached $($row.cached_tokens), output $($row.output_tokens)"
}
}
}