okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2b36e8a02
main
1#!/usr/bin/env python3 2"""Long-context recall check: hide one fact in a long prompt and ask for it back (the guide's "long-context retrieval" cell). 3 4python research/tools/dgx-spark-2026-10/dgx_needle.py --config strata-iq3s-final.json --tokens 12000 5 6Starts the model's server, sends one request, and reports whether the answer contains the fact. The fact is placed at a 7random position in the prompt and never appears in the question, so a correct answer means the context was read. 8""" 9from __future__import annotations 10 11import argparse 12import json 13import random 14import subprocess 15import sys 16import time 17import urllib .error 18import urllib .request 19from pathlib import Path 20 21ROOT = Path (__file__ ).resolve ().parents [3 ] 22WORDS = ("river stone market lantern copper winter garden signal harbor needle orbit velvet canyon ember ledger quartz " 23"meadow anchor violet thunder cedar marble piston ribbon cobalt falcon glacier hollow ivory juniper kernel " 24"lattice mirror nectar oasis pepper quarry raven saddle tundra umber vessel willow xenon yellow zephyr" ).split () 25 26 27def build (tokens :int ,needle :str ,rng :random .Random )-> str : 28words = max (8 ,int (tokens / 1.35 )) 29body = [rng .choice (WORDS )for _ in range (words )] 30at = rng .randrange (len (body )// 4 ,3 * len (body )// 4 ) 31body [at :at ]= [needle ] 32return ("Read the following notes carefully, then answer the question at the end.\n\n" + " " .join (body )+ 33"\n\nQuestion: what is the access code for the harbor gate? Answer with the code only." ) 34 35 36def post (port :int ,prompt :str ,max_tokens :int = 32 )-> dict : 37body = json .dumps ({"messages" : [{"role" :"user" ,"content" :prompt }],"max_tokens" :max_tokens ,"temperature" :0 , 38"chat_template_kwargs" : {"enable_thinking" :False }}).encode () 39req = urllib .request .Request (f"http://127.0.0.1: { port } /v1/chat/completions" ,data = body , 40headers = {"Content-Type" :"application/json" }) 41try : 42with urllib .request .urlopen (req ,timeout = 3600 )as r : 43return json .loads (r .read ()) 44except urllib .error .HTTPError as e : 45raise SystemExit (f"HTTP { e . code } : { e . read ()[: 400 ]!r } " ) 46 47 48def main ()-> int : 49ap = argparse .ArgumentParser () 50ap .add_argument ("--config" ,required = True ) 51ap .add_argument ("--tokens" ,type = int ,default = 12000 ) 52ap .add_argument ("--port" ,type = int ,default = 8091 ) 53ap .add_argument ("--runs" ,type = int ,default = 3 ) 54a = ap .parse_args () 55cfg = json .loads (Path (a .config ).read_text ()) 56cfg ["port" ]= a .port 57cfg ["log" ]= "/tmp/dgx-needle.log" 58cpath = Path ("/tmp/dgx-needle.json" ) 59cpath .write_text (json .dumps (cfg ,indent = 1 )) 60proc = subprocess .Popen ([sys .executable ,str (ROOT / "serve" / "server.py" ),"--engine" ,"strata" ,"--config" , 61str (cpath ),"--port" ,str (a .port )],cwd = str (ROOT ), 62stdout = subprocess .DEVNULL ,stderr = subprocess .DEVNULL ) 63ok = 0 64try : 65for _ in range (600 ): 66try : 67with urllib .request .urlopen (f"http://127.0.0.1: { a . port } /v1/models" ,timeout = 5 )as r : 68if r .status == 200 : 69break 70except Exception : 71pass 72time .sleep (2 ) 73post (a .port ,"Say hi." ,8 ) 74for i in range (a .runs ): 75rng = random .Random (1000 + i ) 76code = f" { rng . randrange ( 100000 , 999999 ) } " 77needle = f"The access code for the harbor gate is { code } ." 78r = post (a .port ,build (a .tokens ,needle ,rng )) 79answer = r ["choices" ][0 ]["message" ].get ("content" ,"" ) 80t = r .get ("timings" )or {} 81hit = code in answer 82ok += hit 83f"run { i } : prompt_n { t . get ( 'prompt_n' ) } at { t . get ( 'prompt_per_second' ) } tok/s, " 84f"answer { answer [: 40 ]!r } -> { 'HIT' if hit else 'MISS' } (code { code } )" ) 85finally : 86proc .terminate () 87try : 88proc .wait (60 ) 89except subprocess .TimeoutExpired : 90proc .kill () 91f" { ok } of { a . runs } recalled" ) 92return 0 if ok == a .runs else 1 93 94 95if __name__ == "__main__" : 96sys .exit (main ())