$ErrorActionPreference='Continue' $B='\build'; $log="$B\e21r_chain.log" function L($m){ "$(Get-Date -Format o) $m" | Out-File -FilePath $log -Append -Encoding utf8 } L "=== e21r start: mask-model prediction test + ub16/ub8 max resident ctx, 4B q4_0 KV ===" $out='\e21_budget\e21r'; New-Item -ItemType Directory -Force $out | Out-Null $res="$out\results.jsonl" $bin='\fork-29827\build-cuda\bin\llama-server.exe' $m4='\q35\Qwen3.5-4B-Q4_K_M.gguf' function PC($id,$kind){ try{ [math]::Round(((Get-Counter "\GPU Process Memory(pid_$($id)*)\$kind" -ErrorAction Stop).CounterSamples | Measure-Object CookedValue -Sum).Sum/1MB) }catch{ $null } } function AllShared(){ try{ [math]::Round(((Get-Counter "\GPU Process Memory(*)\Shared Usage" -ErrorAction Stop).CounterSamples | Measure-Object CookedValue -Sum).Sum/1MB) }catch{ $null } } function Smi(){ [int]((& nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits) -join '') } function Y($ctx,$sc){ @('--override-kv',"qwen35.context_length=int:$ctx",'--rope-scaling','yarn','--rope-scale',"$sc",'--yarn-orig-ctx','262144') } Get-Process llama-server -ErrorAction SilentlyContinue | Stop-Process -Force; Start-Sleep 8 $r=[ordered]@{tag='baseline_no_server';smi_used_mib=(Smi);all_shared_mib=(AllShared);t=(Get-Date -Format o)} ($r|ConvertTo-Json -Compress)|Out-File $res -Append -Encoding utf8; L ("result "+($r|ConvertTo-Json -Compress)) $cfgs=@( @{tag='c512k_ub128';c=524288;ub=128;x=(Y 524288 2)}, @{tag='c512k_ub32';c=524288;ub=32;x=(Y 524288 2)}, @{tag='c256k_ub128';c=262144;ub=128;x=@()}, @{tag='c384k_ub16';c=393216;ub=16;x=(Y 393216 1.5)}, @{tag='c704k_ub8';c=720896;ub=8;x=(Y 720896 2.75)}, @{tag='c736k_ub16';c=753664;ub=16;x=(Y 753664 2.875)}, @{tag='c768k_ub16';c=786432;ub=16;x=(Y 786432 3)}, @{tag='c768k_ub8';c=786432;ub=8;x=(Y 786432 3)}, @{tag='c800k_ub16';c=819200;ub=16;x=(Y 819200 3.125)}, @{tag='c832k_ub16';c=851968;ub=16;x=(Y 851968 3.25)} ) foreach($c in $cfgs){ Get-Process llama-server -ErrorAction SilentlyContinue | Stop-Process -Force; Start-Sleep 6 $pre=AllShared; $lf="$out\srv_$($c.tag).log" $a=@('-m',$m4,'-c',$c.c,'-ngl','99','-fa','on','-np','1','--port','8097','-ctk','q4_0','-ctv','q4_0','-ub',$c.ub,'-b','512','-t','8')+$c.x $p=Start-Process -FilePath $bin -ArgumentList $a -RedirectStandardError $lf -RedirectStandardOutput ($lf+'.out') -PassThru -NoNewWindow $t0=Get-Date; $ok=$false while(((Get-Date)-$t0).TotalSeconds -lt 300 -and -not $p.HasExited){ if(Select-String -Path $lf -Pattern 'listening on' -Quiet -ErrorAction SilentlyContinue){$ok=$true;break}; Start-Sleep 3 } Start-Sleep 15 $sh=@(); $de=@(); for($k=0;$k -lt 5;$k++){ $sh+=PC $p.Id 'Shared Usage'; $de+=PC $p.Id 'Dedicated Usage'; Start-Sleep 2 } $bufs=(Select-String -Path $lf -Pattern 'buffer size' -ErrorAction SilentlyContinue | % { $_.Line.Trim() }) -join ' | ' $short=$null if($ok){ try{ $body=@{prompt=('The quick brown fox jumps over the lazy dog. '*200);n_predict=8;temperature=0;cache_prompt=$false}|ConvertTo-Json -Compress; [IO.File]::WriteAllText("$out\b.json",$body); $null=Invoke-RestMethod -Uri 'http://127.0.0.1:8097/completion' -Method Post -ContentType 'application/json' -InFile "$out\b.json" -TimeoutSec 600; $short=PC $p.Id 'Shared Usage' }catch{ $short='ERR' } } $r=[ordered]@{tag=$c.tag;ctx=$c.c;ub=$c.ub;loaded=$ok;exited=$p.HasExited;all_shared_before_mib=$pre;proc_shared_mib=($sh|Measure-Object -Maximum).Maximum;proc_dedicated_mib=($de|Measure-Object -Maximum).Maximum;smi_used_mib=(Smi);all_shared_mib=(AllShared);proc_shared_after_2k_prompt=$short;buffers=$bufs;t=(Get-Date -Format o)} ($r|ConvertTo-Json -Compress)|Out-File $res -Append -Encoding utf8; L ("result "+($r|ConvertTo-Json -Compress)) try{Stop-Process -Id $p.Id -Force}catch{} } Get-Process llama-server -ErrorAction SilentlyContinue | Stop-Process -Force L "=== E21rDONE ==="