Views
No views yet
pip install git+https://github.com/etowahadams/interprot.git1import torch
2from transformers import AutoTokenizer, EsmModel
3from safetensors.torch import load_file
4from interprot.sae_model import SparseAutoencoder
5from huggingface_hub import hf_hub_download
6
7ESM_DIM = 1280
8SAE_DIM = 4096
9LAYER = 24
10
11device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
13# Load ESM model
14tokenizer = AutoTokenizer.from_pretrained("facebook/esm2_t33_650M_UR50D")
15esm_model = EsmModel.from_pretrained("facebook/esm2_t33_650M_UR50D")
16esm_model.to(device)
17esm_model.eval()
18
19# Load SAE model
20checkpoint_path = hf_hub_download(
21 repo_id="liambai/InterProt-ESM2-SAEs",
22 filename="esm2_plm1280_l24_sae4096.safetensors"
23)
24sae_model = SparseAutoencoder(ESM_DIM, SAE_DIM)
25sae_model.load_state_dict(load_file(checkpoint_path))
26sae_model.to(device)
27sae_model.eval()Lseq = "TTCCPSIVARSNFNVCRLPGTPEALCATYTGCIIIPGATCPGDYAN"
# Tokenize sequence and run ESM inference
inputs = tokenizer(seq, padding=True, return_tensors="pt").to(device)
with torch.no_grad():
outputs = esm_model(**inputs, output_hidden_states=True)
# esm_layer_acts has shape (L+2, ESM_DIM), +2 for BoS and EoS tokens
esm_layer_acts = outputs.hidden_states[LAYER][0]
# Using ESM embeddings from LAYER, run SAE inference
sae_acts = sae_model.get_acts(esm_layer_acts) # (L+2, SAE_DIM)
sae_actsesm2_plm1280_l24_sae4096.safetensors whereas the original is named esm2_plm1280_l24_sae4096_100k.safetensors.esm2_plm1280_l24_sae4096.safetensors, but if you'd like to reproduce the default SAE on interprot.com, you can use esm2_plm1280_l24_sae4096_100k.safetensors. All other layer SAEs are trained with the same configrations as esm2_plm1280_l24_sae4096.safetensors.