Download scripts/render_hidden_states.py from peeache/maze-solving-qwen35: direct link, hf CLI and curl.
- Browser
- Download file 5.25 kB
-
https://huggingface.co/datasets/peeache/maze-solving-qwen35/resolve/main/scripts/render_hidden_states.py
- Command line
-
hf download hf://datasets/peeache/maze-solving-qwen35/scripts/render_hidden_states.py
-
curl -L -o render_hidden_states.py https://huggingface.co/datasets/peeache/maze-solving-qwen35/resolve/main/scripts/render_hidden_states.py
5.25 kB
| """Render the hidden-state figures used by the README.""" | |
| from pathlib import Path | |
| import csv, json | |
| from PIL import Image, ImageDraw, ImageFont | |
| from maze_lab.difficulty import build | |
| from maze_lab.maze_image import render | |
| ROOT = Path(__file__).resolve().parents[1] | |
| DATA = ROOT / 'data' / 'internal' | |
| OUT = ROOT / 'assets' / 'internal' | |
| OUT.mkdir(parents=True, exist_ok=True) | |
| D = json.loads((DATA / 'similarity.json').read_text()) | |
| def svg_chart(path,title,series,ylabel='cosine similarity'): | |
| W,H=900,520; left,right,top,bottom=85,25,55,70 | |
| values=[v for _,a,_ in series for v in a]; ymin=min(values); ymax=max(values) | |
| pad=max((ymax-ymin)*.12,.01); ymin-=pad; ymax+=pad | |
| def X(i): return left+i*(W-left-right)/32 | |
| def Y(v): return top+(ymax-v)*(H-top-bottom)/(ymax-ymin) | |
| s=[f'<svg xmlns="http://www.w3.org/2000/svg" width="{W}" height="{H}" viewBox="0 0 {W} {H}">', | |
| '<rect width="100%" height="100%" fill="white"/>', | |
| f'<text x="{W/2}" y="30" text-anchor="middle" font-family="sans-serif" font-size="22" font-weight="bold">{title}</text>'] | |
| for j in range(6): | |
| v=ymin+j*(ymax-ymin)/5; y=Y(v) | |
| s += [f'<line x1="{left}" y1="{y}" x2="{W-right}" y2="{y}" stroke="#ddd"/>', | |
| f'<text x="{left-10}" y="{y+5}" text-anchor="end" font-family="sans-serif" font-size="13">{v:.2f}</text>'] | |
| for i in (0,4,8,12,16,20,24,28,32): | |
| x=X(i); s += [f'<line x1="{x}" y1="{top}" x2="{x}" y2="{H-bottom}" stroke="#eee"/>', | |
| f'<text x="{x}" y="{H-bottom+25}" text-anchor="middle" font-family="sans-serif" font-size="13">{i}</text>'] | |
| for name,a,color in series: | |
| points=' '.join(f'{X(i):.1f},{Y(v):.1f}' for i,v in enumerate(a)) | |
| s.append(f'<polyline points="{points}" fill="none" stroke="{color}" stroke-width="3"/>') | |
| lx=left | |
| for name,_,color in series: | |
| s += [f'<line x1="{lx}" y1="{H-24}" x2="{lx+28}" y2="{H-24}" stroke="{color}" stroke-width="4"/>', | |
| f'<text x="{lx+35}" y="{H-19}" font-family="sans-serif" font-size="14">{name}</text>'] | |
| lx += 190 | |
| s += [f'<text x="{W/2}" y="{H-42}" text-anchor="middle" font-family="sans-serif" font-size="15">hidden state index</text>', | |
| f'<text transform="translate(20 {H/2}) rotate(-90)" text-anchor="middle" font-family="sans-serif" font-size="15">{ylabel}</text>', '</svg>'] | |
| path.write_text('\n'.join(s)) | |
| for mode,M in D['modes'].items(): | |
| ct=M['cell_type']; co=M['coordinate']; sa=M['salient']; dist=M['distance'] | |
| svg_chart(OUT/f'cell_type_{mode}.svg',f'Cell-type similarity, {mode}',[ | |
| ('wall to wall',ct['wall_wall'],'#30343b'),('floor to floor',ct['floor_floor'],'#218c74'),('wall to floor',ct['wall_floor'],'#d35400')]) | |
| svg_chart(OUT/f'coordinate_{mode}.svg',f'Coordinate similarity, {mode}',[ | |
| ('same coordinate',co['same_coordinate'],'#3455db'),('other same-type cell',co['other_same_type'],'#d35400')]) | |
| svg_chart(OUT/f'salient_{mode}.svg',f'S and E similarity, {mode}',[ | |
| ('S to S',sa['start_start'],'#159957'),('E to E',sa['goal_goal'],'#d7443e'),('S to E',sa['start_goal'],'#6c5ce7')]) | |
| svg_chart(OUT/f'distance_{mode}.svg',f'Spatial-distance similarity, {mode}',[ | |
| ('1 cell apart',dist['1'],'#193cb8'),('2 to 3',dist['2-3'],'#2980b9'),('4 to 6',dist['4-6'],'#e67e22'),('7 or more',dist['7+'],'#c0392b')]) | |
| # Token-grid overlay | |
| m=D['mapping']; maze=build(m['level'],m['seed']); img=render(maze,labels=True).convert('RGBA') | |
| over=Image.new('RGBA',img.size,(0,0,0,0)); draw=ImageDraw.Draw(over); gr,gc=m['token_grid'] | |
| for r in range(gr+1): | |
| y=round(r*img.height/gr); draw.line((0,y,img.width,y),fill=(20,90,230,150),width=2) | |
| for c in range(gc+1): | |
| x=round(c*img.width/gc); draw.line((x,0,x,img.height),fill=(20,90,230,150),width=2) | |
| Image.alpha_composite(img,over).convert('RGB').save(OUT/'visual_token_grid.png') | |
| with (DATA/'visual_token_mapping.csv').open('w',newline='') as f: | |
| w=csv.writer(f); w.writerow(['token','token_row','token_col','overlapping_cells']) | |
| for t in m['tokens']: | |
| cells=' '.join(f'({r},{c}):{frac:.4f}' for r,c,frac in t['overlaps']) | |
| w.writerow([t['token'],t['token_row'],t['token_col'],cells]) | |
| # Layer heatmaps | |
| hm=D['heatmaps']; layers=list(hm['layers']); scale=76; gap=18; title=45 | |
| canvas=Image.new('RGB',(len(layers)*13*scale//3+(len(layers)-1)*gap,13*scale//3+title+30),'white') | |
| d=ImageDraw.Draw(canvas); font=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',18) | |
| cell=scale//3 | |
| allv=[v for a in hm['layers'].values() for v in a]; lo,hi=min(allv),max(allv) | |
| def color(v): | |
| t=max(0,min(1,(v-lo)/(hi-lo))); return (round(255*(1-t)),round(235*(1-abs(2*t-1))),round(255*t)) | |
| for pi,L in enumerate(layers): | |
| ox=pi*(13*cell+gap); d.text((ox+13*cell/2,title/2),f'layer {L}',anchor='mm',font=font,fill='black') | |
| vals=hm['layers'][L] | |
| for i,v in enumerate(vals): | |
| r,c=divmod(i,13); box=(ox+c*cell,title+r*cell,ox+(c+1)*cell-1,title+(r+1)*cell-1) | |
| d.rectangle(box,fill=color(v),outline=(230,230,230)) | |
| qr,qc=hm['query_cell']; d.rectangle((ox+qc*cell,title+qr*cell,ox+(qc+1)*cell-1,title+(qr+1)*cell-1),outline='black',width=3) | |
| canvas.save(OUT/'coordinate_heatmaps.png') | |
| print('wrote internal-analysis figures to', OUT) | |