$ErrorActionPreference='Continue' $B='C:\AGI\agi-research\build'; $log="$B\e21j_chain.log" function L($m){ "$(Get-Date -Format o) $m" | Out-File -FilePath $log -Append -Encoding utf8 } L "=== e21j start: same multikey needle as E21i; 4B accuracy-vs-context curve (128K-512K) and 9B 480K ===" $out='C:\AGI\agi-research\e21_budget\e21j'; New-Item -ItemType Directory -Force $out | Out-Null $res="$out\results.jsonl" $bin='C:\AGI\llama.cpp\fork-29827\build-cuda\bin\llama-server.exe' $m4='C:\AGI\gguf\q35\Qwen3.5-4B-Q4_K_M.gguf'; $m9='C:\AGI\gguf\q35\Qwen3.5-9B-Q4_K_M.gguf' function Shared($id){ try{ [math]::Round(((Get-Counter "\GPU Process Memory(pid_$($id)*)\Shared Usage" -ErrorAction Stop).CounterSamples | Measure-Object CookedValue -Sum).Sum/1MB) }catch{ $null } } function Start-Srv($c,$lf){ Get-Process llama-server -ErrorAction SilentlyContinue | Stop-Process -Force; Start-Sleep 5 $a=@('-m',$c.m,'-c',$c.c,'-ngl','99','-fa','on','-np','1','--port','8097','-ctk','q4_0','-ctv','q4_0','-ub','64','-b','512','-t','8','-v') + $(if($c.x){$c.x}else{@()}) $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 8 return @($p,$ok) } # haystack: real paragraphs from MATH-train SFT data, shuffled with a fixed seed, cycled with a new shuffle each pass $paras=New-Object System.Collections.Generic.List[string] foreach($l in Get-Content 'C:\AGI\agi-research\e20\data\sft.jsonl' -Encoding utf8){ try{ $d=$l|ConvertFrom-Json; foreach($v in @($d.problem,$d.completion)){ if($v){ foreach($pp in ([string]$v -split "`n`n")){ $t=$pp.Trim(); if($t.Length -gt 80 -and $t -notmatch 'passcode'){ $paras.Add($t) } } } } }catch{} } L "haystack paragraphs: $($paras.Count)" function Tok($text){ $b=@{content=$text}|ConvertTo-Json -Compress; [IO.File]::WriteAllText("$out\tok.json",$b); $r=Invoke-RestMethod -Uri 'http://127.0.0.1:8097/tokenize' -Method Post -ContentType 'application/json' -InFile "$out\tok.json" -TimeoutSec 1200; return $r.tokens.Count } $keys=@(@('red','7319-4826'),@('blue','5082-1937'),@('green','6640-2815'),@('orange','9157-3402')) function Build($nPara,$depth,$target,$seed){ $rng=New-Object System.Random($seed); $sb=New-Object System.Text.StringBuilder; $i=0 $others=@($keys | ? { $_[0] -ne $target[0] }); $dd=@(0.25,0.6,0.8); $dp=@{} for($k=0;$k -lt 3;$k++){ $x=$dd[$k]; if([math]::Abs($x-$depth) -lt 0.08){ $x=$x+0.12 }; $dp[[int]($nPara*$x)]=$others[$k] } $tpos=[int]($nPara*$depth) while($i -lt $nPara){ $idx=0..($paras.Count-1) | Sort-Object { $rng.Next() } foreach($j in $idx){ if($i -ge $nPara){break} [void]$sb.Append($paras[$j]).Append("`n`n") if($i -eq $tpos){ [void]$sb.Append("The secret passcode for the $($target[0]) vault is $($target[1]).`n`n") } if($dp.ContainsKey($i)){ $o=$dp[$i]; [void]$sb.Append("The secret passcode for the $($o[0]) vault is $($o[1]).`n`n") } $i++ } } return $sb.ToString() } function Run($c,$tag,$depth,$tgtIdx){ $lf="$out\srv_$tag.log"; $r0=Start-Srv $c $lf; $p=$r0[0] if(-not $r0[1]){ L "${tag}: server did not load"; return } $s0=Shared $p.Id if($s0 -gt 300){ L "${tag}: wontfit shared $s0"; ($([ordered]@{tag=$tag;status='SKIPPED-WONTFIT';idle_shared_mib=$s0})|ConvertTo-Json -Compress)|Out-File $res -Append -Encoding utf8; try{Stop-Process -Id $p.Id -Force}catch{}; return } $target=$keys[$tgtIdx]; $limit=$c.c-1500 $q="`n`nQuestion: What is the secret passcode for the $($target[0]) vault? Answer with the passcode only.`nAnswer:" # calibrate paragraph count to the token budget $n=[int]($limit/95); $text=$null; $nt=0 for($it=0;$it -lt 4;$it++){ $text=(Build $n $depth $target ($c.c+$tgtIdx)) + $q; $nt=Tok $text; L "${tag}: calib n=$n tokens=$nt limit=$limit"; if($nt -le $limit -and $nt -gt $limit*0.97){break}; $n=[int]($n*($limit*0.985)/$nt) } if($nt -gt $limit){ $n=[int]($n*0.97); $text=(Build $n $depth $target ($c.c+$tgtIdx)) + $q; $nt=Tok $text } $body=@{prompt=$text;n_predict=512;temperature=0;cache_prompt=$false}|ConvertTo-Json -Compress [IO.File]::WriteAllText("$out\body_$tag.json",$body) $job=Start-Job -ScriptBlock { param($f) try{ $r=Invoke-RestMethod -Uri 'http://127.0.0.1:8097/completion' -Method Post -ContentType 'application/json' -InFile $f -TimeoutSec 7200; $r|ConvertTo-Json -Compress -Depth 5 }catch{ "ERR "+$_.Exception.Message } } -ArgumentList "$out\body_$tag.json" $t0=Get-Date; $maxU=0; $maxS=0 while($job.State -eq 'Running' -and ((Get-Date)-$t0).TotalMinutes -lt 125){ $u=[int]((& nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits) -join ''); if($u -gt $maxU){$maxU=$u} $s=Shared $p.Id; if($s -gt $maxS){$maxS=$s}; Start-Sleep 10 } $o=Receive-Job $job -Wait -ErrorAction SilentlyContinue | Out-String; Remove-Job $job -Force try{Stop-Process -Id $p.Id -Force}catch{} Remove-Item "$out\body_$tag.json" -ErrorAction SilentlyContinue $content=$null;$np=$null;$pps=$null try{$j=$o|ConvertFrom-Json; $content=$j.content; $np=$j.timings.prompt_n; $pps=$j.timings.prompt_per_second}catch{} # strict grader: first NNNN-NNNN code in the final answer (after any thinking block) $s=[string]$content; $fa= if($s -match ''){ ($s -split '')[-1] } elseif($s -match ''){ '' } else { $s } $first=([regex]::Match($fa,'\d{4}-\d{4}')).Value $hitD=@($keys | ? { $_[1] -ne $target[1] -and $first -eq $_[1] } | % { $_[0] }) $r=[ordered]@{phase='multikey';tag=$tag;model=(Split-Path $c.m -Leaf);ctx=$c.c;depth=$depth;target=$target[0];prompt_tokens=$np;prompt_tps=$pps;first_code=$first;pass=($first -eq $target[1]);distractor_chosen=($hitD -join ',');answer=$s.Substring(0,[math]::Min(600,$s.Length));max_vram_used_mib=$maxU;max_shared_mib=$maxS;wall_min=[math]::Round(((Get-Date)-$t0).TotalMinutes,1);t=(Get-Date -Format o)} ($r|ConvertTo-Json -Compress)|Out-File $res -Append -Encoding utf8; L ("result "+($r|ConvertTo-Json -Compress)) } function Y($ctx,$sc){ @('--override-kv',"qwen35.context_length=int:$ctx",'--rope-scaling','yarn','--rope-scale',"$sc",'--yarn-orig-ctx','262144') } $c4_128=@{m=$m4;c=131072}; $c4_256=@{m=$m4;c=262144}; $c4_384=@{m=$m4;c=393216;x=(Y 393216 1.5)}; $c4_512=@{m=$m4;c=524288;x=(Y 524288 2)} $c9_480=@{m=$m9;c=491520;x=(Y 491520 1.875)} Run $c4_128 '4b_128k_d50' 0.5 2 Run $c4_256 '4b_256k_d10' 0.1 1 Run $c4_256 '4b_256k_d50' 0.5 2 Run $c4_256 '4b_256k_d90' 0.9 3 Run $c4_384 '4b_384k_d50' 0.5 2 Run $c4_512 '4b_512k_d10' 0.1 1 Run $c4_512 '4b_512k_d90' 0.9 3 Run $c9_480 '9b_480k_d50' 0.5 2 Get-Process llama-server -ErrorAction SilentlyContinue | Stop-Process -Force L "=== E21jDONE ==="