indic-sae-universality / load_example.py
malharinamdar's picture
Release verified 54M model weights, layer-3 SAEs and concept RSA artifacts
d0354a9 verified
Raw History Blame Contribute Delete
1.37 kB
"""Run from a download of the Hub repository containing loader/ (Python, torch, safetensors, huggingface_hub)."""
import json
from pathlib import Path
import sys
import torch
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
sys.path.insert(0,str(Path(__file__).resolve().parent/'loader'))
from slm import GPT, GPTConfig
from sae import BatchTopKMatryoshkaSAE
repo='malharinamdar/indic-sae-universality'
def fetch(path):
local=Path(__file__).resolve().parent/path
return str(local) if local.is_file() else hf_hub_download(repo,path)
def config(folder):
return json.loads(Path(fetch(folder+'/config.json')).read_text())
model_dir='base/hindi_54M'
sae_dir='sae/october/hindi_54M'
model=GPT(GPTConfig(**config(model_dir))).eval()
state=load_file(fetch(model_dir+'/model.safetensors'))
state['lm_head.weight']=state['transformer.wte.weight']
model.load_state_dict(state,strict=True)
s=config(sae_dir)
sae=BatchTopKMatryoshkaSAE(s['d_model'],s['d_hidden'],s['prefixes'],k=s['k'],aux_k=s['aux_k']).eval()
sae.load_state_dict(load_file(fetch(sae_dir+'/model.safetensors')),strict=True)
with torch.no_grad():
# Three valid vocabulary IDs suffice for a shape check. Use Sarvam-1 for real text.
residual=model.residual_at_layer(torch.tensor([[1,2,3]]),s['layer'])
z=sae.encode(residual*s['scale'])
print(tuple(z.shape))