$ErrorActionPreference='Continue' $out='C:\AGI\agi-research\e21_budget\e22p'; New-Item -ItemType Directory -Force $out | Out-Null $log="$out\chain.log"; $res="$out\results.jsonl" function L($m){ "$(Get-Date -Format o) $m" | Out-File -FilePath $log -Append -Encoding utf8 } L "=== E22p start: question position vs repetition, 4 arms per hypotheses.md (end1=question at end, start1=question at start, both=both ends, neut=same-length neutral instruction at start + question at end) on the SAME haystack per trial, randomized arm order, independent server per request, 4B native 256K, KV q4_0, n=8 ===" $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'; $CTX=262144 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($lf){ Get-Process llama-server -ErrorAction SilentlyContinue | Stop-Process -Force; Start-Sleep 5 $a=@('-m',$m4,'-c',$CTX,'-ngl','99','-fa','on','-np','1','--port','8097','-ctk','q4_0','-ctv','q4_0','-ub','64','-b','512','-t','8') $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) } $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 } $colors=@('red','blue','green','orange','purple','yellow','silver','black') function MkKeys($seed){ $r=New-Object System.Random($seed*7919+13); $cs=$colors | Sort-Object { $r.Next() } | Select -First 4; $ks=@(); foreach($cn in $cs){ $ks+=,@($cn, ('{0:D4}-{1:D4}' -f $r.Next(1000,10000),$r.Next(1000,10000))) }; return ,$ks } function Build($keys,$nPara,$depth,$target,$seed){ $rng=New-Object System.Random($seed); $sb=New-Object System.Text.StringBuilder; $i=0; $needleChar=-1 $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 }; if($x -gt 0.97){$x=0.97}; $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){ $needleChar=$sb.Length; [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(),$needleChar) } function Prompt($arm,$H,$Q){ switch($arm){ 'end1' { return $H + "`n`n" + $Q + "`nAnswer:" } 'start1' { return $Q + "`n`nDocument:`n`n" + $H + "`n`nAnswer:" } 'both' { return $Q + "`n`nDocument:`n`n" + $H + "`n`n" + $Q + "`nAnswer:" } 'neut' { return $script:N + "`n`nDocument:`n`n" + $H + "`n`n" + $Q + "`nAnswer:" } } } function MkNeutral($Q){ $qn=Tok $Q; $w=('Instruction: Please read the following document carefully and in full, paying close attention to every single paragraph before you continue on to the end of the text below.' -split ' ') $best=''; $bn=0 for($k=1;$k -le $w.Count;$k++){ $s=($w[0..($k-1)] -join ' '); $n=Tok $s; if($n -eq $qn){ return @($s,$n,$qn) }; if($n -gt $qn){break}; $best=$s; $bn=$n } for($z=0;$z -lt 20 -and $bn -lt $qn;$z++){ $best=$best+' now'; $bn=Tok $best } return @($best,$bn,$qn) } function RunArm($tr,$arm,$H,$Q,$keys,$target,$meta){ $tag="t$($tr.id)_d$([int]($tr.d*100))_$arm"; $lf="$out\srv_$tag.log" $r0=Start-Srv $lf; $p=$r0[0] if(-not $r0[1]){ L "${tag}: server did not load"; return } $nn=$null; $qn=$null if($arm -eq 'neut'){ $mn=MkNeutral $Q; $script:N=$mn[0]; $nn=$mn[1]; $qn=$mn[2]; L "${tag}: neutral tokens=$nn question tokens=$qn text=$($script:N)" } $text=Prompt $arm $H $Q; $nt=Tok $text $pre=$text.Substring(0,$text.IndexOf("The secret passcode for the $($target[0]) vault is ")); $needleTok=Tok $pre $body=@{prompt=$text;n_predict=64;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 60){ $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;$eos=$null;$ntp=$null try{$j=$o|ConvertFrom-Json; $content=$j.content; $np=$j.timings.prompt_n; $pps=$j.timings.prompt_per_second; $eos=$j.stopped_eos; $ntp=$j.tokens_predicted}catch{} $s=[string]$content; $first=([regex]::Match($s,'\d{4}-\d{4}')).Value $hitD=@($keys | ? { $_[1] -ne $target[1] -and $first -eq $_[1] } | % { $_[0] }) $clean=($s.Trim() -match '^\d{4}-\d{4}\.?$') -and ($eos -eq $true) $r=[ordered]@{phase='e22p_qpos';arm=$arm;trial=$tr.id;order_idx=$meta.oi;arm_order=$meta.order;seed=$tr.seed;kv='q4_0';tag=$tag;ctx=$CTX;depth=$tr.d;target=$target[0];target_code=$target[1];haystack_tokens=$meta.htok;prompt_tokens_tokenized=$nt;prompt_tokens=$np;neutral_tokens=$nn;question_tokens=$qn;needle_token_offset=$needleTok;needle_char_offset_in_haystack=$meta.nchar;prompt_tps=$pps;first_code=$first;pass=($first -eq $target[1]);distractor_chosen=($hitD -join ',');stopped_eos=$eos;tokens_predicted=$ntp;clean_stop=$clean;answer=$s.Substring(0,[math]::Min(200,$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)) } $depths=@(0.1,0.9,0.3,0.7,0.5,0.2,0.8,0.4) $arms=@('end1','start1','both','neut') for($t=0;$t -lt 8;$t++){ $tr=@{id=$t;seed=(2500+$t);d=$depths[$t];ti=($t % 4)} $keys=MkKeys $tr.seed; $target=$keys[$tr.ti] $Q="Question: What is the secret passcode for the $($target[0]) vault? Answer with the passcode only." $hf="$out\haystack_trial$t.txt"; $mf="$out\haystack_trial$t.json" if(Test-Path $hf){ $H=[IO.File]::ReadAllText($hf); $meta0=Get-Content $mf | ConvertFrom-Json; $htok=$meta0.htok; $nchar=$meta0.nchar } else { $r0=Start-Srv "$out\srv_calib_t$t.log"; $p=$r0[0] $limit=$CTX-1500-200; $n=[int]($limit/95); $H=$null; $htok=0; $nchar=-1 for($it=0;$it -lt 4;$it++){ $b=Build $keys $n $tr.d $target $tr.seed; $H=$b[0]; $nchar=$b[1]; $htok=Tok $H; L "t${t}: calib n=$n haystack_tokens=$htok limit=$limit"; if($htok -le $limit -and $htok -gt $limit*0.97){break}; $n=[int]($n*($limit*0.985)/$htok) } if($htok -gt $limit){ $n=[int]($n*0.97); $b=Build $keys $n $tr.d $target $tr.seed; $H=$b[0]; $nchar=$b[1]; $htok=Tok $H } try{Stop-Process -Id $p.Id -Force}catch{} [IO.File]::WriteAllText($hf,$H); (@{htok=$htok;nchar=$nchar}|ConvertTo-Json -Compress)|Out-File $mf -Encoding utf8 } $rng=New-Object System.Random(9100+$t); $order=@($arms | Sort-Object { $rng.Next() }) L "t${t}: seed=$($tr.seed) depth=$($tr.d) target=$($target[0]) haystack_tokens=$htok order=$($order -join ',')" for($oi=0;$oi -lt 4;$oi++){ RunArm $tr $order[$oi] $H $Q $keys $target @{oi=$oi;order=($order -join ',');htok=$htok;nchar=$nchar} } } Get-Process llama-server -ErrorAction SilentlyContinue | Stop-Process -Force L "=== E22pDONE ==="