Model-Observatory / verify.py
alphabell-research's picture
Keep every decoder block visible on desktop
fe959a9 verified
Raw History Blame
9.28 kB
"""Exercise the public viewer with real bundled captures, sequentially in one browser."""
import argparse,json,pathlib,tempfile
from playwright.sync_api import sync_playwright,expect
def main():
parser=argparse.ArgumentParser();parser.add_argument('--base',default='http://localhost:8000');parser.add_argument('--chromium');parser.add_argument('--out',default='reports');args=parser.parse_args()
out=pathlib.Path(args.out);out.mkdir(parents=True,exist_ok=True);checks=[];errors=[];failed_requests=[]
def passed(name):checks.append(name);print('PASS '+name,flush=True)
with sync_playwright() as p:
browser=p.chromium.launch(headless=True,**({'executable_path':args.chromium} if args.chromium else {}),args=['--disable-dev-shm-usage'])
try:
context=browser.new_context(viewport={'width':1440,'height':1100},permissions=['clipboard-read','clipboard-write'],accept_downloads=True)
page=context.new_page();page.on('pageerror',lambda e:errors.append(str(e)));page.on('response',lambda r:failed_requests.append({'url':r.url,'status':r.status}) if r.status>=400 else None)
page.goto(args.base);page.wait_for_function('typeof S!=="undefined"&&S.frames.length===59&&!S.loadingTrace',timeout=90000)
assert page.evaluate('S.model.id==="flint"&&S.frames[0].layers.length===49&&S.frame===0');expect(page.locator('#expertGrid button')).to_have_count(256);passed('Flint example: 59 measured frames and 49 blocks')
expected=page.evaluate('S.frames[S.frame].layers[S.layer].heads[S.head].toFixed(3)+" RMS"');expect(page.locator('#headValue')).to_have_text(expected);passed('Inspector displays actual selected-frame RMS')
page.locator('#openBlock').click();expect(page.locator('#detailDialog')).to_be_visible();expect(page.locator('#dBlockExperts button')).to_have_count(256);expect(page.locator('#dBlockRoutes button')).to_have_count(6);page.keyboard.press('Escape');passed('Fullscreen block exposes every expert and six actual routes')
page.locator('#expertList button').first.click();expect(page.locator('#dExpertStatus')).to_have_text('Selected');expect(page.locator('#dTimeline button')).to_have_count(59);page.locator('#detailClose').click();passed('Selected expert has a complete token timeline')
page.locator('#playSpeed').select_option('250');page.locator('#runButton').click();page.wait_for_function('S.frame>=2',timeout=10000);page.locator('#playButton').click();assert page.evaluate('S.timer===null');page.locator('#liveButton').click();assert page.evaluate('S.frame===58');page.locator('#stepBack').click();assert page.evaluate('S.frame===57');passed('Replay, speed, pause, step and jump to end change selected frames')
page.locator('#historyButton').click();page.wait_for_function('S.geometry.history===true');page.locator('#structureButton').click();page.wait_for_function('S.geometry.history===false');passed('Token-history heatmap and structure switch')
with tempfile.TemporaryDirectory() as temp:
export=pathlib.Path(temp)/'trace.json'
with page.expect_download() as d:page.locator('#exportButton').click()
d.value.save_as(export);trace=json.loads(export.read_text());assert len(trace['frames'])==59
page.locator('#importFile').set_input_files(str(export));page.wait_for_function('!S.loadingTrace&&S.trace.frames.length===59');assert page.evaluate('S.frame===58');passed('Export/import retains the complete measured trace')
trace['frames'][0]['layers'][12]['heads'][0]=-1;bad=pathlib.Path(temp)/'invalid.json';bad.write_text(json.dumps(trace))
page.locator('#importFile').set_input_files(str(bad));expect(page.locator('#toast')).to_contain_text('invalid head measurements');assert page.evaluate('S.frames[0].layers[12].heads[0]>=0&&S.frame===58');expect(page.locator('#runButton')).to_be_enabled();page.locator('#runButton').click();page.wait_for_function('S.frame>=1',timeout=10000);page.locator('#playButton').click();passed('Invalid import preserves the loaded capture and working replay')
page.locator('#scrubber').fill('12');page.locator('#modelSelect').select_option('qwen');page.wait_for_function('S.model.id==="qwen"&&S.frames.length===44&&!S.switching',timeout=90000)
expect(page.locator('#headGrid button')).to_have_count(48);assert page.evaluate('S.frames[0].layers.length===64');page.locator('#flowAttention').click();expect(page.locator('#dMemory')).to_be_visible();assert 'Write gate' in page.locator('#dMemory').inner_text();page.keyboard.press('Escape');passed('Qwen has 64 blocks and measured recurrent memory')
for _ in range(3):page.locator('#nextLayer').click()
expect(page.locator('#headGrid button')).to_have_count(24);page.locator('#flowAttention').click();expect(page.locator('#dAttentionRows')).to_be_visible();page.keyboard.press('Escape');page.locator('#flowMlp').click();expect(page.locator('#dFfnWidth')).to_have_text('17408');page.keyboard.press('Escape');passed('Qwen full-attention and dense feed-forward inspectors')
page.locator('#modelSelect').select_option('flint');page.wait_for_function('S.model.id==="flint"&&!S.switching');assert page.evaluate('S.frame===12');passed('Switching models preserves each selected trace and token')
page.locator('#modelCanvas').focus();old=page.evaluate('S.layer');page.keyboard.press('ArrowDown');assert page.evaluate('S.layer')==old+1;page.keyboard.press('Enter');expect(page.locator('#detailDialog')).to_be_visible();page.keyboard.press('Escape');passed('Keyboard topology navigation and fullscreen access')
page.locator('#shareButton').click();expect(page.locator('#toast')).to_contain_text('view link copied');url=page.evaluate('navigator.clipboard.readText()');assert 'model=flint' in url and 'token=13' in url;passed('Share link records model, layer and token')
page.locator('#toast').wait_for(state='hidden');page.screenshot(path=str(out/'desktop.png'),full_page=True)
for width in [390,320]:
page.set_viewport_size({'width':width,'height':844});assert page.evaluate('document.documentElement.scrollWidth<=innerWidth'),width
page.locator('#openBlock').click();assert page.locator('#detailDialog').bounding_box()['width']<=width+1;page.keyboard.press('Escape')
page.screenshot(path=str(out/'phone.png'),full_page=True);passed('320px and 390px layouts and fullscreen fit their viewport')
for width,height in [(1440,720),(1440,900),(1440,1100),(1024,768)]:
page.set_viewport_size({'width':width,'height':height})
page.wait_for_function("document.querySelector('.model-panel').getBoundingClientRect().bottom<=document.querySelector('main').getBoundingClientRect().bottom+1")
assert page.evaluate("document.querySelector('#modelCanvas').getBoundingClientRect().bottom<=document.querySelector('.model-panel').getBoundingClientRect().bottom+1")
assert page.evaluate("document.querySelector('.saved-section').getBoundingClientRect().bottom<=document.querySelector('main').getBoundingClientRect().bottom+1")
passed('All decoder blocks and trace controls fit short and tall desktop panels')
page.goto(args.base.rstrip('/')+'/?model=qwen&layer=3&token=7');page.wait_for_function('S.model?.id==="qwen"&&S.frames.length===44&&!S.loadingTrace&&S.frame===6',timeout=90000);assert page.evaluate('S.layer===3');passed('Shared deep link opens the requested example, layer and token')
page.locator('#aboutButton').click();expect(page.locator('#aboutContent')).to_contain_text('95th percentile');expect(page.locator('#aboutSource')).to_have_attribute('href','https://huggingface.co/Qwen/Qwen3.8-27B/blob/main/config.json');page.locator('#closeAbout').click();passed('Explanation and model configuration link follow model selection')
assert not errors,errors;assert not failed_requests,failed_requests;passed('No uncaught browser errors or failed release requests')
context.close()
# Failure injection uses a fresh context so a successful cache cannot hide it.
recovery=browser.new_context(viewport={'width':390,'height':844});page=recovery.new_page();blocked=[True]
def capture_route(route):
if blocked[0]:route.abort()
else:route.continue_()
page.route('**/traces/*.json.gz',capture_route);page.goto(args.base);expect(page.locator('#engineStatus')).to_have_text('Trace unavailable',timeout=30000);assert page.evaluate('S.config.num_hidden_layers===49');blocked[0]=False;page.locator('#runButton').click();page.wait_for_function('S.frames.length===59&&!S.loadingTrace',timeout=90000);expect(page.locator('#engineStatus')).to_contain_text('Measured Q8 replay');recovery.close();passed('Failed capture leaves topology visible and Retry recovers')
touch=browser.new_context(viewport={'width':390,'height':844},has_touch=True,is_mobile=True);page=touch.new_page();page.goto(args.base);page.wait_for_function('S.frames.length===59&&!S.loadingTrace',timeout=90000);page.locator('#openBlock').tap();expect(page.locator('#detailDialog')).to_be_visible();page.locator('#detailClose').tap();touch.close();passed('Touch interaction opens and closes fullscreen inspection')
finally:browser.close()
(out/'results.json').write_text(json.dumps({'passed':len(checks),'checks':checks,'browser_errors':errors,'failed_requests':failed_requests},indent=2)+'\n')
print(json.dumps({'passed':len(checks),'errors':errors,'failed_requests':failed_requests}))
if __name__=='__main__':main()